cmTarget.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #include "cmTarget.h"
  14. #include "cmMakefile.h"
  15. #include "cmSourceFile.h"
  16. #include <map>
  17. #include <set>
  18. void cmTarget::GenerateSourceFilesFromSourceLists( cmMakefile &mf)
  19. {
  20. // this is only done for non install targets
  21. if ((this->m_TargetType == cmTarget::INSTALL_FILES)
  22. || (this->m_TargetType == cmTarget::INSTALL_PROGRAMS))
  23. {
  24. return;
  25. }
  26. // for each src lists add the classes
  27. for (std::vector<std::string>::const_iterator s = m_SourceLists.begin();
  28. s != m_SourceLists.end(); ++s)
  29. {
  30. int done = 0;
  31. // replace any variables
  32. std::string temps = *s;
  33. mf.ExpandVariablesInString(temps);
  34. // Next if one wasn't found then assume it is a single class
  35. if (!done && mf.GetSource(temps.c_str()))
  36. {
  37. m_SourceFiles.push_back(mf.GetSource(temps.c_str()));
  38. done = 1;
  39. }
  40. // if it wasn't a source file listed with the makefile
  41. // see if it is a variable. This is for old CMake 1.2 compatability
  42. // where a source list would be passed into here, by making it
  43. // a vector we need to possibly lookup the variable to maintain
  44. // CMake 1.2 compatability.
  45. const char* versionValue
  46. = mf.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  47. if (!done)
  48. {
  49. if (!versionValue || atof(versionValue) <= 1.2)
  50. {
  51. const char* varValue =
  52. mf.GetDefinition(temps.c_str());
  53. // if the definition exists
  54. if (varValue)
  55. {
  56. std::vector<std::string> tval;
  57. tval.push_back(varValue);
  58. std::vector<std::string> args;
  59. cmSystemTools::ExpandListArguments(tval, args);
  60. unsigned int i;
  61. for (i = 0; i < args.size(); ++i)
  62. {
  63. if (mf.GetSource(args[i].c_str()))
  64. {
  65. m_SourceFiles.push_back(mf.GetSource(args[i].c_str()));
  66. }
  67. else
  68. {
  69. cmSourceFile file;
  70. file.SetProperty("ABSTRACT","0");
  71. file.SetName(args[i].c_str(), mf.GetCurrentDirectory(),
  72. mf.GetSourceExtensions(),
  73. mf.GetHeaderExtensions());
  74. m_SourceFiles.push_back(mf.AddSource(file));
  75. }
  76. }
  77. done = 1;
  78. }
  79. }
  80. }
  81. // if we still are not done, try to create the SourceFile structure
  82. if (!done)
  83. {
  84. cmSourceFile file;
  85. file.SetProperty("ABSTRACT","0");
  86. file.SetName(temps.c_str(), mf.GetCurrentDirectory(),
  87. mf.GetSourceExtensions(),
  88. mf.GetHeaderExtensions());
  89. m_SourceFiles.push_back(mf.AddSource(file));
  90. done = 1;
  91. }
  92. }
  93. // expand any link library variables whle we are at it
  94. LinkLibraries::iterator p = m_LinkLibraries.begin();
  95. for (;p != m_LinkLibraries.end(); ++p)
  96. {
  97. mf.ExpandVariablesInString(p->first);
  98. }
  99. }
  100. void cmTarget::MergeLinkLibraries( cmMakefile& mf,
  101. const char *selfname,
  102. const LinkLibraries& libs )
  103. {
  104. // Only add on libraries we haven't added on before.
  105. // Assumption: the global link libraries could only grow, never shrink
  106. LinkLibraries::const_iterator i = libs.begin();
  107. i += m_PrevLinkedLibraries.size();
  108. for( ; i != libs.end(); ++i )
  109. {
  110. // We call this so that the dependencies get written to the cache
  111. this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
  112. }
  113. m_PrevLinkedLibraries = libs;
  114. }
  115. void cmTarget::AddLinkDirectory(const char* d)
  116. {
  117. // Make sure we don't add unnecessary search directories.
  118. if( std::find( m_LinkDirectories.begin(), m_LinkDirectories.end(), d )
  119. == m_LinkDirectories.end() )
  120. m_LinkDirectories.push_back( d );
  121. }
  122. void cmTarget::AddLinkLibrary(const std::string& lib,
  123. LinkLibraryType llt)
  124. {
  125. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  126. }
  127. void cmTarget::AddLinkLibrary(cmMakefile& mf,
  128. const char *target, const char* lib,
  129. LinkLibraryType llt)
  130. {
  131. // Never add a self dependency, even if the user asks for it.
  132. if(strcmp( target, lib ) == 0)
  133. {
  134. return;
  135. }
  136. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  137. if(llt != cmTarget::GENERAL)
  138. {
  139. std::string linkTypeName = lib;
  140. linkTypeName += "_LINK_TYPE";
  141. switch(llt)
  142. {
  143. case cmTarget::DEBUG:
  144. mf.AddCacheDefinition(linkTypeName.c_str(),
  145. "debug", "Library is used for debug links only",
  146. cmCacheManager::STATIC);
  147. break;
  148. case cmTarget::OPTIMIZED:
  149. mf.AddCacheDefinition(linkTypeName.c_str(),
  150. "optimized", "Library is used for debug links only",
  151. cmCacheManager::STATIC);
  152. break;
  153. case cmTarget::GENERAL: break;
  154. }
  155. }
  156. // Add the explicit dependency information for this target. This is
  157. // simply a set of libraries separated by ";". There should always
  158. // be a trailing ";". These library names are not canonical, in that
  159. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  160. // only add depend information for library targets
  161. if(m_TargetType >= STATIC_LIBRARY && m_TargetType <= MODULE_LIBRARY)
  162. {
  163. std::string targetEntry = target;
  164. targetEntry += "_LIB_DEPENDS";
  165. std::string dependencies;
  166. const char* old_val = mf.GetDefinition( targetEntry.c_str() );
  167. if( old_val )
  168. {
  169. dependencies += old_val;
  170. }
  171. if( dependencies.find( lib ) == std::string::npos )
  172. {
  173. dependencies += lib;
  174. dependencies += ";";
  175. }
  176. mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
  177. "Dependencies for the target",
  178. cmCacheManager::STATIC );
  179. }
  180. }
  181. bool cmTarget::HasCxx() const
  182. {
  183. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  184. i != m_SourceFiles.end(); ++i)
  185. {
  186. if((*i)->GetSourceExtension() != "c" &&
  187. (*i)->GetSourceExtension() != "h")
  188. {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. void
  195. cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
  196. {
  197. // There are two key parts of the dependency analysis: (1)
  198. // determining the libraries in the link line, and (2) constructing
  199. // the dependency graph for those libraries.
  200. //
  201. // The latter is done using the cache entries that record the
  202. // dependencies of each library.
  203. //
  204. // The former is a more thorny issue, since it is not clear how to
  205. // determine if two libraries listed on the link line refer to the a
  206. // single library or not. For example, consider the link "libraries"
  207. // /usr/lib/libtiff.so -ltiff
  208. // Is this one library or two? The solution implemented here is the
  209. // simplest (and probably the only practical) one: two libraries are
  210. // the same if their "link strings" are identical. Thus, the two
  211. // libraries above are considered distinct. This also means that for
  212. // dependency analysis to be effective, the CMake user must specify
  213. // libraries build by his project without using any linker flags or
  214. // file extensions. That is,
  215. // LINK_LIBRARIES( One Two )
  216. // instead of
  217. // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
  218. // The former is probably what most users would do, but it never
  219. // hurts to document the assumptions. :-) Therefore, in the analysis
  220. // code, the "canonical name" of a library is simply its name as
  221. // given to a LINK_LIBRARIES command.
  222. //
  223. // Also, we will leave the original link line intact; we will just add any
  224. // dependencies that were missing.
  225. typedef std::vector< std::string > LinkLine;
  226. // The dependency map.
  227. DependencyMap dep_map;
  228. // Keeps track of which dependencies have already been emitted for a given
  229. // target. This could be via this function, or because they were already
  230. // satisfied on the original link line.
  231. DependencyMap satisfied;
  232. // If LIBRARY_OUTPUT_PATH is not set, then we must add search paths
  233. // for all the new libraries added by the dependency analysis.
  234. const char* libOutPath = mf.GetDefinition("LIBRARY_OUTPUT_PATH");
  235. bool addLibDirs = (libOutPath==0 || strcmp(libOutPath,"")==0);
  236. // 1. Determine the dependencies already satisfied by the original link
  237. // line.
  238. for(LinkLibraries::iterator lib = m_LinkLibraries.begin();
  239. lib != m_LinkLibraries.end(); ++lib)
  240. {
  241. for( LinkLibraries::iterator lib2 = lib;
  242. lib2 != m_LinkLibraries.end(); ++lib2)
  243. {
  244. satisfied[ lib->first ].insert( lib2->first );
  245. }
  246. }
  247. // 2. Build the explicit dependency map
  248. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  249. lib != m_LinkLibraries.rend(); ++lib)
  250. {
  251. this->GatherDependencies( mf, lib->first, dep_map );
  252. }
  253. // 3. Create the new link line by simply emitting any dependencies that are
  254. // missing. Start from the back and keep adding.
  255. std::set<cmStdString> done, visited;
  256. std::vector<std::string> newLinkLibraries;
  257. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  258. lib != m_LinkLibraries.rend(); ++lib)
  259. {
  260. // skip zero size library entries, this may happen
  261. // if a variable expands to nothing.
  262. if (lib->first.size() == 0) continue;
  263. // Emit all the dependencies that are not already satisfied on the
  264. // original link line.
  265. if( dep_map.find(lib->first) != dep_map.end() ) // does it have dependencies?
  266. {
  267. const std::set<cmStdString>& dep_on = dep_map.find( lib->first )->second;
  268. std::set<cmStdString>::const_iterator i;
  269. for( i = dep_on.begin(); i != dep_on.end(); ++i )
  270. {
  271. if( satisfied[lib->first].end() == satisfied[lib->first].find( *i ) )
  272. {
  273. Emit( *i, dep_map, done, visited, newLinkLibraries );
  274. }
  275. }
  276. }
  277. }
  278. // 4. Add the new libraries to the link line.
  279. for( std::vector<std::string>::reverse_iterator k = newLinkLibraries.rbegin();
  280. k != newLinkLibraries.rend(); ++k )
  281. {
  282. if( addLibDirs )
  283. {
  284. // who the hell knows what this is, I think that K contains the
  285. // name of a library but ... Ken
  286. std::string libPathStr = *k + "_CMAKE_PATH";
  287. const char* libpath = mf.GetDefinition( libPathStr.c_str() );
  288. if( libpath )
  289. {
  290. // Don't add a link directory that is already present.
  291. if(std::find(m_LinkDirectories.begin(),
  292. m_LinkDirectories.end(), libpath) == m_LinkDirectories.end())
  293. {
  294. m_LinkDirectories.push_back(libpath);
  295. }
  296. }
  297. }
  298. std::string linkType = *k;
  299. linkType += "_LINK_TYPE";
  300. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  301. const char* linkTypeString = mf.GetDefinition( linkType.c_str() );
  302. if(linkTypeString)
  303. {
  304. if(strcmp(linkTypeString, "debug") == 0)
  305. {
  306. llt = cmTarget::DEBUG;
  307. }
  308. if(strcmp(linkTypeString, "optimized") == 0)
  309. {
  310. llt = cmTarget::OPTIMIZED;
  311. }
  312. }
  313. m_LinkLibraries.push_back( std::make_pair(*k,llt) );
  314. }
  315. }
  316. void cmTarget::Emit( const std::string& lib,
  317. const DependencyMap& dep_map,
  318. std::set<cmStdString>& emitted,
  319. std::set<cmStdString>& visited,
  320. std::vector<std::string>& link_line ) const
  321. {
  322. // It's already been emitted
  323. if( emitted.find(lib) != emitted.end() )
  324. {
  325. return;
  326. }
  327. // If this library hasn't been visited before, then emit all its
  328. // dependencies before emitting the library itself. If it has been
  329. // visited before, then there is a dependency cycle. Just emit the
  330. // library itself, and let the recursion that got us here deal with
  331. // emitting the dependencies for the library.
  332. if( visited.insert(lib).second )
  333. {
  334. if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
  335. {
  336. const std::set<cmStdString>& dep_on = dep_map.find( lib )->second;
  337. std::set<cmStdString>::const_iterator i;
  338. for( i = dep_on.begin(); i != dep_on.end(); ++i )
  339. {
  340. Emit( *i, dep_map, emitted, visited, link_line );
  341. }
  342. }
  343. }
  344. link_line.push_back( lib );
  345. emitted.insert(lib);
  346. }
  347. void cmTarget::GatherDependencies( const cmMakefile& mf,
  348. const std::string& lib,
  349. DependencyMap& dep_map )
  350. {
  351. // If the library is already in the dependency map, then it has
  352. // already been fully processed.
  353. if( dep_map.find(lib) != dep_map.end() )
  354. return;
  355. const char* deps = mf.GetDefinition( (lib+"_LIB_DEPENDS").c_str() );
  356. if( deps && strcmp(deps,"") != 0 )
  357. {
  358. // Make sure this library is in the map, even if it has an empty
  359. // set of dependencies. This distinguishes the case of explicitly
  360. // no dependencies with that of unspecified dependencies.
  361. dep_map[lib];
  362. // Parse the dependency information, which is simply a set of
  363. // libraries separated by ";". There is always a trailing ";".
  364. std::string depline = deps;
  365. std::string::size_type start = 0;
  366. std::string::size_type end;
  367. end = depline.find( ";", start );
  368. while( end != std::string::npos )
  369. {
  370. std::string l = depline.substr( start, end-start );
  371. if( l.size() != 0 )
  372. {
  373. dep_map[ lib ].insert( l );
  374. GatherDependencies( mf, l, dep_map );
  375. }
  376. start = end+1; // skip the ;
  377. end = depline.find( ";", start );
  378. }
  379. dep_map[lib].erase(lib); // cannot depend on itself
  380. }
  381. }
  382. // return true if lib1 depends on lib2
  383. bool cmTarget::DependsOn( const std::string& lib1, const std::string& lib2,
  384. const DependencyMap& dep_map,
  385. std::set<cmStdString>& visited ) const
  386. {
  387. if( !visited.insert( lib1 ).second )
  388. {
  389. return false; // already visited here
  390. }
  391. if( lib1 == lib2 )
  392. {
  393. return false;
  394. }
  395. if( dep_map.find(lib1) == dep_map.end() )
  396. {
  397. return false; // lib1 doesn't have any dependencies
  398. }
  399. const std::set<cmStdString>& dep_set = dep_map.find(lib1)->second;
  400. if( dep_set.end() != dep_set.find( lib2 ) )
  401. {
  402. return true; // lib1 doesn't directly depend on lib2.
  403. }
  404. // Do a recursive check: does lib1 depend on x which depends on lib2?
  405. for( std::set<cmStdString>::const_iterator itr = dep_set.begin();
  406. itr != dep_set.end(); ++itr )
  407. {
  408. if( this->DependsOn( *itr, lib2, dep_map, visited ) )
  409. {
  410. return true;
  411. }
  412. }
  413. return false;
  414. }