[cmake-commits] king committed cmLocalGenerator.cxx 1.145 1.146 cmOrderLinkDirectories.cxx 1.30 1.31 cmOrderLinkDirectories.h 1.18 1.19

cmake-commits at cmake.org cmake-commits at cmake.org
Fri Sep 15 14:09:12 EDT 2006


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv802/Source

Modified Files:
	cmLocalGenerator.cxx cmOrderLinkDirectories.cxx 
	cmOrderLinkDirectories.h 
Log Message:
ENH: Adding support to link specifically to an archive or a shared library based on the file name specified.  This fixes the problem of having -lfoo linking to libfoo.so even when it came from libfoo.a being specified.


Index: cmLocalGenerator.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmLocalGenerator.cxx,v
retrieving revision 1.145
retrieving revision 1.146
diff -u -d -r1.145 -r1.146
--- cmLocalGenerator.cxx	29 Aug 2006 14:03:47 -0000	1.145
+++ cmLocalGenerator.cxx	15 Sep 2006 18:09:09 -0000	1.146
@@ -1639,14 +1639,60 @@
   const std::vector<std::string>&
     linkDirectories = target.GetLinkDirectories();
 
+  // Get the language used for linking.
+  const char* linkLanguage =
+    target.GetLinkerLanguage(this->GetGlobalGenerator());
+  if(!linkLanguage)
+    {
+    cmSystemTools::
+      Error("CMake can not determine linker language for target:",
+            target.GetName());
+    return;
+    }
+
+  // Lookup link type selection flags.
+  const char* static_link_type_flag = 0;
+  const char* shared_link_type_flag = 0;
+  const char* target_type_str = 0;
+  switch(target.GetType())
+    {
+    case cmTarget::EXECUTABLE:     target_type_str = "EXE"; break;
+    case cmTarget::SHARED_LIBRARY: target_type_str = "SHARED_LIBRARY"; break;
+    case cmTarget::MODULE_LIBRARY: target_type_str = "SHARED_MODULE"; break;
+    default: break;
+    }
+  if(target_type_str)
+    {
+    std::string static_link_type_flag_var = "CMAKE_";
+    static_link_type_flag_var += target_type_str;
+    static_link_type_flag_var += "_LINK_STATIC_";
+    static_link_type_flag_var += linkLanguage;
+    static_link_type_flag_var += "_FLAGS";
+    static_link_type_flag =
+      this->Makefile->GetDefinition(static_link_type_flag_var.c_str());
+
+    std::string shared_link_type_flag_var = "CMAKE_";
+    shared_link_type_flag_var += target_type_str;
+    shared_link_type_flag_var += "_LINK_DYNAMIC_";
+    shared_link_type_flag_var += linkLanguage;
+    shared_link_type_flag_var += "_FLAGS";
+    shared_link_type_flag =
+      this->Makefile->GetDefinition(shared_link_type_flag_var.c_str());
+    }
+
   // Compute the link directory order needed to link the libraries.
   cmOrderLinkDirectories orderLibs;
