cmOrderLinkDirectories.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "cmOrderLinkDirectories.h"
  2. #include "cmSystemTools.h"
  3. #include "cmsys/RegularExpression.hxx"
  4. //-------------------------------------------------------------------
  5. bool cmOrderLinkDirectories::LibraryInDirectory(const char* dir,
  6. const char* libIn)
  7. {
  8. cmStdString path = dir;
  9. path += "/";
  10. path += libIn;
  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(libIn))
  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(this->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::FindPathNotInDirectoryToAfterList(
  115. cmStdString& path)
  116. {
  117. for(std::map<cmStdString, std::vector<cmStdString> >::iterator i
  118. = m_DirectoryToAfterList.begin();
  119. i != m_DirectoryToAfterList.end(); ++i)
  120. {
  121. const cmStdString& p = i->first;
  122. bool found = false;
  123. for(std::map<cmStdString, std::vector<cmStdString> >::iterator j
  124. = m_DirectoryToAfterList.begin(); j != m_DirectoryToAfterList.end()
  125. && !found; ++j)
  126. {
  127. if(j != i)
  128. {
  129. found = (std::find(j->second.begin(), j->second.end(), p) != j->second.end());
  130. }
  131. }
  132. if(!found)
  133. {
  134. path = p;
  135. m_DirectoryToAfterList.erase(i);
  136. return true;
  137. }
  138. }
  139. path = "";
  140. return false;
  141. }
  142. //-------------------------------------------------------------------
  143. void cmOrderLinkDirectories::OrderPaths(std::vector<cmStdString>&
  144. orderedPaths)
  145. {
  146. cmStdString path;
  147. // This is a topological sort implementation
  148. // One at a time find paths that are not in any other paths after list
  149. // and put them into the orderedPaths vector in that order
  150. // FindPathNotInDirectoryToAfterList removes the path from the
  151. // m_DirectoryToAfterList once it is found
  152. while(this->FindPathNotInDirectoryToAfterList(path))
  153. {
  154. orderedPaths.push_back(path);
  155. }
  156. // at this point if there are still paths in m_DirectoryToAfterList
  157. // then there is a cycle and we are stuck
  158. if(m_DirectoryToAfterList.size())
  159. {
  160. for(std::map<cmStdString, std::vector<cmStdString> >::iterator i
  161. = m_DirectoryToAfterList.begin();
  162. i != m_DirectoryToAfterList.end(); ++i)
  163. {
  164. m_ImposibleDirectories.insert(i->first);
  165. // still put it in the path list in the order we find them
  166. orderedPaths.push_back(i->first);
  167. }
  168. }
  169. }
  170. //-------------------------------------------------------------------
  171. void cmOrderLinkDirectories::SetLinkInformation(const cmTarget& target,
  172. cmTarget::LinkLibraryType
  173. linktype,
  174. const char* targetLibrary)
  175. {
  176. // collect the search paths from the target into paths set
  177. const std::vector<std::string>& searchPaths = target.GetLinkDirectories();
  178. std::vector<cmStdString> empty;
  179. for(std::vector<std::string>::const_iterator p = searchPaths.begin();
  180. p != searchPaths.end(); ++p)
  181. {
  182. m_DirectoryToAfterList[*p] = empty;
  183. m_LinkPathSet.insert(*p);
  184. }
  185. // collect the link items from the target and put it into libs
  186. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  187. std::vector<cmStdString> libs;
  188. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  189. lib != tlibs.end(); ++lib)
  190. {
  191. // skip zero size library entries, this may happen
  192. // if a variable expands to nothing.
  193. if (lib->first.size() == 0)
  194. {
  195. continue;
  196. }
  197. // Don't link the library against itself!
  198. if(targetLibrary && (lib->first == targetLibrary))
  199. {
  200. continue;
  201. }
  202. // use the correct lib for the current configuration
  203. if (lib->second == cmTarget::DEBUG && linktype != cmTarget::DEBUG)
  204. {
  205. continue;
  206. }
  207. if (lib->second == cmTarget::OPTIMIZED &&
  208. linktype != cmTarget::OPTIMIZED)
  209. {
  210. continue;
  211. }
  212. m_RawLinkItems.push_back(lib->first);
  213. }
  214. }
  215. //-------------------------------------------------------------------
  216. bool cmOrderLinkDirectories::DetermineLibraryPathOrder()
  217. {
  218. // set up all the regular expressions
  219. this->CreateRegularExpressions();
  220. std::vector<cmStdString> finalOrderPaths;
  221. // find all libs that are full paths
  222. Library aLib;
  223. cmStdString dir;
  224. cmStdString file;
  225. std::vector<cmStdString> empty;
  226. for(unsigned int i=0; i < m_RawLinkItems.size(); ++i)
  227. {
  228. if(cmSystemTools::FileIsFullPath(m_RawLinkItems[i].c_str()))
  229. {
  230. cmSystemTools::SplitProgramPath(m_RawLinkItems[i].c_str(),
  231. dir, file);
  232. m_DirectoryToAfterList[dir] = empty;
  233. m_LinkPathSet.insert(dir);
  234. aLib.FullPath = m_RawLinkItems[i];
  235. aLib.File = file;
  236. aLib.Path = dir;
  237. m_FullPathLibraries[aLib.FullPath] = aLib;
  238. m_LinkItems.push_back(file);
  239. }
  240. else
  241. {
  242. m_LinkItems.push_back(m_RawLinkItems[i]);
  243. }
  244. }
  245. this->FindLibrariesInSeachPaths();
  246. for(std::map<cmStdString, std::vector<cmStdString> >::iterator lib =
  247. m_LibraryToDirectories.begin(); lib!= m_LibraryToDirectories.end();
  248. ++lib)
  249. {
  250. if(lib->second.size() > 0)
  251. {
  252. m_MultiDirectoryLibraries.push_back(m_FullPathLibraries[lib->first]);
  253. }
  254. else
  255. {
  256. m_SingleDirectoryLibraries.push_back(m_FullPathLibraries[lib->first]);
  257. }
  258. }
  259. this->FindIndividualLibraryOrders();
  260. m_SortedSearchPaths.clear();
  261. this->OrderPaths(m_SortedSearchPaths);
  262. // now turn libfoo.a into foo and foo.a into foo
  263. // This will prepare the link items for -litem
  264. this->PrepareLinkTargets();
  265. if(m_ImposibleDirectories.size())
  266. {
  267. cmSystemTools::Message(this->GetWarnings().c_str());
  268. return false;
  269. }
  270. return true;
  271. }
  272. std::string cmOrderLinkDirectories::GetWarnings()
  273. {
  274. 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";
  275. for(std::set<cmStdString>::iterator i = m_ImposibleDirectories.begin();
  276. i != m_ImposibleDirectories.end(); ++i)
  277. {
  278. warning += "Directory: ";
  279. warning += *i;
  280. warning += " contains:\n";
  281. std::map<cmStdString, std::vector<cmStdString> >::iterator j;
  282. for(j = m_LibraryToDirectories.begin();
  283. j != m_LibraryToDirectories.end(); ++j)
  284. {
  285. if(std::find(j->second.begin(), j->second.end(), *i)
  286. != j->second.end())
  287. {
  288. warning += "Library: ";
  289. warning += j->first;
  290. warning += "\n";
  291. }
  292. }
  293. warning += "\n";
  294. }
  295. warning += "\n";
  296. return warning;
  297. }
  298. //-------------------------------------------------------------------
  299. void
  300. cmOrderLinkDirectories::PrintMap(const char* name,
  301. std::map<cmStdString, std::vector<cmStdString> >& m)
  302. {
  303. std::cerr << name << "\n";
  304. for(std::map<cmStdString, std::vector<cmStdString> >::iterator i =
  305. m.begin(); i != m.end();
  306. ++i)
  307. {
  308. std::cerr << i->first << ": ";
  309. for(std::vector<cmStdString>::iterator l = i->second.begin();
  310. l != i->second.end(); ++l)
  311. {
  312. std::cerr << *l << " ";
  313. }
  314. std::cerr << "\n";
  315. }
  316. }
  317. void cmOrderLinkDirectories::GetFullPathLibraries(std::vector<cmStdString>&
  318. libs)
  319. {
  320. for(std::map<cmStdString, Library>::iterator i = m_FullPathLibraries.begin();
  321. i != m_FullPathLibraries.end(); ++i)
  322. {
  323. libs.push_back(i->first);
  324. }
  325. }