This commit is contained in:
Andrei Astafev 2022-09-29 13:34:57 +03:00
parent 84e8fe4dae
commit ed56c18888
6 changed files with 115 additions and 2 deletions

View File

@ -3,9 +3,12 @@ cmake_policy(SET CMP0057 NEW) # IN_LIST operator
get_filename_component(MYX_CMAKE_SOURCE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY)
set(MYX_CMAKE_CONFIG_DIR "${MYX_CMAKE_SOURCE_DIR}/config")
set(MYX_CMAKE_BACKPORTS_DIR "${MYX_CMAKE_SOURCE_DIR}/backports")
set(MYX_CMAKE_LIB_DIR "${MYX_CMAKE_SOURCE_DIR}/lib")
include(${MYX_CMAKE_CONFIG_DIR}/Globals.cmake)
include(${MYX_CMAKE_BACKPORTS_DIR}/IncludeGuard.cmake)
include(${MYX_CMAKE_BACKPORTS_DIR}/TopLevelProject.cmake)
if(${CMAKE_VERSION} VERSION_LESS "3.11.0")
@ -18,8 +21,12 @@ include(${MYX_CMAKE_LIB_DIR}/DirectoriesGuards.cmake)
include(${MYX_CMAKE_LIB_DIR}/SemanticProjectVersion.cmake)
include(${MYX_CMAKE_LIB_DIR}/NinjaGeneratorWrapper.cmake)
include(${MYX_CMAKE_LIB_DIR}/AddLibrary.cmake)
include(${MYX_CMAKE_LIB_DIR}/Qt5TargetSetup.cmake)
unset(MYX_CMAKE_SOURCE_DIR)
unset(MYX_CMAKE_BACKPORTS_DIR)
unset(MYX_CMAKE_CONFIG_DIR)
unset(MYX_CMAKE_LIB_DIR)
cmake_policy(POP)

View File

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

View File

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

8
config/Globals.cmake Normal file
View File

@ -0,0 +1,8 @@
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR} PARENT_SCOPE)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR} PARENT_SCOPE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR} PARENT_SCOPE)
#set(MYX_CMAKE_SCRIPTS_PREFIX ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} PARENT_SCOPE)

56
lib/AddLibrary.cmake Normal file
View File

@ -0,0 +1,56 @@
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()

42
lib/Qt5TargetSetup.cmake Normal file
View File

@ -0,0 +1,42 @@
include_guard(GLOBAL)
function(myx_qt5_target_setup)
set(options)
set(oneValueArgs)
set(multiValueArgs COMPONENTS PRIVATE SOURCES MOC UI QRC LANGS)
cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(arg_COMPONENTS)
find_package(Qt5 COMPONENTS ${arg_COMPONENTS} REQUIRED)
foreach(iter ${arg_COMPONENTS})
target_include_directories(${PROJECT_NAME} PRIVATE ${Qt5${iter}_INCLUDE_DIRS})
endforeach()
endif()
if(arg_PRIVATE)
foreach(iter ${arg_PRIVATE})
find_package(Qt5${iter} COMPONENTS Private REQUIRED)
endforeach()
endif()
qt5_wrap_cpp(moc_cpp ${arg_MOC})
qt5_wrap_ui(ui_h ${arg_UI})
qt5_add_resources(qrc_cpp ${arg_QRC})
if("LinguistTools" IN_LIST arg_COMPONENTS)
set(ts)
foreach(iter ${arg_LANGS})
list(APPEND ts ${PROJECT_SOURCE_DIR}/l10n/${PROJECT_NAME}_${iter}.ts)
endforeach()
qt5_create_translation(qm
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
${ts}
)
endif()
target_sources(${PROJECT_NAME}
PRIVATE
${arg_SOURCES} ${moc_cpp} ${ui_h} ${qrc_cpp} ${qm})
endfunction()