cmOrderLinkDirectories.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. const cmTargetManifest& manifest,
  49. const char* configSubdir);
  50. ///! Compute the best order for -L paths from GetLinkLibraries
  51. bool DetermineLibraryPathOrder();
  52. ///! Get the results from DetermineLibraryPathOrder
  53. void GetLinkerInformation(std::vector<cmStdString>& searchPaths,
  54. std::vector<cmStdString>& linkItems)
  55. {
  56. linkItems = this->LinkItems;
  57. searchPaths = this->SortedSearchPaths;
  58. }
  59. // should be set from CMAKE_STATIC_LIBRARY_SUFFIX,
  60. // CMAKE_SHARED_LIBRARY_SUFFIX
  61. // CMAKE_LINK_LIBRARY_SUFFIX
  62. void AddLinkExtension(const char* e)
  63. {
  64. if(e && *e)
  65. {
  66. this->LinkExtensions.push_back(e);
  67. }
  68. }
  69. // should be set from CMAKE_STATIC_LIBRARY_PREFIX
  70. void SetLinkPrefix(const char* s)
  71. {
  72. if(s)
  73. {
  74. this->LinkPrefix = s;
  75. }
  76. }
  77. // Return any warnings if the exist
  78. std::string GetWarnings();
  79. // return a list of all full path libraries
  80. void GetFullPathLibraries(std::vector<cmStdString>& libs);
  81. // structure to hold a full path library link item
  82. struct Library
  83. {
  84. cmStdString FullPath;
  85. cmStdString File;
  86. cmStdString Path;
  87. };
  88. friend struct cmOrderLinkDirectoriesCompare;
  89. void DebugOn()
  90. {
  91. this->Debug = true;
  92. }
  93. private:
  94. void CreateRegularExpressions();
  95. void DetermineLibraryPathOrder(std::vector<cmStdString>& searchPaths,
  96. std::vector<cmStdString>& libs,
  97. std::vector<cmStdString>& sortedPaths);
  98. void PrepareLinkTargets();
  99. bool LibraryInDirectory(const char* desiredLib,
  100. const char* dir, const char* lib);
  101. void FindLibrariesInSearchPaths();
  102. void FindIndividualLibraryOrders();
  103. void PrintMap(const char* name,
  104. std::map<cmStdString, std::vector<cmStdString> >& m);
  105. void PrintVector(const char* name,
  106. std::vector<std::pair<cmStdString,
  107. std::vector<cmStdString> > >& m);
  108. void OrderPaths(std::vector<cmStdString>& paths);
  109. bool FindPathNotInDirectoryToAfterList(cmStdString& path);
  110. std::string NoCaseExpression(const char* str);
  111. bool LibraryMayConflict(const char* desiredLib,
  112. const char* dir, const char* fname);
  113. private:
  114. // set of files that will exist when the build occurs
  115. std::set<cmStdString> ManifestFiles;
  116. // map from library to directories that it is in other than its full path
  117. std::map<cmStdString, std::vector<cmStdString> > LibraryToDirectories;
  118. // map from directory to vector of directories that must be after it
  119. std::vector<std::pair<cmStdString, std::vector<cmStdString> > >
  120. DirectoryToAfterList;
  121. std::set<cmStdString> DirectoryToAfterListEmitted;
  122. // map from full path to a Library struct
  123. std::map<cmStdString, Library> FullPathLibraries;
  124. // libraries that are found in multiple directories
  125. std::vector<Library> MultiDirectoryLibraries;
  126. // libraries that are only found in one directory
  127. std::vector<Library> SingleDirectoryLibraries;
  128. // This is a vector of all the link objects -lm or m
  129. std::vector<cmStdString> LinkItems;
  130. // Unprocessed link items
  131. std::vector<cmStdString> RawLinkItems;
  132. // This vector holds the sorted -L paths
  133. std::vector<cmStdString> SortedSearchPaths;
  134. // This vector holds the -F paths
  135. std::set<cmStdString> EmittedFrameworkPaths;
  136. // This is the set of -L paths unsorted, but unique
  137. std::set<cmStdString> LinkPathSet;
  138. // the names of link extensions
  139. std::vector<cmStdString> LinkExtensions;
  140. // the names of link prefixes
  141. cmStdString LinkPrefix;
  142. // set of directories that can not be put in the correct order
  143. std::set<cmStdString> ImpossibleDirectories;
  144. // Name of target
  145. cmStdString TargetName;
  146. // Subdirectory used for this configuration if any.
  147. cmStdString ConfigSubdir;
  148. // library regular expressions
  149. cmsys::RegularExpression RemoveLibraryExtension;
  150. cmsys::RegularExpression ExtractBaseLibraryName;
  151. cmsys::RegularExpression ExtractBaseLibraryNameNoPrefix;
  152. cmsys::RegularExpression SplitFramework;
  153. bool Debug;
  154. };
  155. #endif