diff --git a/Help/command/project.rst b/Help/command/project.rst index a95012da50535f28286e799643cc3ce6f49401eb..7e33ccdbc2ac6842775d4f76bdaca68777f1de3c 100644 --- a/Help/command/project.rst +++ b/Help/command/project.rst @@ -31,6 +31,10 @@ Further variables are set by the optional arguments described in the following. If any of these arguments is not used, then the corresponding variables are set to the empty string. +If the variable :variable:`CMAKE_PROJECT_INCLUDE_BEFORE` exists, the file +pointed to by that variable will be included as the first step of the project +command. + If the variable :variable:`CMAKE_PROJECT__INCLUDE` or :variable:`CMAKE_PROJECT_INCLUDE` exists, the file pointed to by that variable will be included as the last step of the project command. diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index e1584ef63bbcb838367b21dd4de7f3febfe1693a..e9dca47256bbc228fdc179dde979fed339b5c3bf 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -196,6 +196,7 @@ Variables that Change Behavior /variable/CMAKE_PREFIX_PATH /variable/CMAKE_PROGRAM_PATH /variable/CMAKE_PROJECT_INCLUDE + /variable/CMAKE_PROJECT_INCLUDE_BEFORE /variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE /variable/CMAKE_SKIP_INSTALL_ALL_DEPENDENCY /variable/CMAKE_STAGING_PREFIX diff --git a/Help/release/dev/project-include-before.rst b/Help/release/dev/project-include-before.rst new file mode 100644 index 0000000000000000000000000000000000000000..3f16cd737513db2c0071f05ee9776d795e2c1995 --- /dev/null +++ b/Help/release/dev/project-include-before.rst @@ -0,0 +1,5 @@ +cmake_project_include_before +---------------------------- + +* A variable :variable:`CMAKE_PROJECT_INCLUDE_BEFORE` was added to allow + injection of custom code into the project. diff --git a/Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst b/Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst new file mode 100644 index 0000000000000000000000000000000000000000..12a5263e9983a265b3e4212b70b67f7979d16c68 --- /dev/null +++ b/Help/variable/CMAKE_PROJECT_INCLUDE_BEFORE.rst @@ -0,0 +1,6 @@ +CMAKE_PROJECT_INCLUDE_BEFORE +---------------------------- + +A CMake language file or module to be included before processing the +:command:`project` command. This is intended for injecting custom code into +project builds without modifying their source. diff --git a/Source/cmProjectCommand.cxx b/Source/cmProjectCommand.cxx index de5d1edefbb7e6c7a0d30212010fe08872a36648..0bf63d4f6e31343e7711fb19c31ac2a12d87ac91 100644 --- a/Source/cmProjectCommand.cxx +++ b/Source/cmProjectCommand.cxx @@ -25,6 +25,10 @@ bool cmProjectCommand::InitialPass(std::vector const& args, return false; } + if (!this->IncludeByVariable("CMAKE_PROJECT_INCLUDE_BEFORE")) { + return false; + } + std::string const& projectName = args[0]; this->Makefile->SetProjectName(projectName); diff --git a/Tests/CMakeOnly/CMakeLists.txt b/Tests/CMakeOnly/CMakeLists.txt index 57f0cf80dc944c9ddb55c535fa287655b8651b65..1aeab8b390293c01769dc4b5bef3329db3dda412 100644 --- a/Tests/CMakeOnly/CMakeLists.txt +++ b/Tests/CMakeOnly/CMakeLists.txt @@ -62,6 +62,12 @@ add_test(CMakeOnly.ProjectIncludeAny ${CMAKE_CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/Test.cmake ) +add_test(CMakeOnly.ProjectIncludeBefore ${CMAKE_CMAKE_COMMAND} + -DTEST=ProjectIncludeBefore + -DCMAKE_ARGS=-DCMAKE_PROJECT_INCLUDE_BEFORE=${CMAKE_CURRENT_SOURCE_DIR}/ProjectIncludeBefore/include.cmake + -P ${CMAKE_CURRENT_BINARY_DIR}/Test.cmake + ) + include(CMakeParseArguments) function(add_major_test module) diff --git a/Tests/CMakeOnly/ProjectIncludeBefore/CMakeLists.txt b/Tests/CMakeOnly/ProjectIncludeBefore/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5cd9cba2c01fbde19a8ed764de4e0fd5dcffeae9 --- /dev/null +++ b/Tests/CMakeOnly/ProjectIncludeBefore/CMakeLists.txt @@ -0,0 +1,5 @@ +set(FOO TRUE) +project(ProjectInclude LANGUAGES NONE) +if(NOT AUTO_INCLUDE) + message(FATAL_ERROR "include file not found") +endif() diff --git a/Tests/CMakeOnly/ProjectIncludeBefore/include.cmake b/Tests/CMakeOnly/ProjectIncludeBefore/include.cmake new file mode 100644 index 0000000000000000000000000000000000000000..0a4799df35d89c224a7756a4e5e7f15aadd56035 --- /dev/null +++ b/Tests/CMakeOnly/ProjectIncludeBefore/include.cmake @@ -0,0 +1,9 @@ +if(NOT FOO) + message(FATAL_ERROR "FOO is not set") +endif() + +if(NOT "${PROJECT_NAME}" STREQUAL "") + message(FATAL_ERROR "PROJECT_NAME should be empty") +endif() + +set(AUTO_INCLUDE TRUE)