cmMakefileLibraryTargetGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 "cmMakefileLibraryTargetGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalUnixMakefileGenerator3.h"
  16. #include "cmLocalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmTarget.h"
  20. #include "cmake.h"
  21. #include <memory> // auto_ptr
  22. //----------------------------------------------------------------------------
  23. cmMakefileLibraryTargetGenerator
  24. ::cmMakefileLibraryTargetGenerator(cmTarget* target):
  25. cmMakefileTargetGenerator(target)
  26. {
  27. this->CustomCommandDriver = OnDepends;
  28. this->Target->GetLibraryNames(
  29. this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
  30. this->TargetNameImport, this->TargetNamePDB, this->ConfigName);
  31. if(this->Target->IsFrameworkOnApple())
  32. {
  33. this->FrameworkVersion = this->Target->GetFrameworkVersion();
  34. this->MacContentDirectory = this->Target->GetDirectory(this->ConfigName);
  35. this->MacContentDirectory += "/";
  36. this->MacContentDirectory += this->TargetNameOut;
  37. this->MacContentDirectory += ".framework/Versions/";
  38. this->MacContentDirectory += this->FrameworkVersion;
  39. this->MacContentDirectory += "/";
  40. }
  41. }
  42. //----------------------------------------------------------------------------
  43. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  44. {
  45. // create the build.make file and directory, put in the common blocks
  46. this->CreateRuleFile();
  47. // write rules used to help build object files
  48. this->WriteCommonCodeRules();
  49. // write in rules for object files and custom commands
  50. this->WriteTargetBuildRules();
  51. // write the per-target per-language flags
  52. this->WriteTargetLanguageFlags();
  53. // write the link rules
  54. // Write the rule for this target type.
  55. switch(this->Target->GetType())
  56. {
  57. case cmTarget::STATIC_LIBRARY:
  58. this->WriteStaticLibraryRules();
  59. break;
  60. case cmTarget::SHARED_LIBRARY:
  61. this->WriteSharedLibraryRules(false);
  62. if(this->Target->NeedRelinkBeforeInstall())
  63. {
  64. // Write rules to link an installable version of the target.
  65. this->WriteSharedLibraryRules(true);
  66. }
  67. break;
  68. case cmTarget::MODULE_LIBRARY:
  69. this->WriteModuleLibraryRules(false);
  70. if(this->Target->NeedRelinkBeforeInstall())
  71. {
  72. // Write rules to link an installable version of the target.
  73. this->WriteModuleLibraryRules(true);
  74. }
  75. break;
  76. default:
  77. // If language is not known, this is an error.
  78. cmSystemTools::Error("Unknown Library Type");
  79. break;
  80. }
  81. // Write the requires target.
  82. this->WriteTargetRequiresRules();
  83. // Write clean target
  84. this->WriteTargetCleanRules();
  85. // Write the dependency generation rule. This must be done last so
  86. // that multiple output pair information is available.
  87. this->WriteTargetDependRules();
  88. // close the streams
  89. this->CloseFileStreams();
  90. }
  91. //----------------------------------------------------------------------------
  92. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  93. {
  94. const char* linkLanguage = this->Target->GetLinkerLanguage();
  95. std::string linkRuleVar = "CMAKE_";
  96. if (linkLanguage)
  97. {
  98. linkRuleVar += linkLanguage;
  99. }
  100. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  101. std::string extraFlags;
  102. this->LocalGenerator->AppendFlags
  103. (extraFlags,this->Target->GetProperty("STATIC_LIBRARY_FLAGS"));
  104. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), false);
  105. }
  106. //----------------------------------------------------------------------------
  107. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  108. {
  109. if(this->Target->IsFrameworkOnApple())
  110. {
  111. this->WriteFrameworkRules(relink);
  112. return;
  113. }
  114. const char* linkLanguage = this->Target->GetLinkerLanguage();
  115. std::string linkRuleVar = "CMAKE_";
  116. if (linkLanguage)
  117. {
  118. linkRuleVar += linkLanguage;
  119. }
  120. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  121. std::string extraFlags;
  122. this->LocalGenerator->AppendFlags
  123. (extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  124. std::string linkFlagsConfig = "LINK_FLAGS_";
  125. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  126. this->LocalGenerator->AppendFlags
  127. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  128. this->LocalGenerator->AddConfigVariableFlags
  129. (extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  130. if(this->Makefile->IsOn("WIN32") && !(this->Makefile->IsOn("CYGWIN")
  131. || this->Makefile->IsOn("MINGW")))
  132. {
  133. const std::vector<cmSourceFile*>& sources =
  134. this->Target->GetSourceFiles();
  135. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  136. i != sources.end(); ++i)
  137. {
  138. cmSourceFile* sf = *i;
  139. if(sf->GetExtension() == "def")
  140. {
  141. extraFlags += " ";
  142. extraFlags +=
  143. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  144. extraFlags +=
  145. this->Convert(sf->GetFullPath().c_str(),
  146. cmLocalGenerator::START_OUTPUT,
  147. cmLocalGenerator::SHELL);
  148. }
  149. }
  150. }
  151. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  152. }
  153. //----------------------------------------------------------------------------
  154. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  155. {
  156. const char* linkLanguage = this->Target->GetLinkerLanguage();
  157. std::string linkRuleVar = "CMAKE_";
  158. if (linkLanguage)
  159. {
  160. linkRuleVar += linkLanguage;
  161. }
  162. linkRuleVar += "_CREATE_SHARED_MODULE";
  163. std::string extraFlags;
  164. this->LocalGenerator->AppendFlags(extraFlags,
  165. this->Target->GetProperty("LINK_FLAGS"));
  166. std::string linkFlagsConfig = "LINK_FLAGS_";
  167. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  168. this->LocalGenerator->AppendFlags
  169. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  170. this->LocalGenerator->AddConfigVariableFlags
  171. (extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  172. // TODO: .def files should be supported here also.
  173. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  174. }
  175. //----------------------------------------------------------------------------
  176. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  177. {
  178. const char* linkLanguage = this->Target->GetLinkerLanguage();
  179. std::string linkRuleVar = "CMAKE_";
  180. if (linkLanguage)
  181. {
  182. linkRuleVar += linkLanguage;
  183. }
  184. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  185. std::string extraFlags;
  186. this->LocalGenerator->AppendFlags(extraFlags,
  187. this->Target->GetProperty("LINK_FLAGS"));
  188. std::string linkFlagsConfig = "LINK_FLAGS_";
  189. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  190. this->LocalGenerator->AppendFlags
  191. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  192. this->LocalGenerator->AddConfigVariableFlags
  193. (extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  194. // TODO: .def files should be supported here also.
  195. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  196. }
  197. //----------------------------------------------------------------------------
  198. void
  199. cmMakefileLibraryTargetGenerator
  200. ::CreateFramework(std::string const& targetName)
  201. {
  202. // Configure the Info.plist file into the Resources directory.
  203. this->MacContentFolders.insert("Resources");
  204. std::string plist = this->MacContentDirectory + "Resources/Info.plist";
  205. this->LocalGenerator->GenerateFrameworkInfoPList(this->Target,
  206. targetName.c_str(),
  207. plist.c_str());
  208. // TODO: Use the cmMakefileTargetGenerator::ExtraFiles vector to
  209. // drive rules to create these files at build time.
  210. std::string oldName;
  211. std::string newName;
  212. // Compute the location of the top-level foo.framework directory.
  213. std::string top = this->Target->GetDirectory(this->ConfigName);
  214. top += "/";
  215. top += this->TargetNameOut;
  216. top += ".framework/";
  217. // Make foo.framework/Versions
  218. std::string versions = top;
  219. versions += "Versions";
  220. cmSystemTools::MakeDirectory(versions.c_str());
  221. // Make foo.framework/Versions/version
  222. std::string version = versions;
  223. version += "/";
  224. version += this->FrameworkVersion;
  225. cmSystemTools::MakeDirectory(version.c_str());
  226. // Current -> version
  227. oldName = this->FrameworkVersion;
  228. newName = versions;
  229. newName += "/Current";
  230. cmSystemTools::RemoveFile(newName.c_str());
  231. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  232. this->Makefile->AddCMakeOutputFile(newName.c_str());
  233. // foo -> Versions/Current/foo
  234. oldName = "Versions/Current/";
  235. oldName += this->TargetNameOut;
  236. newName = top;
  237. newName += this->TargetNameOut;
  238. cmSystemTools::RemoveFile(newName.c_str());
  239. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  240. this->Makefile->AddCMakeOutputFile(newName.c_str());
  241. // Resources -> Versions/Current/Resources
  242. if(this->MacContentFolders.find("Resources") !=
  243. this->MacContentFolders.end())
  244. {
  245. oldName = "Versions/Current/Resources";
  246. newName = top;
  247. newName += "Resources";
  248. cmSystemTools::RemoveFile(newName.c_str());
  249. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  250. this->Makefile->AddCMakeOutputFile(newName.c_str());
  251. }
  252. // Headers -> Versions/Current/Headers
  253. if(this->MacContentFolders.find("Headers") !=
  254. this->MacContentFolders.end())
  255. {
  256. oldName = "Versions/Current/Headers";
  257. newName = top;
  258. newName += "Headers";
  259. cmSystemTools::RemoveFile(newName.c_str());
  260. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  261. this->Makefile->AddCMakeOutputFile(newName.c_str());
  262. }
  263. // PrivateHeaders -> Versions/Current/PrivateHeaders
  264. if(this->MacContentFolders.find("PrivateHeaders") !=
  265. this->MacContentFolders.end())
  266. {
  267. oldName = "Versions/Current/PrivateHeaders";
  268. newName = top;
  269. newName += "PrivateHeaders";
  270. cmSystemTools::RemoveFile(newName.c_str());
  271. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  272. this->Makefile->AddCMakeOutputFile(newName.c_str());
  273. }
  274. }
  275. //----------------------------------------------------------------------------
  276. void cmMakefileLibraryTargetGenerator::WriteLibraryRules
  277. (const char* linkRuleVar, const char* extraFlags, bool relink)
  278. {
  279. // TODO: Merge the methods that call this method to avoid
  280. // code duplication.
  281. std::vector<std::string> commands;
  282. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  283. std::string objTarget;
  284. // Build list of dependencies.
  285. std::vector<std::string> depends;
  286. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  287. obj != this->Objects.end(); ++obj)
  288. {
  289. objTarget = relPath;
  290. objTarget += *obj;
  291. depends.push_back(objTarget);
  292. }
  293. // Add dependencies on targets that must be built first.
  294. this->AppendTargetDepends(depends);
  295. // Add a dependency on the rule file itself.
  296. this->LocalGenerator->AppendRuleDepend(depends,
  297. this->BuildFileNameFull.c_str());
  298. for(std::vector<std::string>::const_iterator obj
  299. = this->ExternalObjects.begin();
  300. obj != this->ExternalObjects.end(); ++obj)
  301. {
  302. depends.push_back(*obj);
  303. }
  304. // Get the language to use for linking this library.
  305. const char* linkLanguage = this->Target->GetLinkerLanguage();
  306. // Make sure we have a link language.
  307. if(!linkLanguage)
  308. {
  309. cmSystemTools::Error("Cannot determine link language for target \"",
  310. this->Target->GetName(), "\".");
  311. return;
  312. }
  313. // Create set of linking flags.
  314. std::string linkFlags;
  315. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  316. // Add OSX version flags, if any.
  317. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  318. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  319. {
  320. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  321. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  322. }
  323. // Construct the name of the library.
  324. std::string targetName;
  325. std::string targetNameSO;
  326. std::string targetNameReal;
  327. std::string targetNameImport;
  328. std::string targetNamePDB;
  329. this->Target->GetLibraryNames(
  330. targetName, targetNameSO, targetNameReal, targetNameImport, targetNamePDB,
  331. this->ConfigName);
  332. // Construct the full path version of the names.
  333. std::string outpath;
  334. std::string outpathImp;
  335. if(this->Target->IsFrameworkOnApple())
  336. {
  337. outpath = this->MacContentDirectory;
  338. this->CreateFramework(targetName);
  339. }
  340. else if(relink)
  341. {
  342. outpath = this->Makefile->GetStartOutputDirectory();
  343. outpath += cmake::GetCMakeFilesDirectory();
  344. outpath += "/CMakeRelink.dir";
  345. cmSystemTools::MakeDirectory(outpath.c_str());
  346. outpath += "/";
  347. if(!targetNameImport.empty())
  348. {
  349. outpathImp = outpath;
  350. }
  351. }
  352. else
  353. {
  354. outpath = this->Target->GetDirectory(this->ConfigName);
  355. cmSystemTools::MakeDirectory(outpath.c_str());
  356. outpath += "/";
  357. if(!targetNameImport.empty())
  358. {
  359. outpathImp = this->Target->GetDirectory(this->ConfigName, true);
  360. cmSystemTools::MakeDirectory(outpathImp.c_str());
  361. outpathImp += "/";
  362. }
  363. }
  364. std::string targetFullPath = outpath + targetName;
  365. std::string targetFullPathPDB = outpath + targetNamePDB;
  366. std::string targetFullPathSO = outpath + targetNameSO;
  367. std::string targetFullPathReal = outpath + targetNameReal;
  368. std::string targetFullPathImport = outpathImp + targetNameImport;
  369. // Construct the output path version of the names for use in command
  370. // arguments.
  371. std::string targetOutPathPDB =
  372. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::FULL,
  373. cmLocalGenerator::SHELL);
  374. std::string targetOutPath =
  375. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  376. cmLocalGenerator::SHELL);
  377. std::string targetOutPathSO =
  378. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  379. cmLocalGenerator::SHELL);
  380. std::string targetOutPathReal =
  381. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  382. cmLocalGenerator::SHELL);
  383. std::string targetOutPathImport =
  384. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  385. cmLocalGenerator::SHELL);
  386. if(!this->NoRuleMessages)
  387. {
  388. // Add the link message.
  389. std::string buildEcho = "Linking ";
  390. buildEcho += linkLanguage;
  391. switch(this->Target->GetType())
  392. {
  393. case cmTarget::STATIC_LIBRARY:
  394. buildEcho += " static library ";
  395. break;
  396. case cmTarget::SHARED_LIBRARY:
  397. buildEcho += " shared library ";
  398. break;
  399. case cmTarget::MODULE_LIBRARY:
  400. buildEcho += " shared module ";
  401. break;
  402. default:
  403. buildEcho += " library ";
  404. break;
  405. }
  406. buildEcho += targetOutPath.c_str();
  407. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  408. cmLocalUnixMakefileGenerator3::EchoLink);
  409. }
  410. const char* forbiddenFlagVar = 0;
  411. switch(this->Target->GetType())
  412. {
  413. case cmTarget::SHARED_LIBRARY:
  414. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  415. break;
  416. case cmTarget::MODULE_LIBRARY:
  417. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  418. break;
  419. default: break;
  420. }
  421. // Clean files associated with this library.
  422. std::vector<std::string> libCleanFiles;
  423. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  424. cmLocalGenerator::START_OUTPUT,
  425. cmLocalGenerator::UNCHANGED));
  426. if(targetNameReal != targetName)
  427. {
  428. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  429. cmLocalGenerator::START_OUTPUT,
  430. cmLocalGenerator::UNCHANGED));
  431. }
  432. if(targetNameSO != targetName &&
  433. targetNameSO != targetNameReal)
  434. {
  435. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  436. cmLocalGenerator::START_OUTPUT,
  437. cmLocalGenerator::UNCHANGED));
  438. }
  439. if(!targetNameImport.empty())
  440. {
  441. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  442. cmLocalGenerator::START_OUTPUT,
  443. cmLocalGenerator::UNCHANGED));
  444. }
  445. // List the PDB for cleaning only when the whole target is
  446. // cleaned. We do not want to delete the .pdb file just before
  447. // linking the target.
  448. this->CleanFiles.push_back
  449. (this->Convert(targetFullPathPDB.c_str(),
  450. cmLocalGenerator::START_OUTPUT,
  451. cmLocalGenerator::UNCHANGED));
  452. #ifdef _WIN32
  453. // There may be a manifest file for this target. Add it to the
  454. // clean set just in case.
  455. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  456. {
  457. libCleanFiles.push_back(
  458. this->Convert((targetFullPath+".manifest").c_str(),
  459. cmLocalGenerator::START_OUTPUT,
  460. cmLocalGenerator::UNCHANGED));
  461. }
  462. #endif
  463. std::vector<std::string> commands1;
  464. // Add a command to remove any existing files for this library.
  465. // for static libs only
  466. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  467. {
  468. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  469. *this->Target, "target");
  470. this->LocalGenerator->CreateCDCommand
  471. (commands1,
  472. this->Makefile->GetStartOutputDirectory(),
  473. cmLocalGenerator::HOME_OUTPUT);
  474. commands.insert(commands.end(), commands1.begin(), commands1.end());
  475. commands1.clear();
  476. }
  477. // Add the pre-build and pre-link rules building but not when relinking.
  478. if(!relink)
  479. {
  480. this->LocalGenerator
  481. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  482. this->Target);
  483. this->LocalGenerator
  484. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  485. this->Target);
  486. }
  487. // Determine whether a link script will be used.
  488. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  489. // Select whether to use a response file for objects.
  490. bool useResponseFile = false;
  491. {
  492. std::string responseVar = "CMAKE_";
  493. responseVar += linkLanguage;
  494. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  495. if(this->Makefile->IsOn(responseVar.c_str()))
  496. {
  497. useResponseFile = true;
  498. }
  499. }
  500. // For static libraries there might be archiving rules.
  501. bool haveStaticLibraryRule = false;
  502. std::vector<std::string> archiveCreateCommands;
  503. std::vector<std::string> archiveAppendCommands;
  504. std::vector<std::string> archiveFinishCommands;
  505. std::string::size_type archiveCommandLimit = std::string::npos;
  506. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  507. {
  508. haveStaticLibraryRule =
  509. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  510. std::string arCreateVar = "CMAKE_";
  511. arCreateVar += linkLanguage;
  512. arCreateVar += "_ARCHIVE_CREATE";
  513. if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
  514. {
  515. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  516. }
  517. std::string arAppendVar = "CMAKE_";
  518. arAppendVar += linkLanguage;
  519. arAppendVar += "_ARCHIVE_APPEND";
  520. if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
  521. {
  522. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  523. }
  524. std::string arFinishVar = "CMAKE_";
  525. arFinishVar += linkLanguage;
  526. arFinishVar += "_ARCHIVE_FINISH";
  527. if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
  528. {
  529. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  530. }
  531. }
  532. // Decide whether to use archiving rules.
  533. bool useArchiveRules =
  534. !haveStaticLibraryRule &&
  535. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  536. if(useArchiveRules)
  537. {
  538. // Archiving rules are always run with a link script.
  539. useLinkScript = true;
  540. // Archiving rules never use a response file.
  541. useResponseFile = false;
  542. // Limit the length of individual object lists to less than the
  543. // 32K command line length limit on Windows. We could make this a
  544. // platform file variable but this should work everywhere.
  545. archiveCommandLimit = 30000;
  546. }
  547. // Expand the rule variables.
  548. std::vector<std::string> real_link_commands;
  549. {
  550. // Set path conversion for link script shells.
  551. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  552. // Collect up flags to link in needed libraries.
  553. cmOStringStream linklibs;
  554. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  555. {
  556. this->LocalGenerator
  557. ->OutputLinkLibraries(linklibs, *this->Target, relink);
  558. }
  559. // Construct object file lists that may be needed to expand the
  560. // rule.
  561. std::string buildObjs;
  562. this->CreateObjectLists(useLinkScript, useArchiveRules, useResponseFile,
  563. buildObjs, depends);
  564. cmLocalGenerator::RuleVariables vars;
  565. vars.TargetPDB = targetOutPathPDB.c_str();
  566. // Setup the target version.
  567. std::string targetVersionMajor;
  568. std::string targetVersionMinor;
  569. {
  570. cmOStringStream majorStream;
  571. cmOStringStream minorStream;
  572. int major;
  573. int minor;
  574. this->Target->GetTargetVersion(major, minor);
  575. majorStream << major;
  576. minorStream << minor;
  577. targetVersionMajor = majorStream.str();
  578. targetVersionMinor = minorStream.str();
  579. }
  580. vars.TargetVersionMajor = targetVersionMajor.c_str();
  581. vars.TargetVersionMinor = targetVersionMinor.c_str();
  582. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  583. vars.CMTarget = this->Target;
  584. vars.Language = linkLanguage;
  585. vars.Objects = buildObjs.c_str();
  586. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  587. objdir += this->Target->GetName();
  588. objdir += ".dir";
  589. objdir = this->Convert(objdir.c_str(),
  590. cmLocalGenerator::START_OUTPUT,
  591. cmLocalGenerator::SHELL);
  592. vars.ObjectDir = objdir.c_str();
  593. vars.Target = targetOutPathReal.c_str();
  594. std::string linkString = linklibs.str();
  595. vars.LinkLibraries = linkString.c_str();
  596. vars.ObjectsQuoted = buildObjs.c_str();
  597. vars.TargetSOName= targetNameSO.c_str();
  598. vars.LinkFlags = linkFlags.c_str();
  599. // Compute the directory portion of the install_name setting.
  600. std::string install_name_dir;
  601. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  602. {
  603. // Get the install_name directory for the build tree.
  604. install_name_dir =
  605. this->Target->GetInstallNameDirForBuildTree(this->ConfigName);
  606. // Set the rule variable replacement value.
  607. if(install_name_dir.empty())
  608. {
  609. vars.TargetInstallNameDir = "";
  610. }
  611. else
  612. {
  613. // Convert to a path for the native build tool.
  614. install_name_dir =
  615. this->LocalGenerator->Convert(install_name_dir.c_str(),
  616. cmLocalGenerator::NONE,
  617. cmLocalGenerator::SHELL, false);
  618. vars.TargetInstallNameDir = install_name_dir.c_str();
  619. }
  620. }
  621. std::string langFlags;
  622. this->LocalGenerator
  623. ->AddLanguageFlags(langFlags, linkLanguage, this->ConfigName);
  624. // remove any language flags that might not work with the
  625. // particular os
  626. if(forbiddenFlagVar)
  627. {
  628. this->RemoveForbiddenFlags(forbiddenFlagVar,
  629. linkLanguage, langFlags);
  630. }
  631. vars.LanguageCompileFlags = langFlags.c_str();
  632. // Construct the main link rule and expand placeholders.
  633. this->LocalGenerator->TargetImplib = targetOutPathImport;
  634. if(useArchiveRules)
  635. {
  636. // Construct the individual object list strings.
  637. std::vector<std::string> object_strings;
  638. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  639. // Create the archive with the first set of objects.
  640. std::vector<std::string>::iterator osi = object_strings.begin();
  641. {
  642. vars.Objects = osi->c_str();
  643. for(std::vector<std::string>::const_iterator
  644. i = archiveCreateCommands.begin();
  645. i != archiveCreateCommands.end(); ++i)
  646. {
  647. std::string cmd = *i;
  648. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  649. real_link_commands.push_back(cmd);
  650. }
  651. }
  652. // Append to the archive with the other object sets.
  653. for(++osi; osi != object_strings.end(); ++osi)
  654. {
  655. vars.Objects = osi->c_str();
  656. for(std::vector<std::string>::const_iterator
  657. i = archiveAppendCommands.begin();
  658. i != archiveAppendCommands.end(); ++i)
  659. {
  660. std::string cmd = *i;
  661. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  662. real_link_commands.push_back(cmd);
  663. }
  664. }
  665. // Finish the archive.
  666. vars.Objects = "";
  667. for(std::vector<std::string>::const_iterator
  668. i = archiveFinishCommands.begin();
  669. i != archiveFinishCommands.end(); ++i)
  670. {
  671. std::string cmd = *i;
  672. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  673. real_link_commands.push_back(cmd);
  674. }
  675. }
  676. else
  677. {
  678. // Get the set of commands.
  679. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  680. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  681. // Expand placeholders.
  682. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  683. i != real_link_commands.end(); ++i)
  684. {
  685. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  686. }
  687. }
  688. this->LocalGenerator->TargetImplib = "";
  689. // Restore path conversion to normal shells.
  690. this->LocalGenerator->SetLinkScriptShell(false);
  691. }
  692. // Optionally convert the build rule to use a script to avoid long
  693. // command lines in the make shell.
  694. if(useLinkScript)
  695. {
  696. // Use a link script.
  697. const char* name = (relink? "relink.txt" : "link.txt");
  698. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  699. }
  700. else
  701. {
  702. // No link script. Just use the link rule directly.
  703. commands1 = real_link_commands;
  704. }
  705. this->LocalGenerator->CreateCDCommand
  706. (commands1,
  707. this->Makefile->GetStartOutputDirectory(),
  708. cmLocalGenerator::HOME_OUTPUT);
  709. commands.insert(commands.end(), commands1.begin(), commands1.end());
  710. commands1.clear();
  711. // Add a rule to create necessary symlinks for the library.
  712. if(targetOutPath != targetOutPathReal)
  713. {
  714. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  715. symlink += targetOutPathReal;
  716. symlink += " ";
  717. symlink += targetOutPathSO;
  718. symlink += " ";
  719. symlink += targetOutPath;
  720. commands1.push_back(symlink);
  721. this->LocalGenerator->CreateCDCommand(commands1,
  722. this->Makefile->GetStartOutputDirectory(),
  723. cmLocalGenerator::HOME_OUTPUT);
  724. commands.insert(commands.end(), commands1.begin(), commands1.end());
  725. commands1.clear();
  726. }
  727. // Add the post-build rules when building but not when relinking.
  728. if(!relink)
  729. {
  730. this->LocalGenerator->
  731. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  732. this->Target);
  733. }
  734. // Write the build rule.
  735. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  736. targetFullPathReal.c_str(),
  737. depends, commands, false);
  738. // Some targets have more than one output file. Create rules to
  739. // drive the build if any extra outputs are missing.
  740. std::vector<std::string> extraOutputs;
  741. if(targetNameSO != targetNameReal)
  742. {
  743. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  744. targetFullPathReal.c_str());
  745. }
  746. if(targetName != targetNameSO &&
  747. targetName != targetNameReal)
  748. {
  749. this->GenerateExtraOutput(targetFullPath.c_str(),
  750. targetFullPathReal.c_str());
  751. }
  752. // Write the main driver rule to build everything in this target.
  753. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  754. // Clean all the possible library names and symlinks.
  755. this->CleanFiles.insert(this->CleanFiles.end(),
  756. libCleanFiles.begin(),libCleanFiles.end());
  757. }
  758. //----------------------------------------------------------------------------
  759. void
  760. cmMakefileLibraryTargetGenerator
  761. ::AppendOSXVerFlag(std::string& flags, const char* lang,
  762. const char* name, bool so)
  763. {
  764. // Lookup the flag to specify the version.
  765. std::string fvar = "CMAKE_";
  766. fvar += lang;
  767. fvar += "_OSX_";
  768. fvar += name;
  769. fvar += "_VERSION_FLAG";
  770. const char* flag = this->Makefile->GetDefinition(fvar.c_str());
  771. // Skip if no such flag.
  772. if(!flag)
  773. {
  774. return;
  775. }
  776. // Lookup the target version information.
  777. int major;
  778. int minor;
  779. int patch;
  780. this->Target->GetTargetVersion(so, major, minor, patch);
  781. if(major > 0 || minor > 0 || patch > 0)
  782. {
  783. // Append the flag since a non-zero version is specified.
  784. cmOStringStream vflag;
  785. vflag << flag << major << "." << minor << "." << patch;
  786. this->LocalGenerator->AppendFlags(flags, vflag.str().c_str());
  787. }
  788. }