Selaa lähdekoodia

Refactor target COMPILE_OPTIONS and COMPILE_FLAGS handling

Replace the cmLocalGenerator GetCompileOptions method with an
AddCompileOptions method since all call sites of the former simply
append the result to a flags string anyway.

Add a "lang" argument to AddCompileOptions and move the
CMAKE_<LANG>_FLAGS_REGEX filter into it.  Move the call sites in each
generator to a location that has both the language and configuration
available.  In the Makefile generator this also moves the flags from
build.make to flags.make where they belong.
Brad King 12 vuotta sitten
vanhempi
sitoutus
d221eac812

+ 1 - 28
Source/cmExtraSublimeTextGenerator.cxx

@@ -429,34 +429,7 @@ cmExtraSublimeTextGenerator::ComputeFlagsForObject(cmSourceFile* source,
   lg->AppendFlags(flags, makefile->GetDefineFlags());
 
   // Add target-specific flags.
-  std::string targetFlags;
-  lg->GetCompileOptions(targetFlags, target, config);
-  if (!targetFlags.empty())
-    {
-    std::string langIncludeExpr = "CMAKE_";
-    langIncludeExpr += language;
-    langIncludeExpr += "_FLAG_REGEX";
-    const char* regex = makefile->GetDefinition(langIncludeExpr.c_str());
-    if(regex)
-      {
-      cmsys::RegularExpression r(regex);
-      std::vector<std::string> args;
-      cmSystemTools::
-        ParseWindowsCommandLine(targetFlags.c_str(), args);
-      for(std::vector<std::string>::iterator i = args.begin();
-          i != args.end(); ++i)
-        {
-        if(r.find(i->c_str()))
-          {
-          lg->AppendFlags(flags, i->c_str());
-          }
-        }
-      }
-    else
-      {
-      lg->AppendFlags(flags, targetFlags.c_str());
-      }
-    }
+  lg->AddCompileOptions(flags, target, config, language);
 
   // Add source file specific flags.
   lg->AppendFlags(flags, source->GetProperty("COMPILE_FLAGS"));

+ 5 - 6
Source/cmGlobalXCodeGenerator.cxx

@@ -681,12 +681,6 @@ cmGlobalXCodeGenerator::CreateXCodeSourceFile(cmLocalGenerator* lg,
 {
   // Add flags from target and source file properties.
   std::string flags;
-  std::string targetFlags;
-  lg->GetCompileOptions(targetFlags, &cmtarget, 0); // TODO: Config?
-  if(!targetFlags.empty())
-    {
-    lg->AppendFlags(flags, targetFlags.c_str());
-    }
   const char* srcfmt = sf->GetProperty("Fortran_FORMAT");
   switch(this->CurrentLocalGenerator->GetFortranFormat(srcfmt))
     {
@@ -1704,6 +1698,8 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
       this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
       this->CurrentLocalGenerator->AddCMP0018Flags(cflags, &target,
                                                    "C", configName);
+      this->CurrentLocalGenerator->
+        AddCompileOptions(cflags, &target, "C", configName);
       }
 
     // Add language-specific flags.
@@ -1715,6 +1711,9 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
 
     this->CurrentLocalGenerator->AddVisibilityPresetFlags(flags, &target,
                                                    lang);
+
+    this->CurrentLocalGenerator->
+      AddCompileOptions(flags, &target, lang, configName);
     }
   else if(binary)
   {

+ 40 - 12
Source/cmLocalGenerator.cxx

@@ -1339,22 +1339,50 @@ std::string cmLocalGenerator::GetIncludeFlags(
 }
 
 //----------------------------------------------------------------------------
-void cmLocalGenerator::GetCompileOptions(std::string& flags,
-                                             cmTarget* target,
-                                             const char *config)
+void cmLocalGenerator::AddCompileOptions(
+  std::string& flags, cmTarget* target,
+  const char* lang, const char* config
+  )
 {
-  // Add target-specific flags.
-  if(const char *prop = target->GetProperty("COMPILE_FLAGS"))
+  std::string langFlagRegexVar = std::string("CMAKE_")+lang+"_FLAG_REGEX";
+  if(const char* langFlagRegexStr =
+     this->Makefile->GetDefinition(langFlagRegexVar.c_str()))
     {
-    this->AppendFlags(flags, prop);
+    // Filter flags acceptable to this language.
+    cmsys::RegularExpression r(langFlagRegexStr);
+    std::vector<std::string> opts;
+    if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
+      {
+      cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
+      }
+    target->GetCompileOptions(opts, config);
+    for(std::vector<std::string>::const_iterator i = opts.begin();
+        i != opts.end(); ++i)
+      {
+      if(r.find(i->c_str()))
+        {
+        // (Re-)Escape this flag.  COMPILE_FLAGS were already parsed
+        // as a command line above, and COMPILE_OPTIONS are escaped.
+        this->AppendFlagEscape(flags, i->c_str());
+        }
+      }
     }
-
-  std::vector<std::string> opts; // TODO: Emitted.
-  target->GetCompileOptions(opts, config);
-  for(std::vector<std::string>::const_iterator li = opts.begin();
-      li != opts.end(); ++li)
+  else
     {
-    this->AppendFlagEscape(flags, li->c_str());
+    // Use all flags.
+    if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
+      {
+      // COMPILE_FLAGS are not escaped for historical reasons.
+      this->AppendFlags(flags, targetFlags);
+      }
+    std::vector<std::string> opts; // TODO: Emitted.
+    target->GetCompileOptions(opts, config);
+    for(std::vector<std::string>::const_iterator i = opts.begin();
+        i != opts.end(); ++i)
+      {
+      // COMPILE_OPTIONS are escaped.
+      this->AppendFlagEscape(flags, i->c_str());
+      }
     }
 }
 

+ 2 - 3
Source/cmLocalGenerator.h

@@ -218,9 +218,8 @@ public:
                              cmGeneratorTarget* target,
                              const char* lang = "C", const char *config = 0,
                              bool stripImplicitInclDirs = true);
-  void GetCompileOptions(std::string& flags,
-                         cmTarget* target,
-                         const char *config);
+  void AddCompileOptions(std::string& flags, cmTarget* target,
+                         const char* lang, const char* config);
 
   /** Compute the language used to compile the given source file.  */
   const char* GetSourceFileLanguage(const cmSourceFile& source);

+ 8 - 26
Source/cmLocalVisualStudio6Generator.cxx

@@ -1674,6 +1674,14 @@ void cmLocalVisualStudio6Generator
       flagVar = baseFlagVar + "_RELWITHDEBINFO";
       flagsRelWithDebInfo = this->Makefile->GetSafeDefinition(flagVar.c_str());
       flagsRelWithDebInfo += " -DCMAKE_INTDIR=\\\"RelWithDebInfo\\\" ";
+
+      this->AddCompileOptions(flags, &target, linkLanguage, 0);
+      this->AddCompileOptions(flagsDebug, &target, linkLanguage, "Debug");
+      this->AddCompileOptions(flagsRelease, &target, linkLanguage, "Release");
+      this->AddCompileOptions(flagsMinSizeRel, &target, linkLanguage,
+                              "MinSizeRel");
+      this->AddCompileOptions(flagsRelWithDebInfo, &target, linkLanguage,
+                              "RelWithDebInfo");
       }
 
     // if _UNICODE and _SBCS are not found, then add -D_MBCS
@@ -1686,32 +1694,6 @@ void cmLocalVisualStudio6Generator
       flags += " /D \"_MBCS\"";
       }
 
