cmTarget.cxx 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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 "cmLocalGenerator.h"
  17. #include "cmGlobalGenerator.h"
  18. #include <map>
  19. #include <set>
  20. #include <queue>
  21. #include <stdlib.h> // required for atof
  22. void cmTarget::SetType(TargetType type, const char* name)
  23. {
  24. m_Name = name;
  25. // only add dependency information for library targets
  26. m_TargetType = type;
  27. if(m_TargetType >= STATIC_LIBRARY && m_TargetType <= MODULE_LIBRARY) {
  28. m_RecordDependencies = true;
  29. } else {
  30. m_RecordDependencies = false;
  31. }
  32. }
  33. void cmTarget::TraceVSDependencies(std::string projFile,
  34. cmMakefile *makefile)
  35. {
  36. // get the classes from the source lists then add them to the groups
  37. std::vector<cmSourceFile*> & classes = this->GetSourceFiles();
  38. // use a deck to keep track of processed source files
  39. std::queue<std::string> srcFilesToProcess;
  40. std::set<cmStdString> srcFilesQueued;
  41. std::string name;
  42. std::vector<cmSourceFile*> newClasses;
  43. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  44. i != classes.end(); ++i)
  45. {
  46. name = (*i)->GetSourceName();
  47. if ((*i)->GetSourceExtension() != "rule")
  48. {
  49. name += ".";
  50. name += (*i)->GetSourceExtension();
  51. }
  52. srcFilesToProcess.push(name);
  53. srcFilesQueued.insert(name);
  54. // does this sourcefile have object depends on it?
  55. // If so then add them as well
  56. const char* additionalDeps = (*i)->GetProperty("OBJECT_DEPENDS");
  57. std::vector<std::string> depends = (*i)->GetDepends();
  58. if (additionalDeps || depends.size())
  59. {
  60. if(additionalDeps)
  61. {
  62. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  63. }
  64. for(std::vector<std::string>::iterator id = depends.begin();
  65. id != depends.end(); ++id)
  66. {
  67. // if there is a custom rule to generate that dependency
  68. // then add it to the list
  69. cmSourceFile* outsf =
  70. makefile->GetSourceFileWithOutput(id->c_str());
  71. // if a source file was found then add it
  72. if (outsf &&
  73. (std::find(classes.begin(),classes.end(),outsf) == classes.end()) &&
  74. (std::find(newClasses.begin(),newClasses.end(),outsf) == newClasses.end()))
  75. {
  76. // then add the source to this target and add it to the queue
  77. newClasses.push_back(outsf);
  78. name = outsf->GetSourceName();
  79. if (outsf->GetSourceExtension() != "rule")
  80. {
  81. name += ".";
  82. name += outsf->GetSourceExtension();
  83. }
  84. std::string temp =
  85. cmSystemTools::GetFilenamePath(outsf->GetFullPath());
  86. temp += "/";
  87. temp += name;
  88. // if it hasn't been processed
  89. if (srcFilesQueued.find(temp) == srcFilesQueued.end())
  90. {
  91. srcFilesToProcess.push(temp);
  92. srcFilesQueued.insert(temp);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. for(std::vector<cmSourceFile*>::const_iterator i = newClasses.begin();
  99. i != newClasses.end(); ++i)
  100. {
  101. classes.push_back(*i);
  102. }
  103. // add in the project file itself
  104. if (projFile.size())
  105. {
  106. srcFilesToProcess.push(projFile);
  107. srcFilesQueued.insert(projFile);
  108. }
  109. // add in the library depends for custom targets
  110. if (this->GetType() == cmTarget::UTILITY)
  111. {
  112. for (std::vector<cmCustomCommand>::iterator ic =
  113. this->GetPostBuildCommands().begin();
  114. ic != this->GetPostBuildCommands().end(); ++ic)
  115. {
  116. cmCustomCommand &c = *ic;
  117. for (std::vector<std::string>::const_iterator i = c.GetDepends().begin();
  118. i != c.GetDepends().end(); ++i)
  119. {
  120. srcFilesToProcess.push(*i);
  121. srcFilesQueued.insert(*i);
  122. }
  123. }
  124. }
  125. while (!srcFilesToProcess.empty())
  126. {
  127. // is this source the output of a custom command
  128. cmSourceFile* outsf =
  129. makefile->GetSourceFileWithOutput(srcFilesToProcess.front().c_str());
  130. if (outsf)
  131. {
  132. // is it not already in the target?
  133. if (std::find(classes.begin(),classes.end(),outsf) == classes.end())
  134. {
  135. // then add the source to this target and add it to the queue
  136. classes.push_back(outsf);
  137. name = outsf->GetSourceName();
  138. if (outsf->GetSourceExtension() != "rule")
  139. {
  140. name += ".";
  141. name += outsf->GetSourceExtension();
  142. }
  143. std::string temp =
  144. cmSystemTools::GetFilenamePath(outsf->GetFullPath());
  145. temp += "/";
  146. temp += name;
  147. // if it hasn't been processed
  148. if (srcFilesQueued.find(temp) == srcFilesQueued.end())
  149. {
  150. srcFilesToProcess.push(temp);
  151. srcFilesQueued.insert(temp);
  152. }
  153. }
  154. // add its dependencies to the list to check
  155. unsigned int i;
  156. for (i = 0; i < outsf->GetCustomCommand()->GetDepends().size(); ++i)
  157. {
  158. std::string dep = cmSystemTools::GetFilenameName(
  159. outsf->GetCustomCommand()->GetDepends()[i]);
  160. if (cmSystemTools::GetFilenameLastExtension(dep) == ".exe")
  161. {
  162. dep = cmSystemTools::GetFilenameWithoutLastExtension(dep);
  163. }
  164. // watch for target dependencies,
  165. std::string libPath = dep + "_CMAKE_PATH";
  166. const char* cacheValue = makefile->GetDefinition(libPath.c_str());
  167. if (cacheValue && *cacheValue)
  168. {
  169. // add the depend as a utility on the target
  170. this->AddUtility(dep.c_str());
  171. }
  172. else
  173. {
  174. if (srcFilesQueued.find(outsf->GetCustomCommand()->GetDepends()[i])
  175. == srcFilesQueued.end())
  176. {
  177. srcFilesToProcess.push(outsf->GetCustomCommand()->GetDepends()[i]);
  178. srcFilesQueued.insert(outsf->GetCustomCommand()->GetDepends()[i]);
  179. }
  180. }
  181. }
  182. }
  183. // finished with this SF move to the next
  184. srcFilesToProcess.pop();
  185. }
  186. }
  187. void cmTarget::GenerateSourceFilesFromSourceLists( cmMakefile &mf)
  188. {
  189. // this is only done for non install targets
  190. if ((this->m_TargetType == cmTarget::INSTALL_FILES)
  191. || (this->m_TargetType == cmTarget::INSTALL_PROGRAMS))
  192. {
  193. return;
  194. }
  195. // for each src lists add the classes
  196. for (std::vector<std::string>::const_iterator s = m_SourceLists.begin();
  197. s != m_SourceLists.end(); ++s)
  198. {
  199. int done = 0;
  200. // replace any variables
  201. std::string temps = *s;
  202. mf.ExpandVariablesInString(temps);
  203. // Next if one wasn't found then assume it is a single class
  204. // check to see if it is an existing source file
  205. if (!done)
  206. {
  207. cmSourceFile* sourceFile = mf.GetSource(temps.c_str());
  208. if ( sourceFile )
  209. {
  210. m_SourceFiles.push_back(sourceFile);
  211. done = 1;
  212. }
  213. }
  214. // if we still are not done, try to create the SourceFile structure
  215. if (!done)
  216. {
  217. cmSourceFile file;
  218. file.SetProperty("ABSTRACT","0");
  219. file.SetName(temps.c_str(), mf.GetCurrentDirectory(),
  220. mf.GetSourceExtensions(),
  221. mf.GetHeaderExtensions());
  222. m_SourceFiles.push_back(mf.AddSource(file));
  223. }
  224. }
  225. // expand any link library variables whle we are at it
  226. LinkLibraries::iterator p = m_LinkLibraries.begin();
  227. for (;p != m_LinkLibraries.end(); ++p)
  228. {
  229. mf.ExpandVariablesInString(p->first);
  230. }
  231. }
  232. void cmTarget::MergeLinkLibraries( cmMakefile& mf,
  233. const char *selfname,
  234. const LinkLibraries& libs )
  235. {
  236. // Only add on libraries we haven't added on before.
  237. // Assumption: the global link libraries could only grow, never shrink
  238. LinkLibraries::const_iterator i = libs.begin();
  239. i += m_PrevLinkedLibraries.size();
  240. for( ; i != libs.end(); ++i )
  241. {
  242. // We call this so that the dependencies get written to the cache
  243. this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
  244. }
  245. m_PrevLinkedLibraries = libs;
  246. }
  247. void cmTarget::AddLinkDirectory(const char* d)
  248. {
  249. // Make sure we don't add unnecessary search directories.
  250. if( std::find( m_LinkDirectories.begin(), m_LinkDirectories.end(), d )
  251. == m_LinkDirectories.end() )
  252. m_LinkDirectories.push_back( d );
  253. }
  254. void cmTarget::ClearDependencyInformation( cmMakefile& mf, const char* target )
  255. {
  256. // Clear the dependencies. The cache variable must exist iff we are
  257. // recording dependency information for this target.
  258. std::string depname = target;
  259. depname += "_LIB_DEPENDS";
  260. if (m_RecordDependencies)
  261. {
  262. mf.AddCacheDefinition(depname.c_str(), "",
  263. "Dependencies for target", cmCacheManager::STATIC);
  264. }
  265. else
  266. {
  267. if (mf.GetDefinition( depname.c_str() ))
  268. {
  269. std::string message = "Target ";
  270. message += target;
  271. message += " has dependency information when it shouldn't.\n";
  272. message += "Your cache is probably stale. Please remove the entry\n ";
  273. message += depname;
  274. message += "\nfrom the cache.";
  275. cmSystemTools::Error( message.c_str() );
  276. }
  277. }
  278. }
  279. void cmTarget::AddLinkLibrary(const std::string& lib,
  280. LinkLibraryType llt)
  281. {
  282. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  283. }
  284. void cmTarget::AddLinkLibrary(cmMakefile& mf,
  285. const char *target, const char* lib,
  286. LinkLibraryType llt)
  287. {
  288. // Never add a self dependency, even if the user asks for it.
  289. if(strcmp( target, lib ) == 0)
  290. {
  291. return;
  292. }
  293. m_LinkLibraries.push_back( std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt) );
  294. if(llt != cmTarget::GENERAL)
  295. {
  296. // Store the library's link type in the cache. If it is a
  297. // conflicting type then assume it is always used. This is the
  298. // case when the user sets the cache entries for debug and
  299. // optimized versions of the library to the same value.
  300. std::string linkTypeName = lib;
  301. linkTypeName += "_LINK_TYPE";
  302. switch(llt)
  303. {
  304. case cmTarget::DEBUG:
  305. {
  306. const char* def = mf.GetDefinition(linkTypeName.c_str());
  307. if(!def || strcmp(def, "debug") == 0)
  308. {
  309. mf.AddCacheDefinition(linkTypeName.c_str(),
  310. "debug", "Library is used for debug links only",
  311. cmCacheManager::STATIC);
  312. }
  313. else
  314. {
  315. mf.AddCacheDefinition(linkTypeName.c_str(),
  316. "general", "Library is used for both debug and optimized links",
  317. cmCacheManager::STATIC);
  318. }
  319. }
  320. break;
  321. case cmTarget::OPTIMIZED:
  322. {
  323. const char* def = mf.GetDefinition(linkTypeName.c_str());
  324. if(!def || strcmp(def, "optimized") == 0)
  325. {
  326. mf.AddCacheDefinition(linkTypeName.c_str(),
  327. "optimized", "Library is used for debug links only",
  328. cmCacheManager::STATIC);
  329. }
  330. else
  331. {
  332. mf.AddCacheDefinition(linkTypeName.c_str(),
  333. "general", "Library is used for both debug and optimized links",
  334. cmCacheManager::STATIC);
  335. }
  336. }
  337. break;
  338. case cmTarget::GENERAL:
  339. break;
  340. }
  341. }
  342. // Add the explicit dependency information for this target. This is
  343. // simply a set of libraries separated by ";". There should always
  344. // be a trailing ";". These library names are not canonical, in that
  345. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  346. // We shouldn't remove duplicates here because external libraries
  347. // may be purposefully duplicated to handle recursive dependencies,
  348. // and we removing one instance will break the link line. Duplicates
  349. // will be appropriately eliminated at emit time.
  350. if(m_RecordDependencies)
  351. {
  352. std::string targetEntry = target;
  353. targetEntry += "_LIB_DEPENDS";
  354. std::string dependencies;
  355. const char* old_val = mf.GetDefinition( targetEntry.c_str() );
  356. if( old_val )
  357. {
  358. dependencies += old_val;
  359. }
  360. dependencies += lib;
  361. dependencies += ";";
  362. mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
  363. "Dependencies for the target",
  364. cmCacheManager::STATIC );
  365. }
  366. }
  367. void
  368. cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
  369. {
  370. // There are two key parts of the dependency analysis: (1)
  371. // determining the libraries in the link line, and (2) constructing
  372. // the dependency graph for those libraries.
  373. //
  374. // The latter is done using the cache entries that record the
  375. // dependencies of each library.
  376. //
  377. // The former is a more thorny issue, since it is not clear how to
  378. // determine if two libraries listed on the link line refer to the a
  379. // single library or not. For example, consider the link "libraries"
  380. // /usr/lib/libtiff.so -ltiff
  381. // Is this one library or two? The solution implemented here is the
  382. // simplest (and probably the only practical) one: two libraries are
  383. // the same if their "link strings" are identical. Thus, the two
  384. // libraries above are considered distinct. This also means that for
  385. // dependency analysis to be effective, the CMake user must specify
  386. // libraries build by his project without using any linker flags or
  387. // file extensions. That is,
  388. // LINK_LIBRARIES( One Two )
  389. // instead of
  390. // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
  391. // The former is probably what most users would do, but it never
  392. // hurts to document the assumptions. :-) Therefore, in the analysis
  393. // code, the "canonical name" of a library is simply its name as
  394. // given to a LINK_LIBRARIES command.
  395. //
  396. // Also, we will leave the original link line intact; we will just add any
  397. // dependencies that were missing.
  398. //
  399. // There is a problem with recursive external libraries
  400. // (i.e. libraries with no dependency information that are
  401. // recursively dependent). We must make sure that the we emit one of
  402. // the libraries twice to satisfy the recursion, but we shouldn't
  403. // emit it more times than necessary. In particular, we must make
  404. // sure that handling this improbable case doesn't cost us when
  405. // dealing with the common case of non-recursive libraries. The
  406. // solution is to assume that the recursion is satisfied at one node
  407. // of the dependency tree. To illustrate, assume libA and libB are
  408. // extrenal and mutually dependent. Suppose libX depends on
  409. // libA, and libY on libA and libX. Then
  410. // TARGET_LINK_LIBRARIES( Y X A B A )
  411. // TARGET_LINK_LIBRARIES( X A B A )
  412. // TARGET_LINK_LIBRARIES( Exec Y )
  413. // would result in "-lY -lX -lA -lB -lA". This is the correct way to
  414. // specify the dependencies, since the mutual dependency of A and B
  415. // is resolved *every time libA is specified*.
  416. //
  417. // Something like
  418. // TARGET_LINK_LIBRARIES( Y X A B A )
  419. // TARGET_LINK_LIBRARIES( X A B )
  420. // TARGET_LINK_LIBRARIES( Exec Y )
  421. // would result in "-lY -lX -lA -lB", and the mutual dependency
  422. // information is lost. This is because in some case (Y), the mutual
  423. // dependency of A and B is listed, while in another other case (X),
  424. // it is not. Depending on which line actually emits A, the mutual
  425. // dependency may or may not be on the final link line. We can't
  426. // handle this pathalogical case cleanly without emitting extra
  427. // libraries for the normal cases. Besides, the dependency
  428. // information for X is wrong anyway: if we build an executable
  429. // depending on X alone, we would not have the mutual dependency on
  430. // A and B resolved.
  431. //
  432. // IMPROVEMENTS:
  433. // -- The current algorithm will not always pick the "optimal" link line
  434. // when recursive dependencies are present. It will instead break the
  435. // cycles at an aribtrary point. The majority of projects won't have
  436. // cyclic dependencies, so this is probably not a big deal. Note that
  437. // the link line is always correct, just not necessary optimal.
  438. typedef std::vector< std::string > LinkLine;
  439. // The dependency map.
  440. DependencyMap dep_map;
  441. // If LIBRARY_OUTPUT_PATH is not set, then we must add search paths
  442. // for all the new libraries added by the dependency analysis.
  443. const char* libOutPath = mf.GetDefinition("LIBRARY_OUTPUT_PATH");
  444. bool addLibDirs = (libOutPath==0 || strcmp(libOutPath,"")==0);
  445. // 1. Build the dependency graph
  446. //
  447. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  448. lib != m_LinkLibraries.rend(); ++lib)
  449. {
  450. this->GatherDependencies( mf, lib->first, dep_map );
  451. }
  452. // 2. Remove any dependencies that are already satisfied in the original
  453. // link line.
  454. //
  455. for(LinkLibraries::iterator lib = m_LinkLibraries.begin();
  456. lib != m_LinkLibraries.end(); ++lib)
  457. {
  458. for( LinkLibraries::iterator lib2 = lib;
  459. lib2 != m_LinkLibraries.end(); ++lib2)
  460. {
  461. DeleteDependency( dep_map, lib->first, lib2->first );
  462. }
  463. }
  464. // 3. Create the new link line by simply emitting any dependencies that are
  465. // missing. Start from the back and keep adding.
  466. //
  467. std::set<cmStdString> done, visited;
  468. std::vector<std::string> newLinkLibraries;
  469. for(LinkLibraries::reverse_iterator lib = m_LinkLibraries.rbegin();
  470. lib != m_LinkLibraries.rend(); ++lib)
  471. {
  472. // skip zero size library entries, this may happen
  473. // if a variable expands to nothing.
  474. if (lib->first.size() != 0)
  475. {
  476. Emit( lib->first, dep_map, done, visited, newLinkLibraries );
  477. }
  478. }
  479. // 4. Add the new libraries to the link line.
  480. //
  481. for( std::vector<std::string>::reverse_iterator k = newLinkLibraries.rbegin();
  482. k != newLinkLibraries.rend(); ++k )
  483. {
  484. if( addLibDirs )
  485. {
  486. // who the hell knows what this is, I think that K contains the
  487. // name of a library but ... Ken
  488. // k contains the same stuff that are on the LINK_LIBRARIES
  489. // commands. Normally, they would just be library names. -- Amitha.
  490. std::string libPathStr = *k + "_CMAKE_PATH";
  491. const char* libpath = mf.GetDefinition( libPathStr.c_str() );
  492. if( libpath )
  493. {
  494. // Don't add a link directory that is already present.
  495. if(std::find(m_LinkDirectories.begin(),
  496. m_LinkDirectories.end(), libpath) == m_LinkDirectories.end())
  497. {
  498. m_LinkDirectories.push_back(libpath);
  499. }
  500. }
  501. }
  502. std::string linkType = *k;
  503. linkType += "_LINK_TYPE";
  504. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  505. const char* linkTypeString = mf.GetDefinition( linkType.c_str() );
  506. if(linkTypeString)
  507. {
  508. if(strcmp(linkTypeString, "debug") == 0)
  509. {
  510. llt = cmTarget::DEBUG;
  511. }
  512. if(strcmp(linkTypeString, "optimized") == 0)
  513. {
  514. llt = cmTarget::OPTIMIZED;
  515. }
  516. }
  517. m_LinkLibraries.push_back( std::make_pair(*k,llt) );
  518. }
  519. }
  520. void cmTarget::InsertDependency( DependencyMap& depMap,
  521. const cmStdString& lib,
  522. const cmStdString& dep )
  523. {
  524. depMap[lib].push_back(dep);
  525. }
  526. void cmTarget::DeleteDependency( DependencyMap& depMap,
  527. const cmStdString& lib,
  528. const cmStdString& dep )
  529. {
  530. // Make sure there is an entry in the map for lib. If so, delete all
  531. // dependencies to dep. There may be repeated entries because of
  532. // external libraries that are specified multiple times.
  533. DependencyMap::iterator map_itr = depMap.find( lib );
  534. if( map_itr != depMap.end() )
  535. {
  536. DependencyList& depList = map_itr->second;
  537. DependencyList::iterator itr;
  538. while( (itr = std::find(depList.begin(), depList.end(), dep)) != depList.end() )
  539. {
  540. depList.erase( itr );
  541. }
  542. }
  543. }
  544. void cmTarget::Emit( const std::string& lib,
  545. const DependencyMap& dep_map,
  546. std::set<cmStdString>& emitted,
  547. std::set<cmStdString>& visited,
  548. std::vector<std::string>& link_line )
  549. {
  550. // It's already been emitted
  551. if( emitted.find(lib) != emitted.end() )
  552. return;
  553. // Emit the dependencies only if this library node hasn't been
  554. // visited before. If it has, then we have a cycle. The recursion
  555. // that got us here should take care of everything.
  556. if( visited.insert(lib).second )
  557. {
  558. if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
  559. {
  560. const DependencyList& dep_on = dep_map.find( lib )->second;
  561. DependencyList::const_reverse_iterator i;
  562. // To cater for recursive external libraries, we must emit
  563. // duplicates on this link line *unless* they were emitted by
  564. // some other node, in which case we assume that the recursion
  565. // was resolved then. We making the simplifying assumption that
  566. // any duplicates on a single link line are on purpose, and must
  567. // be preserved.
  568. // This variable will keep track of the libraries that were
  569. // emitted directory from the current node, and not from a
  570. // recursive call. This way, if we come across a library that
  571. // has already been emitted, we repeat it iff it has been
  572. // emitted here.
  573. std::set<cmStdString> emitted_here;
  574. for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
  575. {
  576. if( emitted_here.find(*i) != emitted_here.end() )
  577. {
  578. // a repeat. Must emit.
  579. emitted.insert(*i);
  580. link_line.push_back( *i );
  581. }
  582. else
  583. {
  584. // Emit only if no-one else has
  585. if( emitted.find(*i) == emitted.end() )
  586. {
  587. // emit dependencies
  588. Emit( *i, dep_map, emitted, visited, link_line );
  589. // emit self
  590. emitted.insert(*i);
  591. emitted_here.insert(*i);
  592. link_line.push_back( *i );
  593. }
  594. }
  595. }
  596. }
  597. }
  598. }
  599. void cmTarget::GatherDependencies( const cmMakefile& mf,
  600. const std::string& lib,
  601. DependencyMap& dep_map )
  602. {
  603. // If the library is already in the dependency map, then it has
  604. // already been fully processed.
  605. if( dep_map.find(lib) != dep_map.end() )
  606. return;
  607. const char* deps = mf.GetDefinition( (lib+"_LIB_DEPENDS").c_str() );
  608. if( deps && strcmp(deps,"") != 0 )
  609. {
  610. // Make sure this library is in the map, even if it has an empty
  611. // set of dependencies. This distinguishes the case of explicitly
  612. // no dependencies with that of unspecified dependencies.
  613. dep_map[lib];
  614. // Parse the dependency information, which is simply a set of
  615. // libraries separated by ";". There is always a trailing ";".
  616. std::string depline = deps;
  617. std::string::size_type start = 0;
  618. std::string::size_type end;
  619. end = depline.find( ";", start );
  620. while( end != std::string::npos )
  621. {
  622. std::string l = depline.substr( start, end-start );
  623. if( l.size() != 0 )
  624. {
  625. InsertDependency( dep_map, lib, l );
  626. GatherDependencies( mf, l, dep_map );
  627. }
  628. start = end+1; // skip the ;
  629. end = depline.find( ";", start );
  630. }
  631. DeleteDependency( dep_map, lib, lib); // cannot depend on itself
  632. }
  633. }
  634. void cmTarget::SetProperty(const char* prop, const char* value)
  635. {
  636. if (!prop)
  637. {
  638. return;
  639. }
  640. if (!value)
  641. {
  642. value = "NOTFOUND";
  643. }
  644. m_Properties[prop] = value;
  645. }
  646. void cmTarget::UpdateLocation()
  647. {
  648. // make sure we have a makefile
  649. if (!m_Makefile)
  650. {
  651. return;
  652. }
  653. // set the LOCATION property of the target
  654. std::string target_location;
  655. switch( this->GetType() )
  656. {
  657. case cmTarget::STATIC_LIBRARY:
  658. case cmTarget::MODULE_LIBRARY:
  659. case cmTarget::SHARED_LIBRARY:
  660. target_location =
  661. m_Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  662. break;
  663. case cmTarget::EXECUTABLE:
  664. target_location =
  665. m_Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  666. break;
  667. default:
  668. return;
  669. }
  670. if ( target_location.size() == 0 )
  671. {
  672. target_location += m_Makefile->GetStartOutputDirectory();
  673. }
  674. if ( target_location.size() > 0 )
  675. {
  676. target_location += "/";
  677. }
  678. const char* cfgid = m_Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  679. if ( cfgid && strcmp(cfgid, ".") != 0 )
  680. {
  681. target_location += cfgid;
  682. target_location += "/";
  683. }
  684. target_location += this->GetFullName(m_Makefile);
  685. this->SetProperty("LOCATION",target_location.c_str());
  686. }
  687. const char *cmTarget::GetProperty(const char* prop)
  688. {
  689. // watch for special "computed" properties that are dependent on other
  690. // properties or variables, always recompute them
  691. if (!strcmp(prop,"LOCATION"))
  692. {
  693. this->UpdateLocation();
  694. }
  695. // the type property returns what type the target is
  696. if (!strcmp(prop,"TYPE"))
  697. {
  698. switch( this->GetType() )
  699. {
  700. case cmTarget::STATIC_LIBRARY:
  701. return "STATIC_LIBRARY";
  702. break;
  703. case cmTarget::MODULE_LIBRARY:
  704. return "MODULE_LIBRARY";
  705. break;
  706. case cmTarget::SHARED_LIBRARY:
  707. return "SHARED_LIBRARY";
  708. break;
  709. case cmTarget::EXECUTABLE:
  710. return "EXECUTABLE";
  711. break;
  712. case cmTarget::UTILITY:
  713. return "UTILITY";
  714. break;
  715. case cmTarget::INSTALL_FILES:
  716. return "INSTALL_FILES";
  717. break;
  718. case cmTarget::INSTALL_PROGRAMS:
  719. return "INSTALL_PROGRAMS";
  720. break;
  721. }
  722. return 0;
  723. }
  724. std::map<cmStdString,cmStdString>::const_iterator i =
  725. m_Properties.find(prop);
  726. if (i != m_Properties.end())
  727. {
  728. return i->second.c_str();
  729. }
  730. return 0;
  731. }
  732. bool cmTarget::GetPropertyAsBool(const char* prop)
  733. {
  734. std::map<cmStdString,cmStdString>::const_iterator i =
  735. m_Properties.find(prop);
  736. if (i != m_Properties.end())
  737. {
  738. return cmSystemTools::IsOn(i->second.c_str());
  739. }
  740. return false;
  741. }
  742. const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
  743. {
  744. if(this->GetProperty("HAS_CXX"))
  745. {
  746. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
  747. }
  748. const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
  749. if(linkerLang)
  750. {
  751. return linkerLang;
  752. }
  753. std::set<cmStdString> languages;
  754. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  755. i != m_SourceFiles.end(); ++i)
  756. {
  757. const char* lang =
  758. gg->GetLanguageFromExtension((*i)->GetSourceExtension().c_str());
  759. if(lang)
  760. {
  761. languages.insert(lang);
  762. }
  763. }
  764. if(languages.size() == 0)
  765. {
  766. return 0;
  767. }
  768. if(languages.size() == 1)
  769. {
  770. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", languages.begin()->c_str());
  771. return this->GetProperty("LINKER_LANGUAGE");
  772. }
  773. const char* prefLang = 0;
  774. for(std::set<cmStdString>::const_iterator s = languages.begin();
  775. s != languages.end(); ++s)
  776. {
  777. const char* lpref = gg->GetLinkerPreference(s->c_str());
  778. if(lpref[0] == 'P')
  779. {
  780. if(prefLang && !(*s == prefLang))
  781. {
  782. std::string m = "Error Target: ";
  783. m += m_Name + " Contains more than one Prefered language: ";
  784. m += *s;
  785. m += " and ";
  786. m += prefLang;
  787. m += "\nYou must set the LINKER_LANGUAGE property for this target.";
  788. cmSystemTools::Error(m.c_str());
  789. }
  790. else
  791. {
  792. prefLang = s->c_str();
  793. }
  794. }
  795. }
  796. if(!prefLang)
  797. {
  798. prefLang = languages.begin()->c_str();
  799. }
  800. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", prefLang);
  801. return this->GetProperty("LINKER_LANGUAGE");
  802. }
  803. const char* cmTarget::GetCreateRuleVariable()
  804. {
  805. switch(this->GetType())
  806. {
  807. case cmTarget::STATIC_LIBRARY:
  808. return "_CREATE_STATIC_LIBRARY";
  809. case cmTarget::SHARED_LIBRARY:
  810. return "_CREATE_SHARED_LIBRARY";
  811. case cmTarget::MODULE_LIBRARY:
  812. return "_CREATE_SHARED_MODULE";
  813. case cmTarget::EXECUTABLE:
  814. return "_LINK_EXECUTABLE";
  815. case cmTarget::UTILITY:
  816. case cmTarget::INSTALL_FILES:
  817. case cmTarget::INSTALL_PROGRAMS:
  818. break;
  819. }
  820. return "";
  821. }
  822. const char* cmTarget::GetSuffixVariable()
  823. {
  824. return this->GetSuffixVariableInternal(this->GetType());
  825. }
  826. const char* cmTarget::GetSuffixVariableInternal(TargetType type)
  827. {
  828. switch(type)
  829. {
  830. case cmTarget::STATIC_LIBRARY:
  831. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  832. case cmTarget::SHARED_LIBRARY:
  833. return "CMAKE_SHARED_LIBRARY_SUFFIX";
  834. case cmTarget::MODULE_LIBRARY:
  835. return "CMAKE_SHARED_MODULE_SUFFIX";
  836. case cmTarget::EXECUTABLE:
  837. return cmSystemTools::GetExecutableExtension();
  838. case cmTarget::UTILITY:
  839. case cmTarget::INSTALL_FILES:
  840. case cmTarget::INSTALL_PROGRAMS:
  841. break;
  842. }
  843. return "";
  844. }
  845. const char* cmTarget::GetPrefixVariable()
  846. {
  847. return this->GetPrefixVariableInternal(this->GetType());
  848. }
  849. const char* cmTarget::GetPrefixVariableInternal(TargetType type)
  850. {
  851. switch(type)
  852. {
  853. case cmTarget::STATIC_LIBRARY:
  854. return "CMAKE_STATIC_LIBRARY_PREFIX";
  855. case cmTarget::SHARED_LIBRARY:
  856. return "CMAKE_SHARED_LIBRARY_PREFIX";
  857. case cmTarget::MODULE_LIBRARY:
  858. return "CMAKE_SHARED_MODULE_PREFIX";
  859. case cmTarget::EXECUTABLE:
  860. return cmSystemTools::GetExecutableExtension();
  861. case cmTarget::UTILITY:
  862. case cmTarget::INSTALL_FILES:
  863. case cmTarget::INSTALL_PROGRAMS:
  864. break;
  865. }
  866. return "";
  867. }
  868. std::string cmTarget::GetFullName(cmMakefile* mf)
  869. {
  870. return this->GetFullNameInternal(mf, this->GetType());
  871. }
  872. std::string cmTarget::GetFullNameInternal(cmMakefile* mf,
  873. TargetType type)
  874. {
  875. const char* targetPrefix = this->GetProperty("PREFIX");
  876. const char* targetSuffix = this->GetProperty("SUFFIX");
  877. if(!targetSuffix && type == cmTarget::EXECUTABLE)
  878. {
  879. targetSuffix = cmSystemTools::GetExecutableExtension();
  880. }
  881. const char* prefixVar = this->GetPrefixVariableInternal(type);
  882. const char* suffixVar = this->GetSuffixVariableInternal(type);
  883. const char* ll =
  884. this->GetLinkerLanguage(
  885. mf->GetLocalGenerator()->GetGlobalGenerator());
  886. // first try language specific suffix
  887. if(ll)
  888. {
  889. if(!targetSuffix)
  890. {
  891. std::string langSuff = suffixVar + std::string("_") + ll;
  892. targetSuffix = mf->GetDefinition(langSuff.c_str());
  893. }
  894. if(!targetPrefix)
  895. {
  896. std::string langPrefix = prefixVar + std::string("_") + ll;
  897. targetPrefix = mf->GetDefinition(langPrefix.c_str());
  898. }
  899. }
  900. // if there is no prefix on the target use the cmake definition
  901. if(!targetPrefix && prefixVar)
  902. {
  903. targetPrefix = mf->GetSafeDefinition(prefixVar);
  904. }
  905. // if there is no suffix on the target use the cmake definition
  906. if(!targetSuffix && suffixVar)
  907. {
  908. targetSuffix = mf->GetSafeDefinition(suffixVar);
  909. }
  910. // Begin the final name with the prefix.
  911. std::string name = targetPrefix?targetPrefix:"";
  912. // Append the target name or property-specified name. Support this
  913. // only for executable targets.
  914. const char* outname = this->GetProperty("OUTPUT_NAME");
  915. if(outname && type == cmTarget::EXECUTABLE)
  916. {
  917. name += outname;
  918. }
  919. else
  920. {
  921. name += this->GetName();
  922. }
  923. // Append the suffix.
  924. name += targetSuffix?targetSuffix:"";
  925. // Return the final name.
  926. return name;
  927. }
  928. std::string cmTarget::GetBaseName(cmMakefile* mf)
  929. {
  930. return this->GetBaseNameInternal(mf, this->GetType());
  931. }
  932. std::string
  933. cmTarget::GetBaseNameInternal(cmMakefile* mf, TargetType type)
  934. {
  935. std::string pathPrefix = "";
  936. #ifdef __APPLE__
  937. if(this->GetPropertyAsBool("MACOSX_BUNDLE"))
  938. {
  939. pathPrefix = this->GetName();
  940. pathPrefix += ".app/Contents/MacOS/";
  941. }
  942. #endif
  943. const char* targetPrefix = this->GetProperty("PREFIX");
  944. const char* prefixVar = this->GetPrefixVariableInternal(type);
  945. // if there is no prefix on the target use the cmake definition
  946. if(!targetPrefix && prefixVar)
  947. {
  948. // first check for a language specific suffix var
  949. const char* ll =
  950. this->GetLinkerLanguage(
  951. mf->GetLocalGenerator()->GetGlobalGenerator());
  952. if(ll)
  953. {
  954. std::string langPrefix = prefixVar + std::string("_") + ll;
  955. targetPrefix = mf->GetDefinition(langPrefix.c_str());
  956. }
  957. // if there not a language specific suffix then use the general one
  958. if(!targetPrefix)
  959. {
  960. targetPrefix = mf->GetSafeDefinition(prefixVar);
  961. }
  962. }
  963. std::string name = pathPrefix;
  964. name += targetPrefix?targetPrefix:"";
  965. name += this->GetName();
  966. return name;
  967. }
  968. void cmTarget::GetLibraryNames(cmMakefile* mf,
  969. std::string& name,
  970. std::string& soName,
  971. std::string& realName,
  972. std::string& baseName)
  973. {
  974. // Get the names based on the real type of the library.
  975. this->GetLibraryNamesInternal(mf, name, soName, realName, this->GetType());
  976. // The library name without extension.
  977. baseName = this->GetBaseName(mf);
  978. }
  979. void cmTarget::GetLibraryCleanNames(cmMakefile* mf,
  980. std::string& staticName,
  981. std::string& sharedName,
  982. std::string& sharedSOName,
  983. std::string& sharedRealName)
  984. {
  985. // Get the name as if this were a static library.
  986. std::string soName;
  987. std::string realName;
  988. this->GetLibraryNamesInternal(mf, staticName, soName, realName,
  989. cmTarget::STATIC_LIBRARY);
  990. // Get the names as if this were a shared library.
  991. if(this->GetType() == cmTarget::STATIC_LIBRARY)
  992. {
  993. // Since the real type is static then the user either specified
  994. // STATIC or did not specify a type. In the former case the
  995. // shared library will never be present. In the latter case the
  996. // type will never be MODULE. Either way the only names that
  997. // might have to be cleaned are the shared library names.
  998. this->GetLibraryNamesInternal(mf, sharedName, sharedSOName,
  999. sharedRealName, cmTarget::SHARED_LIBRARY);
  1000. }
  1001. else
  1002. {
  1003. // Use the name of the real type of the library (shared or module).
  1004. this->GetLibraryNamesInternal(mf, sharedName, sharedSOName,
  1005. sharedRealName, this->GetType());
  1006. }
  1007. }
  1008. void cmTarget::GetLibraryNamesInternal(cmMakefile* mf,
  1009. std::string& name,
  1010. std::string& soName,
  1011. std::string& realName,
  1012. TargetType type)
  1013. {
  1014. // Construct the name of the soname flag variable for this language.
  1015. const char* ll =
  1016. this->GetLinkerLanguage(
  1017. mf->GetLocalGenerator()->GetGlobalGenerator());
  1018. std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
  1019. if(ll)
  1020. {
  1021. sonameFlag += "_";
  1022. sonameFlag += ll;
  1023. }
  1024. sonameFlag += "_FLAG";
  1025. // Check for library version properties.
  1026. const char* version = this->GetProperty("VERSION");
  1027. const char* soversion = this->GetProperty("SOVERSION");
  1028. if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
  1029. !mf->GetDefinition(sonameFlag.c_str()))
  1030. {
  1031. // Versioning is supported only for shared libraries and modules,
  1032. // and then only when the platform supports an soname flag.
  1033. version = 0;
  1034. soversion = 0;
  1035. }
  1036. if(version && !soversion)
  1037. {
  1038. // The soversion must be set if the library version is set. Use
  1039. // the library version as the soversion.
  1040. soversion = version;
  1041. }
  1042. // The library name.
  1043. name = this->GetFullNameInternal(mf, type);
  1044. // The library's soname.
  1045. soName = name;
  1046. if(soversion)
  1047. {
  1048. soName += ".";
  1049. soName += soversion;
  1050. }
  1051. // The library's real name on disk.
  1052. realName = name;
  1053. if(version)
  1054. {
  1055. realName += ".";
  1056. realName += version;
  1057. }
  1058. else if(soversion)
  1059. {
  1060. realName += ".";
  1061. realName += soversion;
  1062. }
  1063. }
  1064. void cmTarget::GetExecutableNames(cmMakefile* mf,
  1065. std::string& name,
  1066. std::string& realName)
  1067. {
  1068. // Get the names based on the real type of the executable.
  1069. this->GetExecutableNamesInternal(mf, name, realName, this->GetType());
  1070. }
  1071. void cmTarget::GetExecutableCleanNames(cmMakefile* mf,
  1072. std::string& name,
  1073. std::string& realName)
  1074. {
  1075. // Get the name and versioned name of this executable.
  1076. this->GetExecutableNamesInternal(mf, name, realName, cmTarget::EXECUTABLE);
  1077. }
  1078. void cmTarget::GetExecutableNamesInternal(cmMakefile* mf,
  1079. std::string& name,
  1080. std::string& realName,
  1081. TargetType type)
  1082. {
  1083. // This versioning is supported only for executables and then only
  1084. // when the platform supports symbolic links.
  1085. #if defined(_WIN32) && !defined(__CYGWIN__)
  1086. const char* version = 0;
  1087. #else
  1088. // Check for executable version properties.
  1089. const char* version = this->GetProperty("VERSION");
  1090. if(type != cmTarget::EXECUTABLE)
  1091. {
  1092. version = 0;
  1093. }
  1094. #endif
  1095. // The executable name.
  1096. name = this->GetFullNameInternal(mf, type);
  1097. // The executable's real name on disk.
  1098. realName = name;
  1099. if(version)
  1100. {
  1101. realName += "-";
  1102. realName += version;
  1103. }
  1104. }