+  orderLibs.SetLinkTypeInformation(cmOrderLinkDirectories::LinkShared,
+                                   static_link_type_flag,
+                                   shared_link_type_flag);
   orderLibs.SetLinkPrefix(
     this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
   orderLibs.AddLinkExtension(
-    this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"));
+    this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
+    cmOrderLinkDirectories::LinkStatic);
   orderLibs.AddLinkExtension(
-    this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"));
+    this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
+    cmOrderLinkDirectories::LinkShared);
   orderLibs.AddLinkExtension(
     this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"));
   orderLibs.AddLinkExtension(

Index: cmOrderLinkDirectories.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmOrderLinkDirectories.cxx,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- cmOrderLinkDirectories.cxx	12 May 2006 17:39:34 -0000	1.30
+++ cmOrderLinkDirectories.cxx	15 Sep 2006 18:09:10 -0000	1.31
@@ -3,14 +3,63 @@
 #include "cmsys/RegularExpression.hxx"
 #include <ctype.h>
 
+//#define CM_ORDER_LINK_DIRECTORIES_DEBUG
 
 //-------------------------------------------------------------------
 cmOrderLinkDirectories::cmOrderLinkDirectories()
 {
+  this->StartLinkType = LinkUnknown;
+  this->LinkTypeEnabled = false;
   this->Debug = false;
 }
 
 //-------------------------------------------------------------------
+void
+cmOrderLinkDirectories
+::SetLinkTypeInformation(LinkType start_link_type,
+                         const char* static_link_type_flag,
+                         const char* shared_link_type_flag)
+{
+  // We can support link type switching only if all needed flags are
+  // known.
+  this->StartLinkType = start_link_type;
+  if(static_link_type_flag && *static_link_type_flag &&
+     shared_link_type_flag && *shared_link_type_flag)
+    {
+    this->LinkTypeEnabled = true;
+    this->StaticLinkTypeFlag = static_link_type_flag;
+    this->SharedLinkTypeFlag = shared_link_type_flag;
+    }
+  else
+    {
+    this->LinkTypeEnabled = false;
+    this->StaticLinkTypeFlag = "";
+    this->SharedLinkTypeFlag = "";
+    }
+}
+
+//-------------------------------------------------------------------
+void cmOrderLinkDirectories::SetCurrentLinkType(LinkType lt)
+{
+  if(this->CurrentLinkType != lt)
+    {
+    this->CurrentLinkType = lt;
+
+    if(this->LinkTypeEnabled)
+      {
+      switch(this->CurrentLinkType)
+        {
+        case LinkStatic:
+          this->LinkItems.push_back(this->StaticLinkTypeFlag); break;
+        case LinkShared:
+          this->LinkItems.push_back(this->SharedLinkTypeFlag); break;
+        default: break;
+        }
+      }
+    }
+}
+
+//-------------------------------------------------------------------
 bool cmOrderLinkDirectories::LibraryInDirectory(const char* desiredLib,
                                                 const char* dir,
                                                 const char* libIn)
@@ -126,16 +175,75 @@
 void cmOrderLinkDirectories::CreateRegularExpressions()
 {
   this->SplitFramework.compile("(.*)/(.*)\\.framework$");
+
+  // Compute a regex to match link extensions.
+  cmStdString libext = this->CreateExtensionRegex(this->LinkExtensions);
+
+  // Create regex to remove any library extension.
+  cmStdString reg("(.*)");
+  reg += libext;
+  this->RemoveLibraryExtension.compile(reg.c_str());
+
+  // Create a regex to match a library name.  Match index 1 will be
+  // the prefix if it exists and empty otherwise.  Match index 2 will
+  // be the library name.  Match index 3 will be the library
+  // extension.
+  reg = "^(";
+  if(!this->LinkPrefix.empty())
+    {
+    reg += this->LinkPrefix;
+    reg += "|";
+    }
+  reg += ")";
+  reg += "([^/]*)";
+
+  // Create a regex to match any library name.
+  cmStdString reg_any = reg;
+  reg_any += libext;
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+  fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
+#endif
+  this->ExtractAnyLibraryName.compile(reg_any.c_str());
+
+  // Create a regex to match static library names.
+  if(!this->StaticLinkExtensions.empty())
+    {
+    cmStdString reg_static = reg;
+    reg_static += this->CreateExtensionRegex(this->StaticLinkExtensions);
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+  fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
+#endif
+    this->ExtractStaticLibraryName.compile(reg_static.c_str());
+    }
+
+  // Create a regex to match shared library names.
+  if(!this->SharedLinkExtensions.empty())
+    {
+    cmStdString reg_shared = reg;
+    reg_shared += this->CreateExtensionRegex(this->SharedLinkExtensions);
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+  fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
+#endif
+    this->ExtractSharedLibraryName.compile(reg_shared.c_str());
+    }
+}
+
+//-------------------------------------------------------------------
+std::string
+cmOrderLinkDirectories::CreateExtensionRegex(
+  std::vector<cmStdString> const& exts)
+{
+  // Build a list of extension choices.
   cmStdString libext = "(";
-  bool first = true;
-  for(std::vector<cmStdString>::iterator i = this->LinkExtensions.begin();
-      i != this->LinkExtensions.end(); ++i)
+  const char* sep = "";
+  for(std::vector<cmStdString>::const_iterator i = exts.begin();
+      i != exts.end(); ++i)
     {
-    if(!first)
-      {
-      libext += "|";
-      }
-    first = false;
+    // Separate this choice from the previous one.
+    libext += sep;
+    sep = "|";
+
+    // Store this extension choice with the "." escaped.
     libext += "\\";
 #if defined(_WIN32) && !defined(__CYGWIN__)
     libext += this->NoCaseExpression(i->c_str());
@@ -143,41 +251,65 @@
     libext += *i;
 #endif
     }
+
+  // Finish the list.
   libext += ").*";
-  cmStdString reg("(.*)");
-  reg += libext;
-  this->RemoveLibraryExtension.compile(reg.c_str());
-  reg = "";
-  if(this->LinkPrefix.size())
-    {
-    reg = "^";
-    reg += this->LinkPrefix;
-    }
-  reg += "([^/]*)";
-  reg += libext;
-  this->ExtractBaseLibraryName.compile(reg.c_str());
-  reg = "([^/]*)";
-  reg += libext;
-  this->ExtractBaseLibraryNameNoPrefix.compile(reg.c_str());
+  return libext;
 }
 
-
 //-------------------------------------------------------------------
 void cmOrderLinkDirectories::PrepareLinkTargets()
 {
-  for(std::vector<cmStdString>::iterator i = this->LinkItems.begin();
-      i != this->LinkItems.end(); ++i)
+  std::vector<cmStdString> originalLinkItems = this->LinkItems;
+  this->LinkItems.clear();
+  this->CurrentLinkType = this->StartLinkType;
+  for(std::vector<cmStdString>::iterator i = originalLinkItems.begin();
+      i != originalLinkItems.end(); ++i)
     {
     // separate the library name from libfoo.a or foo.a
-    if(this->ExtractBaseLibraryName.find(*i))
+    if(this->ExtractStaticLibraryName.find(*i))
       {
-      *i = this->ExtractBaseLibraryName.match(1);
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+      fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
+              this->ExtractStaticLibraryName.match(1).c_str(),
+              this->ExtractStaticLibraryName.match(2).c_str(),
+              this->ExtractStaticLibraryName.match(3).c_str());
+#endif
+      this->SetCurrentLinkType(LinkStatic);
+      this->LinkItems.push_back(this->ExtractStaticLibraryName.match(2));
       }
-    else if(this->ExtractBaseLibraryNameNoPrefix.find(*i))
+    else if(this->ExtractSharedLibraryName.find(*i))
       {
-      *i = this->ExtractBaseLibraryNameNoPrefix.match(1);
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+      fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
+              this->ExtractSharedLibraryName.match(1).c_str(),
+              this->ExtractSharedLibraryName.match(2).c_str(),
+              this->ExtractSharedLibraryName.match(3).c_str());
+#endif
+      this->SetCurrentLinkType(LinkShared);
+      this->LinkItems.push_back(this->ExtractSharedLibraryName.match(2));
+      }
+    else if(this->ExtractAnyLibraryName.find(*i))
+      {
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+      fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
+              this->ExtractAnyLibraryName.match(1).c_str(),
+              this->ExtractAnyLibraryName.match(2).c_str(),
+              this->ExtractAnyLibraryName.match(3).c_str());
+#endif
+      this->SetCurrentLinkType(this->StartLinkType);
+      this->LinkItems.push_back(this->ExtractAnyLibraryName.match(2));
+      }
+    else
+      {
+      this->SetCurrentLinkType(this->StartLinkType);
+      this->LinkItems.push_back(*i);
       }
     }
+
+  // Restore the original linking type so system runtime libraries are
+  // linked properly.
+  this->SetCurrentLinkType(this->StartLinkType);
 }
 
 //-------------------------------------------------------------------
