This commit is contained in:
Andrei Astafev 2022-09-29 17:20:02 +03:00
parent 2a29e33794
commit 3e8648a045
5 changed files with 54 additions and 55 deletions

View File

@ -1,4 +1,4 @@
set(MYX_CMAKE_PACKAGE_VERSION "1.99.7") set(MYX_CMAKE_PACKAGE_VERSION "1.99.8")
if(MYX_CMAKE_PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) if(MYX_CMAKE_PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE) set(PACKAGE_VERSION_COMPATIBLE FALSE)
else() else()

View File

@ -11,6 +11,6 @@
`project` добавить строку: `project` добавить строку:
```cmake ```cmake
find_package(MyxCMake 1.99.7 PATHS cmake/myx NO_DEFAULT_PATH QUIET REQUIRED) find_package(MyxCMake 1.99.8 PATHS cmake/myx NO_DEFAULT_PATH QUIET REQUIRED)
``` ```

View File

@ -2,87 +2,88 @@ include_guard(GLOBAL)
include(GenerateExportHeader) include(GenerateExportHeader)
function(myx_add_library) function(myx_add_library NAME)
set(options) set(options)
set(oneValueArgs PCH TYPE) set(oneValueArgs PCH TYPE)
set(multiValueArgs SOURCES COMPILE_DEFINITIONS) set(multiValueArgs SOURCES LINK_LIBRARIES COMPILE_DEFINITIONS)
cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT arg_TYPE) if(NOT arg_TYPE)
set(arg_TYPE "OBJECT") set(arg_TYPE "OBJECT")
endif() endif()
add_library(${PROJECT_NAME} ${arg_TYPE}) add_library(${NAME} ${arg_TYPE})
if(arg_TYPE STREQUAL INTERFACE) if(arg_TYPE STREQUAL INTERFACE)
target_include_directories(${PROJECT_NAME} target_include_directories(${NAME}
INTERFACE INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
) )
else() else()
string(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPER) string(TOUPPER ${NAME} PROJECT_NAME_UPPER)
# Опция для разрешения сборки разделяемой библиотеки # Опция для разрешения сборки разделяемой библиотеки
option(${PROJECT_NAME_UPPER}_BUILD_SHARED "Build shared library" ON) option(${PROJECT_NAME_UPPER}_BUILD_SHARED "Build shared library" ON)
# Опция для разрешения сборки статической библиотеки # Опция для разрешения сборки статической библиотеки
option(${PROJECT_NAME_UPPER}_BUILD_STATIC "Build static library" ON) option(${PROJECT_NAME_UPPER}_BUILD_STATIC "Build static library" ON)
target_include_directories(${PROJECT_NAME} target_include_directories(${NAME}
PUBLIC PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
PRIVATE PRIVATE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
) )
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(${NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
string(TOUPPER ${PROJECT_NAME} suffix) string(TOUPPER ${NAME} suffix)
generate_export_header(${PROJECT_NAME} generate_export_header(${NAME}
BASE_NAME "${suffix}" BASE_NAME "${suffix}"
EXPORT_MACRO_NAME "EXPORT_${suffix}" EXPORT_MACRO_NAME "EXPORT_${suffix}"
DEPRECATED_MACRO_NAME "DEPRECATED_${suffix}" DEPRECATED_MACRO_NAME "DEPRECATED_${suffix}"
NO_DEPRECATED_MACRO_NAME "NO_DEPRECATED_${suffix}" NO_DEPRECATED_MACRO_NAME "NO_DEPRECATED_${suffix}"
NO_EXPORT_MACRO_NAME "NO_EXPORT_${suffix}" NO_EXPORT_MACRO_NAME "NO_EXPORT_${suffix}"
STATIC_DEFINE "STATIC_DEFINE_${suffix}" STATIC_DEFINE "STATIC_DEFINE_${suffix}"
EXPORT_FILE_NAME "${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/export_autogen.hpp" EXPORT_FILE_NAME "${PROJECT_SOURCE_DIR}/include/${NAME}/export_autogen.hpp"
DEFINE_NO_DEPRECATED DEFINE_NO_DEPRECATED
) )
# Цель для создания разделяемой библиотеки из объектных файлов # Цель для создания разделяемой библиотеки из объектных файлов
if(${PROJECT_NAME_UPPER}_BUILD_SHARED) if(${PROJECT_NAME_UPPER}_BUILD_SHARED)
# Для создания разделяемой библиотеки используются объектные файлы цели ${PROJECT_NAME} # Для создания разделяемой библиотеки используются объектные файлы цели ${NAME}
add_library(${PROJECT_NAME}_shared SHARED $<TARGET_OBJECTS:${PROJECT_NAME}>) add_library(${NAME}_shared SHARED $<TARGET_OBJECTS:${NAME}>)
# Установка дополнительных свойств для цели ${PROJECT_NAME}_shared # Установка дополнительных свойств для цели ${NAME}_shared
# OUTPUT_NAME: базовое имя выходного файла (без учёта расширения) # OUTPUT_NAME: базовое имя выходного файла (без учёта расширения)
# VERSION: версия библиотеки # VERSION: версия библиотеки
# SOVERSION: мажорная версия библиотеки # SOVERSION: мажорная версия библиотеки
set_target_properties(${PROJECT_NAME}_shared set_target_properties(${NAME}_shared
PROPERTIES PROPERTIES
VERSION ${PROJECT_VERSION} VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR} SOVERSION ${PROJECT_VERSION_MAJOR}
OUTPUT_NAME ${PROJECT_NAME}) OUTPUT_NAME ${NAME})
endif() endif()
# Цель для создания статической библиотеки из объектных файлов # Цель для создания статической библиотеки из объектных файлов
if(${PROJECT_NAME_UPPER}_BUILD_STATIC) if(${PROJECT_NAME_UPPER}_BUILD_STATIC)
# Для создания статической библиотеки используются объектные файлы цели ${PROJECT_NAME} # Для создания статической библиотеки используются объектные файлы цели ${NAME}
add_library(${PROJECT_NAME}_static STATIC $<TARGET_OBJECTS:${PROJECT_NAME}>) add_library(${NAME}_static STATIC $<TARGET_OBJECTS:${NAME}>)
# Установка дополнительных свойств для цели ${PROJECT_NAME}_static # Установка дополнительных свойств для цели ${NAME}_static
# OUTPUT_NAME: базовое имя выходного файла (без учёта расширения) # OUTPUT_NAME: базовое имя выходного файла (без учёта расширения)
set_target_properties(${PROJECT_NAME}_static set_target_properties(${NAME}_static
PROPERTIES PROPERTIES
OUTPUT_NAME ${PROJECT_NAME}) OUTPUT_NAME ${NAME})
endif() endif()
endif() endif()
if(arg_PCH) if(arg_PCH)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16) if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.16)
target_precompile_headers(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/${arg_PCH}) target_precompile_headers(${NAME} PRIVATE ${PROJECT_SOURCE_DIR}/${arg_PCH})
else() else()
target_compile_options(${PROJECT_NAME} PRIVATE -include ${PROJECT_SOURCE_DIR}/${arg_PCH}) target_compile_options(${NAME} PRIVATE -include ${PROJECT_SOURCE_DIR}/${arg_PCH})
endif() endif()
endif() endif()
target_sources(${PROJECT_NAME} PRIVATE ${arg_SOURCES}) target_sources(${NAME} PRIVATE ${arg_SOURCES} ${PROJECT_SOURCE_DIR}/${arg_PCH})
target_compile_definitions(${PROJECT_NAME} PRIVATE ${arg_COMPILE_DEFINITIONS}) target_compile_definitions(${NAME} PRIVATE ${arg_COMPILE_DEFINITIONS})
target_link_libraries(${NAME} PRIVATE ${arg_LINK_LIBRARIES})
endfunction() endfunction()

