myx-cmake/MyxCMake/modules/MyxCMakeCommonTargetProperties.cmake

158 lines
6.2 KiB
CMake
Raw Normal View History

2021-12-08 11:32:04 +00:00
function(myx_cmake_common_target_properties target)
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_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)
set_target_properties(
${target} PROPERTIES 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()
endif()
if(APPLE)
target_compile_definitions(${target} ${__visibility} Darwin)
endif()
if(_target_type STREQUAL OBJECT_LIBRARY)
# target_include_directories(${target} PUBLIC
# $<INSTALL_INTERFACE:include/${target}>)
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()
# 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_clang_tidy_analyze(${target})
myx_cmake_clang_check_analyze(${target})
myx_cmake_clazy_analyze(${target})
myx_cmake_pvs_studio_analyze(${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 ${target}-symlinks AND UNIX)
add_custom_target(
${target}-symlinks
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} ${target}-symlinks)
endif()
endfunction()