@@ -318,6 +450,9 @@
   for(unsigned int i=0; i < this->RawLinkItems.size(); ++i)
     {
     bool framework = false;
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+    fprintf(stderr, "Raw link item [%s]\n", this->RawLinkItems[i].c_str());
+#endif
     if(cmSystemTools::FileIsFullPath(this->RawLinkItems[i].c_str()))
       {
       if(cmSystemTools::FileIsDirectory(this->RawLinkItems[i].c_str()))
@@ -378,6 +513,9 @@
         aLib.File = file;
         aLib.Path = dir;
         this->FullPathLibraries[aLib.FullPath] = aLib;
+#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
+        fprintf(stderr, "Storing item [%s]\n", file.c_str());
+#endif
         this->LinkItems.push_back(file);
         }
       }

Index: cmOrderLinkDirectories.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmOrderLinkDirectories.h,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- cmOrderLinkDirectories.h	12 May 2006 17:39:34 -0000	1.18
+++ cmOrderLinkDirectories.h	15 Sep 2006 18:09:10 -0000	1.19
@@ -66,10 +66,19 @@
   // should be set from CMAKE_STATIC_LIBRARY_SUFFIX,
   // CMAKE_SHARED_LIBRARY_SUFFIX
   // CMAKE_LINK_LIBRARY_SUFFIX
