2022-09-29 14:54:13 +00:00
|
|
|
|
include_guard(GLOBAL)
|
|
|
|
|
|
2022-10-19 11:39:26 +00:00
|
|
|
|
function(myx_target_setup TARGET_NAME)
|
2022-09-29 14:54:13 +00:00
|
|
|
|
set(options)
|
|
|
|
|
set(oneValueArgs PCH)
|
2023-12-14 11:00:20 +00:00
|
|
|
|
set(multiValueArgs PACKAGES
|
|
|
|
|
COMPILE_DEFINITIONS INCLUDE_DIRECTORIES LINK_LIBRARIES
|
2022-10-19 11:39:26 +00:00
|
|
|
|
CPP PUBLIC_HEADERS PRIVATE_HEADERS)
|
2022-10-01 20:34:07 +00:00
|
|
|
|
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
|
|
|
|
2022-10-19 11:39:26 +00:00
|
|
|
|
if(NOT TARGET ${TARGET_NAME})
|
|
|
|
|
myx_message_fatal_error("Target ${TARGET_NAME} does not exists.")
|
2022-10-01 20:34:07 +00:00
|
|
|
|
endif()
|
|
|
|
|
|
2024-03-12 13:27:30 +00:00
|
|
|
|
get_target_property(target_type ${TARGET_NAME} TYPE)
|
|
|
|
|
if(${target_type} STREQUAL "INTERFACE_LIBRARY")
|
2022-10-20 08:50:52 +00:00
|
|
|
|
myx_message_fatal_error("This function will not work for interface targets.")
|
|
|
|
|
endif()
|
2022-10-01 20:34:07 +00:00
|
|
|
|
|
2022-10-04 03:08:31 +00:00
|
|
|
|
foreach(iter ${ARG_PACKAGES})
|
2022-10-19 11:39:26 +00:00
|
|
|
|
target_include_directories(${TARGET_NAME} PRIVATE ${${iter}_INCLUDE_DIRS})
|
|
|
|
|
target_compile_definitions(${TARGET_NAME} PRIVATE ${${iter}_COMPILE_DEFINITIONS})
|
2022-09-29 14:54:13 +00:00
|
|
|
|
endforeach()
|
|
|
|
|
|
2022-10-02 10:12:07 +00:00
|
|
|
|
if(ARG_CPP)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY CPP_FILES ${ARG_CPP})
|
|
|
|
|
set_property(TARGET ${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)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PUBLIC_HEADER_FILES "${ARG_PUBLIC_HEADERS}")
|
|
|
|
|
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_PUBLIC_HEADERS})
|
2022-10-02 13:22:43 +00:00
|
|
|
|
endif()
|
|
|
|
|
|
2022-10-03 14:15:53 +00:00
|
|
|
|
if(ARG_PRIVATE_HEADERS)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PRIVATE_HEADER_FILES "${ARG_PRIVATE_HEADERS}")
|
|
|
|
|
set_property(TARGET ${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-19 11:39:26 +00:00
|
|
|
|
target_precompile_headers(${TARGET_NAME} PRIVATE ${ARG_PCH})
|
2022-09-29 14:54:13 +00:00
|
|
|
|
else()
|
2022-10-19 11:39:26 +00:00
|
|
|
|
target_compile_options(${TARGET_NAME} PRIVATE -include ${ARG_PCH})
|
2022-10-01 20:34:07 +00:00
|
|
|
|
endif()
|
2022-10-19 11:39:26 +00:00
|
|
|
|
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PRIVATE_HEADER_FILES "${ARG_PCH}")
|
2022-10-01 20:34:07 +00:00
|
|
|
|
endif()
|
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
target_include_directories(${PROJECT_NAME} PRIVATE
|
|
|
|
|
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)
|
2022-10-04 05:08:30 +00:00
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/src")
|
|
|
|
|
target_include_directories(${PROJECT_NAME} PRIVATE
|
|
|
|
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
|
2022-10-01 20:34:07 +00:00
|
|
|
|
endif()
|
|
|
|
|
|
2024-03-12 13:27:30 +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()
|
2022-10-08 14:23:13 +00:00
|
|
|
|
if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}")
|
|
|
|
|
target_include_directories(${PROJECT_NAME} PRIVATE
|
|
|
|
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}>)
|
|
|
|
|
endif()
|
2022-10-01 20:34:07 +00:00
|
|
|
|
|
2022-10-19 11:39:26 +00:00
|
|
|
|
set_target_properties(${TARGET_NAME} PROPERTIES
|
2022-10-01 20:34:07 +00:00
|
|
|
|
POSITION_INDEPENDENT_CODE ON
|
|
|
|
|
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}
|
|
|
|
|
)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
2022-10-01 20:34:07 +00:00
|
|
|
|
if(CMAKE_CXX_COMPILE_OPTIONS_PIE)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
target_compile_options(${TARGET_NAME} PUBLIC ${CMAKE_CXX_COMPILE_OPTIONS_PIE})
|
2022-09-29 14:54:13 +00:00
|
|
|
|
endif()
|
2022-10-19 11:39:26 +00:00
|
|
|
|
install(TARGETS ${TARGET_NAME} COMPONENT MAIN RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
2022-09-29 14:54:13 +00:00
|
|
|
|
endif()
|
|
|
|
|
|
2024-03-11 09:14:20 +00:00
|
|
|
|
# target_sources(${TARGET_NAME} PUBLIC ${ARG_PUBLIC_HEADERS})
|
2022-10-20 08:50:52 +00:00
|
|
|
|
target_sources(${TARGET_NAME} PRIVATE ${ARG_CPP} ${ARG_PCH} ${ARG_PRIVATE_HEADERS})
|
2024-03-11 09:14:20 +00:00
|
|
|
|
target_sources(${TARGET_NAME} PUBLIC $<BUILD_INTERFACE:${ARG_PUBLIC_HEADERS}>)
|
2022-10-20 08:50:52 +00:00
|
|
|
|
target_compile_definitions(${TARGET_NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS})
|
2023-12-14 11:00:20 +00:00
|
|
|
|
target_include_directories(${TARGET_NAME} PRIVATE ${ARG_INCLUDE_DIRECTORIES})
|
2022-10-06 21:03:48 +00:00
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
# CMake до версии 3.12 не умеет извлекать из целей типа `OBJECT_LIBRARY`
|
|
|
|
|
# информацию о заголовочных файлах. Это обход.
|
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER "3.11.99")
|
|
|
|
|
target_link_libraries(${TARGET_NAME} PRIVATE ${ARG_LINK_LIBRARIES})
|
2022-10-06 21:03:48 +00:00
|
|
|
|
else()
|
2024-03-12 13:27:30 +00:00
|
|
|
|
if((NOT ${target_type} STREQUAL "OBJECT_LIBRARY") AND
|
|
|
|
|
(NOT ${target_type} STREQUAL "EXECUTABLE"))
|
2022-10-19 11:39:26 +00:00
|
|
|
|
target_link_libraries(${TARGET_NAME} PRIVATE ${ARG_LINK_LIBRARIES})
|
2022-10-06 21:03:48 +00:00
|
|
|
|
else()
|
2022-10-20 08:50:52 +00:00
|
|
|
|
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_dirs ${__link_library} INTERFACE_INCLUDE_DIRECTORIES)
|
|
|
|
|
if(__include_dirs)
|
|
|
|
|
target_include_directories(${TARGET_NAME} PUBLIC ${__include_dirs})
|
|
|
|
|
endif()
|
2024-03-12 13:27:30 +00:00
|
|
|
|
if(${target_type} STREQUAL "EXECUTABLE")
|
2022-10-20 08:50:52 +00:00
|
|
|
|
if(${__lib_type} STREQUAL "OBJECT_LIBRARY")
|
|
|
|
|
if(TARGET ${__link_library}_static)
|
|
|
|
|
target_link_libraries(${TARGET_NAME} PRIVATE ${__link_library}_static)
|
2022-10-06 21:03:48 +00:00
|
|
|
|
else()
|
2022-10-20 08:50:52 +00:00
|
|
|
|
target_link_libraries(${TARGET_NAME} PRIVATE ${__link_library}_shared)
|
|
|
|
|
endif()
|
|
|
|
|
else()
|
|
|
|
|
target_link_libraries(${TARGET_NAME} PRIVATE ${__link_library})
|
2022-10-06 21:03:48 +00:00
|
|
|
|
endif()
|
|
|
|
|
endif()
|
|
|
|
|
endif()
|
2023-04-13 06:58:45 +00:00
|
|
|
|
else()
|
2024-03-12 13:27:30 +00:00
|
|
|
|
if((${CMAKE_VERSION} VERSION_GREATER "3.7.99") OR (NOT target_type STREQUAL "OBJECT_LIBRARY"))
|
2024-02-12 13:45:38 +00:00
|
|
|
|
target_link_libraries(${TARGET_NAME} PRIVATE ${__link_library})
|
|
|
|
|
endif()
|
2022-10-20 08:50:52 +00:00
|
|
|
|
endif()
|
|
|
|
|
endforeach()
|
2022-10-06 21:03:48 +00:00
|
|
|
|
endif()
|
2022-10-05 07:01:02 +00:00
|
|
|
|
endif()
|
2022-10-02 13:22:43 +00:00
|
|
|
|
|
|
|
|
|
# Установка публичных заголовочных файлов
|
2022-10-20 08:50:52 +00:00
|
|
|
|
if(PROJECT_IS_TOP_LEVEL AND ARG_PUBLIC_HEADERS)
|
2022-10-08 22:01:31 +00:00
|
|
|
|
myx_install_relative(${PROJECT_SOURCE_DIR}
|
2022-10-19 11:39:26 +00:00
|
|
|
|
FILES ${ARG_PUBLIC_HEADERS}
|
2022-10-02 13:33:14 +00:00
|
|
|
|
DESTINATION ${CMAKE_INSTALL_PREFIX}
|
2022-10-09 01:23:55 +00:00
|
|
|
|
COMPONENT DEV
|
2022-10-02 13:33:14 +00:00
|
|
|
|
)
|
|
|
|
|
endif()
|
2022-09-29 14:54:13 +00:00
|
|
|
|
endfunction()
|