cmTarget.cxx 49 KB

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