Обновление MyxCMake

This commit is contained in:
Andrei Astafev 2022-12-28 10:46:16 +03:00
parent 16bfa7aac9
commit c6673c84b8
21 changed files with 181 additions and 110 deletions

View File

@ -1,7 +1,15 @@
cmake_policy(PUSH)
cmake_policy(SET CMP0057 NEW) # IN_LIST operator
# Каталог для модулей, выполняющих поиск библиотек
# CMake выполняет проверку системного окружения с помощью модулей,
# расположенных в следующих каталогах:
# /usr/share/cmake-${CMAKE_VERSION}/Modules
# /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/cmake
# /usr/lib/cmake
# Если для используемой программы или библиотеки нет стандартного
# модуля для поиска, то можно использовать собственный.
# С помощью переменной `CMAKE_MODULE_PATH` указывается перечень
# дополнительных каталогов, в которых производится поиск модулей.
set(MYX_CMAKE_FIND_DIR "${PROJECT_SOURCE_DIR}/cmake/find")
if(IS_DIRECTORY "${MYX_CMAKE_FIND_DIR}")
if(NOT ${MYX_CMAKE_FIND_DIR} IN_LIST CMAKE_MODULE_PATH)
@ -14,6 +22,7 @@ get_filename_component(MYX_CMAKE_SOURCE_DIR "${CMAKE_CURRENT_LIST_FILE}" DIRECTO
set(MYX_CMAKE_BACKPORTS_DIR "${MYX_CMAKE_SOURCE_DIR}/backports")
set(MYX_CMAKE_LIB_DIR "${MYX_CMAKE_SOURCE_DIR}/lib")
# Модули для обеспечения обратной совместимости со старыми версиями CMake
include(${MYX_CMAKE_BACKPORTS_DIR}/IncludeGuard.cmake)
include(${MYX_CMAKE_BACKPORTS_DIR}/TopLevelProject.cmake)
if(${CMAKE_VERSION} VERSION_LESS "3.11.0")
@ -22,13 +31,17 @@ else()
include(FetchContent)
endif()
# Загрузка стандартных модулей
include(GNUInstallDirs)
include(CMakeDependentOption)
# Полезные макросы
include(${MYX_CMAKE_LIB_DIR}/macro/CreateSymlink.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/FindPackages.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/InstallRelative.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/CheckEnableCxxCompilerFlag.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/GTest.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/QTest.cmake)
include(${MYX_CMAKE_LIB_DIR}/ColoredMessages.cmake)
include(${MYX_CMAKE_LIB_DIR}/PopulateCMakeBinaryDir.cmake)
@ -39,9 +52,10 @@ include(${MYX_CMAKE_LIB_DIR}/SemanticProjectVersion.cmake)
include(${MYX_CMAKE_LIB_DIR}/NinjaGeneratorWrapper.cmake)
include(${MYX_CMAKE_LIB_DIR}/FetchContentAdd.cmake)
include(${MYX_CMAKE_LIB_DIR}/LSBInfo.cmake)
include(${MYX_CMAKE_LIB_DIR}/Toolchain.cmake)
include(${MYX_CMAKE_LIB_DIR}/CompilerFlags.cmake)
set(MYX_CMAKE_TOOLCHAINS_DIR "${MYX_CMAKE_LIB_DIR}/toolchains")
include(${MYX_CMAKE_LIB_DIR}/Toolchains.cmake)
unset(MYX_CMAKE_TOOLCHAINS_DIR)
include(${MYX_CMAKE_LIB_DIR}/AddExecutable.cmake)
include(${MYX_CMAKE_LIB_DIR}/AddInterfaceLibrary.cmake)
include(${MYX_CMAKE_LIB_DIR}/AddObjectLibrary.cmake)

View File

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

View File

@ -1,10 +1,9 @@
include_guard(GLOBAL)
if(${CMAKE_VERSION} VERSION_LESS 3.21)
get_property(nt DIRECTORY PROPERTY PARENT_DIRECTORY)
if(NOT nt)
get_property(__parent_directory DIRECTORY PROPERTY PARENT_DIRECTORY)
if(NOT __parent_directory)
set(PROJECT_IS_TOP_LEVEL true)
endif()
unset(nt)
unset(__parent_directory)
endif()

View File

@ -1,28 +0,0 @@
include_guard(GLOBAL)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# cmake-format: off
if((MYX_CMAKE_LSB_DISTRIBUTOR_ID STREQUAL "AstraLinuxSE") AND
(MYX_CMAKE_LSB_CODENAME STREQUAL "smolensk") AND
(MYX_CMAKE_LSB_RELEASE_VERSION STREQUAL "1.5"))
# cmake-format: on
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS ON)
check_enable_cxx_compiler_flag(-Wno-shadow)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "e2k")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS ON)
check_enable_cxx_compiler_flag(-Wno-invalid-offsetof)
list(APPEND CMAKE_LIBRARY_PATH "/usr/lib/e2k-linux-gnu")
endif()
if(CMAKE_COLOR_MAKEFILE)
check_enable_cxx_compiler_flag(-fdiagnostics-color=auto)
endif()
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if(CMAKE_COLOR_MAKEFILE)
check_enable_cxx_compiler_flag(-fcolor-diagnostics)
endif()
endif()

