cmTarget.cxx 49 KB

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