cmOrderLinkDirectories.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmOrderLinkDirectories_h
  14. #define cmOrderLinkDirectories_h
  15. #include <cmStandardIncludes.h>
  16. #include <map>
  17. #include <vector>
  18. #include "cmTarget.h"
  19. #include "cmsys/RegularExpression.hxx"
  20. /** \class cmOrderLinkDirectories
  21. * \brief Compute the best -L path order
  22. *
  23. * This class computes the best order for -L paths.
  24. * It tries to make sure full path specified libraries are
  25. * used. For example if you have /usr/mylib/libfoo.a on as
  26. * a link library for a target, and you also have /usr/lib/libbar.a
  27. * and you also have /usr/lib/libfoo.a, then you would
  28. * want -L/usr/mylib -L/usr/lib to make sure the correct libfoo.a is
  29. * found by the linker. The algorithm is as follows:
  30. * - foreach library create a vector of directories it exists in.
  31. * - foreach directory create a vector of directories that must come
  32. * after it, put this in a map<dir, vector<dir>> mapping from a directory
  33. * to the vector of directories that it must be before.
  34. * - put all directories into a vector
  35. * - sort the vector with a compare function CanBeBefore
  36. * CanBeBefore returns true if a directory is OK to be before
  37. * another directory. This is determined by looking at the
  38. * map<dir vector<dir>> and seeing if d1 is in the vector for d2.
  39. */
  40. class cmOrderLinkDirectories
  41. {
  42. public:
  43. cmOrderLinkDirectories();
  44. ///! set link information from the target
  45. void SetLinkInformation(const char* targetName,
  46. const std::vector<std::string>& linkLibraries,
  47. const std::vector<std::string>& linkDirectories);
  48. ///! Compute the best order for -L paths from GetLinkLibraries
  49. bool DetermineLibraryPathOrder();
  50. ///! Get the results from DetermineLibraryPathOrder
  51. void GetLinkerInformation(std::vector<cmStdString>& searchPaths,
  52. std::vector<cmStdString>& linkItems)
  53. {
  54. linkItems = this->LinkItems;
  55. searchPaths = this->SortedSearchPaths;
  56. }
  57. // should be set from CMAKE_STATIC_LIBRARY_SUFFIX,
  58. // CMAKE_SHARED_LIBRARY_SUFFIX
  59. // CMAKE_LINK_LIBRARY_SUFFIX
  60. void AddLinkExtension(const char* e)
  61. {
  62. if(e && *e)
  63. {
  64. this->LinkExtensions.push_back(e);
  65. }
  66. }
  67. // should be set from CMAKE_STATIC_LIBRARY_PREFIX
  68. void SetLinkPrefix(const char* s)
  69. {
  70. if(s)
  71. {
  72. this->LinkPrefix = s;
  73. }
  74. }
  75. // Return any warnings if the exist
  76. std::string GetWarnings();
  77. // return a list of all full path libraries
  78. void GetFullPathLibraries(std::vector<cmStdString>& libs);
  79. // structure to hold a full path library link item
  80. struct Library
  81. {
  82. cmStdString FullPath;
  83. cmStdString File;
  84. cmStdString Path;
  85. };
  86. friend struct cmOrderLinkDirectoriesCompare;
  87. void DebugOn()
  88. {
  89. this->Debug = true;
  90. }
  91. private:
  92. void CreateRegularExpressions();
  93. void DetermineLibraryPathOrder(std::vector<cmStdString>& searchPaths,
  94. std::vector<cmStdString>& libs,
  95. std::vector<cmStdString>& sortedPaths);
  96. void PrepareLinkTargets();
  97. bool LibraryInDirectory(const char* dir, const char* lib);
  98. void FindLibrariesInSearchPaths();
  99. void FindIndividualLibraryOrders();
  100. void PrintMap(const char* name,
  101. std::map<cmStdString, std::vector<cmStdString> >& m);
  102. void PrintVector(const char* name,
  103. std::vector<std::pair<cmStdString,
  104. std::vector<cmStdString> > >& m);
  105. void OrderPaths(std::vector<cmStdString>& paths);
  106. bool FindPathNotInDirectoryToAfterList(cmStdString& path);
  107. std::string NoCaseExpression(const char* str);
  108. private:
  109. // map from library to directories that it is in other than its full path
  110. std::map<cmStdString, std::vector<cmStdString> > LibraryToDirectories;
  111. // map from directory to vector of directories that must be after it
  112. std::vector<std::pair<cmStdString, std::vector<cmStdString> > > DirectoryToAfterList;
  113. std::set<cmStdString> DirectoryToAfterListEmitted;
  114. // map from full path to a Library struct
  115. std::map<cmStdString, Library> FullPathLibraries;
  116. // libraries that are found in multiple directories
  117. std::vector<Library> MultiDirectoryLibraries;
  118. // libraries that are only found in one directory
  119. std::vector<Library> SingleDirectoryLibraries;
  120. // This is a vector of all the link objects -lm or m
  121. std::vector<cmStdString> LinkItems;
  122. // Unprocessed link items
  123. std::vector<cmStdString> RawLinkItems;
  124. // This vector holds the sorted -L paths
  125. std::vector<cmStdString> SortedSearchPaths;
  126. // This vector holds the -F paths
  127. std::set<cmStdString> EmittedFrameworkPaths;
  128. // This is the set of -L paths unsorted, but unique
  129. std::set<cmStdString> LinkPathSet;
  130. // the names of link extensions
  131. std::vector<cmStdString> LinkExtensions;
  132. // the names of link prefixes
  133. cmStdString LinkPrefix;
  134. // set of directories that can not be put in the correct order
  135. std::set<cmStdString> ImpossibleDirectories;
  136. // Name of target
  137. cmStdString TargetName;
  138. // library regular expressions
  139. cmsys::RegularExpression RemoveLibraryExtension;
  140. cmsys::RegularExpression ExtractBaseLibraryName;
  141. cmsys::RegularExpression ExtractBaseLibraryNameNoPrefix;
  142. cmsys::RegularExpression SplitFramework;
  143. bool Debug;
  144. };
  145. #endif