From 88548a45fbd35205469ac9fd332349551e172496 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 26 Jan 2011 16:32:17 -0500 Subject: [PATCH] Handle relative WORKING_DIRECTORY in add_custom_(command|target) This also fixes handling of trailing slashes in the directory name. --- Source/cmAddCustomCommandCommand.cxx | 7 +++++++ Source/cmAddCustomCommandCommand.h | 2 ++ Source/cmAddCustomTargetCommand.cxx | 8 ++++++++ Source/cmAddCustomTargetCommand.h | 2 ++ Tests/CustomCommandWorkingDirectory/CMakeLists.txt | 11 +++++++++-- 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Source/cmAddCustomCommandCommand.cxx b/Source/cmAddCustomCommandCommand.cxx index 7af6ec8d7..502829ef8 100644 --- a/Source/cmAddCustomCommandCommand.cxx +++ b/Source/cmAddCustomCommandCommand.cxx @@ -286,6 +286,13 @@ bool cmAddCustomCommandCommand return false; } + // Convert working directory to a full path. + if(!working.empty()) + { + const char* build_dir = this->Makefile->GetCurrentOutputDirectory(); + working = cmSystemTools::CollapseFullPath(working.c_str(), build_dir); + } + // Choose which mode of the command to use. bool escapeOldStyle = !verbatim; if(source.empty() && output.empty()) diff --git a/Source/cmAddCustomCommandCommand.h b/Source/cmAddCustomCommandCommand.h index 490e043ae..47b542c81 100644 --- a/Source/cmAddCustomCommandCommand.h +++ b/Source/cmAddCustomCommandCommand.h @@ -110,6 +110,8 @@ public: "will be treated as PRE_LINK.\n" "If WORKING_DIRECTORY is specified the command will be executed " "in the directory given. " + "If it is a relative path it will be interpreted relative to the " + "build tree directory corresponding to the current source directory. " "If COMMENT is set, the value will be displayed as a " "message before the commands are executed at build time. " "If APPEND is specified the COMMAND and DEPENDS option values " diff --git a/Source/cmAddCustomTargetCommand.cxx b/Source/cmAddCustomTargetCommand.cxx index edb787b0f..27dea986f 100644 --- a/Source/cmAddCustomTargetCommand.cxx +++ b/Source/cmAddCustomTargetCommand.cxx @@ -166,6 +166,14 @@ bool cmAddCustomTargetCommand } } + // Convert working directory to a full path. + if(!working_directory.empty()) + { + const char* build_dir = this->Makefile->GetCurrentOutputDirectory(); + working_directory = + cmSystemTools::CollapseFullPath(working_directory.c_str(), build_dir); + } + // Add the utility target to the makefile. bool escapeOldStyle = !verbatim; cmTarget* target = diff --git a/Source/cmAddCustomTargetCommand.h b/Source/cmAddCustomTargetCommand.h index 7a2b3962b..6d94fb292 100644 --- a/Source/cmAddCustomTargetCommand.h +++ b/Source/cmAddCustomTargetCommand.h @@ -79,6 +79,8 @@ public: "empty target will be created. " "If WORKING_DIRECTORY is set, then the command will be run in that " "directory. " + "If it is a relative path it will be interpreted relative to the " + "build tree directory corresponding to the current source directory. " "If COMMENT is set, the value will be displayed as a " "message before the commands are executed at build time. " "Dependencies listed with the DEPENDS argument may reference files " diff --git a/Tests/CustomCommandWorkingDirectory/CMakeLists.txt b/Tests/CustomCommandWorkingDirectory/CMakeLists.txt index d272ffe3c..36d32e4a0 100644 --- a/Tests/CustomCommandWorkingDirectory/CMakeLists.txt +++ b/Tests/CustomCommandWorkingDirectory/CMakeLists.txt @@ -10,6 +10,7 @@ ADD_CUSTOM_COMMAND( SET_SOURCE_FILES_PROPERTIES( "${TestWorkingDir_BINARY_DIR}/customTarget.c" + "${TestWorkingDir_BINARY_DIR}/customTarget2.c" PROPERTIES GENERATED 1) ADD_EXECUTABLE(working "${TestWorkingDir_BINARY_DIR}/working.c" @@ -28,8 +29,14 @@ add_custom_command( OUTPUT working2.c # Relative to build tree COMMAND "${CMAKE_COMMAND}" -E copy ${TestWorkingDir_SOURCE_DIR}/working.c.in ../working2.c DEPENDS ${TestWorkingDir_SOURCE_DIR}/working.c.in/ # trailing slash should be removed - WORKING_DIRECTORY ${TestWorkingDir_BINARY_DIR}/work + WORKING_DIRECTORY work/ # Relative to build tree, trailing slash ) -add_executable(working2 working2.c ${TestWorkingDir_BINARY_DIR}/customTarget.c) +add_executable(working2 working2.c ${TestWorkingDir_BINARY_DIR}/customTarget2.c) + +add_custom_target( + Custom2 ALL + COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${TestWorkingDir_SOURCE_DIR}/customTarget.c ../customTarget2.c + WORKING_DIRECTORY work/ # Relative to build tree, trailing slash +) add_dependencies(working2 Custom2)