diff options
author | Asit Dhal <dhal.asitk@gmail.com> | 2021-01-31 16:57:39 (GMT) |
---|---|---|
committer | Asit Dhal <dhal.asitk@gmail.com> | 2021-02-03 07:42:05 (GMT) |
commit | 5af38a11ce8da342006bb29d6af00596f79e002c (patch) | |
tree | 078050ad13f867021b49e2a05d62db5c4f8eab23 /Source | |
parent | af695cee7346fa0eee478f71952fd02f0df989cf (diff) | |
download | CMake-5af38a11ce8da342006bb29d6af00596f79e002c.zip CMake-5af38a11ce8da342006bb29d6af00596f79e002c.tar.gz CMake-5af38a11ce8da342006bb29d6af00596f79e002c.tar.bz2 |
try_run: Allow to set working directory
Fixes: #17634
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmTryRunCommand.cxx | 21 | ||||
-rw-r--r-- | Source/cmTryRunCommand.h | 1 |
2 files changed, 20 insertions, 2 deletions
diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx index 3f89641..8cac74d 100644 --- a/Source/cmTryRunCommand.cxx +++ b/Source/cmTryRunCommand.cxx @@ -84,6 +84,14 @@ bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv, } i++; this->CompileOutputVariable = argv[i]; + } else if (argv[i] == "WORKING_DIRECTORY") { + if (argv.size() <= (i + 1)) { + cmSystemTools::Error( + "WORKING_DIRECTORY specified but there is no variable"); + return false; + } + i++; + this->WorkingDirectory = argv[i]; } else { tryCompile.push_back(argv[i]); } @@ -102,6 +110,14 @@ bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv, return false; } + if (!this->WorkingDirectory.empty()) { + if (!cmSystemTools::MakeDirectory(this->WorkingDirectory)) { + cmSystemTools::Error(cmStrCat("Error creating working directory \"", + this->WorkingDirectory, "\".")); + return false; + } + } + bool captureRunOutput = false; if (!this->OutputVariable.empty()) { captureRunOutput = true; @@ -188,8 +204,9 @@ void cmTryRunCommand::RunExecutable(const std::string& runArgs, finalCommand += runArgs; } bool worked = cmSystemTools::RunSingleCommand( - finalCommand, out, out, &retVal, nullptr, cmSystemTools::OUTPUT_NONE, - cmDuration::zero()); + finalCommand, out, out, &retVal, + this->WorkingDirectory.empty() ? nullptr : this->WorkingDirectory.c_str(), + cmSystemTools::OUTPUT_NONE, cmDuration::zero()); // set the run var char retChar[16]; const char* retStr; diff --git a/Source/cmTryRunCommand.h b/Source/cmTryRunCommand.h index 070c63c..d45acd8 100644 --- a/Source/cmTryRunCommand.h +++ b/Source/cmTryRunCommand.h @@ -49,4 +49,5 @@ private: std::string OutputVariable; std::string RunOutputVariable; std::string CompileOutputVariable; + std::string WorkingDirectory; }; |