cmMakefileLibraryTargetGenerator.cxx 28 KB

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