cmOrderLinkDirectories.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. ///! set link information from the target
  44. void SetLinkInformation(const cmTarget&, cmTarget::LinkLibraryType,
  45. const char* targetLibrary);
  46. ///! Compute the best order for -L paths from GetLinkLibraries
  47. bool DetermineLibraryPathOrder();
  48. ///! Get the results from DetermineLibraryPathOrder
  49. void GetLinkerInformation(std::vector<cmStdString>& searchPaths,
  50. std::vector<cmStdString>& linkItems)
  51. {
  52. linkItems = m_LinkItems;
  53. searchPaths = m_SortedSearchPaths;
  54. }
  55. // should be set from CMAKE_STATIC_LIBRARY_SUFFIX,
  56. // CMAKE_SHARED_LIBRARY_SUFFIX
  57. // CMAKE_LINK_LIBRARY_SUFFIX
  58. void AddLinkExtension(const char* e)
  59. {
  60. m_LinkExtensions.push_back(e);
  61. }
  62. // Return any warnings if the exist
  63. std::string GetWarnings();
  64. // return a list of all full path libraries
  65. void GetFullPathLibraries(std::vector<cmStdString>& libs);
  66. // structure to hold a full path library link item
  67. struct Library
  68. {
  69. cmStdString FullPath;
  70. cmStdString File;
  71. cmStdString Path;
  72. };
  73. friend struct cmOrderLinkDirectoriesCompare;
  74. private:
  75. void CreateRegularExpressions();
  76. void DetermineLibraryPathOrder(std::vector<cmStdString>& searchPaths,
  77. std::vector<cmStdString>& libs,
  78. std::vector<cmStdString>& sortedPaths);
  79. void PrepareLinkTargets();
  80. bool LibraryInDirectory(const char* dir, const char* lib);
  81. void FindLibrariesInSeachPaths();
  82. void FindIndividualLibraryOrders();
  83. void PrintMap(const char* name,
  84. std::map<cmStdString, std::vector<cmStdString> >& m);
  85. void OrderPaths(std::vector<cmStdString>& paths);
  86. bool CanBeBefore(const cmStdString& d1,
  87. const cmStdString& d2);
  88. void AddImpossible(const cmStdString& ,
  89. const cmStdString& );
  90. private:
  91. // map from library to directories that it is in other than its full path
  92. std::map<cmStdString, std::vector<cmStdString> > m_LibraryToDirectories;
  93. // map from directory to vector of directories that must be after it
  94. std::map<cmStdString, std::vector<cmStdString> > m_DirectoryToAfterList;
  95. // map from full path to a Library struct
  96. std::map<cmStdString, Library> m_FullPathLibraries;
  97. // libraries that are found in multiple directories
  98. std::vector<Library> m_MultiDirectoryLibraries;
  99. // libraries that are only found in one directory
  100. std::vector<Library> m_SingleDirectoryLibraries;
  101. // This is a vector of all the link objects -lm or m
  102. std::vector<cmStdString> m_LinkItems;
  103. // Unprocessed link items
  104. std::vector<cmStdString> m_RawLinkItems;
  105. // This vector holds the sorted -L paths
  106. std::vector<cmStdString> m_SortedSearchPaths;
  107. // This is the set of -L paths unsorted, but unique
  108. std::set<cmStdString> m_LinkPathSet;
  109. // the names of link extensions
  110. std::vector<cmStdString> m_LinkExtensions;
  111. // set of directories that can not be put in the correct order
  112. std::set<cmStdString> m_ImposibleDirectories;
  113. // library regular expressions
  114. cmsys::RegularExpression m_RemoveLibraryExtension;
  115. cmsys::RegularExpression m_ExtractBaseLibraryName;
  116. cmsys::RegularExpression m_ExtractBaseLibraryNameNoPrefix;
  117. };
  118. #endif