cmMakefileLibraryTargetGenerator.cxx 36 KB

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