myx-cmake/MyxCMake/modules/MyxCMakeCommonTargetProperties.cmake

167 lines
6.7 KiB
CMake
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function(myx_cmake_common_target_properties target)
if(NOT TARGET ${target})
message(FATAL_ERROR "MyxCMake: Target ${target} does not exists.")
return()
endif()
get_target_property(_target_type ${target} TYPE)
set(__visibility PUBLIC)
if(_target_type STREQUAL INTERFACE_LIBRARY)
set(__visibility INTERFACE)
else()
#
if(TARGET Qt5::Core)
if(_target_type STREQUAL EXECUTABLE)
target_compile_options(${target}
PUBLIC ${Qt5Core_EXECUTABLE_COMPILE_FLAGS})
endif()
if(NOT MYX_CMAKE_DEBUG_OUTPUT)
target_compile_definitions(${target} PUBLIC QT_NO_DEBUG_OUTPUT)
endif()
if(NOT MYX_CMAKE_INFO_OUTPUT)
target_compile_definitions(${target} PUBLIC QT_NO_INFO_OUTPUT)
endif()
if(NOT MYX_CMAKE_WARNING_OUTPUT)
target_compile_definitions(${target} PUBLIC QT_NO_WARNING_OUTPUT)
endif()
endif()
if(CMAKE_CXX_COMPILER_IS_GCC AND NOT APPLE)
set_property(
TARGET ${target}
APPEND_STRING
PROPERTY LINK_FLAGS " -Wl,--no-as-needed")
endif()
# Исключение файлов qrc из списка объединяемых файлов
get_target_property(__sources ${target} SOURCES)
foreach(src IN LISTS __sources)
string(REGEX MATCH ".*/qrc_.*\\.cpp$" __qrc ${src})
if(__qrc)
set_property(SOURCE ${__qrc} PROPERTY COTIRE_EXCLUDED ON)
set_property(SOURCE ${__qrc} PROPERTY SKIP_UNITY_BUILD_INCLUSION ON)
endif()
endforeach()
target_include_directories(
${target}
PUBLIC $<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>)
endif()
target_compile_features(${target} ${__visibility} cxx_alias_templates
cxx_nullptr cxx_override)
if(_target_type STREQUAL EXECUTABLE)
set_target_properties(${target} PROPERTIES
POSITION_INDEPENDENT_CODE ON
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
if(CMAKE_CXX_COMPILE_OPTIONS_PIE)
target_compile_options(${target} PUBLIC ${CMAKE_CXX_COMPILE_OPTIONS_PIE})
endif()
if(CMAKE_CXX_COMPILER_IS_GCC AND MYX_CMAKE_CODE_COVERAGE)
myx_cmake_code_coverage(${target})
endif()
install(TARGETS ${target} COMPONENT main RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
if(APPLE)
target_compile_definitions(${target} ${__visibility} Darwin)
endif()
if(_target_type STREQUAL OBJECT_LIBRARY)
set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
if(_target_type MATCHES "_LIBRARY$" AND NOT ${target}-install-headers)
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
add_custom_target(${target}-install-headers
COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=dev
-P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
endif()
# LTO only for executables (not libraries) in Release build type
if(_target_type STREQUAL EXECUTABLE AND CMAKE_BUILD_TYPE STREQUAL Release)
check_cxx_compiler_flag(-flto CXX_HAS_LTO_FLAG)
check_cxx_compiler_flag(-fno-fat-lto-objects CXX_HAS_NO_FAT_LTO_FLAG)
if(CXX_HAS_LTO_FLAG)
target_compile_options(${target} PUBLIC "-flto")
set_property(
TARGET ${target}
APPEND_STRING
PROPERTY LINK_FLAGS " -flto")
if(CXX_HAS_NO_FAT_LTO_FLAG)
target_compile_options(${target} PUBLIC "-fno-fat-lto-objects")
set_property(
TARGET ${target}
APPEND_STRING
PROPERTY LINK_FLAGS " -fno-fat-lto-objects")
endif()
endif()
endif()
# cmake-format: off
if(COMMAND cotire)
set_property(TARGET ${target}
PROPERTY COTIRE_ENABLE_PRECOMPILED_HEADER ${MYX_CMAKE_PRECOMPILED_HEADERS})
set_property(TARGET ${target}
PROPERTY COTIRE_ADD_UNITY_BUILD ${MYX_CMAKE_UNITY_BUILD})
endif()
# cmake-format: on
if(CMAKE_BUILD_TYPE STREQUAL Profile)
target_compile_definitions(${target} ${__visibility} PROFILE)
elseif(CMAKE_BUILD_TYPE STREQUAL Debug)
target_compile_definitions(${target} ${__visibility} DEBUG)
elseif(CMAKE_BUILD_TYPE STREQUAL Release)
target_compile_definitions(${target} ${__visibility} RELEASE)
elseif(CMAKE_BUILD_TYPE STREQUAL None)
target_compile_definitions(${target} ${__visibility} ANALYSIS)
endif()
target_compile_definitions(
${target} ${__visibility}
"MYX_CMAKE_LSB_ID_${MYX_CMAKE_LSB_DISTRIBUTOR_ID}")
target_compile_definitions(
${target} ${__visibility}
"MYX_CMAKE_LSB_CODENAME_${MYX_CMAKE_LSB_CODENAME}")
myx_cmake_analyze_clang_tidy(${target})
myx_cmake_analyze_clang_check(${target})
myx_cmake_analyze_clazy(${target})
myx_cmake_analyze_pvs_studio(${target})
myx_cmake_format_sources(${target})
# Создание в каталоге ${CMAKE_BINARY_DIR} стандартных каталогов bin,include,lib
if(NOT TARGET ${target}-default-directories)
add_custom_target(
${target}-default-directories
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/bin
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/include
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/lib
)
add_dependencies(${target} ${target}-default-directories)
endif()
# Создание в каталоге ${CMAKE_BINARY_DIR} символических ссылок на каталоги в ${CMAKE_SOURCE_DIR}/files
if(NOT TARGET myx-cmake-symlinks-to-project-directories AND UNIX)
add_custom_target(myx-cmake-symlinks-to-project-directories
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
# Ссылка на каталог с журналами
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_SOURCE_DIR}/files/log
${CMAKE_BINARY_DIR}/log
# Ссылка на каталог с обрабатываемыми данными
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_SOURCE_DIR}/files/var
${CMAKE_BINARY_DIR}/var
# Ссылка на каталог с постоянными данными
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_SOURCE_DIR}/files/share
${CMAKE_BINARY_DIR}/share
# Ссылка на каталог настроек
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_SOURCE_DIR}/files/etc
${CMAKE_BINARY_DIR}/etc)
add_dependencies(${target} myx-cmake-symlinks-to-project-directories)
endif()
endfunction()