From 390f14a24416f23801f5644eb51590f8317d35c7 Mon Sep 17 00:00:00 2001 From: Ruslan Baratov Date: Wed, 27 Mar 2019 22:33:37 +0300 Subject: Test ProjectInclude: Add NONE to avoid enabling languages --- Tests/CMakeOnly/ProjectInclude/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/CMakeOnly/ProjectInclude/CMakeLists.txt b/Tests/CMakeOnly/ProjectInclude/CMakeLists.txt index a9abb4a..ffce488 100644 --- a/Tests/CMakeOnly/ProjectInclude/CMakeLists.txt +++ b/Tests/CMakeOnly/ProjectInclude/CMakeLists.txt @@ -1,4 +1,4 @@ -project(ProjectInclude) +project(ProjectInclude LANGUAGES NONE) if(NOT AUTO_INCLUDE) message(FATAL_ERROR "include file not found") endif() -- cgit v0.12 From dda01904587627a3d2bda5b684f7bef1e62596bb Mon Sep 17 00:00:00 2001 From: Ruslan Baratov Date: Wed, 27 Mar 2019 22:36:51 +0300 Subject: project: Add variable CMAKE_PROJECT_INCLUDE --- Help/command/project.rst | 6 ++-- Help/manual/cmake-variables.7.rst | 1 + Help/variable/CMAKE_PROJECT_INCLUDE.rst | 6 ++++ Source/cmProjectCommand.cxx | 42 +++++++++++++++++------- Source/cmProjectCommand.h | 1 + Tests/CMakeOnly/CMakeLists.txt | 6 ++++ Tests/CMakeOnly/ProjectIncludeAny/CMakeLists.txt | 4 +++ 7 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 Help/variable/CMAKE_PROJECT_INCLUDE.rst create mode 100644 Tests/CMakeOnly/ProjectIncludeAny/CMakeLists.txt diff --git a/Help/command/project.rst b/Help/command/project.rst index 688e56c..a95012d 100644 --- a/Help/command/project.rst +++ b/Help/command/project.rst @@ -31,9 +31,9 @@ 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` exists, -the file pointed to by that variable will be included as the last 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. Options ^^^^^^^ diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index 48d7550..e1584ef 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -195,6 +195,7 @@ Variables that Change Behavior /variable/CMAKE_POLICY_WARNING_CMPNNNN /variable/CMAKE_PREFIX_PATH /variable/CMAKE_PROGRAM_PATH + /variable/CMAKE_PROJECT_INCLUDE /variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE /variable/CMAKE_SKIP_INSTALL_ALL_DEPENDENCY /variable/CMAKE_STAGING_PREFIX diff --git a/Help/variable/CMAKE_PROJECT_INCLUDE.rst b/Help/variable/CMAKE_PROJECT_INCLUDE.rst new file mode 100644 index 0000000..7de767a --- /dev/null +++ b/Help/variable/CMAKE_PROJECT_INCLUDE.rst @@ -0,0 +1,6 @@ +CMAKE_PROJECT_INCLUDE +--------------------- + +A CMake language file or module to be included by 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 2fe9fe8..de5d1ed 100644 --- a/Source/cmProjectCommand.cxx +++ b/Source/cmProjectCommand.cxx @@ -319,21 +319,41 @@ bool cmProjectCommand::InitialPass(std::vector const& args, languages.emplace_back("CXX"); } this->Makefile->EnableLanguage(languages, false); - std::string extraInclude = "CMAKE_PROJECT_" + projectName + "_INCLUDE"; - const char* include = this->Makefile->GetDefinition(extraInclude); - if (include) { - bool readit = this->Makefile->ReadDependentFile(include); - if (!readit && !cmSystemTools::GetFatalErrorOccured()) { - std::string m = "could not find file:\n" - " "; - m += include; - this->SetError(m); - return false; - } + + if (!this->IncludeByVariable("CMAKE_PROJECT_INCLUDE")) { + return false; + } + + if (!this->IncludeByVariable("CMAKE_PROJECT_" + projectName + "_INCLUDE")) { + return false; } + return true; } +bool cmProjectCommand::IncludeByVariable(const std::string& variable) +{ + const char* include = this->Makefile->GetDefinition(variable); + if (!include) { + return true; + } + + const bool readit = this->Makefile->ReadDependentFile(include); + if (readit) { + return true; + } + + if (cmSystemTools::GetFatalErrorOccured()) { + return true; + } + + std::string m = "could not find file:\n" + " "; + m += include; + this->SetError(m); + return false; +} + void cmProjectCommand::TopLevelCMakeVarCondSet(const char* const name, const char* const value) { diff --git a/Source/cmProjectCommand.h b/Source/cmProjectCommand.h index 365d448..f1d03e7 100644 --- a/Source/cmProjectCommand.h +++ b/Source/cmProjectCommand.h @@ -36,6 +36,7 @@ public: cmExecutionStatus& status) override; private: + bool IncludeByVariable(const std::string& variable); void TopLevelCMakeVarCondSet(const char* name, const char* value); }; diff --git a/Tests/CMakeOnly/CMakeLists.txt b/Tests/CMakeOnly/CMakeLists.txt index f40524f..57f0cf8 100644 --- a/Tests/CMakeOnly/CMakeLists.txt +++ b/Tests/CMakeOnly/CMakeLists.txt @@ -56,6 +56,12 @@ add_test(CMakeOnly.ProjectInclude ${CMAKE_CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/Test.cmake ) +add_test(CMakeOnly.ProjectIncludeAny ${CMAKE_CMAKE_COMMAND} + -DTEST=ProjectIncludeAny + -DCMAKE_ARGS=-DCMAKE_PROJECT_INCLUDE=${CMAKE_CURRENT_SOURCE_DIR}/ProjectInclude/include.cmake + -P ${CMAKE_CURRENT_BINARY_DIR}/Test.cmake + ) + include(CMakeParseArguments) function(add_major_test module) diff --git a/Tests/CMakeOnly/ProjectIncludeAny/CMakeLists.txt b/Tests/CMakeOnly/ProjectIncludeAny/CMakeLists.txt new file mode 100644 index 0000000..ffce488 --- /dev/null +++ b/Tests/CMakeOnly/ProjectIncludeAny/CMakeLists.txt @@ -0,0 +1,4 @@ +project(ProjectInclude LANGUAGES NONE) +if(NOT AUTO_INCLUDE) + message(FATAL_ERROR "include file not found") +endif() -- cgit v0.12 From 7d19b3091d7d2982a0d6b2b4408011efbb56f167 Mon Sep 17 00:00:00 2001 From: Ruslan Baratov Date: Fri, 29 Mar 2019 18:53:12 +0300 Subject: Release note for CMAKE_PROJECT_INCLUDE variable --- Help/release/dev/project-include.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Help/release/dev/project-include.rst diff --git a/Help/release/dev/project-include.rst b/Help/release/dev/project-include.rst new file mode 100644 index 0000000..659e933 --- /dev/null +++ b/Help/release/dev/project-include.rst @@ -0,0 +1,5 @@ +cmake_project_include +--------------------- + +* A variable :variable:`CMAKE_PROJECT_INCLUDE` was added to allow injection + of custom code into the project without knowing the project name a priori. -- cgit v0.12