cmMakefileLibraryTargetGenerator.cxx 27 KB

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