myx/lib/TargetSetup.cmake

91 lines
3.0 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-02 13:22:43 +00:00
set(multiValueArgs COMPILE_DEFINITIONS FIND_PACKAGES LINK_LIBRARIES
CPP PUBLIC_HEADERS PRIVATE_HEADERS SOURCES)
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()
get_target_property(type ${NAME} TYPE)
foreach(iter ${ARG_FIND_PACKAGES})
2022-10-02 10:12:07 +00:00
find_package(${iter} REQUIRED)
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_SOURCES)
2022-10-02 13:22:43 +00:00
myx_message_fatal_error("MyxCMake: Use of SOURCES argument")
2022-10-02 10:12:07 +00:00
endif()
if(ARG_CPP)
2022-10-02 13:22:43 +00:00
set_property(TARGET ${NAME} APPEND PROPERTY CPP_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}")
endif()
if(ARG_PUBLIC_HEADERS)
set_property(TARGET ${NAME} APPEND PROPERTY PRIVATE_HEADER_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-09-29 14:54:13 +00:00
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16)
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-03 12:06:53 +00:00
if(NOT IS_DIRECTORY "${PROJECT_BINARY_DIR}/include")
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/include")
endif()
2022-10-02 10:12:07 +00:00
target_include_directories(${PROJECT_NAME} PRIVATE
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)
2022-10-01 20:34:07 +00:00
if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/src")
target_include_directories(${PROJECT_NAME} PRIVATE
2022-10-03 12:06:53 +00:00
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
2022-10-01 20:34:07 +00:00
endif()
if(type STREQUAL EXECUTABLE)
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-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-01 20:34:07 +00:00
target_link_libraries(${NAME} PRIVATE ${ARG_LINK_LIBRARIES})
target_compile_definitions(${NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS})
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}
FILES ${ARG_PUBLIC_HEADERS}
DESTINATION ${CMAKE_INSTALL_PREFIX}
COMPONENT dev
)
endif()
2022-09-29 14:54:13 +00:00
endfunction()