cmMakefileLibraryTargetGenerator.cxx 36 KB

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