1
0
Эх сурвалжийг харах

project: Add variable CMAKE_PROJECT_INCLUDE

Ruslan Baratov 6 жил өмнө
parent
commit
dda0190458

+ 3 - 3
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
 If any of these arguments is not used, then the corresponding variables are
 set to the empty string.
 set to the empty string.
 
 
-If the variable :variable:`CMAKE_PROJECT_<PROJECT-NAME>_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_<PROJECT-NAME>_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
 Options
 ^^^^^^^
 ^^^^^^^

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -195,6 +195,7 @@ Variables that Change Behavior
    /variable/CMAKE_POLICY_WARNING_CMPNNNN
    /variable/CMAKE_POLICY_WARNING_CMPNNNN
    /variable/CMAKE_PREFIX_PATH
    /variable/CMAKE_PREFIX_PATH
    /variable/CMAKE_PROGRAM_PATH
    /variable/CMAKE_PROGRAM_PATH
+   /variable/CMAKE_PROJECT_INCLUDE
    /variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE
    /variable/CMAKE_PROJECT_PROJECT-NAME_INCLUDE
    /variable/CMAKE_SKIP_INSTALL_ALL_DEPENDENCY
    /variable/CMAKE_SKIP_INSTALL_ALL_DEPENDENCY
    /variable/CMAKE_STAGING_PREFIX
    /variable/CMAKE_STAGING_PREFIX

+ 6 - 0
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.

+ 31 - 11
Source/cmProjectCommand.cxx

@@ -319,21 +319,41 @@ bool cmProjectCommand::InitialPass(std::vector<std::string> const& args,
     languages.emplace_back("CXX");
     languages.emplace_back("CXX");
   }
   }
   this->Makefile->EnableLanguage(languages, false);
   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;
   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,
 void cmProjectCommand::TopLevelCMakeVarCondSet(const char* const name,
                                                const char* const value)
                                                const char* const value)
 {
 {

+ 1 - 0
Source/cmProjectCommand.h

@@ -36,6 +36,7 @@ public:
                    cmExecutionStatus& status) override;
                    cmExecutionStatus& status) override;
 
 
 private:
 private:
+  bool IncludeByVariable(const std::string& variable);
   void TopLevelCMakeVarCondSet(const char* name, const char* value);
   void TopLevelCMakeVarCondSet(const char* name, const char* value);
 };
 };
 
 

+ 6 - 0
Tests/CMakeOnly/CMakeLists.txt

@@ -56,6 +56,12 @@ add_test(CMakeOnly.ProjectInclude ${CMAKE_CMAKE_COMMAND}
   -P ${CMAKE_CURRENT_BINARY_DIR}/Test.cmake
   -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)
 include(CMakeParseArguments)
 
 
 function(add_major_test module)
 function(add_major_test module)

+ 4 - 0
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()