cmMakefileLibraryTargetGenerator.cxx 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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 <set>
  7. #include <sstream>
  8. #include <stddef.h>
  9. #include <utility>
  10. #include <vector>
  11. #include "cmAlgorithms.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalUnixMakefileGenerator3.h"
  15. #include "cmLinkLineComputer.h"
  16. #include "cmLinkLineDeviceComputer.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmLocalUnixMakefileGenerator3.h"
  19. #include "cmMakefile.h"
  20. #include "cmOSXBundleGenerator.h"
  21. #include "cmOutputConverter.h"
  22. #include "cmRulePlaceholderExpander.h"
  23. #include "cmState.h"
  24. #include "cmStateDirectory.h"
  25. #include "cmStateSnapshot.h"
  26. #include "cmStateTypes.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 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::set<std::string> libCleanFiles;
  260. libCleanFiles.insert(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(libCleanFiles.begin(), libCleanFiles.end());
  347. }
  348. std::vector<std::string> commands1;
  349. // Optionally convert the build rule to use a script to avoid long
  350. // command lines in the make shell.
  351. if (useLinkScript) {
  352. // Use a link script.
  353. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  354. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  355. } else {
  356. // No link script. Just use the link rule directly.
  357. commands1 = real_link_commands;
  358. }
  359. this->LocalGenerator->CreateCDCommand(
  360. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  361. this->LocalGenerator->GetBinaryDirectory());
  362. commands.insert(commands.end(), commands1.begin(), commands1.end());
  363. commands1.clear();
  364. // Compute the list of outputs.
  365. std::vector<std::string> outputs(1, targetOutputReal);
  366. // Write the build rule.
  367. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  368. commands, false);
  369. // Write the main driver rule to build everything in this target.
  370. this->WriteTargetDriverRule(targetOutputReal, relink);
  371. #else
  372. static_cast<void>(linkRuleVar);
  373. static_cast<void>(relink);
  374. #endif
  375. }
  376. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  377. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  378. {
  379. // TODO: Merge the methods that call this method to avoid
  380. // code duplication.
  381. std::vector<std::string> commands;
  382. // Get the language to use for linking this library.
  383. std::string linkLanguage =
  384. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  385. // Make sure we have a link language.
  386. if (linkLanguage.empty()) {
  387. cmSystemTools::Error("Cannot determine link language for target \"" +
  388. this->GeneratorTarget->GetName() + "\".");
  389. return;
  390. }
  391. // Build list of dependencies.
  392. std::vector<std::string> depends;
  393. this->AppendLinkDepends(depends, linkLanguage);
  394. if (!this->DeviceLinkObject.empty()) {
  395. depends.push_back(this->DeviceLinkObject);
  396. }
  397. // Create set of linking flags.
  398. std::string linkFlags;
  399. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  400. this->LocalGenerator->AppendIPOLinkerFlags(linkFlags, this->GeneratorTarget,
  401. this->ConfigName, linkLanguage);
  402. // Add OSX version flags, if any.
  403. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  404. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  405. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  406. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  407. }
  408. // Construct the name of the library.
  409. this->GeneratorTarget->GetLibraryNames(this->ConfigName);
  410. // Construct the full path version of the names.
  411. std::string outpath;
  412. std::string outpathImp;
  413. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  414. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  415. this->OSXBundleGenerator->CreateFramework(this->TargetNames.Output,
  416. outpath);
  417. outpath += "/";
  418. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  419. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  420. this->OSXBundleGenerator->CreateCFBundle(this->TargetNames.Output,
  421. outpath);
  422. outpath += "/";
  423. } else if (relink) {
  424. outpath = this->Makefile->GetCurrentBinaryDirectory();
  425. outpath += "/CMakeFiles";
  426. outpath += "/CMakeRelink.dir";
  427. cmSystemTools::MakeDirectory(outpath);
  428. outpath += "/";
  429. if (!this->TargetNames.ImportLibrary.empty()) {
  430. outpathImp = outpath;
  431. }
  432. } else {
  433. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  434. cmSystemTools::MakeDirectory(outpath);
  435. outpath += "/";
  436. if (!this->TargetNames.ImportLibrary.empty()) {
  437. outpathImp = this->GeneratorTarget->GetDirectory(
  438. this->ConfigName, cmStateEnums::ImportLibraryArtifact);
  439. cmSystemTools::MakeDirectory(outpathImp);
  440. outpathImp += "/";
  441. }
  442. }
  443. std::string compilePdbOutputPath =
  444. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  445. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  446. std::string pdbOutputPath =
  447. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  448. cmSystemTools::MakeDirectory(pdbOutputPath);
  449. pdbOutputPath += "/";
  450. std::string targetFullPath = outpath + this->TargetNames.Output;
  451. std::string targetFullPathPDB = pdbOutputPath + this->TargetNames.PDB;
  452. std::string targetFullPathSO = outpath + this->TargetNames.SharedObject;
  453. std::string targetFullPathReal = outpath + this->TargetNames.Real;
  454. std::string targetFullPathImport =
  455. outpathImp + this->TargetNames.ImportLibrary;
  456. // Construct the output path version of the names for use in command
  457. // arguments.
  458. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  459. targetFullPathPDB, cmOutputConverter::SHELL);
  460. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  461. this->LocalGenerator->MaybeConvertToRelativePath(
  462. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  463. cmOutputConverter::SHELL);
  464. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  465. this->LocalGenerator->MaybeConvertToRelativePath(
  466. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO),
  467. cmOutputConverter::SHELL);
  468. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  469. this->LocalGenerator->MaybeConvertToRelativePath(
  470. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  471. cmOutputConverter::SHELL);
  472. std::string targetOutPathImport =
  473. this->LocalGenerator->ConvertToOutputFormat(
  474. this->LocalGenerator->MaybeConvertToRelativePath(
  475. this->LocalGenerator->GetCurrentBinaryDirectory(),
  476. targetFullPathImport),
  477. cmOutputConverter::SHELL);
  478. this->NumberOfProgressActions++;
  479. if (!this->NoRuleMessages) {
  480. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  481. this->MakeEchoProgress(progress);
  482. // Add the link message.
  483. std::string buildEcho = "Linking ";
  484. buildEcho += linkLanguage;
  485. switch (this->GeneratorTarget->GetType()) {
  486. case cmStateEnums::STATIC_LIBRARY:
  487. buildEcho += " static library ";
  488. break;
  489. case cmStateEnums::SHARED_LIBRARY:
  490. buildEcho += " shared library ";
  491. break;
  492. case cmStateEnums::MODULE_LIBRARY:
  493. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  494. buildEcho += " CFBundle";
  495. }
  496. buildEcho += " shared module ";
  497. break;
  498. default:
  499. buildEcho += " library ";
  500. break;
  501. }
  502. buildEcho += targetOutPath;
  503. this->LocalGenerator->AppendEcho(
  504. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  505. }
  506. // Clean files associated with this library.
  507. std::set<std::string> libCleanFiles;
  508. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  509. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  510. std::vector<std::string> commands1;
  511. // Add a command to remove any existing files for this library.
  512. // for static libs only
  513. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  514. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  515. this->GeneratorTarget, "target");
  516. this->LocalGenerator->CreateCDCommand(
  517. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  518. this->LocalGenerator->GetBinaryDirectory());
  519. commands.insert(commands.end(), commands1.begin(), commands1.end());
  520. commands1.clear();
  521. }
  522. if (this->TargetNames.Output != this->TargetNames.Real) {
  523. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  524. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  525. }
  526. if (this->TargetNames.SharedObject != this->TargetNames.Real &&
  527. this->TargetNames.SharedObject != this->TargetNames.Output) {
  528. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  529. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO));
  530. }
  531. if (!this->TargetNames.ImportLibrary.empty()) {
  532. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  533. this->LocalGenerator->GetCurrentBinaryDirectory(),
  534. targetFullPathImport));
  535. std::string implib;
  536. if (this->GeneratorTarget->GetImplibGNUtoMS(
  537. this->ConfigName, targetFullPathImport, implib)) {
  538. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  539. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  540. }
  541. }
  542. // List the PDB for cleaning only when the whole target is
  543. // cleaned. We do not want to delete the .pdb file just before
  544. // linking the target.
  545. this->CleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  546. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  547. #ifdef _WIN32
  548. // There may be a manifest file for this target. Add it to the
  549. // clean set just in case.
  550. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  551. libCleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  552. this->LocalGenerator->GetCurrentBinaryDirectory(),
  553. targetFullPath + ".manifest"));
  554. }
  555. #endif
  556. // Add the pre-build and pre-link rules building but not when relinking.
  557. if (!relink) {
  558. this->LocalGenerator->AppendCustomCommands(
  559. commands, this->GeneratorTarget->GetPreBuildCommands(),
  560. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  561. this->LocalGenerator->AppendCustomCommands(
  562. commands, this->GeneratorTarget->GetPreLinkCommands(),
  563. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  564. }
  565. // Determine whether a link script will be used.
  566. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  567. bool useResponseFileForObjects =
  568. this->CheckUseResponseFileForObjects(linkLanguage);
  569. bool const useResponseFileForLibs =
  570. this->CheckUseResponseFileForLibraries(linkLanguage);
  571. // For static libraries there might be archiving rules.
  572. bool haveStaticLibraryRule = false;
  573. std::vector<std::string> archiveCreateCommands;
  574. std::vector<std::string> archiveAppendCommands;
  575. std::vector<std::string> archiveFinishCommands;
  576. std::string::size_type archiveCommandLimit = std::string::npos;
  577. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  578. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  579. std::string arCreateVar = "CMAKE_";
  580. arCreateVar += linkLanguage;
  581. arCreateVar += "_ARCHIVE_CREATE";
  582. arCreateVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  583. arCreateVar, linkLanguage, this->ConfigName);
  584. if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) {
  585. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  586. }
  587. std::string arAppendVar = "CMAKE_";
  588. arAppendVar += linkLanguage;
  589. arAppendVar += "_ARCHIVE_APPEND";
  590. arAppendVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  591. arAppendVar, linkLanguage, this->ConfigName);
  592. if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) {
  593. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  594. }
  595. std::string arFinishVar = "CMAKE_";
  596. arFinishVar += linkLanguage;
  597. arFinishVar += "_ARCHIVE_FINISH";
  598. arFinishVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  599. arFinishVar, linkLanguage, this->ConfigName);
  600. if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) {
  601. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  602. }
  603. }
  604. // Decide whether to use archiving rules.
  605. bool useArchiveRules = !haveStaticLibraryRule &&
  606. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  607. if (useArchiveRules) {
  608. // Archiving rules are always run with a link script.
  609. useLinkScript = true;
  610. // Archiving rules never use a response file.
  611. useResponseFileForObjects = false;
  612. // Limit the length of individual object lists to less than half of
  613. // the command line length limit (leaving half for other flags).
  614. // This may result in several calls to the archiver.
  615. if (size_t limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
  616. archiveCommandLimit = limit / 2;
  617. } else {
  618. archiveCommandLimit = 8000;
  619. }
  620. }
  621. // Expand the rule variables.
  622. std::vector<std::string> real_link_commands;
  623. {
  624. bool useWatcomQuote =
  625. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  626. // Set path conversion for link script shells.
  627. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  628. // Collect up flags to link in needed libraries.
  629. std::string linkLibs;
  630. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  631. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  632. this->CreateLinkLineComputer(
  633. this->LocalGenerator,
  634. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  635. linkLineComputer->SetForResponse(useResponseFileForLibs);
  636. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  637. linkLineComputer->SetRelink(relink);
  638. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  639. useResponseFileForLibs, depends);
  640. }
  641. // Construct object file lists that may be needed to expand the
  642. // rule.
  643. std::string buildObjs;
  644. this->CreateObjectLists(useLinkScript, useArchiveRules,
  645. useResponseFileForObjects, buildObjs, depends,
  646. useWatcomQuote);
  647. if (!this->DeviceLinkObject.empty()) {
  648. buildObjs += " " +
  649. this->LocalGenerator->ConvertToOutputFormat(
  650. this->LocalGenerator->MaybeConvertToRelativePath(
  651. this->LocalGenerator->GetCurrentBinaryDirectory(),
  652. this->DeviceLinkObject),
  653. cmOutputConverter::SHELL);
  654. }
  655. // maybe create .def file from list of objects
  656. this->GenDefFile(real_link_commands);
  657. std::string manifests = this->GetManifests();
  658. cmRulePlaceholderExpander::RuleVariables vars;
  659. vars.TargetPDB = targetOutPathPDB.c_str();
  660. // Setup the target version.
  661. std::string targetVersionMajor;
  662. std::string targetVersionMinor;
  663. {
  664. std::ostringstream majorStream;
  665. std::ostringstream minorStream;
  666. int major;
  667. int minor;
  668. this->GeneratorTarget->GetTargetVersion(major, minor);
  669. majorStream << major;
  670. minorStream << minor;
  671. targetVersionMajor = majorStream.str();
  672. targetVersionMinor = minorStream.str();
  673. }
  674. vars.TargetVersionMajor = targetVersionMajor.c_str();
  675. vars.TargetVersionMinor = targetVersionMinor.c_str();
  676. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  677. vars.CMTargetType =
  678. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  679. vars.Language = linkLanguage.c_str();
  680. vars.Objects = buildObjs.c_str();
  681. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  682. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  683. this->LocalGenerator->MaybeConvertToRelativePath(
  684. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  685. cmOutputConverter::SHELL);
  686. vars.ObjectDir = objectDir.c_str();
  687. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  688. ? cmOutputConverter::WATCOMQUOTE
  689. : cmOutputConverter::SHELL;
  690. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  691. this->LocalGenerator->MaybeConvertToRelativePath(
  692. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  693. output);
  694. vars.Target = target.c_str();
  695. vars.LinkLibraries = linkLibs.c_str();
  696. vars.ObjectsQuoted = buildObjs.c_str();
  697. if (this->GeneratorTarget->HasSOName(this->ConfigName)) {
  698. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  699. vars.TargetSOName = this->TargetNames.SharedObject.c_str();
  700. }
  701. vars.LinkFlags = linkFlags.c_str();
  702. vars.Manifests = manifests.c_str();
  703. // Compute the directory portion of the install_name setting.
  704. std::string install_name_dir;
  705. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY) {
  706. // Get the install_name directory for the build tree.
  707. install_name_dir =
  708. this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);
  709. // Set the rule variable replacement value.
  710. if (install_name_dir.empty()) {
  711. vars.TargetInstallNameDir = "";
  712. } else {
  713. // Convert to a path for the native build tool.
  714. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  715. install_name_dir, cmOutputConverter::SHELL);
  716. vars.TargetInstallNameDir = install_name_dir.c_str();
  717. }
  718. }
  719. // Add language-specific flags.
  720. std::string langFlags;
  721. this->LocalGenerator->AddLanguageFlagsForLinking(
  722. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  723. this->LocalGenerator->AddArchitectureFlags(
  724. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  725. vars.LanguageCompileFlags = langFlags.c_str();
  726. std::string launcher;
  727. const char* val = this->LocalGenerator->GetRuleLauncher(
  728. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  729. if (val && *val) {
  730. launcher = val;
  731. launcher += " ";
  732. }
  733. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  734. this->LocalGenerator->CreateRulePlaceholderExpander());
  735. // Construct the main link rule and expand placeholders.
  736. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  737. if (useArchiveRules) {
  738. // Construct the individual object list strings.
  739. std::vector<std::string> object_strings;
  740. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  741. // Add the cuda device object to the list of archive files. This will
  742. // only occur on archives which have CUDA_RESOLVE_DEVICE_SYMBOLS enabled
  743. if (!this->DeviceLinkObject.empty()) {
  744. object_strings.push_back(this->LocalGenerator->ConvertToOutputFormat(
  745. this->LocalGenerator->MaybeConvertToRelativePath(
  746. this->LocalGenerator->GetCurrentBinaryDirectory(),
  747. this->DeviceLinkObject),
  748. cmOutputConverter::SHELL));
  749. }
  750. // Create the archive with the first set of objects.
  751. std::vector<std::string>::iterator osi = object_strings.begin();
  752. {
  753. vars.Objects = osi->c_str();
  754. for (std::string const& acc : archiveCreateCommands) {
  755. std::string cmd = launcher + acc;
  756. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  757. cmd, vars);
  758. real_link_commands.push_back(std::move(cmd));
  759. }
  760. }
  761. // Append to the archive with the other object sets.
  762. for (++osi; osi != object_strings.end(); ++osi) {
  763. vars.Objects = osi->c_str();
  764. for (std::string const& aac : archiveAppendCommands) {
  765. std::string cmd = launcher + aac;
  766. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  767. cmd, vars);
  768. real_link_commands.push_back(std::move(cmd));
  769. }
  770. }
  771. // Finish the archive.
  772. vars.Objects = "";
  773. for (std::string const& afc : archiveFinishCommands) {
  774. std::string cmd = launcher + afc;
  775. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, cmd,
  776. vars);
  777. // If there is no ranlib the command will be ":". Skip it.
  778. if (!cmd.empty() && cmd[0] != ':') {
  779. real_link_commands.push_back(std::move(cmd));
  780. }
  781. }
  782. } else {
  783. // Get the set of commands.
  784. std::string linkRule = this->GetLinkRule(linkRuleVar);
  785. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  786. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
  787. (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  788. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  789. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  790. cmakeCommand += " -E __run_co_compile --lwyu=";
  791. cmakeCommand += targetOutPathReal;
  792. real_link_commands.push_back(std::move(cmakeCommand));
  793. }
  794. // Expand placeholders.
  795. for (std::string& real_link_command : real_link_commands) {
  796. real_link_command = launcher + real_link_command;
  797. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  798. real_link_command, vars);
  799. }
  800. }
  801. // Restore path conversion to normal shells.
  802. this->LocalGenerator->SetLinkScriptShell(false);
  803. }
  804. // Optionally convert the build rule to use a script to avoid long
  805. // command lines in the make shell.
  806. if (useLinkScript) {
  807. // Use a link script.
  808. const char* name = (relink ? "relink.txt" : "link.txt");
  809. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  810. } else {
  811. // No link script. Just use the link rule directly.
  812. commands1 = real_link_commands;
  813. }
  814. this->LocalGenerator->CreateCDCommand(
  815. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  816. this->LocalGenerator->GetBinaryDirectory());
  817. commands.insert(commands.end(), commands1.begin(), commands1.end());
  818. commands1.clear();
  819. // Add a rule to create necessary symlinks for the library.
  820. // Frameworks are handled by cmOSXBundleGenerator.
  821. if (targetOutPath != targetOutPathReal &&
  822. !this->GeneratorTarget->IsFrameworkOnApple()) {
  823. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  824. symlink += targetOutPathReal;
  825. symlink += " ";
  826. symlink += targetOutPathSO;
  827. symlink += " ";
  828. symlink += targetOutPath;
  829. commands1.push_back(std::move(symlink));
  830. this->LocalGenerator->CreateCDCommand(
  831. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  832. this->LocalGenerator->GetBinaryDirectory());
  833. commands.insert(commands.end(), commands1.begin(), commands1.end());
  834. commands1.clear();
  835. }
  836. // Add the post-build rules when building but not when relinking.
  837. if (!relink) {
  838. this->LocalGenerator->AppendCustomCommands(
  839. commands, this->GeneratorTarget->GetPostBuildCommands(),
  840. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  841. }
  842. // Compute the list of outputs.
  843. std::vector<std::string> outputs(1, targetFullPathReal);
  844. if (this->TargetNames.SharedObject != this->TargetNames.Real) {
  845. outputs.push_back(targetFullPathSO);
  846. }
  847. if (this->TargetNames.Output != this->TargetNames.SharedObject &&
  848. this->TargetNames.Output != this->TargetNames.Real) {
  849. outputs.push_back(targetFullPath);
  850. }
  851. // Write the build rule.
  852. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  853. commands, false);
  854. // Write the main driver rule to build everything in this target.
  855. this->WriteTargetDriverRule(targetFullPath, relink);
  856. // Clean all the possible library names and symlinks.
  857. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  858. }