cmMakefileLibraryTargetGenerator.cxx 28 KB

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