cmMakefileLibraryTargetGenerator.cxx 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMakefileLibraryTargetGenerator.h"
  4. #include <algorithm>
  5. #include <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. std::string const objExt =
  239. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  240. // Create set of linking flags.
  241. std::string linkFlags;
  242. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  243. // Get the name of the device object to generate.
  244. std::string const targetOutputReal =
  245. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  246. this->DeviceLinkObject = targetOutputReal;
  247. this->NumberOfProgressActions++;
  248. if (!this->NoRuleMessages) {
  249. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  250. this->MakeEchoProgress(progress);
  251. // Add the link message.
  252. std::string buildEcho = "Linking " + linkLanguage + " device code";
  253. buildEcho += targetOutputReal;
  254. this->LocalGenerator->AppendEcho(
  255. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  256. }
  257. // Clean files associated with this library.
  258. std::vector<std::string> libCleanFiles;
  259. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  260. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal));
  261. // Determine whether a link script will be used.
  262. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  263. bool useResponseFileForObjects =
  264. this->CheckUseResponseFileForObjects(linkLanguage);
  265. bool const useResponseFileForLibs =
  266. this->CheckUseResponseFileForLibraries(linkLanguage);
  267. cmRulePlaceholderExpander::RuleVariables vars;
  268. vars.Language = linkLanguage.c_str();
  269. // Expand the rule variables.
  270. std::vector<std::string> real_link_commands;
  271. {
  272. bool useWatcomQuote =
  273. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  274. // Set path conversion for link script shells.
  275. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  276. // Collect up flags to link in needed libraries.
  277. std::string linkLibs;
  278. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  279. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  280. new cmLinkLineDeviceComputer(
  281. this->LocalGenerator,
  282. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  283. linkLineComputer->SetForResponse(useResponseFileForLibs);
  284. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  285. linkLineComputer->SetRelink(relink);
  286. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  287. useResponseFileForLibs, depends);
  288. }
  289. // Construct object file lists that may be needed to expand the
  290. // rule.
  291. std::string buildObjs;
  292. this->CreateObjectLists(useLinkScript, false, // useArchiveRules
  293. useResponseFileForObjects, buildObjs, depends,
  294. useWatcomQuote);
  295. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  296. ? cmOutputConverter::WATCOMQUOTE
  297. : cmOutputConverter::SHELL;
  298. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  299. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  300. this->LocalGenerator->MaybeConvertToRelativePath(
  301. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  302. cmOutputConverter::SHELL);
  303. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  304. this->LocalGenerator->MaybeConvertToRelativePath(
  305. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal),
  306. output);
  307. std::string targetFullPathCompilePDB = this->ComputeTargetCompilePDB();
  308. std::string targetOutPathCompilePDB =
  309. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  310. cmOutputConverter::SHELL);
  311. vars.Objects = buildObjs.c_str();
  312. vars.ObjectDir = objectDir.c_str();
  313. vars.Target = target.c_str();
  314. vars.LinkLibraries = linkLibs.c_str();
  315. vars.ObjectsQuoted = buildObjs.c_str();
  316. vars.LinkFlags = linkFlags.c_str();
  317. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  318. // Add language feature flags.
  319. std::string langFlags;
  320. this->AddFeatureFlags(langFlags, linkLanguage);
  321. vars.LanguageCompileFlags = langFlags.c_str();
  322. std::string launcher;
  323. const char* val = this->LocalGenerator->GetRuleLauncher(
  324. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  325. if (val && *val) {
  326. launcher = val;
  327. launcher += " ";
  328. }
  329. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  330. this->LocalGenerator->CreateRulePlaceholderExpander());
  331. // Construct the main link rule and expand placeholders.
  332. rulePlaceholderExpander->SetTargetImpLib(targetOutputReal);
  333. std::string linkRule = this->GetLinkRule(linkRuleVar);
  334. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  335. // Expand placeholders.
  336. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  337. i != real_link_commands.end(); ++i) {
  338. *i = launcher + *i;
  339. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  340. vars);
  341. }
  342. // Restore path conversion to normal shells.
  343. this->LocalGenerator->SetLinkScriptShell(false);
  344. // Clean all the possible library names and symlinks.
  345. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  346. libCleanFiles.end());
  347. }
  348. std::vector<std::string> commands1;
  349. // Optionally convert the build rule to use a script to avoid long
  350. // command lines in the make shell.
  351. if (useLinkScript) {
  352. // Use a link script.
  353. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  354. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  355. } else {
  356. // No link script. Just use the link rule directly.
  357. commands1 = real_link_commands;
  358. }
  359. this->LocalGenerator->CreateCDCommand(
  360. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  361. this->LocalGenerator->GetBinaryDirectory());
  362. commands.insert(commands.end(), commands1.begin(), commands1.end());
  363. commands1.clear();
  364. // Compute the list of outputs.
  365. std::vector<std::string> outputs(1, targetOutputReal);
  366. // Write the build rule.
  367. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  368. commands, false);
  369. // Write the main driver rule to build everything in this target.
  370. this->WriteTargetDriverRule(targetOutputReal, relink);
  371. #else
  372. static_cast<void>(linkRuleVar);
  373. static_cast<void>(extraFlags);
  374. static_cast<void>(relink);
  375. #endif
  376. }
  377. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  378. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  379. {
  380. // TODO: Merge the methods that call this method to avoid
  381. // code duplication.
  382. std::vector<std::string> commands;
  383. // Build list of dependencies.
  384. std::vector<std::string> depends;
  385. this->AppendLinkDepends(depends);
  386. if (!this->DeviceLinkObject.empty()) {
  387. depends.push_back(this->DeviceLinkObject);
  388. }
  389. // Get the language to use for linking this library.
  390. std::string linkLanguage =
  391. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  392. // Make sure we have a link language.
  393. if (linkLanguage.empty()) {
  394. cmSystemTools::Error("Cannot determine link language for target \"",
  395. this->GeneratorTarget->GetName().c_str(), "\".");
  396. return;
  397. }
  398. // Create set of linking flags.
  399. std::string linkFlags;
  400. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  401. // Add OSX version flags, if any.
  402. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  403. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  404. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  405. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  406. }
  407. // Construct the name of the library.
  408. std::string targetName;
  409. std::string targetNameSO;
  410. std::string targetNameReal;
  411. std::string targetNameImport;
  412. std::string targetNamePDB;
  413. this->GeneratorTarget->GetLibraryNames(targetName, targetNameSO,
  414. targetNameReal, targetNameImport,
  415. targetNamePDB, this->ConfigName);
  416. // Construct the full path version of the names.
  417. std::string outpath;
  418. std::string outpathImp;
  419. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  420. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  421. this->OSXBundleGenerator->CreateFramework(targetName, outpath);
  422. outpath += "/";
  423. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  424. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  425. this->OSXBundleGenerator->CreateCFBundle(targetName, outpath);
  426. outpath += "/";
  427. } else if (relink) {
  428. outpath = this->Makefile->GetCurrentBinaryDirectory();
  429. outpath += cmake::GetCMakeFilesDirectory();
  430. outpath += "/CMakeRelink.dir";
  431. cmSystemTools::MakeDirectory(outpath.c_str());
  432. outpath += "/";
  433. if (!targetNameImport.empty()) {
  434. outpathImp = outpath;
  435. }
  436. } else {
  437. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  438. cmSystemTools::MakeDirectory(outpath.c_str());
  439. outpath += "/";
  440. if (!targetNameImport.empty()) {
  441. outpathImp = this->GeneratorTarget->GetDirectory(this->ConfigName, true);
  442. cmSystemTools::MakeDirectory(outpathImp.c_str());
  443. outpathImp += "/";
  444. }
  445. }
  446. std::string compilePdbOutputPath =
  447. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  448. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  449. std::string pdbOutputPath =
  450. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  451. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  452. pdbOutputPath += "/";
  453. std::string targetFullPath = outpath + targetName;
  454. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  455. std::string targetFullPathSO = outpath + targetNameSO;
  456. std::string targetFullPathReal = outpath + targetNameReal;
  457. std::string targetFullPathImport = outpathImp + targetNameImport;
  458. // Construct the output path version of the names for use in command
  459. // arguments.
  460. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  461. targetFullPathPDB, cmOutputConverter::SHELL);
  462. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  463. this->LocalGenerator->MaybeConvertToRelativePath(
  464. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  465. cmOutputConverter::SHELL);
  466. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  467. this->LocalGenerator->MaybeConvertToRelativePath(
  468. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO),
  469. cmOutputConverter::SHELL);
  470. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  471. this->LocalGenerator->MaybeConvertToRelativePath(
  472. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  473. cmOutputConverter::SHELL);
  474. std::string targetOutPathImport =
  475. this->LocalGenerator->ConvertToOutputFormat(
  476. this->LocalGenerator->MaybeConvertToRelativePath(
  477. this->LocalGenerator->GetCurrentBinaryDirectory(),
  478. targetFullPathImport),
  479. cmOutputConverter::SHELL);
  480. this->NumberOfProgressActions++;
  481. if (!this->NoRuleMessages) {
  482. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  483. this->MakeEchoProgress(progress);
  484. // Add the link message.
  485. std::string buildEcho = "Linking ";
  486. buildEcho += linkLanguage;
  487. switch (this->GeneratorTarget->GetType()) {
  488. case cmStateEnums::STATIC_LIBRARY:
  489. buildEcho += " static library ";
  490. break;
  491. case cmStateEnums::SHARED_LIBRARY:
  492. buildEcho += " shared library ";
  493. break;
  494. case cmStateEnums::MODULE_LIBRARY:
  495. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  496. buildEcho += " CFBundle";
  497. }
  498. buildEcho += " shared module ";
  499. break;
  500. default:
  501. buildEcho += " library ";
  502. break;
  503. }
  504. buildEcho += targetOutPath;
  505. this->LocalGenerator->AppendEcho(
  506. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  507. }
  508. // Clean files associated with this library.
  509. std::vector<std::string> libCleanFiles;
  510. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  511. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  512. std::vector<std::string> commands1;
  513. // Add a command to remove any existing files for this library.
  514. // for static libs only
  515. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  516. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  517. this->GeneratorTarget, "target");
  518. this->LocalGenerator->CreateCDCommand(
  519. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  520. this->LocalGenerator->GetBinaryDirectory());
  521. commands.insert(commands.end(), commands1.begin(), commands1.end());
  522. commands1.clear();
  523. }
  524. if (targetName != targetNameReal) {
  525. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  526. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  527. }
  528. if (targetNameSO != targetNameReal && targetNameSO != targetName) {
  529. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  530. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO));
  531. }
  532. if (!targetNameImport.empty()) {
  533. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  534. this->LocalGenerator->GetCurrentBinaryDirectory(),
  535. targetFullPathImport));
  536. std::string implib;
  537. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  538. implib)) {
  539. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  540. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  541. }
  542. }
  543. // List the PDB for cleaning only when the whole target is
  544. // cleaned. We do not want to delete the .pdb file just before
  545. // linking the target.
  546. this->CleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  547. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  548. #ifdef _WIN32
  549. // There may be a manifest file for this target. Add it to the
  550. // clean set just in case.
  551. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  552. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  553. this->LocalGenerator->GetCurrentBinaryDirectory(),
  554. (targetFullPath + ".manifest").c_str()));
  555. }
  556. #endif
  557. // Add the pre-build and pre-link rules building but not when relinking.
  558. if (!relink) {
  559. this->LocalGenerator->AppendCustomCommands(
  560. commands, this->GeneratorTarget->GetPreBuildCommands(),
  561. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  562. this->LocalGenerator->AppendCustomCommands(
  563. commands, this->GeneratorTarget->GetPreLinkCommands(),
  564. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  565. }
  566. // Determine whether a link script will be used.
  567. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  568. bool useResponseFileForObjects =
  569. this->CheckUseResponseFileForObjects(linkLanguage);
  570. bool const useResponseFileForLibs =
  571. this->CheckUseResponseFileForLibraries(linkLanguage);
  572. // For static libraries there might be archiving rules.
  573. bool haveStaticLibraryRule = false;
  574. std::vector<std::string> archiveCreateCommands;
  575. std::vector<std::string> archiveAppendCommands;
  576. std::vector<std::string> archiveFinishCommands;
  577. std::string::size_type archiveCommandLimit = std::string::npos;
  578. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  579. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  580. std::string arCreateVar = "CMAKE_";
  581. arCreateVar += linkLanguage;
  582. arCreateVar += "_ARCHIVE_CREATE";
  583. if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) {
  584. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  585. }
  586. std::string arAppendVar = "CMAKE_";
  587. arAppendVar += linkLanguage;
  588. arAppendVar += "_ARCHIVE_APPEND";
  589. if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) {
  590. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  591. }
  592. std::string arFinishVar = "CMAKE_";
  593. arFinishVar += linkLanguage;
  594. arFinishVar += "_ARCHIVE_FINISH";
  595. if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) {
  596. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  597. }
  598. }
  599. // Decide whether to use archiving rules.
  600. bool useArchiveRules = !haveStaticLibraryRule &&
  601. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  602. if (useArchiveRules) {
  603. // Archiving rules are always run with a link script.
  604. useLinkScript = true;
  605. // Archiving rules never use a response file.
  606. useResponseFileForObjects = false;
  607. // Limit the length of individual object lists to less than the
  608. // 32K command line length limit on Windows. We could make this a
  609. // platform file variable but this should work everywhere.
  610. archiveCommandLimit = 30000;
  611. }
  612. // Expand the rule variables.
  613. std::vector<std::string> real_link_commands;
  614. {
  615. bool useWatcomQuote =
  616. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  617. // Set path conversion for link script shells.
  618. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  619. // Collect up flags to link in needed libraries.
  620. std::string linkLibs;
  621. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  622. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  623. this->CreateLinkLineComputer(
  624. this->LocalGenerator,
  625. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  626. linkLineComputer->SetForResponse(useResponseFileForLibs);
  627. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  628. linkLineComputer->SetRelink(relink);
  629. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  630. useResponseFileForLibs, depends);
  631. }
  632. // Construct object file lists that may be needed to expand the
  633. // rule.
  634. std::string buildObjs;
  635. this->CreateObjectLists(useLinkScript, useArchiveRules,
  636. useResponseFileForObjects, buildObjs, depends,
  637. useWatcomQuote);
  638. if (!this->DeviceLinkObject.empty()) {
  639. buildObjs += " " +
  640. this->LocalGenerator->ConvertToOutputFormat(
  641. this->LocalGenerator->MaybeConvertToRelativePath(
  642. this->LocalGenerator->GetCurrentBinaryDirectory(),
  643. this->DeviceLinkObject),
  644. cmOutputConverter::SHELL);
  645. }
  646. // maybe create .def file from list of objects
  647. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY &&
  648. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  649. this->GenDefFile(real_link_commands, linkFlags);
  650. }
  651. std::string manifests = this->GetManifests();
  652. cmRulePlaceholderExpander::RuleVariables vars;
  653. vars.TargetPDB = targetOutPathPDB.c_str();
  654. // Setup the target version.
  655. std::string targetVersionMajor;
  656. std::string targetVersionMinor;
  657. {
  658. std::ostringstream majorStream;
  659. std::ostringstream minorStream;
  660. int major;
  661. int minor;
  662. this->GeneratorTarget->GetTargetVersion(major, minor);
  663. majorStream << major;
  664. minorStream << minor;
  665. targetVersionMajor = majorStream.str();
  666. targetVersionMinor = minorStream.str();
  667. }
  668. vars.TargetVersionMajor = targetVersionMajor.c_str();
  669. vars.TargetVersionMinor = targetVersionMinor.c_str();
  670. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  671. vars.CMTargetType =
  672. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  673. vars.Language = linkLanguage.c_str();
  674. vars.Objects = buildObjs.c_str();
  675. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  676. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  677. this->LocalGenerator->MaybeConvertToRelativePath(
  678. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  679. cmOutputConverter::SHELL);
  680. vars.ObjectDir = objectDir.c_str();
  681. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  682. ? cmOutputConverter::WATCOMQUOTE
  683. : cmOutputConverter::SHELL;
  684. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  685. this->LocalGenerator->MaybeConvertToRelativePath(
  686. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  687. output);
  688. vars.Target = target.c_str();
  689. vars.LinkLibraries = linkLibs.c_str();
  690. vars.ObjectsQuoted = buildObjs.c_str();
  691. if (this->GeneratorTarget->HasSOName(this->ConfigName)) {
  692. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  693. vars.TargetSOName = targetNameSO.c_str();
  694. }
  695. vars.LinkFlags = linkFlags.c_str();
  696. vars.Manifests = manifests.c_str();
  697. // Compute the directory portion of the install_name setting.
  698. std::string install_name_dir;
  699. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY) {
  700. // Get the install_name directory for the build tree.
  701. install_name_dir =
  702. this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);
  703. // Set the rule variable replacement value.
  704. if (install_name_dir.empty()) {
  705. vars.TargetInstallNameDir = "";
  706. } else {
  707. // Convert to a path for the native build tool.
  708. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  709. install_name_dir, cmOutputConverter::SHELL);
  710. vars.TargetInstallNameDir = install_name_dir.c_str();
  711. }
  712. }
  713. // Add language feature flags.
  714. std::string langFlags;
  715. this->AddFeatureFlags(langFlags, linkLanguage);
  716. this->LocalGenerator->AddArchitectureFlags(
  717. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  718. vars.LanguageCompileFlags = langFlags.c_str();
  719. std::string launcher;
  720. const char* val = this->LocalGenerator->GetRuleLauncher(
  721. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  722. if (val && *val) {
  723. launcher = val;
  724. launcher += " ";
  725. }
  726. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  727. this->LocalGenerator->CreateRulePlaceholderExpander());
  728. // Construct the main link rule and expand placeholders.
  729. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  730. if (useArchiveRules) {
  731. // Construct the individual object list strings.
  732. std::vector<std::string> object_strings;
  733. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  734. // Create the archive with the first set of objects.
  735. std::vector<std::string>::iterator osi = object_strings.begin();
  736. {
  737. vars.Objects = osi->c_str();
  738. for (std::vector<std::string>::const_iterator i =
  739. archiveCreateCommands.begin();
  740. i != archiveCreateCommands.end(); ++i) {
  741. std::string cmd = launcher + *i;
  742. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  743. cmd, vars);
  744. real_link_commands.push_back(cmd);
  745. }
  746. }
  747. // Append to the archive with the other object sets.
  748. for (++osi; osi != object_strings.end(); ++osi) {
  749. vars.Objects = osi->c_str();
  750. for (std::vector<std::string>::const_iterator i =
  751. archiveAppendCommands.begin();
  752. i != archiveAppendCommands.end(); ++i) {
  753. std::string cmd = launcher + *i;
  754. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  755. cmd, vars);
  756. real_link_commands.push_back(cmd);
  757. }
  758. }
  759. // Finish the archive.
  760. vars.Objects = "";
  761. for (std::vector<std::string>::const_iterator i =
  762. archiveFinishCommands.begin();
  763. i != archiveFinishCommands.end(); ++i) {
  764. std::string cmd = launcher + *i;
  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(cmd);
  770. }
  771. }
  772. } else {
  773. // Get the set of commands.
  774. std::string linkRule = this->GetLinkRule(linkRuleVar);
  775. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  776. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
  777. (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  778. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  779. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  780. cmakeCommand += " -E __run_iwyu --lwyu=";
  781. cmakeCommand += targetOutPathReal;
  782. real_link_commands.push_back(cmakeCommand);
  783. }
  784. // Expand placeholders.
  785. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  786. i != real_link_commands.end(); ++i) {
  787. *i = launcher + *i;
  788. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  789. vars);
  790. }
  791. }
  792. // Restore path conversion to normal shells.
  793. this->LocalGenerator->SetLinkScriptShell(false);
  794. }
  795. // Optionally convert the build rule to use a script to avoid long
  796. // command lines in the make shell.
  797. if (useLinkScript) {
  798. // Use a link script.
  799. const char* name = (relink ? "relink.txt" : "link.txt");
  800. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  801. } else {
  802. // No link script. Just use the link rule directly.
  803. commands1 = real_link_commands;
  804. }
  805. this->LocalGenerator->CreateCDCommand(
  806. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  807. this->LocalGenerator->GetBinaryDirectory());
  808. commands.insert(commands.end(), commands1.begin(), commands1.end());
  809. commands1.clear();
  810. // Add a rule to create necessary symlinks for the library.
  811. // Frameworks are handled by cmOSXBundleGenerator.
  812. if (targetOutPath != targetOutPathReal &&
  813. !this->GeneratorTarget->IsFrameworkOnApple()) {
  814. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  815. symlink += targetOutPathReal;
  816. symlink += " ";
  817. symlink += targetOutPathSO;
  818. symlink += " ";
  819. symlink += targetOutPath;
  820. commands1.push_back(symlink);
  821. this->LocalGenerator->CreateCDCommand(
  822. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  823. this->LocalGenerator->GetBinaryDirectory());
  824. commands.insert(commands.end(), commands1.begin(), commands1.end());
  825. commands1.clear();
  826. }
  827. // Add the post-build rules when building but not when relinking.
  828. if (!relink) {
  829. this->LocalGenerator->AppendCustomCommands(
  830. commands, this->GeneratorTarget->GetPostBuildCommands(),
  831. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  832. }
  833. // Compute the list of outputs.
  834. std::vector<std::string> outputs(1, targetFullPathReal);
  835. if (targetNameSO != targetNameReal) {
  836. outputs.push_back(targetFullPathSO);
  837. }
  838. if (targetName != targetNameSO && targetName != targetNameReal) {
  839. outputs.push_back(targetFullPath);
  840. }
  841. // Write the build rule.
  842. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  843. commands, false);
  844. // Write the main driver rule to build everything in this target.
  845. this->WriteTargetDriverRule(targetFullPath, relink);
  846. // Clean all the possible library names and symlinks.
  847. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  848. libCleanFiles.end());
  849. }