cmOrderLinkDirectories.h 5.1 KB

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