View File

@ -1,22 +1,26 @@
include_guard(GLOBAL) include_guard(GLOBAL)
if(${CMAKE_VERSION} VERSION_LESS "3.17.0") if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(CMAKE_CURRENT_FUNCTION_LIST_DIR "${CMAKE_CURRENT_LIST_DIR}") set(CMAKE_CURRENT_FUNCTION_LIST_DIR ${CMAKE_CURRENT_LIST_DIR})
endif() endif()
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
function(myx_install_library) function(myx_install_library NAME)
if(NOT PROJECT_IS_TOP_LEVEL)
return()
endif()
write_basic_package_version_file( write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/${NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION} VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion COMPATIBILITY AnyNewerVersion
) )
configure_package_config_file( configure_package_config_file(
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/InstallLibraryConfig.cmake.in ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/InstallLibraryConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${NAME}
NO_SET_AND_CHECK_MACRO NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO
) )
@ -26,39 +30,33 @@ function(myx_install_library)
COMPONENT dev COMPONENT dev
) )
install(TARGETS ${PROJECT_NAME} install(EXPORT ${NAME}Targets
EXPORT "${PROJECT_NAME}Targets" DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${NAME}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT dev
)
install(EXPORT "${PROJECT_NAME}Targets"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
COMPONENT dev COMPONENT dev
) )
install( install(
FILES FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" ${PROJECT_BINARY_DIR}/${NAME}ConfigVersion.cmake
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" ${PROJECT_BINARY_DIR}/${NAME}Config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${NAME}
COMPONENT dev COMPONENT dev
) )
if(TARGET ${PROJECT_NAME}_shared) if(TARGET ${NAME}_shared)
install( install(
TARGETS ${PROJECT_NAME}_shared TARGETS ${NAME}_shared
EXPORT ${PROJECT_NAME}Target EXPORT ${NAME}Targets
LIBRARY LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR} DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT lib COMPONENT lib
) )
endif() endif()
if(TARGET ${PROJECT_NAME}_static) if(TARGET ${NAME}_static)
install( install(
TARGETS ${PROJECT_NAME}_static TARGETS ${NAME}_static
EXPORT ${PROJECT_NAME}Target EXPORT ${NAME}Targets
ARCHIVE ARCHIVE
DESTINATION ${CMAKE_INSTALL_LIBDIR} DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT lib COMPONENT lib

View File

@ -1,6 +1,6 @@
include_guard(GLOBAL) include_guard(GLOBAL)
function(myx_qt5_target_setup) function(myx_qt5_target_setup NAME)
set(options) set(options)
set(oneValueArgs) set(oneValueArgs)
set(multiValueArgs COMPONENTS PRIVATE SOURCES MOC UI QRC LANGS) set(multiValueArgs COMPONENTS PRIVATE SOURCES MOC UI QRC LANGS)
@ -9,7 +9,7 @@ function(myx_qt5_target_setup)
if(arg_COMPONENTS) if(arg_COMPONENTS)
find_package(Qt5 COMPONENTS ${arg_COMPONENTS} REQUIRED) find_package(Qt5 COMPONENTS ${arg_COMPONENTS} REQUIRED)
foreach(iter ${arg_COMPONENTS}) foreach(iter ${arg_COMPONENTS})
target_include_directories(${PROJECT_NAME} PRIVATE ${Qt5${iter}_INCLUDE_DIRS}) target_include_directories(${NAME} PRIVATE ${Qt5${iter}_INCLUDE_DIRS})
endforeach() endforeach()
endif() endif()
@ -26,7 +26,7 @@ function(myx_qt5_target_setup)
if("LinguistTools" IN_LIST arg_COMPONENTS) if("LinguistTools" IN_LIST arg_COMPONENTS)
set(ts) set(ts)
foreach(iter ${arg_LANGS}) foreach(iter ${arg_LANGS})
list(APPEND ts ${PROJECT_SOURCE_DIR}/l10n/${PROJECT_NAME}_${iter}.ts) list(APPEND ts ${PROJECT_SOURCE_DIR}/l10n/${NAME}_${iter}.ts)
endforeach() endforeach()
qt5_create_translation(qm qt5_create_translation(qm
@ -36,7 +36,7 @@ function(myx_qt5_target_setup)
) )
endif() endif()
target_sources(${PROJECT_NAME} target_sources(${NAME}
PRIVATE PRIVATE
${arg_SOURCES} ${moc_cpp} ${ui_h} ${qrc_cpp} ${qm}) ${arg_SOURCES} ${moc_cpp} ${ui_h} ${qrc_cpp} ${qm})
endfunction() endfunction()