-    {
-    std::string targetFlags;
-    this->GetCompileOptions(targetFlags, &target, 0);
-    // Add per-target flags.
-    if(!targetFlags.empty())
-      {
-      flags += " ";
-      flags += targetFlags;
-      }
-    }
-#define ADD_FLAGS(CONFIG) \
-    { \
-    std::string targetFlags; \
-    this->GetCompileOptions(targetFlags, &target, #CONFIG); \
-    if(!targetFlags.empty()) \
-      { \
-      flags ## CONFIG += " "; \
-      flags ## CONFIG += targetFlags; \
-      } \
-    }
-
-    ADD_FLAGS(Debug)
-    ADD_FLAGS(Release)
-    ADD_FLAGS(MinSizeRel)
-    ADD_FLAGS(RelWithDebInfo)
-
     // Add per-target and per-configuration preprocessor definitions.
     std::set<std::string> definesSet;
     std::set<std::string> debugDefinesSet;

+ 3 - 9
Source/cmLocalVisualStudio7Generator.cxx

@@ -711,6 +711,9 @@ void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
       {
       flags += " /TP ";
       }
+
+    // Add the target-specific flags.
+    this->AddCompileOptions(flags, &target, linkLanguage, configName);
     }
 
   if(this->FortranProject)
@@ -723,15 +726,6 @@ void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
       }
     }
 
-  std::string targetFlags;
-  this->GetCompileOptions(targetFlags, &target, configName);
-  // Add the target-specific flags.
-  if(!targetFlags.empty())
-    {
-    flags += " ";
-    flags += targetFlags;
-    }
-
   // Get preprocessor definitions for this directory.
   std::string defineFlags = this->Makefile->GetDefineFlags();
   Options::Tool t = Options::Compiler;

+ 4 - 46
Source/cmMakefileTargetGenerator.cxx

@@ -285,6 +285,10 @@ std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
     this->LocalGenerator->
       AppendFlags(flags,this->GetFrameworkFlags().c_str());
 