View File

@ -4,5 +4,5 @@
include_guard(GLOBAL)
if(${CMAKE_VERSION} VERSION_LESS "3.8.0" AND CMAKE_GENERATOR MATCHES Ninja)
myx_message_error("Myx: Old CMake versions should use Makefile generator")
myx_message_send_error("Myx: Old CMake versions should use Makefile generator")
endif()

View File

@ -7,28 +7,33 @@ function(myx_qt5_target_setup TARGET_NAME)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
get_target_property(target_type ${TARGET_NAME} TYPE)
get_target_property(__target_type ${TARGET_NAME} TYPE)
foreach(iter ${ARG_COMPONENTS})
if(target_type STREQUAL "INTERFACE_LIBRARY")
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${TARGET_NAME} INTERFACE ${Qt5${iter}_INCLUDE_DIRS})
else()
target_include_directories(${TARGET_NAME} PRIVATE ${Qt5${iter}_INCLUDE_DIRS})
endif()
if(target_type STREQUAL "EXECUTABLE" AND NOT iter STREQUAL "LinguistTools")
if(NOT iter STREQUAL "LinguistTools")
if(__target_type STREQUAL "EXECUTABLE")
target_link_libraries(${TARGET_NAME} PRIVATE "Qt5::${iter}")
endif()
if((__target_type STREQUAL "SHARED_LIBRARY") OR (__target_type STREQUAL "OBJECT_LIBRARY"))
target_link_libraries(${TARGET_NAME} PUBLIC "Qt5::${iter}")
endif()
endif()
endforeach()
foreach(iter ${ARG_PRIVATE})
if(target_type STREQUAL "INTERFACE_LIBRARY")
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${TARGET_NAME} INTERFACE ${Qt5${iter}_PRIVATE_INCLUDE_DIRS})
else()
target_include_directories(${TARGET_NAME} PRIVATE ${Qt5${iter}_PRIVATE_INCLUDE_DIRS})
endif()
endforeach()
if(target_type STREQUAL "EXECUTABLE")
if(__target_type STREQUAL "EXECUTABLE")
target_compile_options(${TARGET_NAME} PRIVATE ${Qt5Core_EXECUTABLE_COMPILE_FLAGS})
endif()
@ -58,7 +63,7 @@ function(myx_qt5_target_setup TARGET_NAME)
endif()
# Перечень файлов, подлежащих переводу
if(target_type STREQUAL "INTERFACE_LIBRARY")
if(__target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(tr ${TARGET_NAME} INTERFACE_TR_FILES)
else()
get_target_property(tr ${TARGET_NAME} TR_FILES)
@ -87,7 +92,7 @@ function(myx_qt5_target_setup TARGET_NAME)
target_sources(${TARGET_NAME} PRIVATE ${qrc_l10n})
endif()
unset(tr)
if(target_type STREQUAL "INTERFACE_LIBRARY")
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_sources(${TARGET_NAME} INTERFACE ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp})
else()
target_sources(${TARGET_NAME} PRIVATE ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp})

View File

