From 94d1684a8fd0de4310874cd582636a8e26402da2 Mon Sep 17 00:00:00 2001 From: David Cole Date: Wed, 2 Feb 2011 18:18:14 -0500 Subject: [PATCH] Add CMAKE_SCRIPT_MODE_FILE variable (#2828) New CMake variable is set when processing a -P script file, but not when configuring a project. --- Source/cmDocumentVariables.cxx | 8 ++++++++ Source/cmGetCMakePropertyCommand.cxx | 21 +++++++++++---------- Source/cmMakefile.cxx | 5 +++++ Source/cmMakefile.h | 7 ++++++- Source/cmake.cxx | 6 ++++++ Tests/CMakeTests/StringTestScript.cmake | 2 ++ 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Source/cmDocumentVariables.cxx b/Source/cmDocumentVariables.cxx index 6fffecba1b..897881c0d1 100644 --- a/Source/cmDocumentVariables.cxx +++ b/Source/cmDocumentVariables.cxx @@ -96,6 +96,14 @@ void cmDocumentVariables::DefineVariables(cmake* cm) "See also CMAKE_CURRENT_LIST_FILE.",false, "Variables that Provide Information"); + cm->DefineProperty + ("CMAKE_SCRIPT_MODE_FILE", cmProperty::VARIABLE, + "Full path to the -P script file currently being processed. ", + "When run in -P script mode, CMake sets this variable to the full " + "path of the script file. When run to configure a CMakeLists.txt " + "file, this variable is not set.", false, + "Variables that Provide Information"); + cm->DefineProperty ("CMAKE_BUILD_TOOL", cmProperty::VARIABLE, "Tool used for the actual build process.", diff --git a/Source/cmGetCMakePropertyCommand.cxx b/Source/cmGetCMakePropertyCommand.cxx index e3448ded8b..8bb54877ee 100644 --- a/Source/cmGetCMakePropertyCommand.cxx +++ b/Source/cmGetCMakePropertyCommand.cxx @@ -22,21 +22,22 @@ bool cmGetCMakePropertyCommand this->SetError("called with incorrect number of arguments"); return false; } - + std::vector::size_type cc; std::string variable = args[0]; std::string output = "NOTFOUND"; - if ( args[1] == "VARIABLES") + if ( args[1] == "VARIABLES" ) { int cacheonly = 0; std::vector vars = this->Makefile->GetDefinitions(cacheonly); - for ( cc = 0; cc < vars.size(); cc ++ ) + if (vars.size()>0) { - if ( cc > 0 ) - { - output += ";"; - } + output = vars[0]; + } + for ( cc = 1; cc < vars.size(); ++cc ) + { + output += ";"; output += vars[cc]; } } @@ -62,15 +63,15 @@ bool cmGetCMakePropertyCommand } else { - const char *prop = + const char *prop = this->Makefile->GetCMakeInstance()->GetProperty(args[1].c_str()); if (prop) { output = prop; } } + this->Makefile->AddDefinition(variable.c_str(), output.c_str()); - + return true; } - diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index e14e44d548..daea7bf411 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2744,6 +2744,11 @@ void cmMakefile::SetHomeOutputDirectory(const char* lib) } } +void cmMakefile::SetScriptModeFile(const char* scriptfile) +{ + this->AddDefinition("CMAKE_SCRIPT_MODE_FILE", scriptfile); +} + //---------------------------------------------------------------------------- cmSourceFile* cmMakefile::GetSource(const char* sourceName) { diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index 837a352a3c..ebe2801966 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -411,7 +411,12 @@ public: return this->HomeOutputDirectory.c_str(); } //@} - + + /** + * Set CMAKE_SCRIPT_MODE_FILE variable when running a -P script. + */ + void SetScriptModeFile(const char* scriptfile); + //@{ /** * Set/Get the start directory (or output directory). The start directory diff --git a/Source/cmake.cxx b/Source/cmake.cxx index bab0aaf834..e063ea61ec 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -510,6 +510,12 @@ void cmake::ReadListFile(const char *path) (cmSystemTools::GetCurrentWorkingDirectory().c_str()); lg->GetMakefile()->SetStartDirectory (cmSystemTools::GetCurrentWorkingDirectory().c_str()); + if (this->GetScriptMode()) + { + std::string file(cmSystemTools::CollapseFullPath(path)); + cmSystemTools::ConvertToUnixSlashes(file); + lg->GetMakefile()->SetScriptModeFile(file.c_str()); + } if (!lg->GetMakefile()->ReadListFile(0, path)) { cmSystemTools::Error("Error processing file:", path); diff --git a/Tests/CMakeTests/StringTestScript.cmake b/Tests/CMakeTests/StringTestScript.cmake index 37038561dc..d1db2ee37b 100644 --- a/Tests/CMakeTests/StringTestScript.cmake +++ b/Tests/CMakeTests/StringTestScript.cmake @@ -194,6 +194,8 @@ elseif(testname STREQUAL random_with_various_alphabets) # pass string(RANDOM LENGTH 78 ALPHABET "~`!@#$%^&*()_-+={}[]\\|:\\;'\",.<>/?" v) message(STATUS "v='${v}'") + message(STATUS "CMAKE_SCRIPT_MODE_FILE='${CMAKE_SCRIPT_MODE_FILE}'") + else() # fail message(FATAL_ERROR "testname='${testname}' - error: no such test in '${CMAKE_CURRENT_LIST_FILE}'")