summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2021-02-04 13:22:13 (GMT)
committerKitware Robot <kwrobot@kitware.com>2021-02-04 13:22:19 (GMT)
commit4cd23c5e0d5835781b95f6eda778a35d2f55beb8 (patch)
treeb1d1b4380ae20258cf37efd1df15ef2ba21c698e
parentbf6e6550315097db430b5d5773e6973facd5ad17 (diff)
parent5af38a11ce8da342006bb29d6af00596f79e002c (diff)
downloadCMake-4cd23c5e0d5835781b95f6eda778a35d2f55beb8.zip
CMake-4cd23c5e0d5835781b95f6eda778a35d2f55beb8.tar.gz
CMake-4cd23c5e0d5835781b95f6eda778a35d2f55beb8.tar.bz2
Merge topic 'issue-17634'
5af38a11ce try_run: Allow to set working directory Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !5757
-rw-r--r--Help/command/try_run.rst7
-rw-r--r--Help/release/dev/try_run-allow-to-set-working-directory.rst5
-rw-r--r--Source/cmTryRunCommand.cxx21
-rw-r--r--Source/cmTryRunCommand.h1
-rw-r--r--Tests/RunCMake/try_run/RunCMakeTest.cmake2
-rw-r--r--Tests/RunCMake/try_run/WorkingDirArg.cmake9
6 files changed, 43 insertions, 2 deletions
diff --git a/Help/command/try_run.rst b/Help/command/try_run.rst
index 272c8bd..404de98 100644
--- a/Help/command/try_run.rst
+++ b/Help/command/try_run.rst
@@ -20,6 +20,7 @@ Try Compiling and Running Source Files
[COMPILE_OUTPUT_VARIABLE <var>]
[RUN_OUTPUT_VARIABLE <var>]
[OUTPUT_VARIABLE <var>]
+ [WORKING_DIRECTORY <var>]
[ARGS <args>...])
Try compiling a ``<srcfile>``. Returns ``TRUE`` or ``FALSE`` for success
@@ -74,6 +75,12 @@ The options are:
``RUN_OUTPUT_VARIABLE <var>``
Report the output from running the executable in a given variable.
+``WORKING_DIRECTORY <var>``
+ .. versionadded:: 3.20
+
+ Run the executable in the given directory. If no ``WORKING_DIRECTORY`` is
+ specified, the executable will run in ``<bindir>``.
+
Other Behavior Settings
^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/Help/release/dev/try_run-allow-to-set-working-directory.rst b/Help/release/dev/try_run-allow-to-set-working-directory.rst
new file mode 100644
index 0000000..a97e46d
--- /dev/null
+++ b/Help/release/dev/try_run-allow-to-set-working-directory.rst
@@ -0,0 +1,5 @@
+try_run-allow-to-set-working-directory
+--------------------------------------
+
+* The :command:`try_run` command gained a ``WORKING_DIRECTORY`` option to
+ support setting of working directory.
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;
};
diff --git a/Tests/RunCMake/try_run/RunCMakeTest.cmake b/Tests/RunCMake/try_run/RunCMakeTest.cmake
index 3689562..fa30eb4 100644
--- a/Tests/RunCMake/try_run/RunCMakeTest.cmake
+++ b/Tests/RunCMake/try_run/RunCMakeTest.cmake
@@ -8,3 +8,5 @@ if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin|Windows)$" AND
run_cmake(LinkOptions)
unset (RunCMake_TEST_OPTIONS)
endif()
+
+run_cmake(WorkingDirArg)
diff --git a/Tests/RunCMake/try_run/WorkingDirArg.cmake b/Tests/RunCMake/try_run/WorkingDirArg.cmake
new file mode 100644
index 0000000..b583823
--- /dev/null
+++ b/Tests/RunCMake/try_run/WorkingDirArg.cmake
@@ -0,0 +1,9 @@
+try_run(RUN_RESULT COMPILE_RESULT
+ ${CMAKE_CURRENT_BINARY_DIR}/CMakeTmp ${CMAKE_CURRENT_SOURCE_DIR}/src.c
+ RUN_OUTPUT_VARIABLE OUTPUT_VARIABLE
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeTmp/workdir
+ )
+
+if(RUN_RESULT)
+ message(SEND_ERROR "try run failed with result: ${RUN_RESULT}")
+endif()