From 5af38a11ce8da342006bb29d6af00596f79e002c Mon Sep 17 00:00:00 2001 From: Asit Dhal Date: Sun, 31 Jan 2021 17:57:39 +0100 Subject: try_run: Allow to set working directory Fixes: #17634 --- Help/command/try_run.rst | 7 +++++++ .../dev/try_run-allow-to-set-working-directory.rst | 5 +++++ Source/cmTryRunCommand.cxx | 21 +++++++++++++++++++-- Source/cmTryRunCommand.h | 1 + Tests/RunCMake/try_run/RunCMakeTest.cmake | 2 ++ Tests/RunCMake/try_run/WorkingDirArg.cmake | 9 +++++++++ 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 Help/release/dev/try_run-allow-to-set-working-directory.rst create mode 100644 Tests/RunCMake/try_run/WorkingDirArg.cmake 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 ] [RUN_OUTPUT_VARIABLE ] [OUTPUT_VARIABLE ] + [WORKING_DIRECTORY ] [ARGS ...]) Try compiling a ````. Returns ``TRUE`` or ``FALSE`` for success @@ -74,6 +75,12 @@ The options are: ``RUN_OUTPUT_VARIABLE `` Report the output from running the executable in a given variable. +``WORKING_DIRECTORY `` + .. versionadded:: 3.20 + + Run the executable in the given directory. If no ``WORKING_DIRECTORY`` is + specified, the executable will run in ````. + 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 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 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() -- cgit v0.12