@ -1,35 +0,0 @@
include_guard(GLOBAL)
# Предпочтительные пути к утилитам для компоновки
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# cmake-format: off
# Astra Linux Smolensk 1.5
if((MYX_CMAKE_LSB_DISTRIBUTOR_ID STREQUAL "AstraLinuxSE") AND
(MYX_CMAKE_LSB_CODENAME STREQUAL "smolensk") AND
(MYX_CMAKE_LSB_RELEASE_VERSION STREQUAL "1.5"))
find_program(CMAKE_GCC_AR NAMES "/usr/bin/x86_64-linux-gnu-gcc-ar-4.7")
find_program(CMAKE_GCC_NM NAMES "/usr/bin/x86_64-linux-gnu-gcc-nm-4.7")
find_program(CMAKE_GCC_RANLIB NAMES "/usr/bin/x86_64-linux-gnu-gcc-ranlib-4.7")
# Elbrus E2K
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "e2k")
find_program(CMAKE_GCC_AR NAMES "/usr/${CMAKE_SYSTEM_PROCESSOR}-linux/bin/ar")
find_program(CMAKE_GCC_NM NAMES "/usr/${CMAKE_SYSTEM_PROCESSOR}-linux/bin/nm")
find_program(CMAKE_GCC_RANLIB NAMES "/usr/${CMAKE_SYSTEM_PROCESSOR}-linux/bin/ranlib")
# Другие версии Linux
else()
find_program(CMAKE_GCC_AR NAMES "gcc-ar" "ar")
find_program(CMAKE_GCC_NM NAMES "gcc-nm" "nm")
find_program(CMAKE_GCC_RANLIB NAMES "gcc-ranlib" "ranlib")
endif()
# cmake-format: on
if(CMAKE_GCC_AR)
set(CMAKE_AR ${CMAKE_GCC_AR} CACHE STRING "" FORCE)
endif()
if(CMAKE_GCC_NM)
set(CMAKE_NM ${CMAKE_GCC_NM} CACHE STRING "" FORCE)
endif()
if(CMAKE_GCC_RANLIB)
set(CMAKE_RANLIB ${CMAKE_GCC_RANLIB} CACHE STRING "" FORCE)
endif()
endif()

View File

@ -32,3 +32,25 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux)
# cmake-format: on
endif()
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
include("${MYX_CMAKE_TOOLCHAINS_DIR}/GCC.cmake")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
include("${MYX_CMAKE_TOOLCHAINS_DIR}/Clang.cmake")
endif()
if((MYX_CMAKE_LSB_DISTRIBUTOR_ID STREQUAL "AstraLinuxSE") AND
(MYX_CMAKE_LSB_CODENAME STREQUAL "smolensk") AND
(MYX_CMAKE_LSB_RELEASE_VERSION STREQUAL "1.5"))
include("${MYX_CMAKE_TOOLCHAINS_DIR}/AstraLinuxSE-1.5.cmake")
return()
endif()
if((MYX_CMAKE_LSB_DISTRIBUTOR_ID STREQUAL "ElbrusD") AND
(MYX_CMAKE_LSB_CODENAME STREQUAL "Jessie") AND
(MYX_CMAKE_LSB_RELEASE_VERSION VERSION_GREATER "1.4"))
include("${MYX_CMAKE_TOOLCHAINS_DIR}/ElbrusD-1.4.cmake")
return()
endif()

View File

@ -6,7 +6,7 @@
include_guard(GLOBAL)
if(NOT TARGET uninstall)
if((NOT TARGET uninstall) AND (NOT DEFINED ENV{SAPR_PREFIX}))
configure_file(${CMAKE_CURRENT_LIST_DIR}/uninstall.cmake.in
${CMAKE_BINARY_DIR}/cmake_uninstall.cmake IMMEDIATE @ONLY)
add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake)

View File

@ -1,33 +1,34 @@
include_guard(GLOBAL)
# based on https://github.com/bluescarni/yacma
# Добавление флага к списку используемых после проверки
# возможности его использования текущим компилятором
# Основано на https://github.com/bluescarni/yacma
include(CheckCXXCompilerFlag)
macro(check_enable_cxx_compiler_flag flag)
macro(check_enable_cxx_compiler_flag FLAG)
set(options)
set(oneValueArgs TARGET)
set(multiValueArgs)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(CMAKE_REQUIRED_QUIET TRUE)
check_cxx_compiler_flag("${flag}" check_cxx_flag)
check_cxx_compiler_flag("${FLAG}" check_cxx_flag)
unset(CMAKE_REQUIRED_QUIET)
if(check_cxx_flag)
myx_message_notice("'${flag}': flag is supported.")
myx_message_notice("'${FLAG}': flag is supported.")
if(ARG_TARGET)
target_compile_options(${ARG_TARGET} PUBLIC ${flag})
target_compile_options(${ARG_TARGET} PUBLIC ${FLAG})
else()
add_compile_options(${flag})
add_compile_options(${FLAG})
endif()
else()
myx_message_status("'${flag}': flag is NOT supported.")
myx_message_status("'${FLAG}': flag is NOT supported.")
endif()
unset(check_cxx_flag CACHE)
foreach(iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${iter})
foreach(__iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${__iter})
endforeach()
unset(ARG_UNPARSED_ARGUMENTS)
unset(multiValueArgs)

