cmOrderLinkDirectories.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "cmOrderLinkDirectories.h"
  2. #include "cmSystemTools.h"
  3. #include "cmsys/RegularExpression.hxx"
  4. //-------------------------------------------------------------------
  5. bool cmOrderLinkDirectories::LibraryInDirectory(const char* dir,
  6. const char* lib)
  7. {
  8. cmStdString path = dir;
  9. path += "/";
  10. path += lib;
  11. // first look for the library as given
  12. if(cmSystemTools::FileExists(path.c_str()))
  13. {
  14. return true;
  15. }
  16. // next remove the extension (.a, .so ) and look for the library
  17. // under a different name as the linker can do either
  18. if(m_RemoveLibraryExtension.find(lib))
  19. {
  20. cmStdString lib = m_RemoveLibraryExtension.match(1);
  21. cmStdString ext = m_RemoveLibraryExtension.match(2);
  22. for(std::vector<cmStdString>::iterator i = m_LinkExtensions.begin();
  23. i != m_LinkExtensions.end(); ++i)
  24. {
  25. if(ext != *i)
  26. {
  27. path = dir;
  28. path += "/";
  29. path += lib + *i;
  30. if(cmSystemTools::FileExists(path.c_str()))
  31. {
  32. return true;
  33. }
  34. }
  35. }
  36. }
  37. return false;
  38. }
  39. //-------------------------------------------------------------------
  40. void cmOrderLinkDirectories::FindLibrariesInSeachPaths()
  41. {
  42. for(std::set<cmStdString>::iterator dir = m_LinkPathSet.begin();
  43. dir != m_LinkPathSet.end(); ++dir)
  44. {
  45. for(std::map<cmStdString, Library>::iterator lib
  46. = m_FullPathLibraries.begin();
  47. lib != m_FullPathLibraries.end(); ++lib)
  48. {
  49. if(lib->second.Path != *dir)
  50. {
  51. if(LibraryInDirectory(dir->c_str(), lib->second.File.c_str()))
  52. {
  53. m_LibraryToDirectories[lib->second.FullPath].push_back(*dir);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. //-------------------------------------------------------------------
  60. void cmOrderLinkDirectories::FindIndividualLibraryOrders()
  61. {
  62. for(std::vector<Library>::iterator lib = m_MultiDirectoryLibraries.begin();
  63. lib != m_MultiDirectoryLibraries.end(); ++lib)
  64. {
  65. std::vector<cmStdString>& dirs = m_LibraryToDirectories[lib->FullPath];
  66. m_DirectoryToAfterList[lib->Path] = dirs;
  67. }
  68. }
  69. //-------------------------------------------------------------------
  70. void cmOrderLinkDirectories::CreateRegularExpressions()
  71. {
  72. cmStdString libext = "(";
  73. bool first = true;
  74. for(std::vector<cmStdString>::iterator i = m_LinkExtensions.begin();
  75. i != m_LinkExtensions.end(); ++i)
  76. {
  77. if(!first)
  78. {
  79. libext += "|";
  80. }
  81. first = false;
  82. libext += "\\";
  83. libext += *i;
  84. }
  85. libext += ").*";
  86. cmStdString reg("(.*)");
  87. reg += libext;
  88. m_RemoveLibraryExtension.compile(reg.c_str());
  89. reg = "^lib([^/]*)";
  90. reg += libext;
  91. m_ExtractBaseLibraryName.compile(reg.c_str());
  92. reg = "([^/]*)";
  93. reg += libext;
  94. m_ExtractBaseLibraryNameNoPrefix.compile(reg.c_str());
  95. }
  96. //-------------------------------------------------------------------
  97. void cmOrderLinkDirectories::PrepareLinkTargets()
  98. {
  99. for(std::vector<cmStdString>::iterator i = m_LinkItems.begin();
  100. i != m_LinkItems.end(); ++i)
  101. {
  102. // separate the library name from libfoo.a or foo.a
  103. if(m_ExtractBaseLibraryName.find(*i))
  104. {
  105. *i = m_ExtractBaseLibraryName.match(1);
  106. }
  107. else if(m_ExtractBaseLibraryNameNoPrefix.find(*i))
  108. {
  109. *i = m_ExtractBaseLibraryNameNoPrefix.match(1);
  110. }
  111. }
  112. }
  113. //-------------------------------------------------------------------
  114. bool cmOrderLinkDirectories::CanBeBefore(const cmStdString& d1,
  115. const cmStdString& d2)
  116. {
  117. if(m_DirectoryToAfterList.count(d2) == 0)
  118. {
  119. return true;
  120. }
  121. std::vector<cmStdString>& d2dirs = m_DirectoryToAfterList[d2];
  122. // is d1 in the d2's list of directories that d2 must be before
  123. // if so, then d1 can not come before d2
  124. for(std::vector<cmStdString>::iterator i = d2dirs.begin();
  125. i != d2dirs.end(); ++i)
  126. {
  127. if(*i == d1)
  128. {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. // This is a stl function object used to sort
  135. // the vector of library paths. It returns true
  136. // if left directory can be before right directory (no swap).
  137. // It also checks for the impossible case of two libraries and
  138. // two directories that have both libraries.
  139. struct cmOrderLinkDirectoriesCompare
  140. : public std::binary_function <cmStdString, cmStdString, bool>
  141. {
  142. cmOrderLinkDirectoriesCompare()
  143. {
  144. This = 0;
  145. }
  146. bool operator()(
  147. const cmStdString& left,
  148. const cmStdString& right
  149. ) const
  150. {
  151. bool ret = This->CanBeBefore(left, right);
  152. if(!ret)
  153. {
  154. // check for the case when both libraries have to come
  155. // before each other
  156. if(!This->CanBeBefore(right, left))
  157. {
  158. This->AddImpossible(right, left);
  159. }
  160. }
  161. return ret;
  162. }
  163. cmOrderLinkDirectories* This;
  164. };
  165. //-------------------------------------------------------------------
  166. void cmOrderLinkDirectories::AddImpossible(const cmStdString& d1,
  167. const cmStdString& d2)
  168. {
  169. m_ImposibleDirectories.insert(d1);
  170. m_ImposibleDirectories.insert(d2);
  171. }
  172. //-------------------------------------------------------------------
  173. void cmOrderLinkDirectories::OrderPaths(std::vector<cmStdString>&
  174. orderedPaths)
  175. {
  176. cmOrderLinkDirectoriesCompare comp;
  177. comp.This = this;
  178. std::sort(orderedPaths.begin(), orderedPaths.end(), comp);
  179. }
  180. //-------------------------------------------------------------------
  181. void cmOrderLinkDirectories::SetLinkInformation(const cmTarget& target,
  182. cmTarget::LinkLibraryType
  183. linktype,
  184. const char* targetLibrary)
  185. {
  186. // collect the search paths from the target into paths set
  187. const std::vector<std::string>& searchPaths = target.GetLinkDirectories();
  188. for(std::vector<std::string>::const_iterator p = searchPaths.begin();
  189. p != searchPaths.end(); ++p)
  190. {
  191. m_LinkPathSet.insert(*p);
  192. }
  193. // collect the link items from the target and put it into libs
  194. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  195. std::vector<cmStdString> libs;
  196. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  197. lib != tlibs.end(); ++lib)
  198. {
  199. // skip zero size library entries, this may happen
  200. // if a variable expands to nothing.
  201. if (lib->first.size() == 0)
  202. {
  203. continue;
  204. }
  205. // Don't link the library against itself!
  206. if(targetLibrary && (lib->first == targetLibrary))
  207. {
  208. continue;
  209. }
  210. // use the correct lib for the current configuration
  211. if (lib->second == cmTarget::DEBUG && linktype != cmTarget::DEBUG)
  212. {
  213. continue;
  214. }
  215. if (lib->second == cmTarget::OPTIMIZED &&
  216. linktype != cmTarget::OPTIMIZED)
  217. {
  218. continue;
  219. }
  220. m_RawLinkItems.push_back(lib->first);
  221. }
  222. }
  223. //-------------------------------------------------------------------
  224. bool cmOrderLinkDirectories::DetermineLibraryPathOrder()
  225. {
  226. // set up all the regular expressions
  227. this->CreateRegularExpressions();
  228. std::vector<cmStdString> finalOrderPaths;
  229. // find all libs that are full paths
  230. Library aLib;
  231. cmStdString dir;
  232. cmStdString file;
  233. for(unsigned int i=0; i < m_RawLinkItems.size(); ++i)
  234. {
  235. if(cmSystemTools::FileIsFullPath(m_RawLinkItems[i].c_str()))
  236. {
  237. cmSystemTools::SplitProgramPath(m_RawLinkItems[i].c_str(),
  238. dir, file);
  239. m_LinkPathSet.insert(dir);
  240. aLib.FullPath = m_RawLinkItems[i];
  241. aLib.File = file;
  242. aLib.Path = dir;
  243. m_FullPathLibraries[aLib.FullPath] = aLib;
  244. m_LinkItems.push_back(file);
  245. }
  246. else
  247. {
  248. m_LinkItems.push_back(m_RawLinkItems[i]);
  249. }
  250. }
  251. this->FindLibrariesInSeachPaths();
  252. for(std::map<cmStdString, std::vector<cmStdString> >::iterator lib =
  253. m_LibraryToDirectories.begin(); lib!= m_LibraryToDirectories.end();
  254. ++lib)
  255. {
  256. if(lib->second.size() > 0)
  257. {
  258. m_MultiDirectoryLibraries.push_back(m_FullPathLibraries[lib->first]);
  259. }
  260. else
  261. {
  262. m_SingleDirectoryLibraries.push_back(m_FullPathLibraries[lib->first]);
  263. }
  264. }
  265. this->FindIndividualLibraryOrders();
  266. m_SortedSearchPaths.clear();
  267. for(std::set<cmStdString>::iterator i = m_LinkPathSet.begin();
  268. i != m_LinkPathSet.end(); ++i)
  269. {
  270. m_SortedSearchPaths.push_back(*i);
  271. }
  272. this->OrderPaths(m_SortedSearchPaths);
  273. // now turn libfoo.a into foo and foo.a into foo
  274. // This will prepare the link items for -litem
  275. this->PrepareLinkTargets();
  276. if(m_ImposibleDirectories.size())
  277. {
  278. return false;
  279. }
  280. return true;
  281. }
  282. std::string cmOrderLinkDirectories::GetWarnings()
  283. {
  284. std::string warning = "It is impossible to order the linker search path in such a way that libraries specified as full paths will be picked by the linker.\nDirectories and libraries involvied are:\n";
  285. for(std::set<cmStdString>::iterator i = m_ImposibleDirectories.begin();
  286. i != m_ImposibleDirectories.end(); ++i)
  287. {
  288. warning += "Directory: ";
  289. warning += *i;
  290. warning += " contains ";
  291. std::map<cmStdString, std::vector<cmStdString> >::iterator j;
  292. for(j = m_LibraryToDirectories.begin();
  293. j != m_LibraryToDirectories.end(); ++j)
  294. {
  295. if(std::find(j->second.begin(), j->second.end(), *i)
  296. != j->second.end())
  297. {
  298. warning += "Library: ";
  299. warning += j->first;
  300. warning += "\n";
  301. }
  302. }
  303. }
  304. warning += "\n";
  305. return warning;
  306. }
  307. //-------------------------------------------------------------------
  308. void
  309. cmOrderLinkDirectories::PrintMap(const char* name,
  310. std::map<cmStdString, std::vector<cmStdString> >& m)
  311. {
  312. std::cerr << name << "\n";
  313. for(std::map<cmStdString, std::vector<cmStdString> >::iterator i =
  314. m.begin(); i != m.end();
  315. ++i)
  316. {
  317. std::cerr << i->first << ": ";
  318. for(std::vector<cmStdString>::iterator l = i->second.begin();
  319. l != i->second.end(); ++l)
  320. {
  321. std::cerr << *l << " ";
  322. }
  323. std::cerr << "\n";
  324. }
  325. }