cmTarget.cxx 25 KB

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