cmTarget.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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 custom 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 custom 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 && *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. // Store the library's link type in the cache. If it is a
  323. // conflicting type then assume it is always used. This is the
  324. // case when the user sets the cache entries for debug and
  325. // optimized versions of the library to the same value.
  326. std::string linkTypeName = lib;
  327. linkTypeName += "_LINK_TYPE";
  328. switch(llt)
  329. {
  330. case cmTarget::DEBUG:
  331. {
  332. const char* def = mf.GetDefinition(linkTypeName.c_str());
  333. if(!def || strcmp(def, "debug") == 0)
  334. {
  335. mf.AddCacheDefinition(linkTypeName.c_str(),
  336. "debug", "Library is used for debug links only",
  337. cmCacheManager::STATIC);
  338. }
  339. else
  340. {
  341. mf.AddCacheDefinition(linkTypeName.c_str(),
  342. "general", "Library is used for both debug and optimized links",
  343. cmCacheManager::STATIC);
  344. }
  345. }
  346. break;
  347. case cmTarget::OPTIMIZED:
  348. {
  349. const char* def = mf.GetDefinition(linkTypeName.c_str());
  350. if(!def || strcmp(def, "optimized") == 0)
  351. {
  352. mf.AddCacheDefinition(linkTypeName.c_str(),
  353. "optimized", "Library is used for debug links only",
  354. cmCacheManager::STATIC);
  355. }
  356. else
  357. {
  358. mf.AddCacheDefinition(linkTypeName.c_str(),
  359. "general", "Library is used for both debug and optimized links",
  360. cmCacheManager::STATIC);
  361. }
  362. }
  363. break;
  364. case cmTarget::GENERAL:
  365. break;
  366. }
  367. }
  368. // Add the explicit dependency information for this target. This is
  369. // simply a set of libraries separated by ";". There should always
  370. // be a trailing ";". These library names are not canonical, in that
  371. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  372. // We shouldn't remove duplicates here because external libraries
  373. // may be purposefully duplicated to handle recursive dependencies,
  374. // and we removing one instance will break the link line. Duplicates
  375. // will be appropriately eliminated at emit time.
  376. if(m_RecordDependencies)
  377. {
  378. std::string targetEntry = target;
  379. targetEntry += "_LIB_DEPENDS";
  380. std::string dependencies;
  381. const char* old_val = mf.GetDefinition( targetEntry.c_str() );
  382. if( old_val )
  383. {
  384. dependencies += old_val;
  385. }
  386. dependencies += lib;
  387. dependencies += ";";
  388. mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
  389. "Dependencies for the target",
  390. cmCacheManager::STATIC );
  391. }
  392. }
  393. bool cmTarget::HasCxx() const
  394. {
  395. if(this->GetProperty("HAS_CXX"))
  396. {
  397. return true;
  398. }
  399. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  400. i != m_SourceFiles.end(); ++i)
  401. {
  402. if(cmSystemTools::GetFileFormat((*i)->GetSourceExtension().c_str())
  403. == cmSystemTools::CXX_FILE_FORMAT)
  404. {
  405. return true;
  406. }
  407. }
  408. return false;
  409. }
  410. bool cmTarget::HasFortran() const
  411. {
  412. if(this->GetProperty("HAS_FORTRAN"))
  413. {
  414. return true;
  415. }
  416. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  417. i != m_SourceFiles.end(); ++i)
  418. {
  419. if(cmSystemTools::GetFileFormat((*i)->GetSourceExtension().c_str())
  420. == cmSystemTools::FORTRAN_FILE_FORMAT)
  421. {
  422. return true;
  423. }
  424. }
  425. return false;
  426. }
  427. void
  428. cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
  429. {
  430. // There are two key parts of the dependency analysis: (1)
  431. // determining the libraries in the link line, and (2) constructing
  432. // the dependency graph for those libraries.
  433. //
  434. // The latter is done using the cache entries that record the
  435. // dependencies of each library.
  436. //
  437. // The former is a more thorny issue, since it is not clear how to
  438. // determine if two libraries listed on the link line refer to the a
  439. // single library or not. For example, consider the link "libraries"
  440. // /usr/lib/libtiff.so -ltiff
  441. // Is this one library or two? The solution implemented here is the
  442. // simplest (and probably the only practical) one: two libraries are
  443. // the same if their "link strings" are identical. Thus, the two
  444. // libraries above are considered distinct. This also means that for
  445. // dependency analysis to be effective, the CMake user must specify
  446. // libraries build by his project without using any linker flags or
  447. // file extensions. That is,
  448. // LINK_LIBRARIES( One Two )
  449. // instead of
  450. // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
  451. // The former is probably what most users would do, but it never
  452. // hurts to document the assumptions. :-) Therefore, in the analysis
  453. // code, the "canonical name" of a library is simply its name as
  454. // given to a LINK_LIBRARIES command.
  455. //
  456. // Also, we will leave the original link line intact; we will just add any
  457. // dependencies that were missing.
  458. //
  459. // There is a problem with recursive external libraries
  460. // (i.e. libraries with no dependency information that are
  461. // recursively dependent). We must make sure that the we emit one of
  462. // the libraries twice to satisfy the recursion, but we shouldn't
  463. // emit it more times than necessary. In particular, we must make
  464. // sure that handling this improbable case doesn't cost us when
  465. // dealing with the common case of non-recursive libraries. The
  466. // solution is to assume that the recursion is satisfied at one node
  467. // of the dependency tree. To illustrate, assume libA and libB are
  468. // extrenal and mutually dependent. Suppose libX depends on
  469. // libA, and libY on libA and libX. Then
  470. // TARGET_LINK_LIBRARIES( Y X A B A )
  471. // TARGET_LINK_LIBRARIES( X A B A )
  472. // TARGET_LINK_LIBRARIES( Exec Y )
  473. // would result in "-lY -lX -lA -lB -lA". This is the correct way to
  474. // specify the dependencies, since the mutual dependency of A and B
  475. // is resolved *every time libA is specified*.
  476. //
  477. // Something like
  478. // TARGET_LINK_LIBRARIES( Y X A B A )
  479. // TARGET_LINK_LIBRARIES( X A B )
  480. // TARGET_LINK_LIBRARIES( Exec Y )
  481. // would result in "-lY -lX -lA -lB", and the mutual dependency
  482. // information is lost. This is because in some case (Y), the mutual
  483. // dependency of A and B is listed, while in another other case (X),
  484. // it is not. Depending on which line actually emits A, the mutual
  485. // dependency may or may not be on the final link line. We can't
  486. // handle this pathalogical case cleanly without emitting extra
  487. // libraries for the normal cases. Besides, the dependency
  488. // information for X is wrong anyway: if we build an executable
  489. // depending on X alone, we would not have the mutual dependency on
  490. // A and B resolved.
  491. //
  492. // IMPROVEMENTS:
  493. // -- The current algorithm will not always pick the "optimal" link line
  494. // when recursive dependencies are present. It will instead break the
  495. // cycles at an aribtrary point. The majority of projects won't have
  496. // cyclic dependencies, so this is probably not a big deal. Note that
  497. // the link line is always correct, just not necessary optimal.
  498. typedef std::vector< std::string > LinkLine;
  499. // The dependency map.
  500. DependencyMap dep_map;
  501. // If LIBRARY_OUTPUT_PATH is not set, then we must add search paths
  502. // for all the new libraries added by the dependency analysis.
  503. const char* libOutPath = mf.GetDefinition("LIBRARY_OUTPUT_PATH");
  504. bool addLibDirs = (libOutPath==0 || strcmp(libOutPath,"")==0);
  505. // 1. Build the dependency graph
  506. //
  507. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  508. lib != m_LinkLibraries.rend(); ++lib)
  509. {
  510. this->GatherDependencies( mf, lib->first, dep_map );
  511. }
  512. // 2. Remove any dependencies that are already satisfied in the original
  513. // link line.
  514. //
  515. for(LinkLibraries::iterator lib = m_LinkLibraries.begin();
  516. lib != m_LinkLibraries.end(); ++lib)
  517. {
  518. for( LinkLibraries::iterator lib2 = lib;
  519. lib2 != m_LinkLibraries.end(); ++lib2)
  520. {
  521. DeleteDependency( dep_map, lib->first, lib2->first );
  522. }
  523. }
  524. // 3. Create the new link line by simply emitting any dependencies that are
  525. // missing. Start from the back and keep adding.
  526. //
  527. std::set<cmStdString> done, visited;
  528. std::vector<std::string> newLinkLibraries;
  529. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  530. lib != m_LinkLibraries.rend(); ++lib)
  531. {
  532. // skip zero size library entries, this may happen
  533. // if a variable expands to nothing.
  534. if (lib->first.size() != 0)
  535. {
  536. Emit( lib->first, dep_map, done, visited, newLinkLibraries );
  537. }
  538. }
  539. // 4. Add the new libraries to the link line.
  540. //
  541. for( std::vector<std::string>::reverse_iterator k = newLinkLibraries.rbegin();
  542. k != newLinkLibraries.rend(); ++k )
  543. {
  544. if( addLibDirs )
  545. {
  546. // who the hell knows what this is, I think that K contains the
  547. // name of a library but ... Ken
  548. // k contains the same stuff that are on the LINK_LIBRARIES
  549. // commands. Normally, they would just be library names. -- Amitha.
  550. std::string libPathStr = *k + "_CMAKE_PATH";
  551. const char* libpath = mf.GetDefinition( libPathStr.c_str() );
  552. if( libpath )
  553. {
  554. // Don't add a link directory that is already present.
  555. if(std::find(m_LinkDirectories.begin(),
  556. m_LinkDirectories.end(), libpath) == m_LinkDirectories.end())
  557. {
  558. m_LinkDirectories.push_back(libpath);
  559. }
  560. }
  561. }
  562. std::string linkType = *k;
  563. linkType += "_LINK_TYPE";
  564. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  565. const char* linkTypeString = mf.GetDefinition( linkType.c_str() );
  566. if(linkTypeString)
  567. {
  568. if(strcmp(linkTypeString, "debug") == 0)
  569. {
  570. llt = cmTarget::DEBUG;
  571. }
  572. if(strcmp(linkTypeString, "optimized") == 0)
  573. {
  574. llt = cmTarget::OPTIMIZED;
  575. }
  576. }
  577. m_LinkLibraries.push_back( std::make_pair(*k,llt) );
  578. }
  579. }
  580. void cmTarget::InsertDependency( DependencyMap& depMap,
  581. const cmStdString& lib,
  582. const cmStdString& dep ) const
  583. {
  584. depMap[lib].push_back(dep);
  585. }
  586. void cmTarget::DeleteDependency( DependencyMap& depMap,
  587. const cmStdString& lib,
  588. const cmStdString& dep ) const
  589. {
  590. // Make sure there is an entry in the map for lib. If so, delete all
  591. // dependencies to dep. There may be repeated entries because of
  592. // external libraries that are specified multiple times.
  593. DependencyMap::iterator map_itr = depMap.find( lib );
  594. if( map_itr != depMap.end() )
  595. {
  596. DependencyList& depList = map_itr->second;
  597. DependencyList::iterator itr;
  598. while( (itr = std::find(depList.begin(), depList.end(), dep)) != depList.end() )
  599. {
  600. depList.erase( itr );
  601. }
  602. }
  603. }
  604. void cmTarget::Emit( const std::string& lib,
  605. const DependencyMap& dep_map,
  606. std::set<cmStdString>& emitted,
  607. std::set<cmStdString>& visited,
  608. std::vector<std::string>& link_line ) const
  609. {
  610. // It's already been emitted
  611. if( emitted.find(lib) != emitted.end() )
  612. return;
  613. // Emit the dependencies only if this library node hasn't been
  614. // visited before. If it has, then we have a cycle. The recursion
  615. // that got us here should take care of everything.
  616. if( visited.insert(lib).second )
  617. {
  618. if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
  619. {
  620. const DependencyList& dep_on = dep_map.find( lib )->second;
  621. DependencyList::const_reverse_iterator i;
  622. // To cater for recursive external libraries, we must emit
  623. // duplicates on this link line *unless* they were emitted by
  624. // some other node, in which case we assume that the recursion
  625. // was resolved then. We making the simplifying assumption that
  626. // any duplicates on a single link line are on purpose, and must
  627. // be preserved.
  628. // This variable will keep track of the libraries that were
  629. // emitted directory from the current node, and not from a
  630. // recursive call. This way, if we come across a library that
  631. // has already been emitted, we repeat it iff it has been
  632. // emitted here.
  633. std::set<cmStdString> emitted_here;
  634. for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
  635. {
  636. if( emitted_here.find(*i) != emitted_here.end() )
  637. {
  638. // a repeat. Must emit.
  639. emitted.insert(*i);
  640. link_line.push_back( *i );
  641. }
  642. else
  643. {
  644. // Emit only if no-one else has
  645. if( emitted.find(*i) == emitted.end() )
  646. {
  647. // emit dependencies
  648. Emit( *i, dep_map, emitted, visited, link_line );
  649. // emit self
  650. emitted.insert(*i);
  651. emitted_here.insert(*i);
  652. link_line.push_back( *i );
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. void cmTarget::GatherDependencies( const cmMakefile& mf,
  660. const std::string& lib,
  661. DependencyMap& dep_map )
  662. {
  663. // If the library is already in the dependency map, then it has
  664. // already been fully processed.
  665. if( dep_map.find(lib) != dep_map.end() )
  666. return;
  667. const char* deps = mf.GetDefinition( (lib+"_LIB_DEPENDS").c_str() );
  668. if( deps && strcmp(deps,"") != 0 )
  669. {
  670. // Make sure this library is in the map, even if it has an empty
  671. // set of dependencies. This distinguishes the case of explicitly
  672. // no dependencies with that of unspecified dependencies.
  673. dep_map[lib];
  674. // Parse the dependency information, which is simply a set of
  675. // libraries separated by ";". There is always a trailing ";".
  676. std::string depline = deps;
  677. std::string::size_type start = 0;
  678. std::string::size_type end;
  679. end = depline.find( ";", start );
  680. while( end != std::string::npos )
  681. {
  682. std::string l = depline.substr( start, end-start );
  683. if( l.size() != 0 )
  684. {
  685. InsertDependency( dep_map, lib, l );
  686. GatherDependencies( mf, l, dep_map );
  687. }
  688. start = end+1; // skip the ;
  689. end = depline.find( ";", start );
  690. }
  691. DeleteDependency( dep_map, lib, lib); // cannot depend on itself
  692. }
  693. }
  694. void cmTarget::SetProperty(const char* prop, const char* value)
  695. {
  696. if (!prop)
  697. {
  698. return;
  699. }
  700. if (!value)
  701. {
  702. value = "NOTFOUND";
  703. }
  704. m_Properties[prop] = value;
  705. }
  706. const char *cmTarget::GetProperty(const char* prop) const
  707. {
  708. std::map<cmStdString,cmStdString>::const_iterator i =
  709. m_Properties.find(prop);
  710. if (i != m_Properties.end())
  711. {
  712. return i->second.c_str();
  713. }
  714. return 0;
  715. }
  716. bool cmTarget::GetPropertyAsBool(const char* prop) const
  717. {
  718. std::map<cmStdString,cmStdString>::const_iterator i =
  719. m_Properties.find(prop);
  720. if (i != m_Properties.end())
  721. {
  722. return cmSystemTools::IsOn(i->second.c_str());
  723. }
  724. return false;
  725. }