Просмотр исходного кода

ENH: Improved indentation of generated cmake_install.cmake code.

Brad King 18 лет назад
Родитель
Сommit
c83ae4673d

+ 5 - 3
Source/cmInstallGenerator.cxx

@@ -60,7 +60,8 @@ void cmInstallGenerator
                  std::vector<std::string> const& configurations,
                  const char* component /* = 0 */,
                  const char* rename /* = 0 */,
-                 const char* literal_args /* = 0 */
+                 const char* literal_args /* = 0 */,
+                 cmInstallGeneratorIndent const& indent
                  )
 {
   // Use the FILE command to install the file.
@@ -76,6 +77,7 @@ void cmInstallGenerator
     case cmTarget::INSTALL_FILES:
     default:                         stype = "FILE"; break;
     }
+  os << indent;
   os << "FILE(INSTALL DESTINATION \"" << dest << "\" TYPE " << stype.c_str();
   if(optional)
     {
@@ -120,9 +122,9 @@ void cmInstallGenerator
     for(std::vector<std::string>::const_iterator fi = files.begin();
         fi != files.end(); ++fi)
       {
-      os << "\n  \"" << *fi << "\"";
+      os << "\n" << indent << "  \"" << *fi << "\"";
       }
-    os << "\n ";
+    os << "\n" << indent << " ";
     if(!(literal_args && *literal_args))
       {
       os << " ";

+ 28 - 1
Source/cmInstallGenerator.h

@@ -21,6 +21,32 @@
 
 class cmLocalGenerator;
 
+class cmInstallGeneratorIndent
+{
+public:
+  cmInstallGeneratorIndent(): Level(0) {}
+  cmInstallGeneratorIndent(int level): Level(level) {}
+  void Write(std::ostream& os) const
+    {
+    for(int i=0; i < this->Level; ++i)
+      {
+      os << " ";
+      }
+    }
+  cmInstallGeneratorIndent Next(int step = 2) const
+    {
+    return cmInstallGeneratorIndent(this->Level + step);
+    }
+private:
+  int Level;
+};
+inline std::ostream& operator<<(std::ostream& os,
+                                cmInstallGeneratorIndent const& indent)
+{
+  indent.Write(os);
+  return os;
+}
+
 /** \class cmInstallGenerator
  * \brief Support class for generating install scripts.
  *
@@ -46,7 +72,8 @@ public:
     = std::vector<std::string>(),
     const char* component = 0,
     const char* rename = 0,
-    const char* literal_args = 0
+    const char* literal_args = 0,
+    cmInstallGeneratorIndent const& indent = cmInstallGeneratorIndent()
     );
 
   const char* GetDestination() const        {return this->Destination.c_str();}

+ 79 - 31
Source/cmInstallTargetGenerator.cxx

@@ -22,7 +22,6 @@
 #include "cmake.h"
 
 // TODO:
-//   - Fix indentation of generated code
 //   - Consolidate component/configuration checks across multiple
 //     install generators
 //   - Skip IF(EXISTS) checks if nothing is done with the installed file
@@ -49,12 +48,15 @@ cmInstallTargetGenerator
 //----------------------------------------------------------------------------
 void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
 {
+  // Track indentation.
+  Indent indent;
+
   // Begin this block of installation.
   std::string component_test = "NOT CMAKE_INSTALL_COMPONENT OR "
     "\"${CMAKE_INSTALL_COMPONENT}\" MATCHES \"^(";
   component_test += this->Component;
   component_test += ")$\"";
-  os << "IF(" << component_test << ")\n";
+  os << indent << "IF(" << component_test << ")\n";
 
   // Compute the build tree directory from which to copy the target.
   std::string fromDir;
@@ -74,7 +76,8 @@ void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
   if(this->ConfigurationTypes->empty())
     {
     this->GenerateScriptForConfig(os, fromDir.c_str(),
-                                  this->ConfigurationName);
+                                  this->ConfigurationName,
+                                  indent.Next());
     }
   else
     {
@@ -82,18 +85,48 @@ void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
           this->ConfigurationTypes->begin();
         i != this->ConfigurationTypes->end(); ++i)
       {
-      this->GenerateScriptForConfig(os, fromDir.c_str(), i->c_str());
+      this->GenerateScriptForConfig(os, fromDir.c_str(), i->c_str(),
+                                    indent.Next());
       }
     }
 
   // End this block of installation.
-  os << "ENDIF(" << component_test << ")\n";
+  os << indent << "ENDIF(" << component_test << ")\n";
+}
+
+//----------------------------------------------------------------------------
+static std::string cmInstallTargetGeneratorEncodeConfig(const char* config)
+{
+  std::string result;
+  for(const char* c = config; *c; ++c)
+    {
+    if(*c >= 'a' && *c <= 'z')
+      {
+      result += "[";
+      result += *c + ('A' - 'a');
+      result += *c;
+      result += "]";
+      }
+    else if(*c >= 'A' && *c <= 'Z')
+      {
+      result += "[";
+      result += *c;
+      result += *c + ('a' - 'A');
+      result += "]";
+      }
+    else
+      {
+      result += *c;
+      }
+    }
+  return result;
 }
 
 //----------------------------------------------------------------------------
 void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
                                                        const char* fromDir,
-                                                       const char* config)
+                                                       const char* config,
+                                                       Indent const& indent)
 {
   // Compute the per-configuration directory containing the files.
   std::string fromDirConfig = fromDir;
@@ -121,13 +154,30 @@ void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
         }
       }
 
-    // Begin this configuration block.
+    // Generate a per-configuration block.
     config_test = "\"${CMAKE_INSTALL_CONFIG_NAME}\" MATCHES \"^(";
-    config_test += config;
+    config_test += cmInstallTargetGeneratorEncodeConfig(config);
     config_test += ")$\"";
-    os << "  IF(" << config_test << ")\n";
+    os << indent << "IF(" << config_test << ")\n";
+    this->GenerateScriptForConfigDir(os, fromDirConfig.c_str(), config,
+                                     indent.Next());
+    os << indent << "ENDIF(" << config_test << ")\n";
+    }
+  else
+    {
+    this->GenerateScriptForConfigDir(os, fromDirConfig.c_str(), config,
+                                     indent);
     }
+}
 
+//----------------------------------------------------------------------------
+void
+cmInstallTargetGenerator
+::GenerateScriptForConfigDir(std::ostream& os,
+                             const char* fromDirConfig,
+                             const char* config,
+                             Indent const& indent)
+{
   // Compute the list of files to install for this target.
   std::vector<std::string> files;
   std::string literal_args;
@@ -231,7 +281,8 @@ void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
                        optional, no_properties,
                        this->FilePermissions.c_str(), no_dir_permissions,
                        no_configurations, no_component,
-                       no_rename, literal_args.c_str());
+                       no_rename, literal_args.c_str(),
+                       indent);
 
   std::string toFullPath = "$ENV{DESTDIR}";
   toFullPath += this->Destination;
@@ -239,17 +290,11 @@ void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
   toFullPath += this->GetInstallFilename(this->Target, config,
                                          this->ImportLibrary, false);
 
-  os << "    IF(EXISTS \"" << toFullPath << "\")\n";
-  this->AddInstallNamePatchRule(os, config, toFullPath);
-  this->AddRanlibRule(os, type, toFullPath);
-  this->AddStripRule(os, type, toFullPath);
-  os << "    ENDIF(EXISTS \"" << toFullPath << "\")\n";
-
-  if(config && *config)
-    {
-    // End this configuration block.
-    os << "  ENDIF(" << config_test << ")\n";
-    }
+  os << indent << "IF(EXISTS \"" << toFullPath << "\")\n";
+  this->AddInstallNamePatchRule(os, indent.Next(), config, toFullPath);
+  this->AddRanlibRule(os, indent.Next(), type, toFullPath);
+  this->AddStripRule(os, indent.Next(), type, toFullPath);
+  os << indent << "ENDIF(EXISTS \"" << toFullPath << "\")\n";
 }
 
 //----------------------------------------------------------------------------
@@ -321,8 +366,8 @@ std::string cmInstallTargetGenerator::GetInstallFilename(cmTarget* target,
 //----------------------------------------------------------------------------
 void
 cmInstallTargetGenerator
-::AddInstallNamePatchRule(std::ostream& os, const char* config,
-                          std::string const& toFullPath)
+::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
+                          const char* config, std::string const& toFullPath)
 {
   if(this->ImportLibrary ||
      !(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
@@ -411,25 +456,27 @@ cmInstallTargetGenerator
   // install_name value and references.
   if(!new_id.empty() || !install_name_remap.empty())
     {
-    os << "      EXECUTE_PROCESS(COMMAND \"" << installNameTool;
+    os << indent << "EXECUTE_PROCESS(COMMAND \"" << installNameTool;
     os << "\"";
     if(!new_id.empty())
       {
-      os << "\n        -id \"" << new_id << "\"";
+      os << "\n" << indent << "  -id \"" << new_id << "\"";
       }
     for(std::map<cmStdString, cmStdString>::const_iterator
           i = install_name_remap.begin();
         i != install_name_remap.end(); ++i)
       {
-      os << "\n        -change \"" << i->first << "\" \"" << i->second << "\"";
+      os << "\n" << indent << "  -change \""
+         << i->first << "\" \"" << i->second << "\"";
       }
-    os << "\n        \"" << toFullPath << "\")\n";
+    os << "\n" << indent << "  \"" << toFullPath << "\")\n";
     }
 }
 
 //----------------------------------------------------------------------------
 void
 cmInstallTargetGenerator::AddStripRule(std::ostream& os,
+                                       Indent const& indent,
                                        cmTarget::TargetType type,
                                        const std::string& toFullPath)
 {
@@ -453,16 +500,17 @@ cmInstallTargetGenerator::AddStripRule(std::ostream& os,
     return;
     }
 
-  os << "      IF(CMAKE_INSTALL_DO_STRIP)\n";
-  os << "        EXECUTE_PROCESS(COMMAND \""
+  os << indent << "IF(CMAKE_INSTALL_DO_STRIP)\n";
+  os << indent << "  EXECUTE_PROCESS(COMMAND \""
      << this->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
      << "\" \"" << toFullPath << "\")\n";
-  os << "      ENDIF(CMAKE_INSTALL_DO_STRIP)\n";
+  os << indent << "ENDIF(CMAKE_INSTALL_DO_STRIP)\n";
 }
 
 //----------------------------------------------------------------------------
 void
 cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
+                                        Indent const& indent,
                                         cmTarget::TargetType type,
                                         const std::string& toFullPath)
 {
@@ -486,6 +534,6 @@ cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
     return;
     }
 
-  os << "      EXECUTE_PROCESS(COMMAND \""
+  os << indent << "EXECUTE_PROCESS(COMMAND \""
      << ranlib << "\" \"" << toFullPath << "\")\n";
 }

+ 13 - 4
Source/cmInstallTargetGenerator.h

@@ -43,15 +43,24 @@ public:
   const std::vector<std::string>& GetConfigurations() const {return this->Configurations;}
   
 protected:
+  typedef cmInstallGeneratorIndent Indent;
   virtual void GenerateScript(std::ostream& os);
   void GenerateScriptForConfig(std::ostream& os,
                                const char* fromDir,
-                               const char* config);
-  void AddInstallNamePatchRule(std::ostream& os, const char* config,
+                               const char* config,
+                               Indent const& indent);
+  void GenerateScriptForConfigDir(std::ostream& os,
+                                  const char* fromDirConfig,
+                                  const char* config,
+                                  Indent const& indent);
+  void AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
+                               const char* config,
                                const std::string& toFullPath);
-  void AddStripRule(std::ostream& os, cmTarget::TargetType type,
+  void AddStripRule(std::ostream& os, Indent const& indent,
+                    cmTarget::TargetType type,
                     const std::string& toFullPath);
-  void AddRanlibRule(std::ostream& os, cmTarget::TargetType type,
+  void AddRanlibRule(std::ostream& os, Indent const& indent,
+                     cmTarget::TargetType type,
                      const std::string& toFullPath);
 
   cmTarget* Target;