cmTarget.cxx 45 KB

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