2022-10-19 11:39:26 +00:00
|
|
|
|
#[=======================================================================[.rst:
|
2022-10-20 08:50:52 +00:00
|
|
|
|
myx_add_interface_library
|
|
|
|
|
-------------------------
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
Вспомогательная функция для создания интерфейсной библиотеки::
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
myx_add_interface_library(TARGET_NAME
|
|
|
|
|
[ PACKAGES packages ] |
|
|
|
|
|
[ LINK_LIBRARIES link_libraries ] |
|
2022-10-19 11:39:26 +00:00
|
|
|
|
[ OUTPUT_NAME output_name ] |
|
2022-10-20 08:50:52 +00:00
|
|
|
|
[ HEADERS headers ])
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
Обязательные параметры: `TARGET_NAME` - имя библиотеки.
|
2022-10-19 11:39:26 +00:00
|
|
|
|
Параметр `OUTPUT_NAME` определяет базовое имя выходных файлов.
|
|
|
|
|
Все остальные параметры передаются в стандартную функцию `add_library()`
|
|
|
|
|
|
|
|
|
|
#]=======================================================================]
|
|
|
|
|
|
|
|
|
|
include_guard(GLOBAL)
|
|
|
|
|
|
|
|
|
|
include(CMakePackageConfigHelpers)
|
|
|
|
|
|
|
|
|
|
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
|
|
|
|
|
set(MYX_CMAKE_LIB_DIR_BACKPORT "${CMAKE_CURRENT_LIST_DIR}")
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
function(myx_add_interface_library TARGET_NAME)
|
|
|
|
|
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
|
|
|
|
|
set(CMAKE_CURRENT_FUNCTION_LIST_DIR ${MYX_CMAKE_LIB_DIR_BACKPORT})
|
|
|
|
|
endif()
|
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
if(TARGET ${TARGET_NAME})
|
|
|
|
|
myx_message_fatal_error("Target ${TARGET_NAME} already exists")
|
|
|
|
|
endif()
|
|
|
|
|
|
2022-10-19 11:39:26 +00:00
|
|
|
|
set(options)
|
|
|
|
|
set(oneValueArgs OUTPUT_NAME)
|
2022-10-20 08:50:52 +00:00
|
|
|
|
set(multiValueArgs HEADERS LINK_LIBRARIES PACKAGES)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
|
|
|
|
|
|
|
|
if(NOT ARG_OUTPUT_NAME)
|
|
|
|
|
set(ARG_OUTPUT_NAME ${TARGET_NAME})
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
# Вызов стандартной функции `add_library()`
|
2022-10-20 09:45:27 +00:00
|
|
|
|
if(${CMAKE_VERSION} VERSION_LESS "3.19.0")
|
2022-10-20 08:50:52 +00:00
|
|
|
|
add_library(${TARGET_NAME} INTERFACE)
|
2022-10-20 09:45:27 +00:00
|
|
|
|
target_sources(${TARGET_NAME} INTERFACE $<BUILD_INTERFACE:${ARG_HEADERS}>)
|
2022-10-20 08:50:52 +00:00
|
|
|
|
else()
|
|
|
|
|
add_library(${TARGET_NAME} INTERFACE ${ARG_HEADERS})
|
|
|
|
|
endif()
|
|
|
|
|
|
2023-07-30 11:29:25 +00:00
|
|
|
|
|
2022-10-20 08:50:52 +00:00
|
|
|
|
foreach(__iter ${ARG_PACKAGES})
|
|
|
|
|
target_include_directories(${TARGET_NAME} INTERFACE ${${__iter}_INCLUDE_DIRS})
|
|
|
|
|
target_compile_definitions(${TARGET_NAME} INTERFACE ${${__iter}_COMPILE_DEFINITIONS})
|
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
|
|
if(ARG_LINK_LIBRARIES)
|
|
|
|
|
target_link_libraries(${TARGET_NAME} INTERFACE ${ARG_LINK_LIBRARIES})
|
2024-03-10 06:03:45 +00:00
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER "3.15.0")
|
|
|
|
|
foreach(__lib ${ARG_LINK_LIBRARIES})
|
|
|
|
|
if(TARGET ${__lib})
|
|
|
|
|
install(
|
|
|
|
|
TARGETS ${__lib}
|
|
|
|
|
EXPORT ${TARGET_NAME}Targets
|
|
|
|
|
COMPONENT DEV)
|
|
|
|
|
endif()
|
|
|
|
|
endforeach()
|
|
|
|
|
endif()
|
2022-10-20 08:50:52 +00:00
|
|
|
|
endif()
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
|
|
|
|
# Библиотека, состоящая только из заголовочных файлов не требует сборки.
|
|
|
|
|
# Стандартные пути к заголовочным файлам
|
2023-07-30 11:29:25 +00:00
|
|
|
|
target_include_directories(${TARGET_NAME} INTERFACE
|
|
|
|
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
|
|
|
|
$<INSTALL_INTERFACE:include>
|
|
|
|
|
)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
|
|
|
|
# Если вызов был выполнен не из проекта верхнего уровня,
|
|
|
|
|
# то созданная цель исключается из цели `all`.
|
|
|
|
|
# При этом сама цель `${TARGET_NAME}` может участвовать в сборке,
|
|
|
|
|
# если окажется в перечне зависимостей.
|
2023-04-13 07:51:49 +00:00
|
|
|
|
if(NOT PROJECT_IS_TOP_LEVEL)
|
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER "3.17.0")
|
|
|
|
|
set_target_properties(${TARGET_NAME} PROPERTIES EXCLUDE_FROM_ALL True)
|
|
|
|
|
else()
|
|
|
|
|
set_target_properties(${TARGET_NAME} PROPERTIES INTERFACE_EXCLUDE_FROM_ALL True)
|
|
|
|
|
endif()
|
2022-10-19 11:39:26 +00:00
|
|
|
|
return()
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
# Правила для установки библиотек и относящихся к ним файлов.
|
|
|
|
|
# Если вызов был выполнен не из проекта верхнего уровня,
|
|
|
|
|
# то эти правила не обрабатываются.
|
|
|
|
|
write_basic_package_version_file(
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}ConfigVersion.cmake
|
|
|
|
|
VERSION ${PROJECT_VERSION}
|
|
|
|
|
COMPATIBILITY AnyNewerVersion
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
configure_package_config_file(
|
|
|
|
|
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/library-config.cmake.in
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}Config.cmake
|
|
|
|
|
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_NAME}
|
|
|
|
|
NO_SET_AND_CHECK_MACRO
|
|
|
|
|
NO_CHECK_REQUIRED_COMPONENTS_MACRO
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
install(EXPORT ${TARGET_NAME}Targets
|
|
|
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_NAME}
|
2022-10-20 08:50:52 +00:00
|
|
|
|
COMPONENT DEV)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
|
|
|
|
install(
|
|
|
|
|
FILES
|
|
|
|
|
${PROJECT_BINARY_DIR}/${TARGET_NAME}ConfigVersion.cmake
|
|
|
|
|
${PROJECT_BINARY_DIR}/${TARGET_NAME}Config.cmake
|
|
|
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_NAME}
|
2022-10-20 08:50:52 +00:00
|
|
|
|
COMPONENT DEV)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
|
|
|
|
# Формирование файла для pkg-config
|
|
|
|
|
file(WRITE "${PROJECT_BINARY_DIR}/${TARGET_NAME}.pc"
|
|
|
|
|
"prefix=${CMAKE_INSTALL_PREFIX}\n"
|
|
|
|
|
"exec_prefix=${CMAKE_INSTALL_PREFIX}\n"
|
|
|
|
|
"includedir=${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Name: ${TARGET_NAME}\n"
|
|
|
|
|
"Description: ${TARGET_NAME} library\n"
|
|
|
|
|
"Version: ${PROJECT_VERSION}\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Requires:\n"
|
|
|
|
|
"Cflags: -I${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}\n")
|
|
|
|
|
|
|
|
|
|
# Установка файла для pkg-config
|
|
|
|
|
install(
|
|
|
|
|
FILES "${PROJECT_BINARY_DIR}/${TARGET_NAME}.pc"
|
2022-10-20 08:50:52 +00:00
|
|
|
|
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
|
|
|
|
|
COMPONENT DEV)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
|
|
|
|
# Установка библиотеки из заголовочных файлов
|
|
|
|
|
target_include_directories(${TARGET_NAME} SYSTEM INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
|
|
|
|
|
install(
|
|
|
|
|
TARGETS ${TARGET_NAME}
|
|
|
|
|
EXPORT ${TARGET_NAME}Targets
|
2022-10-20 08:50:52 +00:00
|
|
|
|
COMPONENT DEV)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
|
|
|
|
|
# Установка публичных заголовочных файлов
|
2022-10-20 08:50:52 +00:00
|
|
|
|
if(PROJECT_IS_TOP_LEVEL AND ARG_HEADERS)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
myx_install_relative(${PROJECT_SOURCE_DIR}
|
2022-10-20 08:50:52 +00:00
|
|
|
|
FILES ${ARG_HEADERS}
|
2022-10-19 11:39:26 +00:00
|
|
|
|
DESTINATION ${CMAKE_INSTALL_PREFIX}
|
2022-10-20 08:50:52 +00:00
|
|
|
|
COMPONENT DEV)
|
2022-10-19 11:39:26 +00:00
|
|
|
|
endif()
|
|
|
|
|
endfunction()
|