cmMakefileLibraryTargetGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMakefileLibraryTargetGenerator.h"
  4. #include "cmGeneratedFileStream.h"
  5. #include "cmGeneratorTarget.h"
  6. #include "cmGlobalUnixMakefileGenerator3.h"
  7. #include "cmLocalGenerator.h"
  8. #include "cmLocalUnixMakefileGenerator3.h"
  9. #include "cmMakefile.h"
  10. #include "cmOSXBundleGenerator.h"
  11. #include "cmOutputConverter.h"
  12. #include "cmState.h"
  13. #include "cmSystemTools.h"
  14. #include "cmake.h"
  15. #include <sstream>
  16. #include <vector>
  17. cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator(
  18. cmGeneratorTarget* target)
  19. : cmMakefileTargetGenerator(target)
  20. {
  21. this->CustomCommandDriver = OnDepends;
  22. if (this->GeneratorTarget->GetType() != cmState::INTERFACE_LIBRARY) {
  23. this->GeneratorTarget->GetLibraryNames(
  24. this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
  25. this->TargetNameImport, this->TargetNamePDB, this->ConfigName);
  26. }
  27. this->OSXBundleGenerator =
  28. new cmOSXBundleGenerator(target, this->ConfigName);
  29. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  30. }
  31. cmMakefileLibraryTargetGenerator::~cmMakefileLibraryTargetGenerator()
  32. {
  33. delete this->OSXBundleGenerator;
  34. }
  35. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  36. {
  37. // create the build.make file and directory, put in the common blocks
  38. this->CreateRuleFile();
  39. // write rules used to help build object files
  40. this->WriteCommonCodeRules();
  41. // write the per-target per-language flags
  42. this->WriteTargetLanguageFlags();
  43. // write in rules for object files and custom commands
  44. this->WriteTargetBuildRules();
  45. // write the link rules
  46. // Write the rule for this target type.
  47. switch (this->GeneratorTarget->GetType()) {
  48. case cmState::STATIC_LIBRARY:
  49. this->WriteStaticLibraryRules();
  50. break;
  51. case cmState::SHARED_LIBRARY:
  52. this->WriteSharedLibraryRules(false);
  53. if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
  54. // Write rules to link an installable version of the target.
  55. this->WriteSharedLibraryRules(true);
  56. }
  57. break;
  58. case cmState::MODULE_LIBRARY:
  59. this->WriteModuleLibraryRules(false);
  60. if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
  61. // Write rules to link an installable version of the target.
  62. this->WriteModuleLibraryRules(true);
  63. }
  64. break;
  65. case cmState::OBJECT_LIBRARY:
  66. this->WriteObjectLibraryRules();
  67. break;
  68. default:
  69. // If language is not known, this is an error.
  70. cmSystemTools::Error("Unknown Library Type");
  71. break;
  72. }
  73. // Write the requires target.
  74. this->WriteTargetRequiresRules();
  75. // Write clean target
  76. this->WriteTargetCleanRules();
  77. // Write the dependency generation rule. This must be done last so
  78. // that multiple output pair information is available.
  79. this->WriteTargetDependRules();
  80. // close the streams
  81. this->CloseFileStreams();
  82. }
  83. void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
  84. {
  85. std::vector<std::string> commands;
  86. std::vector<std::string> depends;
  87. // Add post-build rules.
  88. this->LocalGenerator->AppendCustomCommands(
  89. commands, this->GeneratorTarget->GetPostBuildCommands(),
  90. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  91. // Depend on the object files.
  92. this->AppendObjectDepends(depends);
  93. // Write the rule.
  94. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  95. this->GeneratorTarget->GetName(),
  96. depends, commands, true);
  97. // Write the main driver rule to build everything in this target.
  98. this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
  99. }
  100. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  101. {
  102. std::string linkLanguage =
  103. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  104. std::string linkRuleVar = "CMAKE_";
  105. linkRuleVar += linkLanguage;
  106. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  107. if (this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
  108. this->Makefile->GetDefinition(linkRuleVar + "_IPO")) {
  109. linkRuleVar += "_IPO";
  110. }
  111. std::string extraFlags;
  112. this->LocalGenerator->GetStaticLibraryFlags(
  113. extraFlags, cmSystemTools::UpperCase(this->ConfigName),
  114. this->GeneratorTarget);
  115. this->WriteLibraryRules(linkRuleVar, extraFlags, false);
  116. }
  117. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  118. {
  119. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  120. this->WriteFrameworkRules(relink);
  121. return;
  122. }
  123. std::string linkLanguage =
  124. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  125. std::string linkRuleVar = "CMAKE_";
  126. linkRuleVar += linkLanguage;
  127. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  128. std::string extraFlags;
  129. this->LocalGenerator->AppendFlags(
  130. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  131. std::string linkFlagsConfig = "LINK_FLAGS_";
  132. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  133. this->LocalGenerator->AppendFlags(
  134. extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  135. this->LocalGenerator->AddConfigVariableFlags(
  136. extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  137. this->AddModuleDefinitionFlag(extraFlags);
  138. if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE")) {
  139. this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed");
  140. }
  141. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  142. }
  143. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  144. {
  145. std::string linkLanguage =
  146. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  147. std::string linkRuleVar = "CMAKE_";
  148. linkRuleVar += linkLanguage;
  149. linkRuleVar += "_CREATE_SHARED_MODULE";
  150. std::string extraFlags;
  151. this->LocalGenerator->AppendFlags(
  152. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  153. std::string linkFlagsConfig = "LINK_FLAGS_";
  154. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  155. this->LocalGenerator->AppendFlags(
  156. extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  157. this->LocalGenerator->AddConfigVariableFlags(
  158. extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  159. this->AddModuleDefinitionFlag(extraFlags);
  160. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  161. }
  162. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  163. {
  164. std::string linkLanguage =
  165. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  166. std::string linkRuleVar = "CMAKE_";
  167. linkRuleVar += linkLanguage;
  168. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  169. std::string extraFlags;
  170. this->LocalGenerator->AppendFlags(
  171. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  172. std::string linkFlagsConfig = "LINK_FLAGS_";
  173. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  174. this->LocalGenerator->AppendFlags(
  175. extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  176. this->LocalGenerator->AddConfigVariableFlags(
  177. extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  178. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  179. }
  180. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  181. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  182. {
  183. // TODO: Merge the methods that call this method to avoid
  184. // code duplication.
  185. std::vector<std::string> commands;
  186. // Build list of dependencies.
  187. std::vector<std::string> depends;
  188. this->AppendLinkDepends(depends);
  189. // Get the language to use for linking this library.
  190. std::string linkLanguage =
  191. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  192. // Make sure we have a link language.
  193. if (linkLanguage.empty()) {
  194. cmSystemTools::Error("Cannot determine link language for target \"",
  195. this->GeneratorTarget->GetName().c_str(), "\".");
  196. return;
  197. }
  198. // Create set of linking flags.
  199. std::string linkFlags;
  200. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  201. // Add OSX version flags, if any.
  202. if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
  203. this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY) {
  204. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  205. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  206. }
  207. // Construct the name of the library.
  208. std::string targetName;
  209. std::string targetNameSO;
  210. std::string targetNameReal;
  211. std::string targetNameImport;
  212. std::string targetNamePDB;
  213. this->GeneratorTarget->GetLibraryNames(targetName, targetNameSO,
  214. targetNameReal, targetNameImport,
  215. targetNamePDB, this->ConfigName);
  216. // Construct the full path version of the names.
  217. std::string outpath;
  218. std::string outpathImp;
  219. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  220. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  221. this->OSXBundleGenerator->CreateFramework(targetName, outpath);
  222. outpath += "/";
  223. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  224. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  225. this->OSXBundleGenerator->CreateCFBundle(targetName, outpath);
  226. outpath += "/";
  227. } else if (relink) {
  228. outpath = this->Makefile->GetCurrentBinaryDirectory();
  229. outpath += cmake::GetCMakeFilesDirectory();
  230. outpath += "/CMakeRelink.dir";
  231. cmSystemTools::MakeDirectory(outpath.c_str());
  232. outpath += "/";
  233. if (!targetNameImport.empty()) {
  234. outpathImp = outpath;
  235. }
  236. } else {
  237. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  238. cmSystemTools::MakeDirectory(outpath.c_str());
  239. outpath += "/";
  240. if (!targetNameImport.empty()) {
  241. outpathImp = this->GeneratorTarget->GetDirectory(this->ConfigName, true);
  242. cmSystemTools::MakeDirectory(outpathImp.c_str());
  243. outpathImp += "/";
  244. }
  245. }
  246. std::string compilePdbOutputPath =
  247. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  248. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  249. std::string pdbOutputPath =
  250. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  251. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  252. pdbOutputPath += "/";
  253. std::string targetFullPath = outpath + targetName;
  254. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  255. std::string targetFullPathSO = outpath + targetNameSO;
  256. std::string targetFullPathReal = outpath + targetNameReal;
  257. std::string targetFullPathImport = outpathImp + targetNameImport;
  258. // Construct the output path version of the names for use in command
  259. // arguments.
  260. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  261. targetFullPathPDB, cmOutputConverter::SHELL);
  262. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  263. this->LocalGenerator->MaybeConvertToRelativePath(
  264. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  265. cmOutputConverter::SHELL);
  266. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  267. this->LocalGenerator->MaybeConvertToRelativePath(
  268. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO),
  269. cmOutputConverter::SHELL);
  270. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  271. this->LocalGenerator->MaybeConvertToRelativePath(
  272. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  273. cmOutputConverter::SHELL);
  274. std::string targetOutPathImport =
  275. this->LocalGenerator->ConvertToOutputFormat(
  276. this->LocalGenerator->MaybeConvertToRelativePath(
  277. this->LocalGenerator->GetCurrentBinaryDirectory(),
  278. targetFullPathImport),
  279. cmOutputConverter::SHELL);
  280. this->NumberOfProgressActions++;
  281. if (!this->NoRuleMessages) {
  282. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  283. this->MakeEchoProgress(progress);
  284. // Add the link message.
  285. std::string buildEcho = "Linking ";
  286. buildEcho += linkLanguage;
  287. switch (this->GeneratorTarget->GetType()) {
  288. case cmState::STATIC_LIBRARY:
  289. buildEcho += " static library ";
  290. break;
  291. case cmState::SHARED_LIBRARY:
  292. buildEcho += " shared library ";
  293. break;
  294. case cmState::MODULE_LIBRARY:
  295. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  296. buildEcho += " CFBundle";
  297. }
  298. buildEcho += " shared module ";
  299. break;
  300. default:
  301. buildEcho += " library ";
  302. break;
  303. }
  304. buildEcho += targetOutPath;
  305. this->LocalGenerator->AppendEcho(
  306. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  307. }
  308. // Clean files associated with this library.
  309. std::vector<std::string> libCleanFiles;
  310. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  311. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  312. if (targetNameReal != targetName) {
  313. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  314. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  315. }
  316. if (targetNameSO != targetName && targetNameSO != targetNameReal) {
  317. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  318. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO));
  319. }
  320. if (!targetNameImport.empty()) {
  321. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  322. this->LocalGenerator->GetCurrentBinaryDirectory(),
  323. targetFullPathImport));
  324. std::string implib;
  325. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  326. implib)) {
  327. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  328. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  329. }
  330. }
  331. // List the PDB for cleaning only when the whole target is
  332. // cleaned. We do not want to delete the .pdb file just before
  333. // linking the target.
  334. this->CleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  335. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  336. #ifdef _WIN32
  337. // There may be a manifest file for this target. Add it to the
  338. // clean set just in case.
  339. if (this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY) {
  340. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  341. this->LocalGenerator->GetCurrentBinaryDirectory(),
  342. (targetFullPath + ".manifest").c_str()));
  343. }
  344. #endif
  345. std::vector<std::string> commands1;
  346. // Add a command to remove any existing files for this library.
  347. // for static libs only
  348. if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY) {
  349. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  350. this->GeneratorTarget, "target");
  351. this->LocalGenerator->CreateCDCommand(
  352. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  353. this->LocalGenerator->GetBinaryDirectory());
  354. commands.insert(commands.end(), commands1.begin(), commands1.end());
  355. commands1.clear();
  356. }
  357. // Add the pre-build and pre-link rules building but not when relinking.
  358. if (!relink) {
  359. this->LocalGenerator->AppendCustomCommands(
  360. commands, this->GeneratorTarget->GetPreBuildCommands(),
  361. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  362. this->LocalGenerator->AppendCustomCommands(
  363. commands, this->GeneratorTarget->GetPreLinkCommands(),
  364. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  365. }
  366. // Determine whether a link script will be used.
  367. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  368. bool useResponseFileForObjects =
  369. this->CheckUseResponseFileForObjects(linkLanguage);
  370. bool const useResponseFileForLibs =
  371. this->CheckUseResponseFileForLibraries(linkLanguage);
  372. // For static libraries there might be archiving rules.
  373. bool haveStaticLibraryRule = false;
  374. std::vector<std::string> archiveCreateCommands;
  375. std::vector<std::string> archiveAppendCommands;
  376. std::vector<std::string> archiveFinishCommands;
  377. std::string::size_type archiveCommandLimit = std::string::npos;
  378. if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY) {
  379. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  380. std::string arCreateVar = "CMAKE_";
  381. arCreateVar += linkLanguage;
  382. arCreateVar += "_ARCHIVE_CREATE";
  383. if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) {
  384. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  385. }
  386. std::string arAppendVar = "CMAKE_";
  387. arAppendVar += linkLanguage;
  388. arAppendVar += "_ARCHIVE_APPEND";
  389. if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) {
  390. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  391. }
  392. std::string arFinishVar = "CMAKE_";
  393. arFinishVar += linkLanguage;
  394. arFinishVar += "_ARCHIVE_FINISH";
  395. if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) {
  396. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  397. }
  398. }
  399. // Decide whether to use archiving rules.
  400. bool useArchiveRules = !haveStaticLibraryRule &&
  401. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  402. if (useArchiveRules) {
  403. // Archiving rules are always run with a link script.
  404. useLinkScript = true;
  405. // Archiving rules never use a response file.
  406. useResponseFileForObjects = false;
  407. // Limit the length of individual object lists to less than the
  408. // 32K command line length limit on Windows. We could make this a
  409. // platform file variable but this should work everywhere.
  410. archiveCommandLimit = 30000;
  411. }
  412. // Expand the rule variables.
  413. std::vector<std::string> real_link_commands;
  414. {
  415. bool useWatcomQuote =
  416. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  417. // Set path conversion for link script shells.
  418. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  419. // Collect up flags to link in needed libraries.
  420. std::string linkLibs;
  421. if (this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY) {
  422. this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
  423. useWatcomQuote);
  424. }
  425. // Construct object file lists that may be needed to expand the
  426. // rule.
  427. std::string buildObjs;
  428. this->CreateObjectLists(useLinkScript, useArchiveRules,
  429. useResponseFileForObjects, buildObjs, depends,
  430. useWatcomQuote);
  431. // maybe create .def file from list of objects
  432. if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY &&
  433. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  434. this->GenDefFile(real_link_commands, linkFlags);
  435. }
  436. std::string manifests = this->GetManifests();
  437. cmLocalGenerator::RuleVariables vars;
  438. vars.TargetPDB = targetOutPathPDB.c_str();
  439. // Setup the target version.
  440. std::string targetVersionMajor;
  441. std::string targetVersionMinor;
  442. {
  443. std::ostringstream majorStream;
  444. std::ostringstream minorStream;
  445. int major;
  446. int minor;
  447. this->GeneratorTarget->GetTargetVersion(major, minor);
  448. majorStream << major;
  449. minorStream << minor;
  450. targetVersionMajor = majorStream.str();
  451. targetVersionMinor = minorStream.str();
  452. }
  453. vars.TargetVersionMajor = targetVersionMajor.c_str();
  454. vars.TargetVersionMinor = targetVersionMinor.c_str();
  455. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  456. vars.CMTarget = this->GeneratorTarget;
  457. vars.Language = linkLanguage.c_str();
  458. vars.Objects = buildObjs.c_str();
  459. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  460. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  461. this->LocalGenerator->MaybeConvertToRelativePath(
  462. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  463. cmOutputConverter::SHELL);
  464. vars.ObjectDir = objectDir.c_str();
  465. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  466. ? cmOutputConverter::WATCOMQUOTE
  467. : cmOutputConverter::SHELL;
  468. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  469. this->LocalGenerator->MaybeConvertToRelativePath(
  470. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  471. output);
  472. vars.Target = target.c_str();
  473. vars.LinkLibraries = linkLibs.c_str();
  474. vars.ObjectsQuoted = buildObjs.c_str();
  475. if (this->GeneratorTarget->HasSOName(this->ConfigName)) {
  476. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  477. vars.TargetSOName = targetNameSO.c_str();
  478. }
  479. vars.LinkFlags = linkFlags.c_str();
  480. vars.Manifests = manifests.c_str();
  481. // Compute the directory portion of the install_name setting.
  482. std::string install_name_dir;
  483. if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY) {
  484. // Get the install_name directory for the build tree.
  485. install_name_dir =
  486. this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);
  487. // Set the rule variable replacement value.
  488. if (install_name_dir.empty()) {
  489. vars.TargetInstallNameDir = "";
  490. } else {
  491. // Convert to a path for the native build tool.
  492. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  493. install_name_dir, cmOutputConverter::SHELL);
  494. vars.TargetInstallNameDir = install_name_dir.c_str();
  495. }
  496. }
  497. // Add language feature flags.
  498. std::string langFlags;
  499. this->AddFeatureFlags(langFlags, linkLanguage);
  500. this->LocalGenerator->AddArchitectureFlags(
  501. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  502. vars.LanguageCompileFlags = langFlags.c_str();
  503. // Construct the main link rule and expand placeholders.
  504. this->LocalGenerator->TargetImplib = targetOutPathImport;
  505. if (useArchiveRules) {
  506. // Construct the individual object list strings.
  507. std::vector<std::string> object_strings;
  508. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  509. // Create the archive with the first set of objects.
  510. std::vector<std::string>::iterator osi = object_strings.begin();
  511. {
  512. vars.Objects = osi->c_str();
  513. for (std::vector<std::string>::const_iterator i =
  514. archiveCreateCommands.begin();
  515. i != archiveCreateCommands.end(); ++i) {
  516. std::string cmd = *i;
  517. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  518. real_link_commands.push_back(cmd);
  519. }
  520. }
  521. // Append to the archive with the other object sets.
  522. for (++osi; osi != object_strings.end(); ++osi) {
  523. vars.Objects = osi->c_str();
  524. for (std::vector<std::string>::const_iterator i =
  525. archiveAppendCommands.begin();
  526. i != archiveAppendCommands.end(); ++i) {
  527. std::string cmd = *i;
  528. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  529. real_link_commands.push_back(cmd);
  530. }
  531. }
  532. // Finish the archive.
  533. vars.Objects = "";
  534. for (std::vector<std::string>::const_iterator i =
  535. archiveFinishCommands.begin();
  536. i != archiveFinishCommands.end(); ++i) {
  537. std::string cmd = *i;
  538. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  539. // If there is no ranlib the command will be ":". Skip it.
  540. if (!cmd.empty() && cmd[0] != ':') {
  541. real_link_commands.push_back(cmd);
  542. }
  543. }
  544. } else {
  545. // Get the set of commands.
  546. std::string linkRule = this->GetLinkRule(linkRuleVar);
  547. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  548. if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE") &&
  549. (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY)) {
  550. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  551. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  552. cmakeCommand += " -E __run_iwyu --lwyu=";
  553. cmakeCommand += targetOutPathReal;
  554. real_link_commands.push_back(cmakeCommand);
  555. }
  556. // Expand placeholders.
  557. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  558. i != real_link_commands.end(); ++i) {
  559. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  560. }
  561. }
  562. this->LocalGenerator->TargetImplib = "";
  563. // Restore path conversion to normal shells.
  564. this->LocalGenerator->SetLinkScriptShell(false);
  565. }
  566. // Optionally convert the build rule to use a script to avoid long
  567. // command lines in the make shell.
  568. if (useLinkScript) {
  569. // Use a link script.
  570. const char* name = (relink ? "relink.txt" : "link.txt");
  571. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  572. } else {
  573. // No link script. Just use the link rule directly.
  574. commands1 = real_link_commands;
  575. }
  576. this->LocalGenerator->CreateCDCommand(
  577. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  578. this->LocalGenerator->GetBinaryDirectory());
  579. commands.insert(commands.end(), commands1.begin(), commands1.end());
  580. commands1.clear();
  581. // Add a rule to create necessary symlinks for the library.
  582. // Frameworks are handled by cmOSXBundleGenerator.
  583. if (targetOutPath != targetOutPathReal &&
  584. !this->GeneratorTarget->IsFrameworkOnApple()) {
  585. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  586. symlink += targetOutPathReal;
  587. symlink += " ";
  588. symlink += targetOutPathSO;
  589. symlink += " ";
  590. symlink += targetOutPath;
  591. commands1.push_back(symlink);
  592. this->LocalGenerator->CreateCDCommand(
  593. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  594. this->LocalGenerator->GetBinaryDirectory());
  595. commands.insert(commands.end(), commands1.begin(), commands1.end());
  596. commands1.clear();
  597. }
  598. // Add the post-build rules when building but not when relinking.
  599. if (!relink) {
  600. this->LocalGenerator->AppendCustomCommands(
  601. commands, this->GeneratorTarget->GetPostBuildCommands(),
  602. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  603. }
  604. // Compute the list of outputs.
  605. std::vector<std::string> outputs(1, targetFullPathReal);
  606. if (targetNameSO != targetNameReal) {
  607. outputs.push_back(targetFullPathSO);
  608. }
  609. if (targetName != targetNameSO && targetName != targetNameReal) {
  610. outputs.push_back(targetFullPath);
  611. }
  612. // Write the build rule.
  613. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  614. commands, false);
  615. // Write the main driver rule to build everything in this target.
  616. this->WriteTargetDriverRule(targetFullPath, relink);
  617. // Clean all the possible library names and symlinks.
  618. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  619. libCleanFiles.end());
  620. }