cmMakefileLibraryTargetGenerator.cxx 40 KB

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