cmTarget.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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::SetType(TargetType type)
  19. {
  20. // only add dependency information for library targets
  21. m_TargetType = type;
  22. if(m_TargetType >= STATIC_LIBRARY && m_TargetType <= MODULE_LIBRARY) {
  23. m_RecordDependencies = true;
  24. } else {
  25. m_RecordDependencies = false;
  26. }
  27. }
  28. void cmTarget::GenerateSourceFilesFromSourceLists( cmMakefile &mf)
  29. {
  30. // this is only done for non install targets
  31. if ((this->m_TargetType == cmTarget::INSTALL_FILES)
  32. || (this->m_TargetType == cmTarget::INSTALL_PROGRAMS))
  33. {
  34. return;
  35. }
  36. // for each src lists add the classes
  37. for (std::vector<std::string>::const_iterator s = m_SourceLists.begin();
  38. s != m_SourceLists.end(); ++s)
  39. {
  40. int done = 0;
  41. // replace any variables
  42. std::string temps = *s;
  43. mf.ExpandVariablesInString(temps);
  44. // Next if one wasn't found then assume it is a single class
  45. if (!done && mf.GetSource(temps.c_str()))
  46. {
  47. m_SourceFiles.push_back(mf.GetSource(temps.c_str()));
  48. done = 1;
  49. }
  50. // if it wasn't a source file listed with the makefile
  51. // see if it is a variable. This is for old CMake 1.2 compatability
  52. // where a source list would be passed into here, by making it
  53. // a vector we need to possibly lookup the variable to maintain
  54. // CMake 1.2 compatability.
  55. const char* versionValue
  56. = mf.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  57. if (!done)
  58. {
  59. if (!versionValue || atof(versionValue) <= 1.2)
  60. {
  61. const char* varValue =
  62. mf.GetDefinition(temps.c_str());
  63. // if the definition exists
  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. unsigned 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.SetProperty("ABSTRACT","0");
  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.SetProperty("ABSTRACT","0");
  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::ClearDependencyInformation( cmMakefile& mf, const char* target )
  133. {
  134. // Clear the dependencies. The cache variable must exist iff we are
  135. // recording dependency information for this target.
  136. std::string depname = target;
  137. depname += "_LIB_DEPENDS";
  138. if (m_RecordDependencies)
  139. {
  140. mf.AddCacheDefinition(depname.c_str(), "",
  141. "Dependencies for target", cmCacheManager::STATIC);
  142. }
  143. else
  144. {
  145. if (mf.GetDefinition( depname.c_str() ))
  146. {
  147. std::string message = "Target ";
  148. message += target;
  149. message += " has dependency information when it shouldn't.\n";
  150. message += "Your cache is probably stale. Please remove the entry\n ";
  151. message += depname;
  152. message += "\nfrom the cache.";
  153. cmSystemTools::Error( message.c_str() );
  154. }
  155. }
  156. }
  157. void cmTarget::AddLinkLibrary(const std::string& lib,
  158. LinkLibraryType llt)
  159. {
  160. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  161. }
  162. void cmTarget::AddLinkLibrary(cmMakefile& mf,
  163. const char *target, const char* lib,
  164. LinkLibraryType llt)
  165. {
  166. // Never add a self dependency, even if the user asks for it.
  167. if(strcmp( target, lib ) == 0)
  168. {
  169. return;
  170. }
  171. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  172. if(llt != cmTarget::GENERAL)
  173. {
  174. std::string linkTypeName = lib;
  175. linkTypeName += "_LINK_TYPE";
  176. switch(llt)
  177. {
  178. case cmTarget::DEBUG:
  179. mf.AddCacheDefinition(linkTypeName.c_str(),
  180. "debug", "Library is used for debug links only",
  181. cmCacheManager::STATIC);
  182. break;
  183. case cmTarget::OPTIMIZED:
  184. mf.AddCacheDefinition(linkTypeName.c_str(),
  185. "optimized", "Library is used for debug links only",
  186. cmCacheManager::STATIC);
  187. break;
  188. case cmTarget::GENERAL: break;
  189. }
  190. }
  191. // Add the explicit dependency information for this target. This is
  192. // simply a set of libraries separated by ";". There should always
  193. // be a trailing ";". These library names are not canonical, in that
  194. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  195. // We shouldn't remove duplicates here because external libraries
  196. // may be purposefully duplicated to handle recursive dependencies,
  197. // and we removing one instance will break the link line. Duplicates
  198. // will be appropriately eliminated at emit time.
  199. if(m_RecordDependencies)
  200. {
  201. std::string targetEntry = target;
  202. targetEntry += "_LIB_DEPENDS";
  203. std::string dependencies;
  204. const char* old_val = mf.GetDefinition( targetEntry.c_str() );
  205. if( old_val )
  206. {
  207. dependencies += old_val;
  208. }
  209. dependencies += lib;
  210. dependencies += ";";
  211. mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
  212. "Dependencies for the target",
  213. cmCacheManager::STATIC );
  214. }
  215. }
  216. bool cmTarget::HasCxx() const
  217. {
  218. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  219. i != m_SourceFiles.end(); ++i)
  220. {
  221. if((*i)->GetSourceExtension() != "c" &&
  222. (*i)->GetSourceExtension() != "h")
  223. {
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. void
  230. cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
  231. {
  232. // There are two key parts of the dependency analysis: (1)
  233. // determining the libraries in the link line, and (2) constructing
  234. // the dependency graph for those libraries.
  235. //
  236. // The latter is done using the cache entries that record the
  237. // dependencies of each library.
  238. //
  239. // The former is a more thorny issue, since it is not clear how to
  240. // determine if two libraries listed on the link line refer to the a
  241. // single library or not. For example, consider the link "libraries"
  242. // /usr/lib/libtiff.so -ltiff
  243. // Is this one library or two? The solution implemented here is the
  244. // simplest (and probably the only practical) one: two libraries are
  245. // the same if their "link strings" are identical. Thus, the two
  246. // libraries above are considered distinct. This also means that for
  247. // dependency analysis to be effective, the CMake user must specify
  248. // libraries build by his project without using any linker flags or
  249. // file extensions. That is,
  250. // LINK_LIBRARIES( One Two )
  251. // instead of
  252. // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
  253. // The former is probably what most users would do, but it never
  254. // hurts to document the assumptions. :-) Therefore, in the analysis
  255. // code, the "canonical name" of a library is simply its name as
  256. // given to a LINK_LIBRARIES command.
  257. //
  258. // Also, we will leave the original link line intact; we will just add any
  259. // dependencies that were missing.
  260. //
  261. // There is a problem with recursive external libraries
  262. // (i.e. libraries with no dependency information that are
  263. // recursively dependent). We must make sure that the we emit one of
  264. // the libraries twice to satisfy the recursion, but we shouldn't
  265. // emit it more times than necessary. In particular, we must make
  266. // sure that handling this improbable case doesn't cost us when
  267. // dealing with the common case of non-recursive libraries. The
  268. // solution is to assume that the recursion is satisfied at one node
  269. // of the dependency tree. To illustrate, assume libA and libB are
  270. // extrenal and mutually dependent. Suppose libX depends on
  271. // libA, and libY on libA and libX. Then
  272. // TARGET_LINK_LIBRARIES( Y X A B A )
  273. // TARGET_LINK_LIBRARIES( X A B A )
  274. // TARGET_LINK_LIBRARIES( Exec Y )
  275. // would result in "-lY -lX -lA -lB -lA". This is the correct way to
  276. // specify the dependencies, since the mutual dependency of A and B
  277. // is resolved *every time libA is specified*.
  278. //
  279. // Something like
  280. // TARGET_LINK_LIBRARIES( Y X A B A )
  281. // TARGET_LINK_LIBRARIES( X A B )
  282. // TARGET_LINK_LIBRARIES( Exec Y )
  283. // would result in "-lY -lX -lA -lB", and the mutual dependency
  284. // information is lost. This is because in some case (Y), the mutual
  285. // dependency of A and B is listed, while in another other case (X),
  286. // it is not. Depending on which line actually emits A, the mutual
  287. // dependency may or may not be on the final link line. We can't
  288. // handle this pathalogical case cleanly without emitting extra
  289. // libraries for the normal cases. Besides, the dependency
  290. // information for X is wrong anyway: if we build an executable
  291. // depending on X alone, we would not have the mutual dependency on
  292. // A and B resolved.
  293. //
  294. // IMPROVEMENTS:
  295. // -- The current algorithm will not always pick the "optimal" link line
  296. // when recursive dependencies are present. It will instead break the
  297. // cycles at an aribtrary point. The majority of projects won't have
  298. // cyclic dependencies, so this is probably not a big deal. Note that
  299. // the link line is always correct, just not necessary optimal.
  300. typedef std::vector< std::string > LinkLine;
  301. // The dependency map.
  302. DependencyMap dep_map;
  303. // If LIBRARY_OUTPUT_PATH is not set, then we must add search paths
  304. // for all the new libraries added by the dependency analysis.
  305. const char* libOutPath = mf.GetDefinition("LIBRARY_OUTPUT_PATH");
  306. bool addLibDirs = (libOutPath==0 || strcmp(libOutPath,"")==0);
  307. // 1. Build the dependency graph
  308. //
  309. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  310. lib != m_LinkLibraries.rend(); ++lib)
  311. {
  312. this->GatherDependencies( mf, lib->first, dep_map );
  313. }
  314. // 2. Remove any dependencies that are already satisfied in the original
  315. // link line.
  316. //
  317. for(LinkLibraries::iterator lib = m_LinkLibraries.begin();
  318. lib != m_LinkLibraries.end(); ++lib)
  319. {
  320. for( LinkLibraries::iterator lib2 = lib;
  321. lib2 != m_LinkLibraries.end(); ++lib2)
  322. {
  323. DeleteDependency( dep_map, lib->first, lib2->first );
  324. }
  325. }
  326. // 3. Create the new link line by simply emitting any dependencies that are
  327. // missing. Start from the back and keep adding.
  328. //
  329. std::set<cmStdString> done, visited;
  330. std::vector<std::string> newLinkLibraries;
  331. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  332. lib != m_LinkLibraries.rend(); ++lib)
  333. {
  334. // skip zero size library entries, this may happen
  335. // if a variable expands to nothing.
  336. if (lib->first.size() != 0)
  337. {
  338. Emit( lib->first, dep_map, done, visited, newLinkLibraries );
  339. }
  340. }
  341. // 4. Add the new libraries to the link line.
  342. //
  343. for( std::vector<std::string>::reverse_iterator k = newLinkLibraries.rbegin();
  344. k != newLinkLibraries.rend(); ++k )
  345. {
  346. if( addLibDirs )
  347. {
  348. // who the hell knows what this is, I think that K contains the
  349. // name of a library but ... Ken
  350. // k contains the same stuff that are on the LINK_LIBRARIES
  351. // commands. Normally, they would just be library names. -- Amitha.
  352. std::string libPathStr = *k + "_CMAKE_PATH";
  353. const char* libpath = mf.GetDefinition( libPathStr.c_str() );
  354. if( libpath )
  355. {
  356. // Don't add a link directory that is already present.
  357. if(std::find(m_LinkDirectories.begin(),
  358. m_LinkDirectories.end(), libpath) == m_LinkDirectories.end())
  359. {
  360. m_LinkDirectories.push_back(libpath);
  361. }
  362. }
  363. }
  364. std::string linkType = *k;
  365. linkType += "_LINK_TYPE";
  366. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  367. const char* linkTypeString = mf.GetDefinition( linkType.c_str() );
  368. if(linkTypeString)
  369. {
  370. if(strcmp(linkTypeString, "debug") == 0)
  371. {
  372. llt = cmTarget::DEBUG;
  373. }
  374. if(strcmp(linkTypeString, "optimized") == 0)
  375. {
  376. llt = cmTarget::OPTIMIZED;
  377. }
  378. }
  379. m_LinkLibraries.push_back( std::make_pair(*k,llt) );
  380. }
  381. }
  382. void cmTarget::InsertDependency( DependencyMap& depMap,
  383. const cmStdString& lib,
  384. const cmStdString& dep ) const
  385. {
  386. depMap[lib].push_back(dep);
  387. }
  388. void cmTarget::DeleteDependency( DependencyMap& depMap,
  389. const cmStdString& lib,
  390. const cmStdString& dep ) const
  391. {
  392. // Make sure there is an entry in the map for lib. If so, delete all
  393. // dependencies to dep. There may be repeated entries because of
  394. // external libraries that are specified multiple times.
  395. DependencyMap::iterator map_itr = depMap.find( lib );
  396. if( map_itr != depMap.end() )
  397. {
  398. DependencyList& depList = map_itr->second;
  399. DependencyList::iterator itr;
  400. while( (itr = std::find(depList.begin(), depList.end(), dep)) != depList.end() )
  401. {
  402. depList.erase( itr );
  403. }
  404. }
  405. }
  406. void cmTarget::Emit( const std::string& lib,
  407. const DependencyMap& dep_map,
  408. std::set<cmStdString>& emitted,
  409. std::set<cmStdString>& visited,
  410. std::vector<std::string>& link_line ) const
  411. {
  412. // It's already been emitted
  413. if( emitted.find(lib) != emitted.end() )
  414. return;
  415. // Emit the dependencies only if this library node hasn't been
  416. // visited before. If it has, then we have a cycle. The recursion
  417. // that got us here should take care of everything.
  418. if( visited.insert(lib).second )
  419. {
  420. if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
  421. {
  422. const DependencyList& dep_on = dep_map.find( lib )->second;
  423. DependencyList::const_reverse_iterator i;
  424. // To cater for recursive external libraries, we must emit
  425. // duplicates on this link line *unless* they were emitted by
  426. // some other node, in which case we assume that the recursion
  427. // was resolved then. We making the simplifying assumption that
  428. // any duplicates on a single link line are on purpose, and must
  429. // be preserved.
  430. // This variable will keep track of the libraries that were
  431. // emitted directory from the current node, and not from a
  432. // recursive call. This way, if we come across a library that
  433. // has already been emitted, we repeat it iff it has been
  434. // emitted here.
  435. std::set<cmStdString> emitted_here;
  436. for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
  437. {
  438. if( emitted_here.find(*i) != emitted_here.end() )
  439. {
  440. // a repeat. Must emit.
  441. emitted.insert(*i);
  442. link_line.push_back( *i );
  443. }
  444. else
  445. {
  446. // Emit only if no-one else has
  447. if( emitted.find(*i) == emitted.end() )
  448. {
  449. // emit dependencies
  450. Emit( *i, dep_map, emitted, visited, link_line );
  451. // emit self
  452. emitted.insert(*i);
  453. emitted_here.insert(*i);
  454. link_line.push_back( *i );
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. void cmTarget::GatherDependencies( const cmMakefile& mf,
  462. const std::string& lib,
  463. DependencyMap& dep_map )
  464. {
  465. // If the library is already in the dependency map, then it has
  466. // already been fully processed.
  467. if( dep_map.find(lib) != dep_map.end() )
  468. return;
  469. const char* deps = mf.GetDefinition( (lib+"_LIB_DEPENDS").c_str() );
  470. if( deps && strcmp(deps,"") != 0 )
  471. {
  472. // Make sure this library is in the map, even if it has an empty
  473. // set of dependencies. This distinguishes the case of explicitly
  474. // no dependencies with that of unspecified dependencies.
  475. dep_map[lib];
  476. // Parse the dependency information, which is simply a set of
  477. // libraries separated by ";". There is always a trailing ";".
  478. std::string depline = deps;
  479. std::string::size_type start = 0;
  480. std::string::size_type end;
  481. end = depline.find( ";", start );
  482. while( end != std::string::npos )
  483. {
  484. std::string l = depline.substr( start, end-start );
  485. if( l.size() != 0 )
  486. {
  487. InsertDependency( dep_map, lib, l );
  488. GatherDependencies( mf, l, dep_map );
  489. }
  490. start = end+1; // skip the ;
  491. end = depline.find( ";", start );
  492. }
  493. DeleteDependency( dep_map, lib, lib); // cannot depend on itself
  494. }
  495. }