View File

@ -1,11 +1,13 @@
include_guard(GLOBAL)
macro(myx_create_symlink original linkname)
if(UNIX AND (NOT EXISTS ${linkname}))
# Создание символических ссылок
macro(myx_create_symlink ORIGINAL_FILENAME LINKNAME)
if(UNIX AND (NOT EXISTS ${LINKNAME}))
if(${CMAKE_VERSION} VERSION_LESS "3.14.0")
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${original} ${linkname})
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
${ORIGINAL_FILENAME} ${LINKNAME})
else()
file(CREATE_LINK ${original} ${linkname} SYMBOLIC)
file(CREATE_LINK ${ORIGINAL_FILENAME} ${LINKNAME} SYMBOLIC)
endif()
endif()
endmacro(myx_create_symlink original linkname)
endmacro(myx_create_symlink ORIGINAL_FILENAME LINKNAME)

View File

@ -24,8 +24,8 @@ macro(myx_find_required_packages)
endforeach()
endif()
foreach(iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${iter})
foreach(__iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${__iter})
endforeach()
unset(ARG_UNPARSED_ARGUMENTS)
unset(multiValueArgs)

View File

@ -0,0 +1,30 @@
include_guard(GLOBAL)
macro(myx_add_gtest TARGET_NAME)
set(options)
set(oneValueArgs)
set(multiValueArgs)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
add_executable(${TARGET_NAME} ${ARG_UNPARSED_ARGUMENTS})
find_package(GTest)
if(GTest_FOUND)
target_link_libraries(${TARGET_NAME} PRIVATE GTest::GTest GTest::Main)
else()
if((NOT TARGET gtest) AND (NOT TARGET gtest_main))
add_subdirectory(/usr/src/googletest/googletest ${CMAKE_BINARY_DIR}/gtest)
endif()
target_link_libraries(${TARGET_NAME} PRIVATE gtest gtest_main)
endif()
add_test(NAME ${TARGET_NAME} COMMAND ${TARGET_NAME})
foreach(__iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${__iter})
endforeach()
unset(ARG_UNPARSED_ARGUMENTS)
unset(multiValueArgs)
unset(oneValueArgs)
unset(options)
endmacro()

View File

@ -0,0 +1,40 @@
include_guard(GLOBAL)
macro(myx_add_qtest TARGET_NAME)
set(options)
set(oneValueArgs)
set(multiValueArgs)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
find_package(Qt5 COMPONENTS Core Test REQUIRED)
if(NOT Qt5Test_FOUND)
myx_message_fatal_error("Qt testing framework is not found")
return()
endif()
foreach(filename ${ARG_UNPARSED_ARGUMENTS})
get_filename_component(basename ${filename} NAME_WE)
list(APPEND cpps "${basename}.cpp")
list(APPEND hpps "${basename}.hpp")
qt5_wrap_cpp(moc "${basename}.hpp")
list(APPEND mocs "${moc}")
endforeach()
add_executable(${TARGET_NAME} ${mocs} ${cpps} ${hpps})
target_link_libraries(${TARGET_NAME} Qt5::Core Qt5::Test)
add_test(NAME ${TARGET_NAME} COMMAND ${TARGET_NAME})
unset(cpps)
unset(hpps)
unset(moc)
unset(mocs)
foreach(__iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${__iter})
endforeach()
unset(ARG_UNPARSED_ARGUMENTS)
unset(multiValueArgs)
unset(oneValueArgs)
unset(options)
endmacro()

View File

