diff --git a/MyxCMakeConfig.cmake b/MyxCMakeConfig.cmake index 8822a69..f6a21b4 100644 --- a/MyxCMakeConfig.cmake +++ b/MyxCMakeConfig.cmake @@ -17,4 +17,10 @@ if(${CMAKE_VERSION} VERSION_LESS "3.11.0") include(${MYX_CMAKE_BACKPORTS_DIR}/FetchContent.cmake) endif() +include(${MYX_CMAKE_LIB_DIR}/ColoredMessages.cmake) +include(${MYX_CMAKE_LIB_DIR}/NinjaGeneratorWarning.cmake) +include(${MYX_CMAKE_LIB_DIR}/DirectoriesGuards.cmake) +include(${MYX_CMAKE_LIB_DIR}/SemanticProjectVersion.cmake) +include(${MYX_CMAKE_LIB_DIR}/NinjaGeneratorWrapper.cmake) + cmake_policy(POP) diff --git a/MyxCMakeConfigVersion.cmake b/MyxCMakeConfigVersion.cmake index 4f522e8..de7768c 100644 --- a/MyxCMakeConfigVersion.cmake +++ b/MyxCMakeConfigVersion.cmake @@ -1,4 +1,4 @@ -set(MYX_CMAKE_PACKAGE_VERSION "1.99.0") +set(MYX_CMAKE_PACKAGE_VERSION "1.99.1") if(MYX_CMAKE_PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) set(PACKAGE_VERSION_COMPATIBLE FALSE) else() diff --git a/README.md b/README.md index 77dffdc..eeeba55 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,6 @@ `project` добавить строку: ```cmake -find_package(MyxCMake 1.99.0 PATHS cmake/myx NO_DEFAULT_PATH QUIET REQUIRED) +find_package(MyxCMake 1.99.1 PATHS cmake/myx NO_DEFAULT_PATH QUIET REQUIRED) ``` diff --git a/lib/ColoredMessages.cmake b/lib/ColoredMessages.cmake new file mode 100644 index 0000000..5ae5f3d --- /dev/null +++ b/lib/ColoredMessages.cmake @@ -0,0 +1,43 @@ +# Обёртки для функций вывода сообщений + +include_guard(GLOBAL) + +if(DEFINED ENV{TERM} AND UNIX) + string(ASCII 27 Esc) + set(MyxColorReset "${Esc}[m") + set(MyxColorBold "${Esc}[1m") + set(MyxColorRed "${Esc}[31m") + set(MyxColorGreen "${Esc}[32m") + set(MyxColorYellow "${Esc}[33m") + set(MyxColorBlue "${Esc}[34m") + set(MyxColorMagenta "${Esc}[35m") + set(MyxColorCyan "${Esc}[36m") + set(MyxColorWhite "${Esc}[37m") + set(MyxColorBoldRed "${Esc}[1;31m") + set(MyxColorBoldGreen "${Esc}[1;32m") + set(MyxColorBoldYellow "${Esc}[1;33m") + set(MyxColorBoldBlue "${Esc}[1;34m") + set(MyxColorBoldMagenta "${Esc}[1;35m") + set(MyxColorBoldCyan "${Esc}[1;36m") + set(MyxColorBoldWhite "${Esc}[1;37m") +endif() + +function(myx_message_error) + message(FATAL_ERROR ${MyxColorBoldRed}${ARGV}${MyxColorReset}) +endfunction() + +function(myx_message_warning) + message(WARNING ${MyxColorRed}${ARGV}${MyxColorReset}) +endfunction() + +function(myx_message_status) + message(STATUS ${MyxColorMagenta}${ARGV}${MyxColorReset}) +endfunction() + +function(myx_message_notice) + message(${MyxColorBold}${ARGV}${MyxColorReset}) +endfunction() + +function(myx_message) + message(${MyxColorReset}${ARGV}${MyxColorReset}) +endfunction() diff --git a/lib/DirectoriesGuards.cmake b/lib/DirectoriesGuards.cmake new file mode 100644 index 0000000..addab82 --- /dev/null +++ b/lib/DirectoriesGuards.cmake @@ -0,0 +1,50 @@ +# Запись результатов сборки проекта внутрь иерархии каталогов с исходными текстами +# приводит к засорению файлами формируемыми на этапе сборки, которые затрудняют +# разработку, поиск в оригинальных файлах и мешают ориентироваться в проекте. +# При работе с несколькими типами сборки, например, отладка и выпуск, появляется +# необходимость корректного полного удаления результатов предыдущего варианта. + +include_guard(GLOBAL) + +get_filename_component(cmake_source_dir "${CMAKE_SOURCE_DIR}" REALPATH) +get_filename_component(cmake_binary_dir "${CMAKE_BINARY_DIR}" REALPATH) +get_filename_component(project_source_dir "${PROJECT_SOURCE_DIR}" REALPATH) +get_filename_component(project_binary_dir "${PROJECT_BINARY_DIR}" REALPATH) +get_filename_component(cmake_install_prefix "${CMAKE_INSTALL_PREFIX}" REALPATH) + +if(cmake_install_prefix STREQUAL cmake_binary_dir) + myx_message_error( + "Myx: Cannot install into build directory ${CMAKE_INSTALL_PREFIX}.") +endif() + +if(cmake_install_prefix STREQUAL cmake_source_dir) + myx_message_error( + "Myx: Cannot install into source directory ${CMAKE_INSTALL_PREFIX}.") +endif() + +if(cmake_install_prefix STREQUAL project_binary_dir) + myx_message_error( + "Myx: Cannot install into build directory ${CMAKE_INSTALL_PREFIX}.") +endif() + +if(cmake_install_prefix STREQUAL project_source_dir) + myx_message_error( + "Myx: Cannot install into source directory ${CMAKE_INSTALL_PREFIX}.") +endif() + +if(cmake_binary_dir STREQUAL cmake_source_dir) + myx_message_error( + "Myx: Cannot build in source directory ${CMAKE_SOURCE_DIR}") +endif() + +if(project_binary_dir STREQUAL project_source_dir) + myx_message_error( + "Myx: Cannot build in source directory ${CMAKE_SOURCE_DIR}") +endif() + +unset(cmake_source_dir) +unset(cmake_binary_dir) +unset(project_source_dir) +unset(project_binary_dir) +unset(cmake_install_prefix) + diff --git a/lib/NinjaGeneratorWarning.cmake b/lib/NinjaGeneratorWarning.cmake new file mode 100644 index 0000000..b9118d7 --- /dev/null +++ b/lib/NinjaGeneratorWarning.cmake @@ -0,0 +1,8 @@ +# Версии CMake, как минимум до 3.8.0, генерируют некорректные +# правила для ninja. + +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") +endif() diff --git a/lib/NinjaGeneratorWrapper.cmake b/lib/NinjaGeneratorWrapper.cmake index e6f0fd1..be97d52 100644 --- a/lib/NinjaGeneratorWrapper.cmake +++ b/lib/NinjaGeneratorWrapper.cmake @@ -1,10 +1,10 @@ -include_guard(GLOBAL) - # Если выбран генератор Ninja, то в основном сборочном каталоге создаётся # файл Makefile, который обрабатывается командой make и передаёт исполнение # системе сборки ninja. Таким образом можно выполнять команду make, # даже если правила сборки проекта сгенерированы для ninja. +include_guard(GLOBAL) + if(CMAKE_GENERATOR MATCHES Ninja AND PROJECT_IS_TOP_LEVEL) file(WRITE ${CMAKE_BINARY_DIR}/Makefile ".PHONY: build\n" "%:\n" "\t@ninja \$@\n" "build:\n" "\t@ninja\n") diff --git a/lib/SemanticProjectVersion.cmake b/lib/SemanticProjectVersion.cmake new file mode 100644 index 0000000..7835349 --- /dev/null +++ b/lib/SemanticProjectVersion.cmake @@ -0,0 +1,18 @@ +include_guard(GLOBAL) + +function(myx_is_semantic_project_version) + if(NOT ${PROJECT_VERSION_PATCH} MATCHES "([0-9]+)") + myx_message_error("Myx: Please set project version in X.Y.Z format") + endif() +endfunction() + +function(myx_project_version_int) + # cmake-format: off + myx_is_semantic_project_version() + math(EXPR v "(${PROJECT_VERSION_MAJOR} << 32) + (${PROJECT_VERSION_MINOR} << 16) + ${PROJECT_VERSION_PATCH}") + myx_message_notice(aaaa ${v}) + set_property(GLOBAL PROPERTY PROJECT_VERSION_INT ${v}) + # cmake-format: on +endfunction() + +myx_project_version_int()