cmMakefileLibraryTargetGenerator.cxx 38 KB

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