@ -0,0 +1,8 @@
find_program(CMAKE_AR NAMES "/usr/bin/x86_64-linux-gnu-gcc-ar-4.7")
find_program(CMAKE_NM NAMES "/usr/bin/x86_64-linux-gnu-gcc-nm-4.7")
find_program(CMAKE_RANLIB NAMES "/usr/bin/x86_64-linux-gnu-gcc-ranlib-4.7")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS ON)
check_enable_cxx_compiler_flag(-Wno-shadow)

View File

@ -0,0 +1,3 @@
if(CMAKE_COLOR_MAKEFILE)
check_enable_cxx_compiler_flag(-fcolor-diagnostics)
endif()

View File

@ -0,0 +1,9 @@
find_program(CMAKE_AR NAMES "/usr/${CMAKE_SYSTEM_PROCESSOR}-linux/bin/ar")
find_program(CMAKE_NM NAMES "/usr/${CMAKE_SYSTEM_PROCESSOR}-linux/bin/nm")
find_program(CMAKE_RANLIB NAMES "/usr/${CMAKE_SYSTEM_PROCESSOR}-linux/bin/ranlib")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS ON)
check_enable_cxx_compiler_flag(-Wno-invalid-offsetof)
list(APPEND CMAKE_LIBRARY_PATH "/usr/lib/e2k-linux-gnu")

View File

@ -0,0 +1,3 @@
if(CMAKE_COLOR_MAKEFILE)
check_enable_cxx_compiler_flag(-fdiagnostics-color=auto)
endif()

View File

@ -19,10 +19,7 @@ function(myx_uncrustify TARGET_NAME)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT ARG_CONFIG)
set(ARG_CONFIG "${PROJECT_SOURCE_DIR}/.uncrustify.cfg")
if(NOT EXISTS ${ARG_CONFIG})
set(ARG_CONFIG "${CMAKE_SOURCE_DIR}/.uncrustify.cfg")
endif()
set(ARG_CONFIG "${PROJECT_SOURCE_DIR}/uncrustify.cfg")
endif()
if(NOT EXISTS ${ARG_CONFIG})
@ -47,7 +44,7 @@ function(myx_uncrustify TARGET_NAME)
# Динамически сгенерированные файлы исключаются
get_target_property(__target_type ${TARGET_NAME} TYPE)
if((${__target_type} STREQUAL "INTERFACE_LIBRARY") AND (${CMAKE_VERSION} VERSION_LESS "3.15.0"))
if((${__target_type} STREQUAL "INTERFACE_LIBRARY") AND (${CMAKE_VERSION} VERSION_LESS "3.17.0"))
get_target_property(__s1 ${TARGET_NAME} INTERFACE_SOURCES)
if(__s1)
list(APPEND __all_sources ${__s1})
@ -66,9 +63,10 @@ function(myx_uncrustify TARGET_NAME)
endif()
endforeach()
target_sources(${TARGET_NAME} PRIVATE ${ARG_CONFIG})
set(__fixed_config ${PROJECT_BINARY_DIR}/uncrustify-${TARGET_NAME}.cfg)
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/uncrustify-${TARGET_NAME}.cfg
add_custom_command(OUTPUT ${__fixed_config}
DEPENDS ${ARG_CONFIG}
COMMAND ${UNCRUSTIFY_EXE} --update-config-with-doc
-c ${ARG_CONFIG} -o ${__fixed_config})
list(APPEND __options -c ${__fixed_config})

View File

@ -20,11 +20,11 @@ if(ENV{MYX_CMAKE_DIR})
set(MYX_CMAKE_DIR $ENV{MYX_CMAKE_DIR})
endif()
if(MYX_CMAKE_DIR)
find_package(MyxCMake 2.0.32 REQUIRED CONFIG PATHS ${MYX_CMAKE_DIR} NO_DEFAULT_PATH)
find_package(MyxCMake 2.2.6 REQUIRED CONFIG PATHS ${MYX_CMAKE_DIR} NO_DEFAULT_PATH)
myx_message_notice("=== MyxCMake directory: ${MyxCMake_CONFIG} ===")
else()
if(MYX_CMAKE_USE_SYSTEM)
find_package(MyxCMake 2.0.32 REQUIRED)
find_package(MyxCMake 2.2.6 REQUIRED)
myx_message_notice("=== MyxCMake directory: ${MyxCMake_CONFIG} ===")
else()
include(${PROJECT_SOURCE_DIR}/cmake/myx/MyxCMakeConfig.cmake)