cmTarget.cxx 15 KB

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