summaryrefslogtreecommitdiff
path: root/Source/cmTryRunCommand.cxx
diff options
context:
space:
mode:
authorAsit Dhal <dhal.asitk@gmail.com>2021-01-31 17:57:39 +0100
committerAsit Dhal <dhal.asitk@gmail.com>2021-02-03 08:42:05 +0100
commit5af38a11ce8da342006bb29d6af00596f79e002c (patch)
tree078050ad13f867021b49e2a05d62db5c4f8eab23 /Source/cmTryRunCommand.cxx
parentaf695cee7346fa0eee478f71952fd02f0df989cf (diff)
downloadcmake-5af38a11ce8da342006bb29d6af00596f79e002c.tar.gz
try_run: Allow to set working directory
Fixes: #17634
Diffstat (limited to 'Source/cmTryRunCommand.cxx')
-rw-r--r--Source/cmTryRunCommand.cxx21
1 files changed, 19 insertions, 2 deletions
diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx
index 3f89641e14..8cac74d340 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;