myx/lib/AddLibrary.cmake

57 lines
1.7 KiB
CMake
Raw Normal View History

2022-09-29 10:34:57 +00:00
include_guard(GLOBAL)
include(GenerateExportHeader)
include(GNUInstallDirs)
function(myx_add_library)
set(options)
set(oneValueArgs PCH TYPE)
set(multiValueArgs SOURCES COMPILE_DEFINITIONS)
cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT arg_TYPE)
set(arg_TYPE "OBJECT")
endif()
add_library(${PROJECT_NAME} ${arg_TYPE})
if(arg_TYPE STREQUAL INTERFACE)
target_include_directories(${PROJECT_NAME}
INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
else()
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
PRIVATE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
string(TOUPPER ${PROJECT_NAME} suffix)
generate_export_header(${PROJECT_NAME}
BASE_NAME "${suffix}"
EXPORT_MACRO_NAME "EXPORT_${suffix}"
DEPRECATED_MACRO_NAME "DEPRECATED_${suffix}"
NO_DEPRECATED_MACRO_NAME "NO_DEPRECATED_${suffix}"
NO_EXPORT_MACRO_NAME "NO_EXPORT_${suffix}"
STATIC_DEFINE "STATIC_DEFINE_${suffix}"
EXPORT_FILE_NAME "${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/export.hpp"
DEFINE_NO_DEPRECATED
)
endif()
if(arg_PCH)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16)
target_precompile_headers(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/${arg_PCH})
else()
target_compile_options(${PROJECT_NAME} PRIVATE -include ${PROJECT_SOURCE_DIR}/${arg_PCH})
endif()
endif()
target_compile_definitions(${PROJECT_NAME} PRIVATE ${arg_COMPILE_DEFINITIONS})
endfunction()