Jelajahi Sumber

Merge topic 'CMP0065-LINK_FLAGS'

0f1fedbd CMP0065: Put computed flags into LINK_FLAGS not LINK_LIBRARIES
7f1cd328 cmLocalGenerator: Rename local variable to be more appropriate
2597bcf8 cmLocalGenerator: Extract policy handling into a method
Brad King 9 tahun lalu
induk
melakukan
89b03498ef

+ 59 - 46
Source/cmLocalGenerator.cxx

@@ -1248,6 +1248,14 @@ void cmLocalGenerator::GetTargetFlags(
         linkFlags += this->Makefile->GetSafeDefinition(exportFlagVar);
         linkFlags += " ";
       }
+
+      std::string cmp0065Flags =
+        this->GetLinkLibsCMP0065(linkLanguage, *target);
+      if (!cmp0065Flags.empty()) {
+        linkFlags += cmp0065Flags;
+        linkFlags += " ";
+      }
+
       const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
       if (targetLinkFlags) {
         linkFlags += targetLinkFlags;
@@ -1428,59 +1436,15 @@ void cmLocalGenerator::OutputLinkLibraries(std::string& linkLibraries,
   }
   cmComputeLinkInformation& cli = *pcli;
 
-  // Collect library linking flags command line options.
-  std::string linkLibs;
-
   std::string linkLanguage = cli.GetLinkLanguage();
 
+  std::string linkLibs;
+
   std::string libPathFlag =
     this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
   std::string libPathTerminator =
     this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
 
-  // Flags to link an executable to shared libraries.
-  if (tgt.GetType() == cmState::EXECUTABLE &&
-      this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
-        "TARGET_SUPPORTS_SHARED_LIBS")) {
-    bool add_shlib_flags = false;
-    switch (tgt.GetPolicyStatusCMP0065()) {
-      case cmPolicies::WARN:
-        if (!tgt.GetPropertyAsBool("ENABLE_EXPORTS") &&
-            this->Makefile->PolicyOptionalWarningEnabled(
-              "CMAKE_POLICY_WARNING_CMP0065")) {
-          std::ostringstream w;
-          /* clang-format off */
-          w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n"
-            "For compatibility with older versions of CMake, "
-            "additional flags may be added to export symbols on all "
-            "executables regardless of their ENABLE_EXPORTS property.";
-          /* clang-format on */
-          this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
-        }
-      case cmPolicies::OLD:
-        // OLD behavior is to always add the flags
-        add_shlib_flags = true;
-        break;
-      case cmPolicies::REQUIRED_IF_USED:
-      case cmPolicies::REQUIRED_ALWAYS:
-        this->IssueMessage(
-          cmake::FATAL_ERROR,
-          cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0065));
-      case cmPolicies::NEW:
-        // NEW behavior is to only add the flags if ENABLE_EXPORTS is on
-        add_shlib_flags = tgt.GetPropertyAsBool("ENABLE_EXPORTS");
-        break;
-    }
-
-    if (add_shlib_flags) {
-      std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
-      linkFlagsVar += linkLanguage;
-      linkFlagsVar += "_FLAGS";
-      linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar);
-      linkLibs += " ";
-    }
-  }
-
   // Append the framework search path flags.
   std::string fwSearchFlagVar = "CMAKE_";
   fwSearchFlagVar += linkLanguage;
@@ -1572,6 +1536,55 @@ void cmLocalGenerator::OutputLinkLibraries(std::string& linkLibraries,
   linkLibraries = fout.str();
 }
 
+std::string cmLocalGenerator::GetLinkLibsCMP0065(
+  std::string const& linkLanguage, cmGeneratorTarget& tgt) const
+{
+  std::string linkFlags;
+
+  // Flags to link an executable to shared libraries.
+  if (tgt.GetType() == cmState::EXECUTABLE &&
+      this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
+        "TARGET_SUPPORTS_SHARED_LIBS")) {
+    bool add_shlib_flags = false;
+    switch (tgt.GetPolicyStatusCMP0065()) {
+      case cmPolicies::WARN:
+        if (!tgt.GetPropertyAsBool("ENABLE_EXPORTS") &&
+            this->Makefile->PolicyOptionalWarningEnabled(
+              "CMAKE_POLICY_WARNING_CMP0065")) {
+          std::ostringstream w;
+          /* clang-format off */
+          w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n"
+            "For compatibility with older versions of CMake, "
+            "additional flags may be added to export symbols on all "
+            "executables regardless of their ENABLE_EXPORTS property.";
+          /* clang-format on */
+          this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
+        }
+      case cmPolicies::OLD:
+        // OLD behavior is to always add the flags
+        add_shlib_flags = true;
+        break;
+      case cmPolicies::REQUIRED_IF_USED:
+      case cmPolicies::REQUIRED_ALWAYS:
+        this->IssueMessage(
+          cmake::FATAL_ERROR,
+          cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0065));
+      case cmPolicies::NEW:
+        // NEW behavior is to only add the flags if ENABLE_EXPORTS is on
+        add_shlib_flags = tgt.GetPropertyAsBool("ENABLE_EXPORTS");
+        break;
+    }
+
+    if (add_shlib_flags) {
+      std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
+      linkFlagsVar += linkLanguage;
+      linkFlagsVar += "_FLAGS";
+      linkFlags = this->Makefile->GetSafeDefinition(linkFlagsVar);
+    }
+  }
+  return linkFlags;
+}
+
 void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
                                             cmGeneratorTarget const* target,
                                             const std::string& lang,

+ 3 - 0
Source/cmLocalGenerator.h

@@ -82,6 +82,9 @@ public:
     return this->GlobalGenerator;
   }
 
+  std::string GetLinkLibsCMP0065(std::string const& linkLanguage,
+                                 cmGeneratorTarget& tgt) const;
+
   cmState* GetState() const;
   cmState::Snapshot GetStateSnapshot() const;
 

+ 5 - 0
Source/cmMakefileExecutableTargetGenerator.cxx

@@ -192,6 +192,11 @@ void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
     this->LocalGenerator->AppendFlags(
       linkFlags, this->Makefile->GetDefinition(export_flag_var));
   }
+
+  this->LocalGenerator->AppendFlags(linkFlags,
+                                    this->LocalGenerator->GetLinkLibsCMP0065(
+                                      linkLanguage, *this->GeneratorTarget));
+
   if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE")) {
     this->LocalGenerator->AppendFlags(linkFlags, " -Wl,--no-as-needed");
   }