Ver código fonte

add_custom_{command,target}: WORKING_DIRECTORY generator expressions

This teaches add_custom_command and add_custom_target WORKING_DIRECTORY
about generator expressions

Fixes: #14089
Jon Chronopoulos 7 anos atrás
pai
commit
f158ac19e1

+ 3 - 0
Help/command/add_custom_command.rst

@@ -182,6 +182,9 @@ The options are:
   If it is a relative path it will be interpreted relative to the
   build tree directory corresponding to the current source directory.
 
+  Arguments to ``WORKING_DIRECTORY`` may use
+  :manual:`generator expressions <cmake-generator-expressions(7)>`.
+
 ``DEPFILE``
   Specify a ``.d`` depfile for the :generator:`Ninja` generator.
   A ``.d`` file holds dependencies usually emitted by the custom

+ 3 - 0
Help/command/add_custom_target.rst

@@ -121,3 +121,6 @@ The options are:
   Execute the command with the given current working directory.
   If it is a relative path it will be interpreted relative to the
   build tree directory corresponding to the current source directory.
+
+  Arguments to ``WORKING_DIRECTORY`` may use
+  :manual:`generator expressions <cmake-generator-expressions(7)>`.

+ 5 - 0
Help/release/dev/custom_command-working_directory-genex.rst

@@ -0,0 +1,5 @@
+custom_command-working_directory-genex
+--------------------------------------
+
+* The :command:`add_custom_command` and :command:`add_custom_target` commands
+  learned to support generator expressions in ``WORKING_DIRECTORY`` options.

+ 8 - 1
Source/cmCustomCommandGenerator.cxx

@@ -64,6 +64,13 @@ cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
     }
     this->Depends.insert(this->Depends.end(), result.begin(), result.end());
   }
+
+  const std::string& workingdirectory = this->CC.GetWorkingDirectory();
+  if (!workingdirectory.empty()) {
+    std::unique_ptr<cmCompiledGeneratorExpression> cge =
+      this->GE->Parse(workingdirectory);
+    this->WorkingDirectory = cge->Evaluate(this->LG, this->Config);
+  }
 }
 
 cmCustomCommandGenerator::~cmCustomCommandGenerator()
@@ -186,7 +193,7 @@ const char* cmCustomCommandGenerator::GetComment() const
 
 std::string cmCustomCommandGenerator::GetWorkingDirectory() const
 {
-  return this->CC.GetWorkingDirectory();
+  return this->WorkingDirectory;
 }
 
 std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const

+ 1 - 0
Source/cmCustomCommandGenerator.h

@@ -23,6 +23,7 @@ class cmCustomCommandGenerator
   cmGeneratorExpression* GE;
   cmCustomCommandLines CommandLines;
   std::vector<std::string> Depends;
+  std::string WorkingDirectory;
 
   const char* GetCrossCompilingEmulator(unsigned int c) const;
   const char* GetArgv0Location(unsigned int c) const;

+ 20 - 0
Tests/CustomCommandWorkingDirectory/CMakeLists.txt

@@ -42,3 +42,23 @@ add_custom_target(
 )
 
 add_dependencies(working2 Custom2)
+
+file(MAKE_DIRECTORY ${TestWorkingDir_BINARY_DIR}/genex)
+add_custom_command(
+  OUTPUT "${TestWorkingDir_BINARY_DIR}/genex/working.c"
+  COMMAND "${CMAKE_COMMAND}" -E copy "${TestWorkingDir_SOURCE_DIR}/working.c.in" "${TestWorkingDir_BINARY_DIR}/genex/working.c"
+  WORKING_DIRECTORY "${TestWorkingDir_BINARY_DIR}/$<1:genex>/"
+  COMMENT "custom command"
+)
+
+add_executable(workinggenex "${TestWorkingDir_BINARY_DIR}/genex/working.c"
+  "${TestWorkingDir_BINARY_DIR}/genex/customTarget.c")
+
+add_custom_target(
+  CustomGenex ALL
+  COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${TestWorkingDir_SOURCE_DIR}/customTarget.c" "${TestWorkingDir_BINARY_DIR}/genex/customTarget.c"
+  BYPRODUCTS "${TestWorkingDir_BINARY_DIR}/genex/customTarget.c"
+  WORKING_DIRECTORY "${TestWorkingDir_BINARY_DIR}/$<1:genex>/"
+)
+
+add_dependencies(workinggenex CustomGenex)