Browse Source

ENH: Adding cleaning of custom command outputs during "make clean".

Brad King 21 years ago
parent
commit
4931afd89d

+ 54 - 30
Source/cmLocalUnixMakefileGenerator2.cxx

@@ -46,10 +46,6 @@
 // TODO: Add "help" target.
 // TODO: Identify remaining relative path violations.
 // TODO: Need test for separate executable/library output path.
-// TODO: Add registered files for cleaning:
-//       $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)
-//       What about cleaning custom command outputs?
-
 
 //----------------------------------------------------------------------------
 cmLocalUnixMakefileGenerator2::cmLocalUnixMakefileGenerator2()
@@ -706,6 +702,35 @@ cmLocalUnixMakefileGenerator2
   preEcho += "...";
   this->WriteMakeRule(ruleFileStream, comment, preEcho.c_str(),
                       cc.GetOutput().c_str(), depends, commands);
+
+  // Write the clean rule for this custom command.
+  std::string cleanTarget = customName;
+  cleanTarget += ".clean";
+  commands.clear();
+  depends.clear();
+  std::vector<std::string> cleanFiles;
+  cleanFiles.push_back(cc.GetOutput().c_str());
+  this->AppendCleanCommand(commands, cleanFiles);
+  this->WriteMakeRule(ruleFileStream,
+                      "Clean the output of this custom command.", 0,
+                      cleanTarget.c_str(), depends, commands);
+
+  // Check whether to attach the clean rule.
+  bool attach = true;
+  if(const char* clean_no_custom =
+     m_Makefile->GetProperty("CLEAN_NO_CUSTOM"))
+    {
+    if(!cmSystemTools::IsOff(clean_no_custom))
+      {
+      attach = false;
+      }
+    }
+
+  // Attach the clean rule to the directory-level clean rule.
+  if(attach)
+    {
+    this->WriteLocalRule(ruleFileStream, "clean", cleanTarget.c_str());
+    }
 }
 
 //----------------------------------------------------------------------------
@@ -1797,23 +1822,17 @@ cmLocalUnixMakefileGenerator2
 
   // Add a command to remove any existing files for this library.
   std::vector<std::string> cleanFiles;
-  std::string remove = "$(CMAKE_COMMAND) -E remove -f ";
-  remove += targetOutPathReal;
-  cleanFiles.push_back(targetOutPathReal);
+  cleanFiles.push_back(targetFullPathReal);
   if(targetOutPathSO != targetOutPathReal)
     {
-    remove += " ";
-    remove += targetOutPathSO;
-    cleanFiles.push_back(targetOutPathSO);
+    cleanFiles.push_back(targetFullPathSO);
     }
   if(targetOutPath != targetOutPathSO &&
      targetOutPath != targetOutPathReal)
     {
-    remove += " ";
-    remove += targetOutPath;
-    cleanFiles.push_back(targetOutPath);
+    cleanFiles.push_back(targetFullPath);
     }
-  commands.push_back(remove);
+  this->AppendCleanCommand(commands, cleanFiles);
 
   // TODO: Pre-build and pre-link rules.
 
@@ -2008,14 +2027,7 @@ cmLocalUnixMakefileGenerator2
   cleanTarget += ".clean";
 
   // Construct the clean command.
-  std::string remove = "$(CMAKE_COMMAND) -E remove -f";
-  for(std::vector<std::string>::const_iterator f = files.begin();
-      f != files.end(); ++f)
-    {
-    remove += " ";
-    remove += *f;
-    }
-  commands.push_back(remove);
+  this->AppendCleanCommand(commands, files);
 
   // Write the rule.
   this->WriteMakeRule(ruleFileStream, 0, 0, cleanTarget.c_str(),
@@ -2110,16 +2122,9 @@ cmLocalUnixMakefileGenerator2
   else
     {
     // Have extra files to clean.  Write the action to remove them.
-    std::string remove = "$(CMAKE_COMMAND) -E remove -f";
-    for(std::vector<std::string>::iterator i = files.begin();
-        i != files.end(); ++i)
-      {
-      remove += " ";
-      remove += this->ConvertToRelativeOutputPath(i->c_str());
-      }
     std::vector<std::string> no_depends;
     std::vector<std::string> commands;
-    commands.push_back(remove);
+    this->AppendCleanCommand(commands, files);
     this->WriteMakeRule(makefileStream,
                         "Clean extra files in this directory.", 0,
                         "clean.local", no_depends, commands);
@@ -2719,6 +2724,25 @@ cmLocalUnixMakefileGenerator2
   commands.push_back(cmd);
 }
 
+//----------------------------------------------------------------------------
+void
+cmLocalUnixMakefileGenerator2
+::AppendCleanCommand(std::vector<std::string>& commands,
+                     const std::vector<std::string>& files)
+{
+  if(!files.empty())
+    {
+    std::string remove = "$(CMAKE_COMMAND) -E remove -f";
+    for(std::vector<std::string>::const_iterator f = files.begin();
+        f != files.end(); ++f)
+      {
+      remove += " ";
+      remove += this->ConvertToRelativeOutputPath(f->c_str());
+      }
+    commands.push_back(remove);
+    }
+}
+
 //----------------------------------------------------------------------------
 std::string
 cmLocalUnixMakefileGenerator2

+ 2 - 0
Source/cmLocalUnixMakefileGenerator2.h

@@ -180,6 +180,8 @@ protected:
                             const std::vector<cmCustomCommand>& ccs);
   void AppendCustomCommand(std::vector<std::string>& commands,
                            const cmCustomCommand& cc);