+    // Add target-specific flags.
+    this->LocalGenerator->AddCompileOptions(flags, this->Target,
+                                            lang, this->ConfigName);
+
     ByLanguageMap::value_type entry(l, flags);
     i = this->FlagsByLanguage.insert(entry).first;
     }
@@ -335,25 +339,12 @@ void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
       this->Makefile->GetSafeDefinition(compiler.c_str()) << "\n";
     }
 
-  std::string targetFlags;
   for(std::set<cmStdString>::const_iterator l = languages.begin();
       l != languages.end(); ++l)
     {
     *this->FlagFileStream << *l << "_FLAGS = " << this->GetFlags(*l) << "\n\n";
     *this->FlagFileStream << *l << "_DEFINES = " << this->GetDefines(*l) <<
       "\n\n";
-    std::string targetLangFlags;
-    this->LocalGenerator->GetCompileOptions(targetLangFlags, this->Target,
-                            this->LocalGenerator->ConfigurationName.c_str());
-    if (!targetFlags.empty() && targetFlags != targetLangFlags)
-      {
-      targetFlags += " " + targetLangFlags;
-      }
-    }
-
-  if (!targetFlags.empty())
-    {
-    *this->FlagFileStream << "# TARGET_FLAGS = " << targetFlags << "\n\n";
     }
 }
 
@@ -542,39 +533,6 @@ cmMakefileTargetGenerator
   std::string configUpper =
     cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
 
-  std::string targetFlags;
-  this->LocalGenerator->GetCompileOptions(targetFlags, this->Target,
-                                          configUpper.c_str());
-  if (!targetFlags.empty())
-    {
-    std::string langIncludeExpr = "CMAKE_";
-    langIncludeExpr += lang;
-    langIncludeExpr += "_FLAG_REGEX";
-    const char* regex = this->Makefile->
-      GetDefinition(langIncludeExpr.c_str());
-    if(regex)
-      {
-      cmsys::RegularExpression r(regex);
-      std::vector<std::string> args;
-      cmSystemTools::ParseWindowsCommandLine(
-        targetFlags.c_str(),
-        args);
-      for(std::vector<std::string>::iterator i = args.begin();
-          i != args.end(); ++i)
-        {
-        if(r.find(i->c_str()))
-          {
-          this->LocalGenerator->AppendFlags
-            (flags, i->c_str());
-          }
-        }
-      }
-    else
-      {
-      this->LocalGenerator->AppendFlags(flags, targetFlags.c_str());
-      }
-    }
-
   // Add Fortran format flags.
   if(strcmp(lang, "Fortran") == 0)
     {

+ 2 - 32
Source/cmNinjaTargetGenerator.cxx

@@ -174,38 +174,8 @@ cmNinjaTargetGenerator::ComputeFlagsForObject(cmSourceFile *source,
   this->LocalGenerator->AppendFlags(flags, this->Makefile->GetDefineFlags());
 
   // Add target-specific flags.
-  std::string targetFlags;
-  this->LocalGenerator->GetCompileOptions(targetFlags, this->Target, config);
-  if(!targetFlags.empty())
-    {
-    std::string langIncludeExpr = "CMAKE_";
-    langIncludeExpr += language;
-    langIncludeExpr += "_FLAG_REGEX";
-    const char* regex = this->Makefile->
-      GetDefinition(langIncludeExpr.c_str());
-    if(regex)
-      {
-      cmsys::RegularExpression r(regex);
-      std::vector<std::string> args;
-      cmSystemTools::ParseWindowsCommandLine(
-        targetFlags.c_str(),
-        args);
-      for(std::vector<std::string>::iterator i = args.begin();
-          i != args.end(); ++i)
-        {
-        if(r.find(i->c_str()))
-          {
-          this->LocalGenerator->AppendFlags
-            (flags, i->c_str());
-          }
-        }
-      }
-    else
-      {
-      this->LocalGenerator->AppendFlags
-        (flags, targetFlags.c_str());
-      }
-    }
+  this->LocalGenerator->AddCompileOptions(flags, this->Target,
+                                          language.c_str(), config);
 
     // Add source file specific flags.
     this->LocalGenerator->AppendFlags(flags,

+ 2 - 9
Source/cmVisualStudio10TargetGenerator.cxx

@@ -1276,17 +1276,10 @@ bool cmVisualStudio10TargetGenerator::ComputeClOptions(
       {
       flags += " /TP ";
       }
+    this->LocalGenerator->AddCompileOptions(flags, this->Target,
+                                            linkLanguage, configName.c_str());
     }
 
-  std::string targetFlags;
-  this->LocalGenerator->GetCompileOptions(targetFlags, this->Target,
-                                          configName.c_str());
-  // Add the target-specific flags.
-  if(!targetFlags.empty())
-    {
-    flags += " ";
-    flags += targetFlags;
-    }
   // Get preprocessor definitions for this directory.
   std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
   clOptions.FixExceptionHandlingDefault();