cmMakefileLibraryTargetGenerator.cxx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 <memory>
  5. #include <set>
  6. #include <sstream>
  7. #include <stddef.h>
  8. #include <utility>
  9. #include <vector>
  10. #include "cmAlgorithms.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalUnixMakefileGenerator3.h"
  14. #include "cmLinkLineComputer.h"
  15. #include "cmLinkLineDeviceComputer.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmLocalUnixMakefileGenerator3.h"
  18. #include "cmMakefile.h"
  19. #include "cmOSXBundleGenerator.h"
  20. #include "cmOutputConverter.h"
  21. #include "cmRulePlaceholderExpander.h"
  22. #include "cmState.h"
  23. #include "cmStateDirectory.h"
  24. #include "cmStateSnapshot.h"
  25. #include "cmStateTypes.h"
  26. #include "cmSystemTools.h"
  27. cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator(
  28. cmGeneratorTarget* target)
  29. : cmMakefileTargetGenerator(target)
  30. {
  31. this->CustomCommandDriver = OnDepends;
  32. if (this->GeneratorTarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  33. this->TargetNames =
  34. this->GeneratorTarget->GetLibraryNames(this->ConfigName);
  35. }
  36. this->OSXBundleGenerator =
  37. cm::make_unique<cmOSXBundleGenerator>(target, this->ConfigName);
  38. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  39. }
  40. cmMakefileLibraryTargetGenerator::~cmMakefileLibraryTargetGenerator() =
  41. default;
  42. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  43. {
  44. // create the build.make file and directory, put in the common blocks
  45. this->CreateRuleFile();
  46. // write rules used to help build object files
  47. this->WriteCommonCodeRules();
  48. // write the per-target per-language flags
  49. this->WriteTargetLanguageFlags();
  50. // write in rules for object files and custom commands
  51. this->WriteTargetBuildRules();
  52. // write the link rules
  53. // Write the rule for this target type.
  54. switch (this->GeneratorTarget->GetType()) {
  55. case cmStateEnums::STATIC_LIBRARY:
  56. this->WriteStaticLibraryRules();
  57. break;
  58. case cmStateEnums::SHARED_LIBRARY:
  59. this->WriteSharedLibraryRules(false);
  60. if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
  61. // Write rules to link an installable version of the target.
  62. this->WriteSharedLibraryRules(true);
  63. }
  64. break;
  65. case cmStateEnums::MODULE_LIBRARY:
  66. this->WriteModuleLibraryRules(false);
  67. if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
  68. // Write rules to link an installable version of the target.
  69. this->WriteModuleLibraryRules(true);
  70. }
  71. break;
  72. case cmStateEnums::OBJECT_LIBRARY:
  73. this->WriteObjectLibraryRules();
  74. break;
  75. default:
  76. // If language is not known, this is an error.
  77. cmSystemTools::Error("Unknown Library Type");
  78. break;
  79. }
  80. // Write clean target
  81. this->WriteTargetCleanRules();
  82. // Write the dependency generation rule. This must be done last so
  83. // that multiple output pair information is available.
  84. this->WriteTargetDependRules();
  85. // close the streams
  86. this->CloseFileStreams();
  87. }
  88. void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
  89. {
  90. std::vector<std::string> commands;
  91. std::vector<std::string> depends;
  92. // Add post-build rules.
  93. this->LocalGenerator->AppendCustomCommands(
  94. commands, this->GeneratorTarget->GetPostBuildCommands(),
  95. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  96. // Depend on the object files.
  97. this->AppendObjectDepends(depends);
  98. // Write the rule.
  99. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  100. this->GeneratorTarget->GetName(),
  101. depends, commands, true);
  102. // Write the main driver rule to build everything in this target.
  103. this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
  104. }
  105. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  106. {
  107. const bool requiresDeviceLinking = requireDeviceLinking(
  108. *this->GeneratorTarget, *this->LocalGenerator, this->ConfigName);
  109. if (requiresDeviceLinking) {
  110. std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY";
  111. this->WriteDeviceLibraryRules(linkRuleVar, false);
  112. }
  113. std::string linkLanguage =
  114. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  115. std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  116. linkLanguage, this->ConfigName);
  117. std::string extraFlags;
  118. this->LocalGenerator->GetStaticLibraryFlags(
  119. extraFlags, cmSystemTools::UpperCase(this->ConfigName), linkLanguage,
  120. this->GeneratorTarget);
  121. this->WriteLibraryRules(linkRuleVar, extraFlags, false);
  122. }
  123. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  124. {
  125. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  126. this->WriteFrameworkRules(relink);
  127. return;
  128. }
  129. if (!relink) {
  130. const bool requiresDeviceLinking = requireDeviceLinking(
  131. *this->GeneratorTarget, *this->LocalGenerator, this->ConfigName);
  132. if (requiresDeviceLinking) {
  133. std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY";
  134. this->WriteDeviceLibraryRules(linkRuleVar, relink);
  135. }
  136. }
  137. std::string linkLanguage =
  138. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  139. std::string linkRuleVar = "CMAKE_";
  140. linkRuleVar += linkLanguage;
  141. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  142. std::string extraFlags;
  143. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  144. this->LocalGenerator->AddConfigVariableFlags(
  145. extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  146. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  147. this->CreateLinkLineComputer(
  148. this->LocalGenerator,
  149. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  150. this->AddModuleDefinitionFlag(linkLineComputer.get(), extraFlags);
  151. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  152. this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed");
  153. }
  154. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  155. }
  156. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  157. {
  158. if (!relink) {
  159. const bool requiresDeviceLinking = requireDeviceLinking(
  160. *this->GeneratorTarget, *this->LocalGenerator, this->ConfigName);
  161. if (requiresDeviceLinking) {
  162. std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY";
  163. this->WriteDeviceLibraryRules(linkRuleVar, relink);
  164. }
  165. }
  166. std::string linkLanguage =
  167. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  168. std::string linkRuleVar = "CMAKE_";
  169. linkRuleVar += linkLanguage;
  170. linkRuleVar += "_CREATE_SHARED_MODULE";
  171. std::string extraFlags;
  172. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  173. this->LocalGenerator->AddConfigVariableFlags(
  174. extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  175. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  176. this->CreateLinkLineComputer(
  177. this->LocalGenerator,
  178. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  179. this->AddModuleDefinitionFlag(linkLineComputer.get(), extraFlags);
  180. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  181. }
  182. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  183. {
  184. std::string linkLanguage =
  185. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  186. std::string linkRuleVar = "CMAKE_";
  187. linkRuleVar += linkLanguage;
  188. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  189. std::string extraFlags;
  190. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  191. this->LocalGenerator->AddConfigVariableFlags(
  192. extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  193. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  194. }
  195. void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules(
  196. const std::string& linkRuleVar, bool relink)
  197. {
  198. #ifdef CMAKE_BUILD_WITH_CMAKE
  199. // TODO: Merge the methods that call this method to avoid
  200. // code duplication.
  201. std::vector<std::string> commands;
  202. // Get the language to use for linking this library.
  203. std::string linkLanguage = "CUDA";
  204. std::string const objExt =
  205. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  206. // Build list of dependencies.
  207. std::vector<std::string> depends;
  208. this->AppendLinkDepends(depends, linkLanguage);
  209. // Create set of linking flags.
  210. std::string linkFlags;
  211. this->GetTargetLinkFlags(linkFlags, linkLanguage);
  212. // Get the name of the device object to generate.
  213. std::string const targetOutputReal =
  214. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  215. this->DeviceLinkObject = targetOutputReal;
  216. this->NumberOfProgressActions++;
  217. if (!this->NoRuleMessages) {
  218. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  219. this->MakeEchoProgress(progress);
  220. // Add the link message.
  221. std::string buildEcho = "Linking " + linkLanguage + " device code ";
  222. buildEcho += this->LocalGenerator->ConvertToOutputFormat(
  223. this->LocalGenerator->MaybeConvertToRelativePath(
  224. this->LocalGenerator->GetCurrentBinaryDirectory(),
  225. this->DeviceLinkObject),
  226. cmOutputConverter::SHELL);
  227. this->LocalGenerator->AppendEcho(
  228. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  229. }
  230. // Clean files associated with this library.
  231. std::set<std::string> libCleanFiles;
  232. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  233. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal));
  234. // Determine whether a link script will be used.
  235. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  236. bool useResponseFileForObjects =
  237. this->CheckUseResponseFileForObjects(linkLanguage);
  238. bool const useResponseFileForLibs =
  239. this->CheckUseResponseFileForLibraries(linkLanguage);
  240. cmRulePlaceholderExpander::RuleVariables vars;
  241. vars.Language = linkLanguage.c_str();
  242. // Expand the rule variables.
  243. std::vector<std::string> real_link_commands;
  244. {
  245. bool useWatcomQuote =
  246. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  247. // Set path conversion for link script shells.
  248. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  249. // Collect up flags to link in needed libraries.
  250. std::string linkLibs;
  251. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  252. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  253. new cmLinkLineDeviceComputer(
  254. this->LocalGenerator,
  255. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  256. linkLineComputer->SetForResponse(useResponseFileForLibs);
  257. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  258. linkLineComputer->SetRelink(relink);
  259. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  260. useResponseFileForLibs, depends);
  261. }
  262. // Construct object file lists that may be needed to expand the
  263. // rule.
  264. std::string buildObjs;
  265. this->CreateObjectLists(useLinkScript, false, // useArchiveRules
  266. useResponseFileForObjects, buildObjs, depends,
  267. useWatcomQuote);
  268. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  269. ? cmOutputConverter::WATCOMQUOTE
  270. : cmOutputConverter::SHELL;
  271. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  272. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  273. this->LocalGenerator->MaybeConvertToRelativePath(
  274. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  275. cmOutputConverter::SHELL);
  276. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  277. this->LocalGenerator->MaybeConvertToRelativePath(
  278. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal),
  279. output);
  280. std::string targetFullPathCompilePDB = this->ComputeTargetCompilePDB();
  281. std::string targetOutPathCompilePDB =
  282. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  283. cmOutputConverter::SHELL);
  284. vars.Objects = buildObjs.c_str();
  285. vars.ObjectDir = objectDir.c_str();
  286. vars.Target = target.c_str();
  287. vars.LinkLibraries = linkLibs.c_str();
  288. vars.ObjectsQuoted = buildObjs.c_str();
  289. vars.LinkFlags = linkFlags.c_str();
  290. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  291. // Add language-specific flags.
  292. std::string langFlags;
  293. this->LocalGenerator->AddLanguageFlagsForLinking(
  294. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  295. vars.LanguageCompileFlags = langFlags.c_str();
  296. std::string launcher;
  297. const char* val = this->LocalGenerator->GetRuleLauncher(
  298. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  299. if (val && *val) {
  300. launcher = val;
  301. launcher += " ";
  302. }
  303. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  304. this->LocalGenerator->CreateRulePlaceholderExpander());
  305. // Construct the main link rule and expand placeholders.
  306. rulePlaceholderExpander->SetTargetImpLib(targetOutputReal);
  307. std::string linkRule = this->GetLinkRule(linkRuleVar);
  308. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  309. // Expand placeholders.
  310. for (std::string& real_link_command : real_link_commands) {
  311. real_link_command = launcher + real_link_command;
  312. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  313. real_link_command, vars);
  314. }
  315. // Restore path conversion to normal shells.
  316. this->LocalGenerator->SetLinkScriptShell(false);
  317. // Clean all the possible library names and symlinks.
  318. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  319. }
  320. std::vector<std::string> commands1;
  321. // Optionally convert the build rule to use a script to avoid long
  322. // command lines in the make shell.
  323. if (useLinkScript) {
  324. // Use a link script.
  325. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  326. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  327. } else {
  328. // No link script. Just use the link rule directly.
  329. commands1 = real_link_commands;
  330. }
  331. this->LocalGenerator->CreateCDCommand(
  332. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  333. this->LocalGenerator->GetBinaryDirectory());
  334. cmAppend(commands, commands1);
  335. commands1.clear();
  336. // Compute the list of outputs.
  337. std::vector<std::string> outputs(1, targetOutputReal);
  338. // Write the build rule.
  339. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  340. commands, false);
  341. // Write the main driver rule to build everything in this target.
  342. this->WriteTargetDriverRule(targetOutputReal, relink);
  343. #else
  344. static_cast<void>(linkRuleVar);
  345. static_cast<void>(relink);
  346. #endif
  347. }
  348. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  349. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  350. {
  351. // TODO: Merge the methods that call this method to avoid
  352. // code duplication.
  353. std::vector<std::string> commands;
  354. // Get the language to use for linking this library.
  355. std::string linkLanguage =
  356. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  357. // Make sure we have a link language.
  358. if (linkLanguage.empty()) {
  359. cmSystemTools::Error("Cannot determine link language for target \"" +
  360. this->GeneratorTarget->GetName() + "\".");
  361. return;
  362. }
  363. // Build list of dependencies.
  364. std::vector<std::string> depends;
  365. this->AppendLinkDepends(depends, linkLanguage);
  366. if (!this->DeviceLinkObject.empty()) {
  367. depends.push_back(this->DeviceLinkObject);
  368. }
  369. // Create set of linking flags.
  370. std::string linkFlags;
  371. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  372. this->LocalGenerator->AppendIPOLinkerFlags(linkFlags, this->GeneratorTarget,
  373. this->ConfigName, linkLanguage);
  374. // Add OSX version flags, if any.
  375. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  376. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  377. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  378. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  379. }
  380. // Construct the name of the library.
  381. this->GeneratorTarget->GetLibraryNames(this->ConfigName);
  382. // Construct the full path version of the names.
  383. std::string outpath;
  384. std::string outpathImp;
  385. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  386. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  387. this->OSXBundleGenerator->CreateFramework(this->TargetNames.Output,
  388. outpath);
  389. outpath += "/";
  390. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  391. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  392. this->OSXBundleGenerator->CreateCFBundle(this->TargetNames.Output,
  393. outpath);
  394. outpath += "/";
  395. } else if (relink) {
  396. outpath = this->Makefile->GetCurrentBinaryDirectory();
  397. outpath += "/CMakeFiles";
  398. outpath += "/CMakeRelink.dir";
  399. cmSystemTools::MakeDirectory(outpath);
  400. outpath += "/";
  401. if (!this->TargetNames.ImportLibrary.empty()) {
  402. outpathImp = outpath;
  403. }
  404. } else {
  405. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  406. cmSystemTools::MakeDirectory(outpath);
  407. outpath += "/";
  408. if (!this->TargetNames.ImportLibrary.empty()) {
  409. outpathImp = this->GeneratorTarget->GetDirectory(
  410. this->ConfigName, cmStateEnums::ImportLibraryArtifact);
  411. cmSystemTools::MakeDirectory(outpathImp);
  412. outpathImp += "/";
  413. }
  414. }
  415. std::string compilePdbOutputPath =
  416. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  417. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  418. std::string pdbOutputPath =
  419. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  420. cmSystemTools::MakeDirectory(pdbOutputPath);
  421. pdbOutputPath += "/";
  422. std::string targetFullPath = outpath + this->TargetNames.Output;
  423. std::string targetFullPathPDB = pdbOutputPath + this->TargetNames.PDB;
  424. std::string targetFullPathSO = outpath + this->TargetNames.SharedObject;
  425. std::string targetFullPathReal = outpath + this->TargetNames.Real;
  426. std::string targetFullPathImport =
  427. outpathImp + this->TargetNames.ImportLibrary;
  428. // Construct the output path version of the names for use in command
  429. // arguments.
  430. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  431. targetFullPathPDB, cmOutputConverter::SHELL);
  432. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  433. this->LocalGenerator->MaybeConvertToRelativePath(
  434. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  435. cmOutputConverter::SHELL);
  436. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  437. this->LocalGenerator->MaybeConvertToRelativePath(
  438. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO),
  439. cmOutputConverter::SHELL);
  440. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  441. this->LocalGenerator->MaybeConvertToRelativePath(
  442. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  443. cmOutputConverter::SHELL);
  444. std::string targetOutPathImport =
  445. this->LocalGenerator->ConvertToOutputFormat(
  446. this->LocalGenerator->MaybeConvertToRelativePath(
  447. this->LocalGenerator->GetCurrentBinaryDirectory(),
  448. targetFullPathImport),
  449. cmOutputConverter::SHELL);
  450. this->NumberOfProgressActions++;
  451. if (!this->NoRuleMessages) {
  452. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  453. this->MakeEchoProgress(progress);
  454. // Add the link message.
  455. std::string buildEcho = "Linking ";
  456. buildEcho += linkLanguage;
  457. switch (this->GeneratorTarget->GetType()) {
  458. case cmStateEnums::STATIC_LIBRARY:
  459. buildEcho += " static library ";
  460. break;
  461. case cmStateEnums::SHARED_LIBRARY:
  462. buildEcho += " shared library ";
  463. break;
  464. case cmStateEnums::MODULE_LIBRARY:
  465. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  466. buildEcho += " CFBundle";
  467. }
  468. buildEcho += " shared module ";
  469. break;
  470. default:
  471. buildEcho += " library ";
  472. break;
  473. }
  474. buildEcho += targetOutPath;
  475. this->LocalGenerator->AppendEcho(
  476. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  477. }
  478. // Clean files associated with this library.
  479. std::set<std::string> libCleanFiles;
  480. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  481. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  482. std::vector<std::string> commands1;
  483. // Add a command to remove any existing files for this library.
  484. // for static libs only
  485. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  486. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  487. this->GeneratorTarget, "target");
  488. this->LocalGenerator->CreateCDCommand(
  489. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  490. this->LocalGenerator->GetBinaryDirectory());
  491. cmAppend(commands, commands1);
  492. commands1.clear();
  493. }
  494. if (this->TargetNames.Output != this->TargetNames.Real) {
  495. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  496. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  497. }
  498. if (this->TargetNames.SharedObject != this->TargetNames.Real &&
  499. this->TargetNames.SharedObject != this->TargetNames.Output) {
  500. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  501. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO));
  502. }
  503. if (!this->TargetNames.ImportLibrary.empty()) {
  504. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  505. this->LocalGenerator->GetCurrentBinaryDirectory(),
  506. targetFullPathImport));
  507. std::string implib;
  508. if (this->GeneratorTarget->GetImplibGNUtoMS(
  509. this->ConfigName, targetFullPathImport, implib)) {
  510. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  511. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  512. }
  513. }
  514. // List the PDB for cleaning only when the whole target is
  515. // cleaned. We do not want to delete the .pdb file just before
  516. // linking the target.
  517. this->CleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  518. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  519. #ifdef _WIN32
  520. // There may be a manifest file for this target. Add it to the
  521. // clean set just in case.
  522. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  523. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  524. this->LocalGenerator->GetCurrentBinaryDirectory(),
  525. targetFullPath + ".manifest"));
  526. }
  527. #endif
  528. // Add the pre-build and pre-link rules building but not when relinking.
  529. if (!relink) {
  530. this->LocalGenerator->AppendCustomCommands(
  531. commands, this->GeneratorTarget->GetPreBuildCommands(),
  532. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  533. this->LocalGenerator->AppendCustomCommands(
  534. commands, this->GeneratorTarget->GetPreLinkCommands(),
  535. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  536. }
  537. // Determine whether a link script will be used.
  538. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  539. bool useResponseFileForObjects =
  540. this->CheckUseResponseFileForObjects(linkLanguage);
  541. bool const useResponseFileForLibs =
  542. this->CheckUseResponseFileForLibraries(linkLanguage);
  543. // For static libraries there might be archiving rules.
  544. bool haveStaticLibraryRule = false;
  545. std::vector<std::string> archiveCreateCommands;
  546. std::vector<std::string> archiveAppendCommands;
  547. std::vector<std::string> archiveFinishCommands;
  548. std::string::size_type archiveCommandLimit = std::string::npos;
  549. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  550. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  551. std::string arCreateVar = "CMAKE_";
  552. arCreateVar += linkLanguage;
  553. arCreateVar += "_ARCHIVE_CREATE";
  554. arCreateVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  555. arCreateVar, linkLanguage, this->ConfigName);
  556. if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) {
  557. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  558. }
  559. std::string arAppendVar = "CMAKE_";
  560. arAppendVar += linkLanguage;
  561. arAppendVar += "_ARCHIVE_APPEND";
  562. arAppendVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  563. arAppendVar, linkLanguage, this->ConfigName);
  564. if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) {
  565. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  566. }
  567. std::string arFinishVar = "CMAKE_";
  568. arFinishVar += linkLanguage;
  569. arFinishVar += "_ARCHIVE_FINISH";
  570. arFinishVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  571. arFinishVar, linkLanguage, this->ConfigName);
  572. if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) {
  573. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  574. }
  575. }
  576. // Decide whether to use archiving rules.
  577. bool useArchiveRules = !haveStaticLibraryRule &&
  578. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  579. if (useArchiveRules) {
  580. // Archiving rules are always run with a link script.
  581. useLinkScript = true;
  582. // Archiving rules never use a response file.
  583. useResponseFileForObjects = false;
  584. // Limit the length of individual object lists to less than half of
  585. // the command line length limit (leaving half for other flags).
  586. // This may result in several calls to the archiver.
  587. if (size_t limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
  588. archiveCommandLimit = limit / 2;
  589. } else {
  590. archiveCommandLimit = 8000;
  591. }
  592. }
  593. // Expand the rule variables.
  594. std::vector<std::string> real_link_commands;
  595. {
  596. bool useWatcomQuote =
  597. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  598. // Set path conversion for link script shells.
  599. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  600. // Collect up flags to link in needed libraries.
  601. std::string linkLibs;
  602. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  603. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  604. this->CreateLinkLineComputer(
  605. this->LocalGenerator,
  606. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  607. linkLineComputer->SetForResponse(useResponseFileForLibs);
  608. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  609. linkLineComputer->SetRelink(relink);
  610. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  611. useResponseFileForLibs, depends);
  612. }
  613. // Construct object file lists that may be needed to expand the
  614. // rule.
  615. std::string buildObjs;
  616. this->CreateObjectLists(useLinkScript, useArchiveRules,
  617. useResponseFileForObjects, buildObjs, depends,
  618. useWatcomQuote);
  619. if (!this->DeviceLinkObject.empty()) {
  620. buildObjs += " " +
  621. this->LocalGenerator->ConvertToOutputFormat(
  622. this->LocalGenerator->MaybeConvertToRelativePath(
  623. this->LocalGenerator->GetCurrentBinaryDirectory(),
  624. this->DeviceLinkObject),
  625. cmOutputConverter::SHELL);
  626. }
  627. // maybe create .def file from list of objects
  628. this->GenDefFile(real_link_commands);
  629. std::string manifests = this->GetManifests();
  630. cmRulePlaceholderExpander::RuleVariables vars;
  631. vars.TargetPDB = targetOutPathPDB.c_str();
  632. // Setup the target version.
  633. std::string targetVersionMajor;
  634. std::string targetVersionMinor;
  635. {
  636. std::ostringstream majorStream;
  637. std::ostringstream minorStream;
  638. int major;
  639. int minor;
  640. this->GeneratorTarget->GetTargetVersion(major, minor);
  641. majorStream << major;
  642. minorStream << minor;
  643. targetVersionMajor = majorStream.str();
  644. targetVersionMinor = minorStream.str();
  645. }
  646. vars.TargetVersionMajor = targetVersionMajor.c_str();
  647. vars.TargetVersionMinor = targetVersionMinor.c_str();
  648. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  649. vars.CMTargetType =
  650. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  651. vars.Language = linkLanguage.c_str();
  652. vars.Objects = buildObjs.c_str();
  653. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  654. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  655. this->LocalGenerator->MaybeConvertToRelativePath(
  656. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  657. cmOutputConverter::SHELL);
  658. vars.ObjectDir = objectDir.c_str();
  659. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  660. ? cmOutputConverter::WATCOMQUOTE
  661. : cmOutputConverter::SHELL;
  662. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  663. this->LocalGenerator->MaybeConvertToRelativePath(
  664. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  665. output);
  666. vars.Target = target.c_str();
  667. vars.LinkLibraries = linkLibs.c_str();
  668. vars.ObjectsQuoted = buildObjs.c_str();
  669. if (this->GeneratorTarget->HasSOName(this->ConfigName)) {
  670. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  671. vars.TargetSOName = this->TargetNames.SharedObject.c_str();
  672. }
  673. vars.LinkFlags = linkFlags.c_str();
  674. vars.Manifests = manifests.c_str();
  675. // Compute the directory portion of the install_name setting.
  676. std::string install_name_dir;
  677. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY) {
  678. // Get the install_name directory for the build tree.
  679. install_name_dir =
  680. this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);
  681. // Set the rule variable replacement value.
  682. if (install_name_dir.empty()) {
  683. vars.TargetInstallNameDir = "";
  684. } else {
  685. // Convert to a path for the native build tool.
  686. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  687. install_name_dir, cmOutputConverter::SHELL);
  688. vars.TargetInstallNameDir = install_name_dir.c_str();
  689. }
  690. }
  691. // Add language-specific flags.
  692. std::string langFlags;
  693. this->LocalGenerator->AddLanguageFlagsForLinking(
  694. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  695. this->LocalGenerator->AddArchitectureFlags(
  696. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  697. vars.LanguageCompileFlags = langFlags.c_str();
  698. std::string launcher;
  699. const char* val = this->LocalGenerator->GetRuleLauncher(
  700. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  701. if (val && *val) {
  702. launcher = val;
  703. launcher += " ";
  704. }
  705. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  706. this->LocalGenerator->CreateRulePlaceholderExpander());
  707. // Construct the main link rule and expand placeholders.
  708. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  709. if (useArchiveRules) {
  710. // Construct the individual object list strings.
  711. std::vector<std::string> object_strings;
  712. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  713. // Add the cuda device object to the list of archive files. This will
  714. // only occur on archives which have CUDA_RESOLVE_DEVICE_SYMBOLS enabled
  715. if (!this->DeviceLinkObject.empty()) {
  716. object_strings.push_back(this->LocalGenerator->ConvertToOutputFormat(
  717. this->LocalGenerator->MaybeConvertToRelativePath(
  718. this->LocalGenerator->GetCurrentBinaryDirectory(),
  719. this->DeviceLinkObject),
  720. cmOutputConverter::SHELL));
  721. }
  722. // Create the archive with the first set of objects.
  723. std::vector<std::string>::iterator osi = object_strings.begin();
  724. {
  725. vars.Objects = osi->c_str();
  726. for (std::string const& acc : archiveCreateCommands) {
  727. std::string cmd = launcher + acc;
  728. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  729. cmd, vars);
  730. real_link_commands.push_back(std::move(cmd));
  731. }
  732. }
  733. // Append to the archive with the other object sets.
  734. for (++osi; osi != object_strings.end(); ++osi) {
  735. vars.Objects = osi->c_str();
  736. for (std::string const& aac : archiveAppendCommands) {
  737. std::string cmd = launcher + aac;
  738. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  739. cmd, vars);
  740. real_link_commands.push_back(std::move(cmd));
  741. }
  742. }
  743. // Finish the archive.
  744. vars.Objects = "";
  745. for (std::string const& afc : archiveFinishCommands) {
  746. std::string cmd = launcher + afc;
  747. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, cmd,
  748. vars);
  749. // If there is no ranlib the command will be ":". Skip it.
  750. if (!cmd.empty() && cmd[0] != ':') {
  751. real_link_commands.push_back(std::move(cmd));
  752. }
  753. }
  754. } else {
  755. // Get the set of commands.
  756. std::string linkRule = this->GetLinkRule(linkRuleVar);
  757. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  758. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
  759. (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  760. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  761. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  762. cmakeCommand += " -E __run_co_compile --lwyu=";
  763. cmakeCommand += targetOutPathReal;
  764. real_link_commands.push_back(std::move(cmakeCommand));
  765. }
  766. // Expand placeholders.
  767. for (std::string& real_link_command : real_link_commands) {
  768. real_link_command = launcher + real_link_command;
  769. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  770. real_link_command, vars);
  771. }
  772. }
  773. // Restore path conversion to normal shells.
  774. this->LocalGenerator->SetLinkScriptShell(false);
  775. }
  776. // Optionally convert the build rule to use a script to avoid long
  777. // command lines in the make shell.
  778. if (useLinkScript) {
  779. // Use a link script.
  780. const char* name = (relink ? "relink.txt" : "link.txt");
  781. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  782. } else {
  783. // No link script. Just use the link rule directly.
  784. commands1 = real_link_commands;
  785. }
  786. this->LocalGenerator->CreateCDCommand(
  787. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  788. this->LocalGenerator->GetBinaryDirectory());
  789. cmAppend(commands, commands1);
  790. commands1.clear();
  791. // Add a rule to create necessary symlinks for the library.
  792. // Frameworks are handled by cmOSXBundleGenerator.
  793. if (targetOutPath != targetOutPathReal &&
  794. !this->GeneratorTarget->IsFrameworkOnApple()) {
  795. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  796. symlink += targetOutPathReal;
  797. symlink += " ";
  798. symlink += targetOutPathSO;
  799. symlink += " ";
  800. symlink += targetOutPath;
  801. commands1.push_back(std::move(symlink));
  802. this->LocalGenerator->CreateCDCommand(
  803. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  804. this->LocalGenerator->GetBinaryDirectory());
  805. cmAppend(commands, commands1);
  806. commands1.clear();
  807. }
  808. // Add the post-build rules when building but not when relinking.
  809. if (!relink) {
  810. this->LocalGenerator->AppendCustomCommands(
  811. commands, this->GeneratorTarget->GetPostBuildCommands(),
  812. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  813. }
  814. // Compute the list of outputs.
  815. std::vector<std::string> outputs(1, targetFullPathReal);
  816. if (this->TargetNames.SharedObject != this->TargetNames.Real) {
  817. outputs.push_back(targetFullPathSO);
  818. }
  819. if (this->TargetNames.Output != this->TargetNames.SharedObject &&
  820. this->TargetNames.Output != this->TargetNames.Real) {
  821. outputs.push_back(targetFullPath);
  822. }
  823. // Write the build rule.
  824. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  825. commands, false);
  826. // Write the main driver rule to build everything in this target.
  827. this->WriteTargetDriverRule(targetFullPath, relink);
  828. // Clean all the possible library names and symlinks.
  829. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  830. }