cmTarget.cxx 20 KB

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