cmMakefileLibraryTargetGenerator.cxx 41 KB

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