cmMakefileLibraryTargetGenerator.cxx 28 KB

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