myx/lib/TargetSetup.cmake
2022-10-01 23:34:07 +03:00

57 lines
2.0 KiB
CMake

include_guard(GLOBAL)
function(myx_target_setup NAME)
set(options)
set(oneValueArgs PCH)
set(multiValueArgs COMPILE_DEFINITIONS FIND_PACKAGES LINK_LIBRARIES SOURCES)
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})
find_package(${iter} REQUIRED)
target_include_directories(${NAME} PRIVATE ${${iter}_INCLUDE_DIRS})
target_compile_definitions(${NAME} PRIVATE ${${iter}_COMPILE_DEFINITIONS})
endforeach()
target_compile_definitions(${NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS})
if(ARG_PCH)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16)
target_precompile_headers(${NAME} PRIVATE "${PROJECT_SOURCE_DIR}/${ARG_PCH}")
else()
target_compile_options(${NAME} PRIVATE -include "${PROJECT_SOURCE_DIR}/${ARG_PCH}")
endif()
target_sources(${NAME} PRIVATE "${PROJECT_SOURCE_DIR}/${ARG_PCH}")
endif()
if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/src")
target_include_directories(${PROJECT_NAME} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
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})
endif()
install(TARGETS ${NAME} COMPONENT main RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
target_sources(${NAME} PRIVATE ${ARG_SOURCES})
target_link_libraries(${NAME} PRIVATE ${ARG_LINK_LIBRARIES})
target_compile_definitions(${NAME} PRIVATE ${ARG_COMPILE_DEFINITIONS})
endfunction()