cmMakefileLibraryTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmMakefileLibraryTargetGenerator.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalUnixMakefileGenerator3.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmLocalUnixMakefileGenerator3.h"
  16. #include "cmMakefile.h"
  17. #include "cmOSXBundleGenerator.h"
  18. #include "cmOutputConverter.h"
  19. #include "cmState.h"
  20. #include "cmSystemTools.h"
  21. #include "cmake.h"
  22. #include <sstream>
  23. #include <vector>
  24. cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator(
  25. cmGeneratorTarget* target)
  26. : cmMakefileTargetGenerator(target)
  27. {
  28. this->CustomCommandDriver = OnDepends;
  29. if (this->GeneratorTarget->GetType() != cmState::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 cmState::STATIC_LIBRARY:
  56. this->WriteStaticLibraryRules();
  57. break;
  58. case cmState::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 cmState::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 cmState::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. this->AddModuleDefinitionFlag(extraFlags);
  145. if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE")) {
  146. this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed");
  147. }
  148. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  149. }
  150. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  151. {
  152. std::string linkLanguage =
  153. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  154. std::string linkRuleVar = "CMAKE_";
  155. linkRuleVar += linkLanguage;
  156. linkRuleVar += "_CREATE_SHARED_MODULE";
  157. std::string extraFlags;
  158. this->LocalGenerator->AppendFlags(
  159. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  160. std::string linkFlagsConfig = "LINK_FLAGS_";
  161. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  162. this->LocalGenerator->AppendFlags(
  163. extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  164. this->LocalGenerator->AddConfigVariableFlags(
  165. extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  166. this->AddModuleDefinitionFlag(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->Convert(
  270. targetFullPath, cmOutputConverter::START_OUTPUT, cmOutputConverter::SHELL);
  271. std::string targetOutPathSO =
  272. this->Convert(targetFullPathSO, cmOutputConverter::START_OUTPUT,
  273. cmOutputConverter::SHELL);
  274. std::string targetOutPathReal =
  275. this->Convert(targetFullPathReal, cmOutputConverter::START_OUTPUT,
  276. cmOutputConverter::SHELL);
  277. std::string targetOutPathImport =
  278. this->Convert(targetFullPathImport, cmOutputConverter::START_OUTPUT,
  279. cmOutputConverter::SHELL);
  280. this->NumberOfProgressActions++;
  281. if (!this->NoRuleMessages) {
  282. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  283. this->MakeEchoProgress(progress);
  284. // Add the link message.
  285. std::string buildEcho = "Linking ";
  286. buildEcho += linkLanguage;
  287. switch (this->GeneratorTarget->GetType()) {
  288. case cmState::STATIC_LIBRARY:
  289. buildEcho += " static library ";
  290. break;
  291. case cmState::SHARED_LIBRARY:
  292. buildEcho += " shared library ";
  293. break;
  294. case cmState::MODULE_LIBRARY:
  295. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  296. buildEcho += " CFBundle";
  297. }
  298. buildEcho += " shared module ";
  299. break;
  300. default:
  301. buildEcho += " library ";
  302. break;
  303. }
  304. buildEcho += targetOutPath;
  305. this->LocalGenerator->AppendEcho(
  306. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  307. }
  308. const char* forbiddenFlagVar = CM_NULLPTR;
  309. switch (this->GeneratorTarget->GetType()) {
  310. case cmState::SHARED_LIBRARY:
  311. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  312. break;
  313. case cmState::MODULE_LIBRARY:
  314. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  315. break;
  316. default:
  317. break;
  318. }
  319. // Clean files associated with this library.
  320. std::vector<std::string> libCleanFiles;
  321. libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  322. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  323. if (targetNameReal != targetName) {
  324. libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  325. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  326. }
  327. if (targetNameSO != targetName && targetNameSO != targetNameReal) {
  328. libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  329. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO));
  330. }
  331. if (!targetNameImport.empty()) {
  332. libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  333. this->LocalGenerator->GetCurrentBinaryDirectory(),
  334. targetFullPathImport));
  335. std::string implib;
  336. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  337. implib)) {
  338. libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  339. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  340. }
  341. }
  342. // List the PDB for cleaning only when the whole target is
  343. // cleaned. We do not want to delete the .pdb file just before
  344. // linking the target.
  345. this->CleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  346. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  347. #ifdef _WIN32
  348. // There may be a manifest file for this target. Add it to the
  349. // clean set just in case.
  350. if (this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY) {
  351. libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  352. this->LocalGenerator->GetCurrentBinaryDirectory(),
  353. (targetFullPath + ".manifest").c_str()));
  354. }
  355. #endif
  356. std::vector<std::string> commands1;
  357. // Add a command to remove any existing files for this library.
  358. // for static libs only
  359. if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY) {
  360. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  361. this->GeneratorTarget, "target");
  362. this->LocalGenerator->CreateCDCommand(
  363. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  364. this->LocalGenerator->GetBinaryDirectory());
  365. commands.insert(commands.end(), commands1.begin(), commands1.end());
  366. commands1.clear();
  367. }
  368. // Add the pre-build and pre-link rules building but not when relinking.
  369. if (!relink) {
  370. this->LocalGenerator->AppendCustomCommands(
  371. commands, this->GeneratorTarget->GetPreBuildCommands(),
  372. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  373. this->LocalGenerator->AppendCustomCommands(
  374. commands, this->GeneratorTarget->GetPreLinkCommands(),
  375. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  376. }
  377. // Determine whether a link script will be used.
  378. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  379. bool useResponseFileForObjects =
  380. this->CheckUseResponseFileForObjects(linkLanguage);
  381. bool const useResponseFileForLibs =
  382. this->CheckUseResponseFileForLibraries(linkLanguage);
  383. // For static libraries there might be archiving rules.
  384. bool haveStaticLibraryRule = false;
  385. std::vector<std::string> archiveCreateCommands;
  386. std::vector<std::string> archiveAppendCommands;
  387. std::vector<std::string> archiveFinishCommands;
  388. std::string::size_type archiveCommandLimit = std::string::npos;
  389. if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY) {
  390. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  391. std::string arCreateVar = "CMAKE_";
  392. arCreateVar += linkLanguage;
  393. arCreateVar += "_ARCHIVE_CREATE";
  394. if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) {
  395. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  396. }
  397. std::string arAppendVar = "CMAKE_";
  398. arAppendVar += linkLanguage;
  399. arAppendVar += "_ARCHIVE_APPEND";
  400. if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) {
  401. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  402. }
  403. std::string arFinishVar = "CMAKE_";
  404. arFinishVar += linkLanguage;
  405. arFinishVar += "_ARCHIVE_FINISH";
  406. if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) {
  407. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  408. }
  409. }
  410. // Decide whether to use archiving rules.
  411. bool useArchiveRules = !haveStaticLibraryRule &&
  412. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  413. if (useArchiveRules) {
  414. // Archiving rules are always run with a link script.
  415. useLinkScript = true;
  416. // Archiving rules never use a response file.
  417. useResponseFileForObjects = false;
  418. // Limit the length of individual object lists to less than the
  419. // 32K command line length limit on Windows. We could make this a
  420. // platform file variable but this should work everywhere.
  421. archiveCommandLimit = 30000;
  422. }
  423. // Expand the rule variables.
  424. std::vector<std::string> real_link_commands;
  425. {
  426. bool useWatcomQuote =
  427. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  428. // Set path conversion for link script shells.
  429. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  430. // Collect up flags to link in needed libraries.
  431. std::string linkLibs;
  432. if (this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY) {
  433. this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
  434. useWatcomQuote);
  435. }
  436. // Construct object file lists that may be needed to expand the
  437. // rule.
  438. std::string buildObjs;
  439. this->CreateObjectLists(useLinkScript, useArchiveRules,
  440. useResponseFileForObjects, buildObjs, depends,
  441. useWatcomQuote);
  442. // maybe create .def file from list of objects
  443. if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY &&
  444. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  445. this->GenDefFile(real_link_commands, linkFlags);
  446. }
  447. std::string manifests = this->GetManifests();
  448. cmLocalGenerator::RuleVariables vars;
  449. vars.TargetPDB = targetOutPathPDB.c_str();
  450. // Setup the target version.
  451. std::string targetVersionMajor;
  452. std::string targetVersionMinor;
  453. {
  454. std::ostringstream majorStream;
  455. std::ostringstream minorStream;
  456. int major;
  457. int minor;
  458. this->GeneratorTarget->GetTargetVersion(major, minor);
  459. majorStream << major;
  460. minorStream << minor;
  461. targetVersionMajor = majorStream.str();
  462. targetVersionMinor = minorStream.str();
  463. }
  464. vars.TargetVersionMajor = targetVersionMajor.c_str();
  465. vars.TargetVersionMinor = targetVersionMinor.c_str();
  466. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  467. vars.CMTarget = this->GeneratorTarget;
  468. vars.Language = linkLanguage.c_str();
  469. vars.Objects = buildObjs.c_str();
  470. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  471. objectDir = this->Convert(objectDir, cmOutputConverter::START_OUTPUT,
  472. cmOutputConverter::SHELL);
  473. vars.ObjectDir = objectDir.c_str();
  474. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  475. ? cmOutputConverter::WATCOMQUOTE
  476. : cmOutputConverter::SHELL;
  477. std::string target = this->Convert(
  478. targetFullPathReal, cmOutputConverter::START_OUTPUT, output);
  479. vars.Target = target.c_str();
  480. vars.LinkLibraries = linkLibs.c_str();
  481. vars.ObjectsQuoted = buildObjs.c_str();
  482. if (this->GeneratorTarget->HasSOName(this->ConfigName)) {
  483. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  484. vars.TargetSOName = targetNameSO.c_str();
  485. }
  486. vars.LinkFlags = linkFlags.c_str();
  487. vars.Manifests = manifests.c_str();
  488. // Compute the directory portion of the install_name setting.
  489. std::string install_name_dir;
  490. if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY) {
  491. // Get the install_name directory for the build tree.
  492. install_name_dir =
  493. this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);
  494. // Set the rule variable replacement value.
  495. if (install_name_dir.empty()) {
  496. vars.TargetInstallNameDir = "";
  497. } else {
  498. // Convert to a path for the native build tool.
  499. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  500. install_name_dir, cmOutputConverter::SHELL);
  501. vars.TargetInstallNameDir = install_name_dir.c_str();
  502. }
  503. }
  504. // Add language feature flags.
  505. std::string langFlags;
  506. this->AddFeatureFlags(langFlags, linkLanguage);
  507. this->LocalGenerator->AddArchitectureFlags(
  508. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  509. // remove any language flags that might not work with the
  510. // particular os
  511. if (forbiddenFlagVar) {
  512. this->RemoveForbiddenFlags(forbiddenFlagVar, linkLanguage, langFlags);
  513. }
  514. vars.LanguageCompileFlags = langFlags.c_str();
  515. // Construct the main link rule and expand placeholders.
  516. this->LocalGenerator->TargetImplib = targetOutPathImport;
  517. if (useArchiveRules) {
  518. // Construct the individual object list strings.
  519. std::vector<std::string> object_strings;
  520. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  521. // Create the archive with the first set of objects.
  522. std::vector<std::string>::iterator osi = object_strings.begin();
  523. {
  524. vars.Objects = osi->c_str();
  525. for (std::vector<std::string>::const_iterator i =
  526. archiveCreateCommands.begin();
  527. i != archiveCreateCommands.end(); ++i) {
  528. std::string cmd = *i;
  529. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  530. real_link_commands.push_back(cmd);
  531. }
  532. }
  533. // Append to the archive with the other object sets.
  534. for (++osi; osi != object_strings.end(); ++osi) {
  535. vars.Objects = osi->c_str();
  536. for (std::vector<std::string>::const_iterator i =
  537. archiveAppendCommands.begin();
  538. i != archiveAppendCommands.end(); ++i) {
  539. std::string cmd = *i;
  540. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  541. real_link_commands.push_back(cmd);
  542. }
  543. }
  544. // Finish the archive.
  545. vars.Objects = "";
  546. for (std::vector<std::string>::const_iterator i =
  547. archiveFinishCommands.begin();
  548. i != archiveFinishCommands.end(); ++i) {
  549. std::string cmd = *i;
  550. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  551. // If there is no ranlib the command will be ":". Skip it.
  552. if (!cmd.empty() && cmd[0] != ':') {
  553. real_link_commands.push_back(cmd);
  554. }
  555. }
  556. } else {
  557. // Get the set of commands.
  558. std::string linkRule = this->GetLinkRule(linkRuleVar);
  559. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  560. if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE") &&
  561. (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY)) {
  562. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  563. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  564. cmakeCommand += " -E __run_iwyu --lwyu=";
  565. cmakeCommand += targetOutPathReal;
  566. real_link_commands.push_back(cmakeCommand);
  567. }
  568. // Expand placeholders.
  569. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  570. i != real_link_commands.end(); ++i) {
  571. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  572. }
  573. }
  574. this->LocalGenerator->TargetImplib = "";
  575. // Restore path conversion to normal shells.
  576. this->LocalGenerator->SetLinkScriptShell(false);
  577. }
  578. // Optionally convert the build rule to use a script to avoid long
  579. // command lines in the make shell.
  580. if (useLinkScript) {
  581. // Use a link script.
  582. const char* name = (relink ? "relink.txt" : "link.txt");
  583. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  584. } else {
  585. // No link script. Just use the link rule directly.
  586. commands1 = real_link_commands;
  587. }
  588. this->LocalGenerator->CreateCDCommand(
  589. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  590. this->LocalGenerator->GetBinaryDirectory());
  591. commands.insert(commands.end(), commands1.begin(), commands1.end());
  592. commands1.clear();
  593. // Add a rule to create necessary symlinks for the library.
  594. // Frameworks are handled by cmOSXBundleGenerator.
  595. if (targetOutPath != targetOutPathReal &&
  596. !this->GeneratorTarget->IsFrameworkOnApple()) {
  597. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  598. symlink += targetOutPathReal;
  599. symlink += " ";
  600. symlink += targetOutPathSO;
  601. symlink += " ";
  602. symlink += targetOutPath;
  603. commands1.push_back(symlink);
  604. this->LocalGenerator->CreateCDCommand(
  605. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  606. this->LocalGenerator->GetBinaryDirectory());
  607. commands.insert(commands.end(), commands1.begin(), commands1.end());
  608. commands1.clear();
  609. }
  610. // Add the post-build rules when building but not when relinking.
  611. if (!relink) {
  612. this->LocalGenerator->AppendCustomCommands(
  613. commands, this->GeneratorTarget->GetPostBuildCommands(),
  614. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  615. }
  616. // Compute the list of outputs.
  617. std::vector<std::string> outputs(1, targetFullPathReal);
  618. if (targetNameSO != targetNameReal) {
  619. outputs.push_back(targetFullPathSO);
  620. }
  621. if (targetName != targetNameSO && targetName != targetNameReal) {
  622. outputs.push_back(targetFullPath);
  623. }
  624. // Write the build rule.
  625. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  626. commands, false);
  627. // Write the main driver rule to build everything in this target.
  628. this->WriteTargetDriverRule(targetFullPath, relink);
  629. // Clean all the possible library names and symlinks.
  630. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  631. libCleanFiles.end());
  632. }