cmMakefileLibraryTargetGenerator.cxx 36 KB

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