myx/MyxCMake/lib/TargetSetup.cmake

121 lines
4.6 KiB
CMake
Raw Normal View History

2022-09-29 14:54:13 +00:00
include_guard(GLOBAL)
function(myx_target_setup NAME)
set(options)
set(oneValueArgs PCH)
2022-10-04 03:08:31 +00:00
set(multiValueArgs COMPILE_DEFINITIONS PACKAGES LINK_LIBRARIES
2022-10-04 05:08:30 +00:00
CPP INTERFACE_HEADERS PUBLIC_HEADERS PRIVATE_HEADERS)
2022-09-29 14:54:13 +00:00
2022-10-01 20:34:07 +00:00
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT TARGET ${NAME})
myx_message_fatal_error("Target ${NAME} does not exists.")
endif()
2022-10-04 11:27:58 +00:00
get_target_property(target_type ${NAME} TYPE)
2022-10-01 20:34:07 +00:00
2022-10-04 03:08:31 +00:00
foreach(iter ${ARG_PACKAGES})
2022-10-02 10:12:07 +00:00
target_include_directories(${NAME} PRIVATE ${${iter}_INCLUDE_DIRS})
target_compile_definitions(${NAME} PRIVATE ${${iter}_COMPILE_DEFINITIONS})
2022-09-29 14:54:13 +00:00
endforeach()
2022-10-01 20:34:07 +00:00
target_compile_definitions(${NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS})
2022-09-29 14:54:13 +00:00
2022-10-02 10:12:07 +00:00
if(ARG_CPP)
2022-10-02 13:22:43 +00:00
set_property(TARGET ${NAME} APPEND PROPERTY CPP_FILES ${ARG_CPP})
set_property(TARGET ${NAME} APPEND PROPERTY TR_FILES ${ARG_CPP})
2022-10-02 10:12:07 +00:00
endif()
2022-10-02 13:22:43 +00:00
if(ARG_PUBLIC_HEADERS)
set_property(TARGET ${NAME} APPEND PROPERTY PUBLIC_HEADER_FILES "${ARG_PUBLIC_HEADERS}")
set_property(TARGET ${NAME} APPEND PROPERTY TR_FILES ${ARG_PUBLIC_HEADERS})
2022-10-02 13:22:43 +00:00
endif()
if(ARG_PRIVATE_HEADERS)
2022-10-02 13:22:43 +00:00
set_property(TARGET ${NAME} APPEND PROPERTY PRIVATE_HEADER_FILES "${ARG_PRIVATE_HEADERS}")
set_property(TARGET ${NAME} APPEND PROPERTY TR_FILES ${ARG_PRIVATE_HEADERS})
2022-10-02 10:12:07 +00:00
endif()
2022-10-01 20:34:07 +00:00
if(ARG_PCH)
2022-10-05 07:01:02 +00:00
if((${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16) AND (PROJECT_IS_TOP_LEVEL OR MYX_USE_LOCAL_DIRECTORIES))
2022-10-02 10:12:07 +00:00
target_precompile_headers(${NAME} PRIVATE ${ARG_PCH})
2022-09-29 14:54:13 +00:00
else()
2022-10-02 10:12:07 +00:00
target_compile_options(${NAME} PRIVATE -include ${ARG_PCH})
2022-10-01 20:34:07 +00:00
endif()
2022-10-02 13:22:43 +00:00
set_property(TARGET ${NAME} APPEND PROPERTY PRIVATE_HEADER_FILES "${ARG_PCH}")
2022-10-01 20:34:07 +00:00
endif()
2022-10-04 11:27:58 +00:00
if(NOT target_type STREQUAL "INTERFACE_LIBRARY")
2022-10-01 20:34:07 +00:00
target_include_directories(${PROJECT_NAME} PRIVATE
2022-10-04 05:08:30 +00:00
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)
if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/src")
target_include_directories(${PROJECT_NAME} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
endif()
2022-10-01 20:34:07 +00:00
endif()
2022-10-04 11:27:58 +00:00
if(target_type STREQUAL "EXECUTABLE")
2022-10-01 20:34:07 +00:00
if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
endif()
set_target_properties(${NAME} PROPERTIES
POSITION_INDEPENDENT_CODE ON
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}
)
if(CMAKE_CXX_COMPILE_OPTIONS_PIE)
target_compile_options(${NAME} PUBLIC ${CMAKE_CXX_COMPILE_OPTIONS_PIE})
2022-09-29 14:54:13 +00:00
endif()
2022-10-01 20:34:07 +00:00
install(TARGETS ${NAME} COMPONENT main RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2022-09-29 14:54:13 +00:00
endif()
2022-10-04 10:25:07 +00:00
target_sources(${NAME} PUBLIC $<BUILD_INTERFACE:${ARG_INTERFACE_HEADERS}>)
2022-10-02 13:22:43 +00:00
target_sources(${NAME} PUBLIC ${ARG_PUBLIC_HEADERS})
target_sources(${NAME} PRIVATE ${ARG_CPP} ${ARG_PCH} ${ARG_PRIVATE_HEADERS})
2022-10-06 21:03:48 +00:00
target_compile_definitions(${NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS})
# CMake до версии 3.12 не умеет извлекать из целей типа `OBJECT_LIBRARY`
# информацию о заголовочных файлах. Это обход.
if(${CMAKE_VERSION} VERSION_GREATER "3.11.99")
2022-10-05 07:01:02 +00:00
target_link_libraries(${NAME} PRIVATE ${ARG_LINK_LIBRARIES})
2022-10-06 21:03:48 +00:00
else()
if((NOT ${target_type} STREQUAL "OBJECT_LIBRARY") AND
(NOT ${target_type} STREQUAL "EXECUTABLE"))
target_link_libraries(${NAME} PRIVATE ${ARG_LINK_LIBRARIES})
else()
foreach(link_library ${ARG_LINK_LIBRARIES})
if(TARGET ${link_library})
get_target_property(lib_type ${link_library} TYPE)
if(lib_type)
get_target_property(include_dir ${link_library} INTERFACE_INCLUDE_DIRECTORIES)
if(include_dir)
target_include_directories(${NAME} PUBLIC ${include_dir})
endif()
if(${target_type} STREQUAL "EXECUTABLE")
if(${lib_type} STREQUAL "OBJECT_LIBRARY")
if(TARGET ${link_library}_static)
target_link_libraries(${NAME} PRIVATE ${link_library}_static)
else()
target_link_libraries(${NAME} PRIVATE ${link_library}_shared)
endif()
else()
target_link_libraries(${NAME} PRIVATE ${link_library})
endif()
endif()
endif()
endif()
endforeach()
endif()
2022-10-05 07:01:02 +00:00
endif()
2022-10-02 13:22:43 +00:00
# Установка публичных заголовочных файлов
2022-10-02 13:33:14 +00:00
if(PROJECT_IS_TOP_LEVEL)
install_relative(${PROJECT_SOURCE_DIR}
2022-10-04 05:08:30 +00:00
FILES ${ARG_PUBLIC_HEADERS} ${ARG_INTERFACE_HEADERS}
2022-10-02 13:33:14 +00:00
DESTINATION ${CMAKE_INSTALL_PREFIX}
COMPONENT dev
)
endif()
2022-09-29 14:54:13 +00:00
endfunction()