myx-cmake/MyxCMake/modules/MyxCMakeCommonTargetProperties.cmake

176 lines
7.0 KiB
CMake
Raw Normal View History

2022-04-08 05:13:47 +00:00
function(myx_cmake_common_target_properties target)
if(NOT TARGET ${target})
2021-12-21 10:45:09 +00:00
myx_cmake_message_error("MyxCMake: Target ${target} does not exists.")
endif()
2021-12-08 11:32:04 +00:00
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)
2022-04-07 11:17:40 +00:00
target_compile_options(${target} PUBLIC ${Qt5Core_EXECUTABLE_COMPILE_FLAGS})
2021-12-08 11:32:04 +00:00
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>
2021-12-08 11:32:04 +00:00
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>)
2022-04-08 05:13:47 +00:00
endif()
2022-04-07 11:17:40 +00:00
target_compile_features(${target} ${__visibility} cxx_alias_templates cxx_nullptr cxx_override)
2021-12-08 11:32:04 +00:00
if(_target_type STREQUAL EXECUTABLE)
set_target_properties(${target} PROPERTIES
POSITION_INDEPENDENT_CODE ON
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
2021-12-08 11:32:04 +00:00
if(CMAKE_CXX_COMPILE_OPTIONS_PIE)
target_compile_options(${target} PUBLIC ${CMAKE_CXX_COMPILE_OPTIONS_PIE})
endif()
install(TARGETS ${target} COMPONENT main RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2021-12-08 11:32:04 +00:00
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()
# 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()
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})
2021-12-08 11:32:04 +00:00
myx_cmake_format_sources(${target})
2022-04-07 11:17:40 +00:00
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()
2021-12-08 11:32:04 +00:00
# Создание в каталоге ${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
2021-12-08 11:32:04 +00:00
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)
2021-12-08 11:32:04 +00:00
endif()
endfunction()
2022-04-07 11:17:40 +00:00
function(myx_cmake_common_target_properties_post_link target)
if(NOT TARGET ${target})
myx_cmake_message_error("MyxCMake: Target ${target} does not exists.")
endif()
get_target_property(_target_type ${target} TYPE)
# 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(NOT _target_type STREQUAL INTERFACE_LIBRARY)
if(CMAKE_CXX_COMPILER_IS_GCC AND MYX_CMAKE_CODE_COVERAGE)
myx_cmake_code_coverage(${target})
endif()
add_sanitizers(${target})
endif()
endfunction()