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()
|
|
|
|
|
|
2024-03-12 13:27:30 +00:00
|
|
|
|
foreach(iter ${ARG_PACKAGES})
|
|
|
|
|
target_include_directories(${TARGET_NAME} INTERFACE ${${iter}_INCLUDE_DIRS})
|
|
|
|
|
target_compile_definitions(${TARGET_NAME} INTERFACE ${${iter}_COMPILE_DEFINITIONS})
|
2022-10-20 08:50:52 +00:00
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
|
|
if(ARG_LINK_LIBRARIES)
|
2024-03-12 13:27:30 +00:00
|
|
|
|
foreach(lib ${ARG_LINK_LIBRARIES})
|
|
|
|
|
if(TARGET ${lib})
|
|
|
|
|
set(exlib ${lib})
|
2024-03-16 09:09:46 +00:00
|
|
|
|
get_target_property(type ${lib} TYPE)
|
|
|
|
|
if(type)
|
|
|
|
|
get_target_property(include_dirs ${lib} INTERFACE_INCLUDE_DIRECTORIES)
|
|
|
|
|
if(include_dirs)
|
|
|
|
|
target_include_directories(${TARGET_NAME} INTERFACE ${include_dirs})
|
|
|
|
|
endif()
|
|
|
|
|
endif()
|
2024-03-12 13:23:02 +00:00
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER "3.13.0")
|
2024-03-12 13:27:30 +00:00
|
|
|
|
install(TARGETS ${lib}
|
2024-03-12 13:23:02 +00:00
|
|
|
|
EXPORT ${TARGET_NAME}Targets
|
|
|
|
|
COMPONENT DEV)
|
|
|
|
|
else()
|
2024-03-12 13:27:42 +00:00
|
|
|
|
get_target_property(target_type ${lib} TYPE)
|
2024-03-12 13:27:30 +00:00
|
|
|
|
if(target_type STREQUAL "OBJECT_LIBRARY")
|
|
|
|
|
if(TARGET "${lib}_static")
|
|
|
|
|
set(exlib "${lib}_static")
|
|
|
|
|
elseif(TARGET "${lib}_shared")
|
|
|
|
|
set(exlib "${lib}_shared")
|
2024-03-11 14:26:43 +00:00
|
|
|
|
endif()
|
|
|
|
|
endif()
|
2024-03-10 06:03:45 +00:00
|
|
|
|
endif()
|
2024-03-12 13:27:30 +00:00
|
|
|
|
target_link_libraries(${TARGET_NAME} INTERFACE ${exlib})
|
2024-03-11 14:26:43 +00:00
|
|
|
|
endif()
|
|
|
|
|
endforeach()
|
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
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-12 13:23:02 +00:00
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER "3.13.0")
|
|
|
|
|
install(
|
|
|
|
|
EXPORT ${TARGET_NAME}Targets
|
|
|
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_NAME}
|
|
|
|
|
COMPONENT DEV)
|
|
|
|
|
endif()
|
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}>)
|
2024-03-12 13:23:02 +00:00
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER "3.13.0")
|
|
|
|
|
install(
|
|
|
|
|
TARGETS ${TARGET_NAME}
|
|
|
|
|
EXPORT ${TARGET_NAME}Targets
|
|
|
|
|
COMPONENT DEV)
|
|
|
|
|
endif()
|
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()
|