-  void AddLinkExtension(const char* e)
+  enum LinkType { LinkUnknown, LinkStatic, LinkShared };
+  void AddLinkExtension(const char* e, LinkType type = LinkUnknown)
     {
     if(e && *e)
       {
+      if(type == LinkStatic)
+        {
+        this->StaticLinkExtensions.push_back(e);
+        }
+      if(type == LinkShared)
+        {
+        this->SharedLinkExtensions.push_back(e);
+        }
       this->LinkExtensions.push_back(e);
       }
     }
@@ -86,6 +95,11 @@
   // return a list of all full path libraries
   void GetFullPathLibraries(std::vector<cmStdString>& libs);
 
+  // Provide flags for switching library link type.
+  void SetLinkTypeInformation(LinkType start_link_type,
+                              const char* static_link_type_flag,
+                              const char* shared_link_type_flag);
+
   // structure to hold a full path library link item
   struct Library
   {
@@ -101,6 +115,7 @@
   
 private:
   void CreateRegularExpressions();
+  std::string CreateExtensionRegex(std::vector<cmStdString> const& exts);
   void DetermineLibraryPathOrder(std::vector<cmStdString>& searchPaths,
                                  std::vector<cmStdString>& libs,
                                  std::vector<cmStdString>& sortedPaths);
@@ -145,6 +160,8 @@
   // This is the set of -L paths unsorted, but unique
   std::set<cmStdString> LinkPathSet;
   // the names of link extensions
+  std::vector<cmStdString> StaticLinkExtensions;
+  std::vector<cmStdString> SharedLinkExtensions;
   std::vector<cmStdString> LinkExtensions;
   // the names of link prefixes
   cmStdString LinkPrefix;
@@ -154,10 +171,20 @@
   cmStdString TargetName;
   // Subdirectory used for this configuration if any.
   cmStdString ConfigSubdir;
+
+  // Link type adjustment.
+  LinkType StartLinkType;
+  LinkType CurrentLinkType;
+  cmStdString StaticLinkTypeFlag;
+  cmStdString SharedLinkTypeFlag;
+  bool LinkTypeEnabled;
+  void SetCurrentLinkType(LinkType lt);
+
   // library regular expressions
   cmsys::RegularExpression RemoveLibraryExtension;
-  cmsys::RegularExpression ExtractBaseLibraryName;
-  cmsys::RegularExpression ExtractBaseLibraryNameNoPrefix;
+  cmsys::RegularExpression ExtractStaticLibraryName;
+  cmsys::RegularExpression ExtractSharedLibraryName;
+  cmsys::RegularExpression ExtractAnyLibraryName;
   cmsys::RegularExpression SplitFramework;
   bool Debug;
 };



More information about the Cmake-commits mailing list