cmMakefileLibraryTargetGenerator.cxx 28 KB

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