cmMakefileLibraryTargetGenerator.cxx 37 KB

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