+  void AppendCleanCommand(std::vector<std::string>& commands,
+                          const std::vector<std::string>& files);
   std::string GetRecursiveMakeCall(const char* tgt);
   void WriteJumpAndBuildRules(std::ostream& makefileStream);
 

+ 0 - 1
Tests/MakeClean/CMakeLists.txt

@@ -5,7 +5,6 @@ TRY_COMPILE(TOCLEAN_BUILT
   ${MakeClean_BINARY_DIR}/ToClean
   ${MakeClean_SOURCE_DIR}/ToClean
   ToClean
-  toclean
   OUTPUT_VARIABLE OUTPUT
   )
 IF(TOCLEAN_BUILT)

+ 19 - 4
Tests/MakeClean/ToClean/CMakeLists.txt

@@ -5,10 +5,13 @@ ADD_EXECUTABLE(toclean toclean.cxx)
 
 # List some build-time-generated files.
 GET_TARGET_PROPERTY(TOCLEAN_FILES toclean LOCATION)
-SET(TOCLEAN_FILES ${TOCLEAN_FILES}
-  "${ToClean_BINARY_DIR}/toclean${CMAKE_CXX_OUTPUT_EXTENSION}")
-#SET(TOCLEAN_FILES ${TOCLEAN_FILES}
-#  "${ToClean_BINARY_DIR}/toclean.dir/toclean${CMAKE_CXX_OUTPUT_EXTENSION}")
+IF(CMAKE_GENERATOR_NEW)
+  SET(TOCLEAN_FILES ${TOCLEAN_FILES}
+    "${ToClean_BINARY_DIR}/toclean.dir/toclean${CMAKE_CXX_OUTPUT_EXTENSION}")
+ELSE(CMAKE_GENERATOR_NEW)
+  SET(TOCLEAN_FILES ${TOCLEAN_FILES}
+    "${ToClean_BINARY_DIR}/toclean${CMAKE_CXX_OUTPUT_EXTENSION}")
+ENDIF(CMAKE_GENERATOR_NEW)
 
 # Create a file that must be registered for cleaning.
 FILE(WRITE "${ToClean_BINARY_DIR}/Registered.txt"
@@ -17,6 +20,18 @@ SET_DIRECTORY_PROPERTIES(PROPERTIES
   ADDITIONAL_MAKE_CLEAN_FILES "${ToClean_BINARY_DIR}/Registered.txt")
 SET(TOCLEAN_FILES ${TOCLEAN_FILES} "${ToClean_BINARY_DIR}/Registered.txt")
 
+# Create a custom command whose output should be cleaned.
+IF(CMAKE_GENERATOR_NEW)
+  ADD_CUSTOM_COMMAND(OUTPUT ${ToClean_BINARY_DIR}/generated.txt
+    DEPENDS ${ToClean_SOURCE_DIR}/toclean.cxx
+    COMMAND ${CMAKE_COMMAND}
+    ARGS -E copy ${ToClean_SOURCE_DIR}/toclean.cxx
+                 ${ToClean_BINARY_DIR}/generated.txt
+    )
+  ADD_CUSTOM_TARGET(generate ALL DEPENDS ${ToClean_BINARY_DIR}/generated.txt)
+  SET(TOCLEAN_FILES ${TOCLEAN_FILES} "${ToClean_BINARY_DIR}/generated.txt")
+ENDIF(CMAKE_GENERATOR_NEW)
+
 # Configure a file listing these build-time-generated files.
 CONFIGURE_FILE(${ToClean_SOURCE_DIR}/ToCleanFiles.cmake.in
                ${ToClean_BINARY_DIR}/ToCleanFiles.cmake @ONLY IMMEDIATE)

+ 4 - 0
Tests/Wrapping/CMakeLists.txt

@@ -3,6 +3,10 @@
 #
 PROJECT (Wrapping)
 
+# Disable cleaning of custom command outputs to preserve the hacks
+# used to generate the files using CONFIGURE_FILE.
+SET_DIRECTORY_PROPERTIES(PROPERTIES CLEAN_NO_CUSTOM 1)
+
 #
 # Lib and exe path
 #