cmMakefileLibraryTargetGenerator.cxx 37 KB

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