cmTarget.cxx 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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. //----------------------------------------------------------------------------
  23. cmTarget::cmTarget()
  24. {
  25. this->Makefile = 0;
  26. this->LinkLibrariesAnalyzed = false;
  27. this->LinkDirectoriesComputed = false;
  28. this->HaveInstallRule = false;
  29. }
  30. void cmTarget::SetType(TargetType type, const char* name)
  31. {
  32. this->Name = name;
  33. // only add dependency information for library targets
  34. this->TargetTypeValue = type;
  35. if(this->TargetTypeValue >= STATIC_LIBRARY
  36. && this->TargetTypeValue <= MODULE_LIBRARY)
  37. {
  38. this->RecordDependencies = true;
  39. }
  40. else
  41. {
  42. this->RecordDependencies = false;
  43. }
  44. }
  45. //----------------------------------------------------------------------------
  46. void cmTarget::SetMakefile(cmMakefile* mf)
  47. {
  48. // Set our makefile.
  49. this->Makefile = mf;
  50. // Setup default property values.
  51. this->SetPropertyDefault("INSTALL_NAME_DIR", "");
  52. this->SetPropertyDefault("INSTALL_RPATH", "");
  53. this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
  54. this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
  55. // Collect the set of configuration types.
  56. std::vector<std::string> configNames;
  57. if(const char* configurationTypes =
  58. mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  59. {
  60. cmSystemTools::ExpandListArgument(configurationTypes, configNames);
  61. }
  62. else if(const char* buildType = mf->GetDefinition("CMAKE_BUILD_TYPE"))
  63. {
  64. if(*buildType)
  65. {
  66. configNames.push_back(buildType);
  67. }
  68. }
  69. // Setup per-configuration property default values.
  70. for(std::vector<std::string>::iterator ci = configNames.begin();
  71. ci != configNames.end(); ++ci)
  72. {
  73. // Initialize per-configuration name postfix property from the
  74. // variable only for non-executable targets. This preserves
  75. // compatibility with previous CMake versions in which executables
  76. // did not support this variable. Projects may still specify the
  77. // property directly. TODO: Make this depend on backwards
  78. // compatibility setting.
  79. if(this->TargetTypeValue != cmTarget::EXECUTABLE)
  80. {
  81. std::string property = cmSystemTools::UpperCase(*ci);
  82. property += "_POSTFIX";
  83. this->SetPropertyDefault(property.c_str(), 0);
  84. }
  85. }
  86. }
  87. void cmTarget::TraceVSDependencies(std::string projFile,
  88. cmMakefile *makefile)
  89. {
  90. // get the classes from the source lists then add them to the groups
  91. std::vector<cmSourceFile*> & classes = this->GetSourceFiles();
  92. // use a deck to keep track of processed source files
  93. std::queue<std::string> srcFilesToProcess;
  94. std::set<cmStdString> srcFilesQueued;
  95. std::string name;
  96. std::vector<cmSourceFile*> newClasses;
  97. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  98. i != classes.end(); ++i)
  99. {
  100. name = (*i)->GetSourceName();
  101. if ((*i)->GetSourceExtension() != "rule")
  102. {
  103. name += ".";
  104. name += (*i)->GetSourceExtension();
  105. }
  106. srcFilesToProcess.push(name);
  107. srcFilesQueued.insert(name);
  108. // does this sourcefile have object depends on it?
  109. // If so then add them as well
  110. const char* additionalDeps = (*i)->GetProperty("OBJECT_DEPENDS");
  111. std::vector<std::string> depends = (*i)->GetDepends();
  112. if (additionalDeps || depends.size())
  113. {
  114. if(additionalDeps)
  115. {
  116. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  117. }
  118. for(std::vector<std::string>::iterator id = depends.begin();
  119. id != depends.end(); ++id)
  120. {
  121. // if there is a custom rule to generate that dependency
  122. // then add it to the list
  123. cmSourceFile* outsf =
  124. makefile->GetSourceFileWithOutput(id->c_str());
  125. // if a source file was found then add it
  126. if (outsf &&
  127. (std::find(classes.begin(),classes.end(),outsf) == classes.end())
  128. &&
  129. (std::find(newClasses.begin(),newClasses.end(),outsf)
  130. == newClasses.end()))
  131. {
  132. // then add the source to this target and add it to the queue
  133. newClasses.push_back(outsf);
  134. name = outsf->GetSourceName();
  135. if (outsf->GetSourceExtension() != "rule")
  136. {
  137. name += ".";
  138. name += outsf->GetSourceExtension();
  139. }
  140. std::string temp =
  141. cmSystemTools::GetFilenamePath(outsf->GetFullPath());
  142. temp += "/";
  143. temp += name;
  144. // if it hasn't been processed
  145. if (srcFilesQueued.find(temp) == srcFilesQueued.end())
  146. {
  147. srcFilesToProcess.push(temp);
  148. srcFilesQueued.insert(temp);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. for(std::vector<cmSourceFile*>::const_iterator i = newClasses.begin();
  155. i != newClasses.end(); ++i)
  156. {
  157. classes.push_back(*i);
  158. }
  159. // add in the project file itself
  160. if (projFile.size())
  161. {
  162. srcFilesToProcess.push(projFile);
  163. srcFilesQueued.insert(projFile);
  164. }
  165. // add in the library depends for custom targets
  166. if (this->GetType() == cmTarget::UTILITY ||
  167. this->GetType() == cmTarget::GLOBAL_TARGET)
  168. {
  169. for (std::vector<cmCustomCommand>::iterator ic =
  170. this->GetPostBuildCommands().begin();
  171. ic != this->GetPostBuildCommands().end(); ++ic)
  172. {
  173. cmCustomCommand &c = *ic;
  174. for (std::vector<std::string>::const_iterator i
  175. = c.GetDepends().begin(); i != c.GetDepends().end(); ++i)
  176. {
  177. srcFilesToProcess.push(*i);
  178. srcFilesQueued.insert(*i);
  179. }
  180. }
  181. }
  182. while (!srcFilesToProcess.empty())
  183. {
  184. // is this source the output of a custom command
  185. cmSourceFile* outsf =
  186. makefile->GetSourceFileWithOutput(srcFilesToProcess.front().c_str());
  187. if (outsf)
  188. {
  189. // is it not already in the target?
  190. if (std::find(classes.begin(),classes.end(),outsf) == classes.end())
  191. {
  192. // then add the source to this target and add it to the queue
  193. classes.push_back(outsf);
  194. name = outsf->GetSourceName();
  195. if (outsf->GetSourceExtension() != "rule")
  196. {
  197. name += ".";
  198. name += outsf->GetSourceExtension();
  199. }
  200. std::string temp =
  201. cmSystemTools::GetFilenamePath(outsf->GetFullPath());
  202. temp += "/";
  203. temp += name;
  204. // if it hasn't been processed
  205. if (srcFilesQueued.find(temp) == srcFilesQueued.end())
  206. {
  207. srcFilesToProcess.push(temp);
  208. srcFilesQueued.insert(temp);
  209. }
  210. }
  211. // add its dependencies to the list to check
  212. unsigned int i;
  213. for (i = 0; i < outsf->GetCustomCommand()->GetDepends().size(); ++i)
  214. {
  215. std::string dep = cmSystemTools::GetFilenameName(
  216. outsf->GetCustomCommand()->GetDepends()[i]);
  217. if (cmSystemTools::GetFilenameLastExtension(dep) == ".exe")
  218. {
  219. dep = cmSystemTools::GetFilenameWithoutLastExtension(dep);
  220. }
  221. // watch for target dependencies,
  222. if(this->Makefile->GetLocalGenerator()->
  223. GetGlobalGenerator()->FindTarget(0, dep.c_str()))
  224. {
  225. // add the depend as a utility on the target
  226. this->AddUtility(dep.c_str());
  227. }
  228. else
  229. {
  230. if (srcFilesQueued.find(outsf->GetCustomCommand()->GetDepends()[i])
  231. == srcFilesQueued.end())
  232. {
  233. srcFilesToProcess.push
  234. (outsf->GetCustomCommand()->GetDepends()[i]);
  235. srcFilesQueued.insert(outsf->GetCustomCommand()->GetDepends()[i]);
  236. }
  237. }
  238. }
  239. }
  240. // finished with this SF move to the next
  241. srcFilesToProcess.pop();
  242. }
  243. // mark all custom commands in the targets list of source files as used.
  244. for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
  245. i != this->SourceFiles.end(); ++i)
  246. {
  247. cmCustomCommand* cc = (*i)->GetCustomCommand();
  248. if(cc)
  249. {
  250. cc->SetUsed();
  251. }
  252. }
  253. }
  254. void cmTarget::GenerateSourceFilesFromSourceLists( cmMakefile &mf)
  255. {
  256. // this is only done for non install targets
  257. if ((this->TargetTypeValue == cmTarget::INSTALL_FILES)
  258. || (this->TargetTypeValue == cmTarget::INSTALL_PROGRAMS))
  259. {
  260. return;
  261. }
  262. // for each src lists add the classes
  263. for (std::vector<std::string>::const_iterator s = this->SourceLists.begin();
  264. s != this->SourceLists.end(); ++s)
  265. {
  266. int done = 0;
  267. // replace any variables
  268. std::string temps = *s;
  269. mf.ExpandVariablesInString(temps);
  270. // Next if one wasn't found then assume it is a single class
  271. // check to see if it is an existing source file
  272. if (!done)
  273. {
  274. cmSourceFile* sourceFile = mf.GetSource(temps.c_str());
  275. if ( sourceFile )
  276. {
  277. this->SourceFiles.push_back(sourceFile);
  278. done = 1;
  279. }
  280. }
  281. // if we still are not done, try to create the SourceFile structure
  282. if (!done)
  283. {
  284. cmSourceFile file;
  285. file.SetProperty("ABSTRACT","0");
  286. file.SetName(temps.c_str(), mf.GetCurrentDirectory(),
  287. mf.GetSourceExtensions(),
  288. mf.GetHeaderExtensions(), this->Name.c_str());
  289. this->SourceFiles.push_back(mf.AddSource(file));
  290. }
  291. }
  292. // expand any link library variables whle we are at it
  293. LinkLibraryVectorType::iterator p = this->LinkLibraries.begin();
  294. for (;p != this->LinkLibraries.end(); ++p)
  295. {
  296. mf.ExpandVariablesInString(p->first, true, true);
  297. }
  298. }
  299. void cmTarget::MergeLinkLibraries( cmMakefile& mf,
  300. const char *selfname,
  301. const LinkLibraryVectorType& libs )
  302. {
  303. // Only add on libraries we haven't added on before.
  304. // Assumption: the global link libraries could only grow, never shrink
  305. LinkLibraryVectorType::const_iterator i = libs.begin();
  306. i += this->PrevLinkedLibraries.size();
  307. for( ; i != libs.end(); ++i )
  308. {
  309. // We call this so that the dependencies get written to the cache
  310. this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
  311. }
  312. this->PrevLinkedLibraries = libs;
  313. }
  314. //----------------------------------------------------------------------------
  315. void cmTarget::AddLinkDirectory(const char* d)
  316. {
  317. // Make sure we don't add unnecessary search directories.
  318. if(std::find(this->ExplicitLinkDirectories.begin(),
  319. this->ExplicitLinkDirectories.end(), d)
  320. == this->ExplicitLinkDirectories.end() )
  321. {
  322. this->ExplicitLinkDirectories.push_back( d );
  323. this->LinkDirectoriesComputed = false;
  324. }
  325. }
  326. //----------------------------------------------------------------------------
  327. const std::vector<std::string>& cmTarget::GetLinkDirectories()
  328. {
  329. // Make sure all library dependencies have been analyzed.
  330. if(!this->LinkLibrariesAnalyzed && !this->LinkLibraries.empty())
  331. {
  332. cmSystemTools::Error(
  333. "cmTarget::GetLinkDirectories called before "
  334. "cmTarget::AnalyzeLibDependencies on target ",
  335. this->Name.c_str());
  336. }
  337. // Make sure the complete set of link directories has been computed.
  338. if(!this->LinkDirectoriesComputed)
  339. {
  340. // Compute the full set of link directories including the
  341. // locations of targets that have been linked in. Start with the
  342. // link directories given explicitly.
  343. this->LinkDirectories = this->ExplicitLinkDirectories;
  344. for(LinkLibraryVectorType::iterator ll = this->LinkLibraries.begin();
  345. ll != this->LinkLibraries.end(); ++ll)
  346. {
  347. // If this library is a CMake target then add its location as a
  348. // link directory.
  349. std::string lib = ll->first;
  350. cmTarget* tgt = 0;
  351. if(this->Makefile && this->Makefile->GetLocalGenerator() &&
  352. this->Makefile->GetLocalGenerator()->GetGlobalGenerator())
  353. {
  354. tgt = (this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  355. ->FindTarget(0, lib.c_str()));
  356. }
  357. if(tgt)
  358. {
  359. // Add the directory only if it is not already present. This
  360. // is an N^2 algorithm for adding the directories, but N
  361. // should not get very big.
  362. const char* libpath = tgt->GetDirectory();
  363. if(std::find(this->LinkDirectories.begin(),
  364. this->LinkDirectories.end(),
  365. libpath) == this->LinkDirectories.end())
  366. {
  367. this->LinkDirectories.push_back(libpath);
  368. }
  369. }
  370. }
  371. // The complete set of link directories has now been computed.
  372. this->LinkDirectoriesComputed = true;
  373. }
  374. // Return the complete set of link directories.
  375. return this->LinkDirectories;
  376. }
  377. void cmTarget::ClearDependencyInformation( cmMakefile& mf,
  378. const char* target )
  379. {
  380. // Clear the dependencies. The cache variable must exist iff we are
  381. // recording dependency information for this target.
  382. std::string depname = target;
  383. depname += "_LIB_DEPENDS";
  384. if (this->RecordDependencies)
  385. {
  386. mf.AddCacheDefinition(depname.c_str(), "",
  387. "Dependencies for target", cmCacheManager::STATIC);
  388. }
  389. else
  390. {
  391. if (mf.GetDefinition( depname.c_str() ))
  392. {
  393. std::string message = "Target ";
  394. message += target;
  395. message += " has dependency information when it shouldn't.\n";
  396. message += "Your cache is probably stale. Please remove the entry\n ";
  397. message += depname;
  398. message += "\nfrom the cache.";
  399. cmSystemTools::Error( message.c_str() );
  400. }
  401. }
  402. }
  403. void cmTarget::AddLinkLibrary(const std::string& lib,
  404. LinkLibraryType llt)
  405. {
  406. this->AddFramework(lib.c_str(), llt);
  407. this->LinkLibraries.push_back( std::pair<std::string,
  408. cmTarget::LinkLibraryType>(lib,llt) );
  409. }
  410. bool cmTarget::AddFramework(const std::string& libname, LinkLibraryType llt)
  411. {
  412. (void)llt; // TODO: What is this?
  413. if(cmSystemTools::IsPathToFramework(libname.c_str()))
  414. {
  415. std::string frameworkDir = libname;
  416. frameworkDir += "/../";
  417. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  418. std::vector<std::string>::iterator i =
  419. std::find(this->Frameworks.begin(),
  420. this->Frameworks.end(), frameworkDir);
  421. if(i == this->Frameworks.end())
  422. {
  423. this->Frameworks.push_back(frameworkDir);
  424. }
  425. return true;
  426. }
  427. return false;
  428. }
  429. void cmTarget::AddLinkLibrary(cmMakefile& mf,
  430. const char *target, const char* lib,
  431. LinkLibraryType llt)
  432. {
  433. // Never add a self dependency, even if the user asks for it.
  434. if(strcmp( target, lib ) == 0)
  435. {
  436. return;
  437. }
  438. this->AddFramework(lib, llt);
  439. this->LinkLibraries.push_back( std::pair<std::string,
  440. cmTarget::LinkLibraryType>(lib,llt) );
  441. if(llt != cmTarget::GENERAL)
  442. {
  443. // Store the library's link type in the cache. If it is a
  444. // conflicting type then assume it is always used. This is the
  445. // case when the user sets the cache entries for debug and
  446. // optimized versions of the library to the same value.
  447. std::string linkTypeName = lib;
  448. linkTypeName += "_LINK_TYPE";
  449. switch(llt)
  450. {
  451. case cmTarget::DEBUG:
  452. {
  453. const char* def = mf.GetDefinition(linkTypeName.c_str());
  454. if(!def || strcmp(def, "debug") == 0)
  455. {
  456. mf.AddCacheDefinition(linkTypeName.c_str(),
  457. "debug",
  458. "Library is used for debug links only",
  459. cmCacheManager::STATIC);
  460. }
  461. else
  462. {
  463. mf.AddCacheDefinition
  464. (linkTypeName.c_str(), "general",
  465. "Library is used for both debug and optimized links",
  466. cmCacheManager::STATIC);
  467. }
  468. }
  469. break;
  470. case cmTarget::OPTIMIZED:
  471. {
  472. const char* def = mf.GetDefinition(linkTypeName.c_str());
  473. if(!def || strcmp(def, "optimized") == 0)
  474. {
  475. mf.AddCacheDefinition
  476. (linkTypeName.c_str(), "optimized",
  477. "Library is used for debug links only",
  478. cmCacheManager::STATIC);
  479. }
  480. else
  481. {
  482. mf.AddCacheDefinition
  483. (linkTypeName.c_str(), "general",
  484. "Library is used for both debug and optimized links",
  485. cmCacheManager::STATIC);
  486. }
  487. }
  488. break;
  489. case cmTarget::GENERAL:
  490. break;
  491. }
  492. }
  493. // Add the explicit dependency information for this target. This is
  494. // simply a set of libraries separated by ";". There should always
  495. // be a trailing ";". These library names are not canonical, in that
  496. // they may be "-framework x", "-ly", "/path/libz.a", etc.
  497. // We shouldn't remove duplicates here because external libraries
  498. // may be purposefully duplicated to handle recursive dependencies,
  499. // and we removing one instance will break the link line. Duplicates
  500. // will be appropriately eliminated at emit time.
  501. if(this->RecordDependencies)
  502. {
  503. std::string targetEntry = target;
  504. targetEntry += "_LIB_DEPENDS";
  505. std::string dependencies;
  506. const char* old_val = mf.GetDefinition( targetEntry.c_str() );
  507. if( old_val )
  508. {
  509. dependencies += old_val;
  510. }
  511. dependencies += lib;
  512. dependencies += ";";
  513. mf.AddCacheDefinition( targetEntry.c_str(), dependencies.c_str(),
  514. "Dependencies for the target",
  515. cmCacheManager::STATIC );
  516. }
  517. }
  518. void
  519. cmTarget::AnalyzeLibDependencies( const cmMakefile& mf )
  520. {
  521. // There are two key parts of the dependency analysis: (1)
  522. // determining the libraries in the link line, and (2) constructing
  523. // the dependency graph for those libraries.
  524. //
  525. // The latter is done using the cache entries that record the
  526. // dependencies of each library.
  527. //
  528. // The former is a more thorny issue, since it is not clear how to
  529. // determine if two libraries listed on the link line refer to the a
  530. // single library or not. For example, consider the link "libraries"
  531. // /usr/lib/libtiff.so -ltiff
  532. // Is this one library or two? The solution implemented here is the
  533. // simplest (and probably the only practical) one: two libraries are
  534. // the same if their "link strings" are identical. Thus, the two
  535. // libraries above are considered distinct. This also means that for
  536. // dependency analysis to be effective, the CMake user must specify
  537. // libraries build by his project without using any linker flags or
  538. // file extensions. That is,
  539. // LINK_LIBRARIES( One Two )
  540. // instead of
  541. // LINK_LIBRARIES( -lOne ${binarypath}/libTwo.a )
  542. // The former is probably what most users would do, but it never
  543. // hurts to document the assumptions. :-) Therefore, in the analysis
  544. // code, the "canonical name" of a library is simply its name as
  545. // given to a LINK_LIBRARIES command.
  546. //
  547. // Also, we will leave the original link line intact; we will just add any
  548. // dependencies that were missing.
  549. //
  550. // There is a problem with recursive external libraries
  551. // (i.e. libraries with no dependency information that are
  552. // recursively dependent). We must make sure that the we emit one of
  553. // the libraries twice to satisfy the recursion, but we shouldn't
  554. // emit it more times than necessary. In particular, we must make
  555. // sure that handling this improbable case doesn't cost us when
  556. // dealing with the common case of non-recursive libraries. The
  557. // solution is to assume that the recursion is satisfied at one node
  558. // of the dependency tree. To illustrate, assume libA and libB are
  559. // extrenal and mutually dependent. Suppose libX depends on
  560. // libA, and libY on libA and libX. Then
  561. // TARGET_LINK_LIBRARIES( Y X A B A )
  562. // TARGET_LINK_LIBRARIES( X A B A )
  563. // TARGET_LINK_LIBRARIES( Exec Y )
  564. // would result in "-lY -lX -lA -lB -lA". This is the correct way to
  565. // specify the dependencies, since the mutual dependency of A and B
  566. // is resolved *every time libA is specified*.
  567. //
  568. // Something like
  569. // TARGET_LINK_LIBRARIES( Y X A B A )
  570. // TARGET_LINK_LIBRARIES( X A B )
  571. // TARGET_LINK_LIBRARIES( Exec Y )
  572. // would result in "-lY -lX -lA -lB", and the mutual dependency
  573. // information is lost. This is because in some case (Y), the mutual
  574. // dependency of A and B is listed, while in another other case (X),
  575. // it is not. Depending on which line actually emits A, the mutual
  576. // dependency may or may not be on the final link line. We can't
  577. // handle this pathalogical case cleanly without emitting extra
  578. // libraries for the normal cases. Besides, the dependency
  579. // information for X is wrong anyway: if we build an executable
  580. // depending on X alone, we would not have the mutual dependency on
  581. // A and B resolved.
  582. //
  583. // IMPROVEMENTS:
  584. // -- The current algorithm will not always pick the "optimal" link line
  585. // when recursive dependencies are present. It will instead break the
  586. // cycles at an aribtrary point. The majority of projects won't have
  587. // cyclic dependencies, so this is probably not a big deal. Note that
  588. // the link line is always correct, just not necessary optimal.
  589. typedef std::vector< std::string > LinkLine;
  590. // The dependency map.
  591. DependencyMap dep_map;
  592. if ( this->OriginalLinkLibraries.size() == 0 )
  593. {
  594. this->OriginalLinkLibraries = this->LinkLibraries;
  595. }
  596. // 1. Build the dependency graph
  597. //
  598. for(LinkLibraryVectorType::reverse_iterator lib
  599. = this->LinkLibraries.rbegin();
  600. lib != this->LinkLibraries.rend(); ++lib)
  601. {
  602. this->GatherDependencies( mf, lib->first, dep_map );
  603. }
  604. // 2. Remove any dependencies that are already satisfied in the original
  605. // link line.
  606. //
  607. for(LinkLibraryVectorType::iterator lib = this->LinkLibraries.begin();
  608. lib != this->LinkLibraries.end(); ++lib)
  609. {
  610. for( LinkLibraryVectorType::iterator lib2 = lib;
  611. lib2 != this->LinkLibraries.end(); ++lib2)
  612. {
  613. DeleteDependency( dep_map, lib->first, lib2->first );
  614. }
  615. }
  616. // 3. Create the new link line by simply emitting any dependencies that are
  617. // missing. Start from the back and keep adding.
  618. //
  619. std::set<cmStdString> done, visited;
  620. std::vector<std::string> newLinkLibraries;
  621. for(LinkLibraryVectorType::reverse_iterator lib =
  622. this->LinkLibraries.rbegin();
  623. lib != this->LinkLibraries.rend(); ++lib)
  624. {
  625. // skip zero size library entries, this may happen
  626. // if a variable expands to nothing.
  627. if (lib->first.size() != 0)
  628. {
  629. Emit( lib->first, dep_map, done, visited, newLinkLibraries );
  630. }
  631. }
  632. // 4. Add the new libraries to the link line.
  633. //
  634. for( std::vector<std::string>::reverse_iterator k =
  635. newLinkLibraries.rbegin();
  636. k != newLinkLibraries.rend(); ++k )
  637. {
  638. std::string linkType = *k;
  639. linkType += "_LINK_TYPE";
  640. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  641. const char* linkTypeString = mf.GetDefinition( linkType.c_str() );
  642. if(linkTypeString)
  643. {
  644. if(strcmp(linkTypeString, "debug") == 0)
  645. {
  646. llt = cmTarget::DEBUG;
  647. }
  648. if(strcmp(linkTypeString, "optimized") == 0)
  649. {
  650. llt = cmTarget::OPTIMIZED;
  651. }
  652. }
  653. this->LinkLibraries.push_back( std::make_pair(*k,llt) );
  654. }
  655. this->LinkLibrariesAnalyzed = true;
  656. }
  657. void cmTarget::InsertDependency( DependencyMap& depMap,
  658. const cmStdString& lib,
  659. const cmStdString& dep )
  660. {
  661. depMap[lib].push_back(dep);
  662. }
  663. void cmTarget::DeleteDependency( DependencyMap& depMap,
  664. const cmStdString& lib,
  665. const cmStdString& dep )
  666. {
  667. // Make sure there is an entry in the map for lib. If so, delete all
  668. // dependencies to dep. There may be repeated entries because of
  669. // external libraries that are specified multiple times.
  670. DependencyMap::iterator map_itr = depMap.find( lib );
  671. if( map_itr != depMap.end() )
  672. {
  673. DependencyList& depList = map_itr->second;
  674. DependencyList::iterator itr;
  675. while( (itr = std::find(depList.begin(), depList.end(), dep)) !=
  676. depList.end() )
  677. {
  678. depList.erase( itr );
  679. }
  680. }
  681. }
  682. void cmTarget::Emit( const std::string& lib,
  683. const DependencyMap& dep_map,
  684. std::set<cmStdString>& emitted,
  685. std::set<cmStdString>& visited,
  686. std::vector<std::string>& link_line )
  687. {
  688. // It's already been emitted
  689. if( emitted.find(lib) != emitted.end() )
  690. return;
  691. // Emit the dependencies only if this library node hasn't been
  692. // visited before. If it has, then we have a cycle. The recursion
  693. // that got us here should take care of everything.
  694. if( visited.insert(lib).second )
  695. {
  696. if( dep_map.find(lib) != dep_map.end() ) // does it have dependencies?
  697. {
  698. const DependencyList& dep_on = dep_map.find( lib )->second;
  699. DependencyList::const_reverse_iterator i;
  700. // To cater for recursive external libraries, we must emit
  701. // duplicates on this link line *unless* they were emitted by
  702. // some other node, in which case we assume that the recursion
  703. // was resolved then. We making the simplifying assumption that
  704. // any duplicates on a single link line are on purpose, and must
  705. // be preserved.
  706. // This variable will keep track of the libraries that were
  707. // emitted directory from the current node, and not from a
  708. // recursive call. This way, if we come across a library that
  709. // has already been emitted, we repeat it iff it has been
  710. // emitted here.
  711. std::set<cmStdString> emitted_here;
  712. for( i = dep_on.rbegin(); i != dep_on.rend(); ++i )
  713. {
  714. if( emitted_here.find(*i) != emitted_here.end() )
  715. {
  716. // a repeat. Must emit.
  717. emitted.insert(*i);
  718. link_line.push_back( *i );
  719. }
  720. else
  721. {
  722. // Emit only if no-one else has
  723. if( emitted.find(*i) == emitted.end() )
  724. {
  725. // emit dependencies
  726. Emit( *i, dep_map, emitted, visited, link_line );
  727. // emit self
  728. emitted.insert(*i);
  729. emitted_here.insert(*i);
  730. link_line.push_back( *i );
  731. }
  732. }
  733. }
  734. }
  735. }
  736. }
  737. void cmTarget::GatherDependencies( const cmMakefile& mf,
  738. const std::string& lib,
  739. DependencyMap& dep_map )
  740. {
  741. // If the library is already in the dependency map, then it has
  742. // already been fully processed.
  743. if( dep_map.find(lib) != dep_map.end() )
  744. return;
  745. const char* deps = mf.GetDefinition( (lib+"_LIB_DEPENDS").c_str() );
  746. if( deps && strcmp(deps,"") != 0 )
  747. {
  748. // Make sure this library is in the map, even if it has an empty
  749. // set of dependencies. This distinguishes the case of explicitly
  750. // no dependencies with that of unspecified dependencies.
  751. dep_map[lib];
  752. // Parse the dependency information, which is simply a set of
  753. // libraries separated by ";". There is always a trailing ";".
  754. std::string depline = deps;
  755. std::string::size_type start = 0;
  756. std::string::size_type end;
  757. end = depline.find( ";", start );
  758. while( end != std::string::npos )
  759. {
  760. std::string l = depline.substr( start, end-start );
  761. if( l.size() != 0 )
  762. {
  763. InsertDependency( dep_map, lib, l );
  764. GatherDependencies( mf, l, dep_map );
  765. }
  766. start = end+1; // skip the ;
  767. end = depline.find( ";", start );
  768. }
  769. DeleteDependency( dep_map, lib, lib); // cannot depend on itself
  770. }
  771. }
  772. void cmTarget::SetProperty(const char* prop, const char* value)
  773. {
  774. if (!prop)
  775. {
  776. return;
  777. }
  778. if (!value)
  779. {
  780. value = "NOTFOUND";
  781. }
  782. this->Properties[prop] = value;
  783. }
  784. const char* cmTarget::GetDirectory(const char* config)
  785. {
  786. switch( this->GetType() )
  787. {
  788. case cmTarget::STATIC_LIBRARY:
  789. case cmTarget::MODULE_LIBRARY:
  790. case cmTarget::SHARED_LIBRARY:
  791. this->Directory =
  792. this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  793. break;
  794. case cmTarget::EXECUTABLE:
  795. this->Directory =
  796. this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  797. break;
  798. default:
  799. return 0;
  800. }
  801. if(this->Directory.empty())
  802. {
  803. this->Directory = this->Makefile->GetStartOutputDirectory();
  804. }
  805. if(config)
  806. {
  807. // Add the configuration's subdirectory.
  808. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  809. AppendDirectoryForConfig("/", config, "", this->Directory);
  810. }
  811. return this->Directory.c_str();
  812. }
  813. const char* cmTarget::GetLocation(const char* config)
  814. {
  815. this->Location = this->GetDirectory();
  816. if(!this->Location.empty())
  817. {
  818. this->Location += "/";
  819. }
  820. const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  821. if(cfgid && strcmp(cfgid, ".") != 0)
  822. {
  823. this->Location += cfgid;
  824. this->Location += "/";
  825. }
  826. this->Location += this->GetFullName(config, false);
  827. return this->Location.c_str();
  828. }
  829. const char *cmTarget::GetProperty(const char* prop)
  830. {
  831. // watch for special "computed" properties that are dependent on other
  832. // properties or variables, always recompute them
  833. if (!strcmp(prop,"LOCATION"))
  834. {
  835. // Set the LOCATION property of the target. Note that this cannot take
  836. // into account the per-configuration name of the target because the
  837. // configuration type may not be known at CMake time. We should
  838. // deprecate this feature and instead support transforming an executable
  839. // target name given as the command part of custom commands into the
  840. // proper path at build time. Alternatively we could put environment
  841. // variable settings in all custom commands that hold the name of the
  842. // target for each configuration and then give a reference to the
  843. // variable in the location.
  844. this->SetProperty("LOCATION", this->GetLocation(0));
  845. }
  846. // the type property returns what type the target is
  847. if (!strcmp(prop,"TYPE"))
  848. {
  849. switch( this->GetType() )
  850. {
  851. case cmTarget::STATIC_LIBRARY:
  852. return "STATIC_LIBRARY";
  853. break;
  854. case cmTarget::MODULE_LIBRARY:
  855. return "MODULE_LIBRARY";
  856. break;
  857. case cmTarget::SHARED_LIBRARY:
  858. return "SHARED_LIBRARY";
  859. break;
  860. case cmTarget::EXECUTABLE:
  861. return "EXECUTABLE";
  862. break;
  863. case cmTarget::UTILITY:
  864. return "UTILITY";
  865. break;
  866. case cmTarget::GLOBAL_TARGET:
  867. return "GLOBAL_TARGET";
  868. break;
  869. case cmTarget::INSTALL_FILES:
  870. return "INSTALL_FILES";
  871. break;
  872. case cmTarget::INSTALL_PROGRAMS:
  873. return "INSTALL_PROGRAMS";
  874. break;
  875. case cmTarget::INSTALL_DIRECTORY:
  876. return "INSTALL_DIRECTORY";
  877. break;
  878. }
  879. return 0;
  880. }
  881. std::map<cmStdString,cmStdString>::const_iterator i =
  882. this->Properties.find(prop);
  883. if (i != this->Properties.end())
  884. {
  885. return i->second.c_str();
  886. }
  887. return 0;
  888. }
  889. bool cmTarget::GetPropertyAsBool(const char* prop)
  890. {
  891. std::map<cmStdString,cmStdString>::const_iterator i =
  892. this->Properties.find(prop);
  893. if (i != this->Properties.end())
  894. {
  895. return cmSystemTools::IsOn(i->second.c_str());
  896. }
  897. return false;
  898. }
  899. const char* cmTarget::GetLinkerLanguage(cmGlobalGenerator* gg)
  900. {
  901. if(this->GetProperty("HAS_CXX"))
  902. {
  903. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", "CXX");
  904. }
  905. const char* linkerLang = this->GetProperty("LINKER_LANGUAGE");
  906. if(linkerLang)
  907. {
  908. return linkerLang;
  909. }
  910. std::set<cmStdString> languages;
  911. for(std::vector<cmSourceFile*>::const_iterator i
  912. = this->SourceFiles.begin();
  913. i != this->SourceFiles.end(); ++i)
  914. {
  915. const char* lang =
  916. gg->GetLanguageFromExtension((*i)->GetSourceExtension().c_str());
  917. if(lang)
  918. {
  919. languages.insert(lang);
  920. }
  921. }
  922. if(languages.size() == 0)
  923. {
  924. return 0;
  925. }
  926. if(languages.size() == 1)
  927. {
  928. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE",
  929. languages.begin()->c_str());
  930. return this->GetProperty("LINKER_LANGUAGE");
  931. }
  932. const char* prefLang = 0;
  933. for(std::set<cmStdString>::const_iterator s = languages.begin();
  934. s != languages.end(); ++s)
  935. {
  936. const char* lpref = gg->GetLinkerPreference(s->c_str());
  937. if(lpref[0] == 'P')
  938. {
  939. if(prefLang && !(*s == prefLang))
  940. {
  941. std::string m = "Error Target: ";
  942. m += this->Name + " Contains more than one Prefered language: ";
  943. m += *s;
  944. m += " and ";
  945. m += prefLang;
  946. m += "\nYou must set the LINKER_LANGUAGE property for this target.";
  947. cmSystemTools::Error(m.c_str());
  948. }
  949. else
  950. {
  951. prefLang = s->c_str();
  952. }
  953. }
  954. }
  955. if(!prefLang)
  956. {
  957. prefLang = languages.begin()->c_str();
  958. }
  959. const_cast<cmTarget*>(this)->SetProperty("LINKER_LANGUAGE", prefLang);
  960. return this->GetProperty("LINKER_LANGUAGE");
  961. }
  962. const char* cmTarget::GetCreateRuleVariable()
  963. {
  964. switch(this->GetType())
  965. {
  966. case cmTarget::STATIC_LIBRARY:
  967. return "_CREATE_STATIC_LIBRARY";
  968. case cmTarget::SHARED_LIBRARY:
  969. return "_CREATE_SHARED_LIBRARY";
  970. case cmTarget::MODULE_LIBRARY:
  971. return "_CREATE_SHARED_MODULE";
  972. case cmTarget::EXECUTABLE:
  973. return "_LINK_EXECUTABLE";
  974. case cmTarget::UTILITY:
  975. case cmTarget::GLOBAL_TARGET:
  976. case cmTarget::INSTALL_FILES:
  977. case cmTarget::INSTALL_PROGRAMS:
  978. case cmTarget::INSTALL_DIRECTORY:
  979. break;
  980. }
  981. return "";
  982. }
  983. const char* cmTarget::GetSuffixVariableInternal(TargetType type,
  984. bool implib)
  985. {
  986. switch(type)
  987. {
  988. case cmTarget::STATIC_LIBRARY:
  989. return "CMAKE_STATIC_LIBRARY_SUFFIX";
  990. case cmTarget::SHARED_LIBRARY:
  991. return (implib
  992. ? "CMAKE_IMPORT_LIBRARY_SUFFIX"
  993. : "CMAKE_SHARED_LIBRARY_SUFFIX");
  994. case cmTarget::MODULE_LIBRARY:
  995. return "CMAKE_SHARED_MODULE_SUFFIX";
  996. case cmTarget::EXECUTABLE:
  997. return "CMAKE_EXECUTABLE_SUFFIX";
  998. case cmTarget::UTILITY:
  999. case cmTarget::GLOBAL_TARGET:
  1000. case cmTarget::INSTALL_FILES:
  1001. case cmTarget::INSTALL_PROGRAMS:
  1002. case cmTarget::INSTALL_DIRECTORY:
  1003. break;
  1004. }
  1005. return "";
  1006. }
  1007. const char* cmTarget::GetPrefixVariableInternal(TargetType type,
  1008. bool implib)
  1009. {
  1010. switch(type)
  1011. {
  1012. case cmTarget::STATIC_LIBRARY:
  1013. return "CMAKE_STATIC_LIBRARY_PREFIX";
  1014. case cmTarget::SHARED_LIBRARY:
  1015. return (implib
  1016. ? "CMAKE_IMPORT_LIBRARY_PREFIX"
  1017. : "CMAKE_SHARED_LIBRARY_PREFIX");
  1018. case cmTarget::MODULE_LIBRARY:
  1019. return "CMAKE_SHARED_MODULE_PREFIX";
  1020. case cmTarget::EXECUTABLE:
  1021. case cmTarget::UTILITY:
  1022. case cmTarget::GLOBAL_TARGET:
  1023. case cmTarget::INSTALL_FILES:
  1024. case cmTarget::INSTALL_PROGRAMS:
  1025. case cmTarget::INSTALL_DIRECTORY:
  1026. break;
  1027. }
  1028. return "";
  1029. }
  1030. //----------------------------------------------------------------------------
  1031. std::string cmTarget::GetFullName(const char* config, bool implib)
  1032. {
  1033. return this->GetFullNameInternal(this->GetType(), config, implib);
  1034. }
  1035. //----------------------------------------------------------------------------
  1036. void cmTarget::GetFullName(std::string& prefix, std::string& base,
  1037. std::string& suffix, const char* config,
  1038. bool implib)
  1039. {
  1040. this->GetFullNameInternal(this->GetType(), config, implib,
  1041. prefix, base, suffix);
  1042. }
  1043. //----------------------------------------------------------------------------
  1044. std::string cmTarget::GetFullPath(const char* config, bool implib)
  1045. {
  1046. // Start with the output directory for the target.
  1047. std::string fpath = this->GetDirectory(config);
  1048. fpath += "/";
  1049. // Add the full name of the target.
  1050. fpath += this->GetFullName(config, implib);
  1051. return fpath;
  1052. }
  1053. //----------------------------------------------------------------------------
  1054. std::string cmTarget::GetFullNameInternal(TargetType type, const char* config,
  1055. bool implib)
  1056. {
  1057. std::string prefix;
  1058. std::string base;
  1059. std::string suffix;
  1060. this->GetFullNameInternal(type, config, implib, prefix, base, suffix);
  1061. return prefix+base+suffix;
  1062. }
  1063. //----------------------------------------------------------------------------
  1064. void cmTarget::GetFullNameInternal(TargetType type,
  1065. const char* config,
  1066. bool implib,
  1067. std::string& outPrefix,
  1068. std::string& outBase,
  1069. std::string& outSuffix)
  1070. {
  1071. // Use just the target name for non-main target types.
  1072. if(type != cmTarget::STATIC_LIBRARY &&
  1073. type != cmTarget::SHARED_LIBRARY &&
  1074. type != cmTarget::MODULE_LIBRARY &&
  1075. type != cmTarget::EXECUTABLE)
  1076. {
  1077. outPrefix = "";
  1078. outBase = this->GetName();
  1079. outSuffix = "";
  1080. return;
  1081. }
  1082. // The implib option is only allowed for shared libraries.
  1083. if(type != cmTarget::SHARED_LIBRARY)
  1084. {
  1085. implib = false;
  1086. }
  1087. // Compute the full name for main target types.
  1088. const char* targetPrefix = (implib
  1089. ? this->GetProperty("IMPORT_PREFIX")
  1090. : this->GetProperty("PREFIX"));
  1091. const char* targetSuffix = (implib
  1092. ? this->GetProperty("IMPORT_SUFFIX")
  1093. : this->GetProperty("SUFFIX"));
  1094. const char* configPostfix = 0;
  1095. if(config && *config)
  1096. {
  1097. std::string configProp = cmSystemTools::UpperCase(config);
  1098. configProp += "_POSTFIX";
  1099. configPostfix = this->GetProperty(configProp.c_str());
  1100. }
  1101. const char* prefixVar = this->GetPrefixVariableInternal(type, implib);
  1102. const char* suffixVar = this->GetSuffixVariableInternal(type, implib);
  1103. const char* ll =
  1104. this->GetLinkerLanguage(
  1105. this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
  1106. // first try language specific suffix
  1107. if(ll)
  1108. {
  1109. if(!targetSuffix && suffixVar && *suffixVar)
  1110. {
  1111. std::string langSuff = suffixVar + std::string("_") + ll;
  1112. targetSuffix = this->Makefile->GetDefinition(langSuff.c_str());
  1113. }
  1114. if(!targetPrefix && prefixVar && *prefixVar)
  1115. {
  1116. std::string langPrefix = prefixVar + std::string("_") + ll;
  1117. targetPrefix = this->Makefile->GetDefinition(langPrefix.c_str());
  1118. }
  1119. }
  1120. // if there is no prefix on the target use the cmake definition
  1121. if(!targetPrefix && prefixVar)
  1122. {
  1123. targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
  1124. }
  1125. // if there is no suffix on the target use the cmake definition
  1126. if(!targetSuffix && suffixVar)
  1127. {
  1128. targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
  1129. }
  1130. // Begin the final name with the prefix.
  1131. outPrefix = targetPrefix?targetPrefix:"";
  1132. // Append the target name or property-specified name.
  1133. const char* outName = 0;
  1134. if(config && *config)
  1135. {
  1136. std::string configProp = cmSystemTools::UpperCase(config);
  1137. configProp += "_OUTPUT_NAME";
  1138. outName = this->GetProperty(configProp.c_str());
  1139. }
  1140. if(!outName)
  1141. {
  1142. outName = this->GetProperty("OUTPUT_NAME");
  1143. }
  1144. if(outName)
  1145. {
  1146. outBase = outName;
  1147. }
  1148. else
  1149. {
  1150. outBase = this->GetName();
  1151. }
  1152. // Append the per-configuration postfix.
  1153. outBase += configPostfix?configPostfix:"";
  1154. // Append the suffix.
  1155. outSuffix = targetSuffix?targetSuffix:"";
  1156. }
  1157. void cmTarget::GetLibraryNames(std::string& name,
  1158. std::string& soName,
  1159. std::string& realName,
  1160. std::string& impName,
  1161. const char* config)
  1162. {
  1163. // Get the names based on the real type of the library.
  1164. this->GetLibraryNamesInternal(name, soName, realName, impName,
  1165. this->GetType(), config);
  1166. }
  1167. void cmTarget::GetLibraryCleanNames(std::string& staticName,
  1168. std::string& sharedName,
  1169. std::string& sharedSOName,
  1170. std::string& sharedRealName,
  1171. std::string& importName,
  1172. const char* config)
  1173. {
  1174. // Get the name as if this were a static library.
  1175. std::string soName;
  1176. std::string realName;
  1177. std::string impName;
  1178. this->GetLibraryNamesInternal(staticName, soName, realName, impName,
  1179. cmTarget::STATIC_LIBRARY, config);
  1180. // Get the names as if this were a shared library.
  1181. if(this->GetType() == cmTarget::STATIC_LIBRARY)
  1182. {
  1183. // Since the real type is static then the user either specified
  1184. // STATIC or did not specify a type. In the former case the
  1185. // shared library will never be present. In the latter case the
  1186. // type will never be MODULE. Either way the only names that
  1187. // might have to be cleaned are the shared library names.
  1188. this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
  1189. importName, cmTarget::SHARED_LIBRARY,
  1190. config);
  1191. }
  1192. else
  1193. {
  1194. // Use the name of the real type of the library (shared or module).
  1195. this->GetLibraryNamesInternal(sharedName, sharedSOName, sharedRealName,
  1196. importName, this->GetType(), config);
  1197. }
  1198. }
  1199. void cmTarget::GetLibraryNamesInternal(std::string& name,
  1200. std::string& soName,
  1201. std::string& realName,
  1202. std::string& impName,
  1203. TargetType type,
  1204. const char* config)
  1205. {
  1206. // Construct the name of the soname flag variable for this language.
  1207. const char* ll =
  1208. this->GetLinkerLanguage(
  1209. this->Makefile->GetLocalGenerator()->GetGlobalGenerator());
  1210. std::string sonameFlag = "CMAKE_SHARED_LIBRARY_SONAME";
  1211. if(ll)
  1212. {
  1213. sonameFlag += "_";
  1214. sonameFlag += ll;
  1215. }
  1216. sonameFlag += "_FLAG";
  1217. // Check for library version properties.
  1218. const char* version = this->GetProperty("VERSION");
  1219. const char* soversion = this->GetProperty("SOVERSION");
  1220. if((type != cmTarget::SHARED_LIBRARY && type != cmTarget::MODULE_LIBRARY) ||
  1221. !this->Makefile->GetDefinition(sonameFlag.c_str()))
  1222. {
  1223. // Versioning is supported only for shared libraries and modules,
  1224. // and then only when the platform supports an soname flag.
  1225. version = 0;
  1226. soversion = 0;
  1227. }
  1228. if(version && !soversion)
  1229. {
  1230. // The soversion must be set if the library version is set. Use
  1231. // the library version as the soversion.
  1232. soversion = version;
  1233. }
  1234. // The library name.
  1235. name = this->GetFullNameInternal(type, config, false);
  1236. // The library's soname.
  1237. soName = name;
  1238. if(soversion)
  1239. {
  1240. soName += ".";
  1241. soName += soversion;
  1242. }
  1243. // The library's real name on disk.
  1244. realName = name;
  1245. if(version)
  1246. {
  1247. realName += ".";
  1248. realName += version;
  1249. }
  1250. else if(soversion)
  1251. {
  1252. realName += ".";
  1253. realName += soversion;
  1254. }
  1255. // The import library name.
  1256. if(type == cmTarget::SHARED_LIBRARY)
  1257. {
  1258. impName = this->GetFullNameInternal(type, config, true);
  1259. }
  1260. else
  1261. {
  1262. impName = "";
  1263. }
  1264. }
  1265. void cmTarget::GetExecutableNames(std::string& name,
  1266. std::string& realName,
  1267. const char* config)
  1268. {
  1269. // Get the names based on the real type of the executable.
  1270. this->GetExecutableNamesInternal(name, realName, this->GetType(), config);
  1271. }
  1272. void cmTarget::GetExecutableCleanNames(std::string& name,
  1273. std::string& realName,
  1274. const char* config)
  1275. {
  1276. // Get the name and versioned name of this executable.
  1277. this->GetExecutableNamesInternal(name, realName, cmTarget::EXECUTABLE,
  1278. config);
  1279. }
  1280. void cmTarget::GetExecutableNamesInternal(std::string& name,
  1281. std::string& realName,
  1282. TargetType type,
  1283. const char* config)
  1284. {
  1285. // This versioning is supported only for executables and then only
  1286. // when the platform supports symbolic links.
  1287. #if defined(_WIN32) && !defined(__CYGWIN__)
  1288. const char* version = 0;
  1289. #else
  1290. // Check for executable version properties.
  1291. const char* version = this->GetProperty("VERSION");
  1292. if(type != cmTarget::EXECUTABLE)
  1293. {
  1294. version = 0;
  1295. }
  1296. #endif
  1297. // The executable name.
  1298. name = this->GetFullNameInternal(type, config, false);
  1299. // The executable's real name on disk.
  1300. realName = name;
  1301. if(version)
  1302. {
  1303. realName += "-";
  1304. realName += version;
  1305. }
  1306. }
  1307. //----------------------------------------------------------------------------
  1308. void cmTarget::SetPropertyDefault(const char* property,
  1309. const char* default_value)
  1310. {
  1311. // Compute the name of the variable holding the default value.
  1312. std::string var = "CMAKE_";
  1313. var += property;
  1314. if(const char* value = this->Makefile->GetDefinition(var.c_str()))
  1315. {
  1316. this->SetProperty(property, value);
  1317. }
  1318. else if(default_value)
  1319. {
  1320. this->SetProperty(property, default_value);
  1321. }
  1322. }
  1323. //----------------------------------------------------------------------------
  1324. bool cmTarget::HaveBuildTreeRPATH()
  1325. {
  1326. return (!this->GetPropertyAsBool("SKIP_BUILD_RPATH") &&
  1327. !this->LinkLibraries.empty());
  1328. }
  1329. //----------------------------------------------------------------------------
  1330. bool cmTarget::HaveInstallTreeRPATH()
  1331. {
  1332. const char* install_rpath = this->GetProperty("INSTALL_RPATH");
  1333. return install_rpath && *install_rpath;
  1334. }
  1335. //----------------------------------------------------------------------------
  1336. bool cmTarget::NeedRelinkBeforeInstall()
  1337. {
  1338. // Only executables and shared libraries can have an rpath and may
  1339. // need relinking.
  1340. if(this->TargetTypeValue != cmTarget::EXECUTABLE &&
  1341. this->TargetTypeValue != cmTarget::SHARED_LIBRARY &&
  1342. this->TargetTypeValue != cmTarget::MODULE_LIBRARY)
  1343. {
  1344. return false;
  1345. }
  1346. // If there is no install location this target will not be installed
  1347. // and therefore does not need relinking.
  1348. if(!this->GetHaveInstallRule())
  1349. {
  1350. return false;
  1351. }
  1352. // If skipping all rpaths completely then no relinking is needed.
  1353. if(this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
  1354. {
  1355. return false;
  1356. }
  1357. // If building with the install-tree rpath no relinking is needed.
  1358. if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
  1359. {
  1360. return false;
  1361. }
  1362. // Check for rpath support on this platform.
  1363. if(const char* ll = this->GetLinkerLanguage(
  1364. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()))
  1365. {
  1366. std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1367. flagVar += ll;
  1368. flagVar += "_FLAG";
  1369. if(!this->Makefile->IsSet(flagVar.c_str()))
  1370. {
  1371. // There is no rpath support on this platform so nothing needs
  1372. // relinking.
  1373. return false;
  1374. }
  1375. }
  1376. else
  1377. {
  1378. // No linker language is known. This error will be reported by
  1379. // other code.
  1380. return false;
  1381. }
  1382. // If either a build or install tree rpath is set then the rpath
  1383. // will likely change between the build tree and install tree and
  1384. // this target must be relinked.
  1385. return this->HaveBuildTreeRPATH() || this->HaveInstallTreeRPATH();
  1386. }
  1387. //----------------------------------------------------------------------------
  1388. std::string cmTarget::GetInstallNameDirForBuildTree(const char* config)
  1389. {
  1390. // If building directly for installation then the build tree install_name
  1391. // is the same as the install tree.
  1392. if(this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH"))
  1393. {
  1394. return GetInstallNameDirForInstallTree(config);
  1395. }
  1396. // Use the build tree directory for the target.
  1397. if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
  1398. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
  1399. !this->GetPropertyAsBool("SKIP_BUILD_RPATH"))
  1400. {
  1401. std::string dir = this->GetDirectory(config);
  1402. dir += "/";
  1403. return dir;
  1404. }
  1405. else
  1406. {
  1407. return "";
  1408. }
  1409. }
  1410. //----------------------------------------------------------------------------
  1411. std::string cmTarget::GetInstallNameDirForInstallTree(const char*)
  1412. {
  1413. // Lookup the target property.
  1414. const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  1415. if(this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME") &&
  1416. !this->Makefile->IsOn("CMAKE_SKIP_RPATH") &&
  1417. install_name_dir && *install_name_dir)
  1418. {
  1419. std::string dir = install_name_dir;
  1420. dir += "/";
  1421. return dir;
  1422. }
  1423. else
  1424. {
  1425. return "";
  1426. }
  1427. }