cmMakefileExecutableTargetGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 "cmMakefileExecutableTargetGenerator.h"
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. #include "cmGeneratedFileStream.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmGlobalUnixMakefileGenerator3.h"
  10. #include "cmLinkLineComputer.h"
  11. #include "cmLocalGenerator.h"
  12. #include "cmLocalUnixMakefileGenerator3.h"
  13. #include "cmMakefile.h"
  14. #include "cmOSXBundleGenerator.h"
  15. #include "cmOutputConverter.h"
  16. #include "cmRulePlaceholderExpander.h"
  17. #include "cmState.h"
  18. #include "cmStateDirectory.h"
  19. #include "cmStateSnapshot.h"
  20. #include "cmSystemTools.h"
  21. #include "cm_auto_ptr.hxx"
  22. #include "cmake.h"
  23. cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator(
  24. cmGeneratorTarget* target)
  25. : cmMakefileTargetGenerator(target)
  26. {
  27. this->CustomCommandDriver = OnDepends;
  28. this->GeneratorTarget->GetExecutableNames(
  29. this->TargetNameOut, this->TargetNameReal, this->TargetNameImport,
  30. this->TargetNamePDB, this->ConfigName);
  31. this->OSXBundleGenerator =
  32. new cmOSXBundleGenerator(target, this->ConfigName);
  33. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  34. }
  35. cmMakefileExecutableTargetGenerator::~cmMakefileExecutableTargetGenerator()
  36. {
  37. delete this->OSXBundleGenerator;
  38. }
  39. void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
  40. {
  41. // create the build.make file and directory, put in the common blocks
  42. this->CreateRuleFile();
  43. // write rules used to help build object files
  44. this->WriteCommonCodeRules();
  45. // write the per-target per-language flags
  46. this->WriteTargetLanguageFlags();
  47. // write in rules for object files and custom commands
  48. this->WriteTargetBuildRules();
  49. // write the link rules
  50. this->WriteExecutableRule(false);
  51. if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
  52. // Write rules to link an installable version of the target.
  53. this->WriteExecutableRule(true);
  54. }
  55. // Write the requires target.
  56. this->WriteTargetRequiresRules();
  57. // Write clean target
  58. this->WriteTargetCleanRules();
  59. // Write the dependency generation rule. This must be done last so
  60. // that multiple output pair information is available.
  61. this->WriteTargetDependRules();
  62. // close the streams
  63. this->CloseFileStreams();
  64. }
  65. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  66. {
  67. std::vector<std::string> commands;
  68. // Build list of dependencies.
  69. std::vector<std::string> depends;
  70. this->AppendLinkDepends(depends);
  71. // Get the name of the executable to generate.
  72. std::string targetName;
  73. std::string targetNameReal;
  74. std::string targetNameImport;
  75. std::string targetNamePDB;
  76. this->GeneratorTarget->GetExecutableNames(targetName, targetNameReal,
  77. targetNameImport, targetNamePDB,
  78. this->ConfigName);
  79. // Construct the full path version of the names.
  80. std::string outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  81. if (this->GeneratorTarget->IsAppBundleOnApple()) {
  82. this->OSXBundleGenerator->CreateAppBundle(targetName, outpath);
  83. }
  84. outpath += "/";
  85. std::string outpathImp;
  86. if (relink) {
  87. outpath = this->Makefile->GetCurrentBinaryDirectory();
  88. outpath += cmake::GetCMakeFilesDirectory();
  89. outpath += "/CMakeRelink.dir";
  90. cmSystemTools::MakeDirectory(outpath.c_str());
  91. outpath += "/";
  92. if (!targetNameImport.empty()) {
  93. outpathImp = outpath;
  94. }
  95. } else {
  96. cmSystemTools::MakeDirectory(outpath.c_str());
  97. if (!targetNameImport.empty()) {
  98. outpathImp = this->GeneratorTarget->GetDirectory(this->ConfigName, true);
  99. cmSystemTools::MakeDirectory(outpathImp.c_str());
  100. outpathImp += "/";
  101. }
  102. }
  103. std::string compilePdbOutputPath =
  104. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  105. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  106. std::string pdbOutputPath =
  107. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  108. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  109. pdbOutputPath += "/";
  110. std::string targetFullPath = outpath + targetName;
  111. std::string targetFullPathReal = outpath + targetNameReal;
  112. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  113. std::string targetFullPathImport = outpathImp + targetNameImport;
  114. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  115. targetFullPathPDB, cmOutputConverter::SHELL);
  116. // Convert to the output path to use in constructing commands.
  117. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  118. this->LocalGenerator->MaybeConvertToRelativePath(
  119. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  120. cmOutputConverter::SHELL);
  121. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  122. this->LocalGenerator->MaybeConvertToRelativePath(
  123. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  124. cmOutputConverter::SHELL);
  125. std::string targetOutPathImport =
  126. this->LocalGenerator->ConvertToOutputFormat(
  127. this->LocalGenerator->MaybeConvertToRelativePath(
  128. this->LocalGenerator->GetCurrentBinaryDirectory(),
  129. targetFullPathImport),
  130. cmOutputConverter::SHELL);
  131. // Get the language to use for linking this executable.
  132. std::string linkLanguage =
  133. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  134. // Make sure we have a link language.
  135. if (linkLanguage.empty()) {
  136. cmSystemTools::Error("Cannot determine link language for target \"",
  137. this->GeneratorTarget->GetName().c_str(), "\".");
  138. return;
  139. }
  140. this->NumberOfProgressActions++;
  141. if (!this->NoRuleMessages) {
  142. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  143. this->MakeEchoProgress(progress);
  144. // Add the link message.
  145. std::string buildEcho = "Linking ";
  146. buildEcho += linkLanguage;
  147. buildEcho += " executable ";
  148. buildEcho += targetOutPath;
  149. this->LocalGenerator->AppendEcho(
  150. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  151. }
  152. // Build a list of compiler flags and linker flags.
  153. std::string flags;
  154. std::string linkFlags;
  155. // Add flags to create an executable.
  156. this->LocalGenerator->AddConfigVariableFlags(
  157. linkFlags, "CMAKE_EXE_LINKER_FLAGS", this->ConfigName);
  158. if (this->GeneratorTarget->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  159. this->LocalGenerator->AppendFlags(
  160. linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  161. } else {
  162. this->LocalGenerator->AppendFlags(
  163. linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  164. }
  165. // Add symbol export flags if necessary.
  166. if (this->GeneratorTarget->IsExecutableWithExports()) {
  167. std::string export_flag_var = "CMAKE_EXE_EXPORTS_";
  168. export_flag_var += linkLanguage;
  169. export_flag_var += "_FLAG";
  170. this->LocalGenerator->AppendFlags(
  171. linkFlags, this->Makefile->GetDefinition(export_flag_var));
  172. }
  173. this->LocalGenerator->AppendFlags(linkFlags,
  174. this->LocalGenerator->GetLinkLibsCMP0065(
  175. linkLanguage, *this->GeneratorTarget));
  176. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  177. this->LocalGenerator->AppendFlags(linkFlags, " -Wl,--no-as-needed");
  178. }
  179. // Add language feature flags.
  180. this->AddFeatureFlags(flags, linkLanguage);
  181. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  182. linkLanguage, this->ConfigName);
  183. // Add target-specific linker flags.
  184. this->LocalGenerator->AppendFlags(
  185. linkFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  186. std::string linkFlagsConfig = "LINK_FLAGS_";
  187. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  188. this->LocalGenerator->AppendFlags(
  189. linkFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  190. {
  191. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  192. this->CreateLinkLineComputer(
  193. this->LocalGenerator,
  194. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  195. this->AddModuleDefinitionFlag(linkLineComputer.get(), linkFlags);
  196. }
  197. // Construct a list of files associated with this executable that
  198. // may need to be cleaned.
  199. std::vector<std::string> exeCleanFiles;
  200. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  201. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  202. #ifdef _WIN32
  203. // There may be a manifest file for this target. Add it to the
  204. // clean set just in case.
  205. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  206. this->LocalGenerator->GetCurrentBinaryDirectory(),
  207. (targetFullPath + ".manifest").c_str()));
  208. #endif
  209. if (targetNameReal != targetName) {
  210. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  211. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  212. }
  213. if (!targetNameImport.empty()) {
  214. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  215. this->LocalGenerator->GetCurrentBinaryDirectory(),
  216. targetFullPathImport));
  217. std::string implib;
  218. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  219. implib)) {
  220. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  221. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  222. }
  223. }
  224. // List the PDB for cleaning only when the whole target is
  225. // cleaned. We do not want to delete the .pdb file just before
  226. // linking the target.
  227. this->CleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  228. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  229. // Add the pre-build and pre-link rules building but not when relinking.
  230. if (!relink) {
  231. this->LocalGenerator->AppendCustomCommands(
  232. commands, this->GeneratorTarget->GetPreBuildCommands(),
  233. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  234. this->LocalGenerator->AppendCustomCommands(
  235. commands, this->GeneratorTarget->GetPreLinkCommands(),
  236. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  237. }
  238. // Determine whether a link script will be used.
  239. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  240. // Construct the main link rule.
  241. std::vector<std::string> real_link_commands;
  242. std::string linkRuleVar = "CMAKE_";
  243. linkRuleVar += linkLanguage;
  244. linkRuleVar += "_LINK_EXECUTABLE";
  245. std::string linkRule = this->GetLinkRule(linkRuleVar);
  246. std::vector<std::string> commands1;
  247. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  248. if (this->GeneratorTarget->IsExecutableWithExports()) {
  249. // If a separate rule for creating an import library is specified
  250. // add it now.
  251. std::string implibRuleVar = "CMAKE_";
  252. implibRuleVar += linkLanguage;
  253. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  254. if (const char* rule = this->Makefile->GetDefinition(implibRuleVar)) {
  255. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  256. }
  257. }
  258. bool useResponseFileForObjects =
  259. this->CheckUseResponseFileForObjects(linkLanguage);
  260. bool const useResponseFileForLibs =
  261. this->CheckUseResponseFileForLibraries(linkLanguage);
  262. // Expand the rule variables.
  263. {
  264. bool useWatcomQuote =
  265. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  266. // Set path conversion for link script shells.
  267. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  268. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  269. this->CreateLinkLineComputer(
  270. this->LocalGenerator,
  271. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  272. linkLineComputer->SetForResponse(useResponseFileForLibs);
  273. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  274. linkLineComputer->SetRelink(relink);
  275. // Collect up flags to link in needed libraries.
  276. std::string linkLibs;
  277. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  278. useResponseFileForLibs, depends);
  279. // Construct object file lists that may be needed to expand the
  280. // rule.
  281. std::string buildObjs;
  282. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  283. buildObjs, depends, useWatcomQuote);
  284. // maybe create .def file from list of objects
  285. if (this->GeneratorTarget->IsExecutableWithExports() &&
  286. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  287. this->GenDefFile(real_link_commands, linkFlags);
  288. }
  289. std::string manifests = this->GetManifests();
  290. cmRulePlaceholderExpander::RuleVariables vars;
  291. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  292. vars.CMTargetType =
  293. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  294. vars.Language = linkLanguage.c_str();
  295. vars.Objects = buildObjs.c_str();
  296. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  297. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  298. this->LocalGenerator->MaybeConvertToRelativePath(
  299. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  300. cmOutputConverter::SHELL);
  301. vars.ObjectDir = objectDir.c_str();
  302. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  303. ? cmOutputConverter::WATCOMQUOTE
  304. : cmOutputConverter::SHELL;
  305. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  306. this->LocalGenerator->MaybeConvertToRelativePath(
  307. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  308. output);
  309. vars.Target = target.c_str();
  310. vars.TargetPDB = targetOutPathPDB.c_str();
  311. // Setup the target version.
  312. std::string targetVersionMajor;
  313. std::string targetVersionMinor;
  314. {
  315. std::ostringstream majorStream;
  316. std::ostringstream minorStream;
  317. int major;
  318. int minor;
  319. this->GeneratorTarget->GetTargetVersion(major, minor);
  320. majorStream << major;
  321. minorStream << minor;
  322. targetVersionMajor = majorStream.str();
  323. targetVersionMinor = minorStream.str();
  324. }
  325. vars.TargetVersionMajor = targetVersionMajor.c_str();
  326. vars.TargetVersionMinor = targetVersionMinor.c_str();
  327. vars.LinkLibraries = linkLibs.c_str();
  328. vars.Flags = flags.c_str();
  329. vars.LinkFlags = linkFlags.c_str();
  330. vars.Manifests = manifests.c_str();
  331. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  332. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  333. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  334. cmakeCommand += " -E __run_iwyu --lwyu=";
  335. cmakeCommand += targetOutPathReal;
  336. real_link_commands.push_back(cmakeCommand);
  337. }
  338. std::string launcher;
  339. const char* val = this->LocalGenerator->GetRuleLauncher(
  340. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  341. if (val && *val) {
  342. launcher = val;
  343. launcher += " ";
  344. }
  345. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  346. this->LocalGenerator->CreateRulePlaceholderExpander());
  347. // Expand placeholders in the commands.
  348. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  349. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  350. i != real_link_commands.end(); ++i) {
  351. *i = launcher + *i;
  352. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  353. vars);
  354. }
  355. // Restore path conversion to normal shells.
  356. this->LocalGenerator->SetLinkScriptShell(false);
  357. }
  358. // Optionally convert the build rule to use a script to avoid long
  359. // command lines in the make shell.
  360. if (useLinkScript) {
  361. // Use a link script.
  362. const char* name = (relink ? "relink.txt" : "link.txt");
  363. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  364. } else {
  365. // No link script. Just use the link rule directly.
  366. commands1 = real_link_commands;
  367. }
  368. this->LocalGenerator->CreateCDCommand(
  369. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  370. this->LocalGenerator->GetBinaryDirectory());
  371. commands.insert(commands.end(), commands1.begin(), commands1.end());
  372. commands1.clear();
  373. // Add a rule to create necessary symlinks for the library.
  374. if (targetOutPath != targetOutPathReal) {
  375. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  376. symlink += targetOutPathReal;
  377. symlink += " ";
  378. symlink += targetOutPath;
  379. commands1.push_back(symlink);
  380. this->LocalGenerator->CreateCDCommand(
  381. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  382. this->LocalGenerator->GetBinaryDirectory());
  383. commands.insert(commands.end(), commands1.begin(), commands1.end());
  384. commands1.clear();
  385. }
  386. // Add the post-build rules when building but not when relinking.
  387. if (!relink) {
  388. this->LocalGenerator->AppendCustomCommands(
  389. commands, this->GeneratorTarget->GetPostBuildCommands(),
  390. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  391. }
  392. // Write the build rule.
  393. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  394. targetFullPathReal, depends, commands,
  395. false);
  396. // The symlink name for the target should depend on the real target
  397. // so if the target version changes it rebuilds and recreates the
  398. // symlink.
  399. if (targetFullPath != targetFullPathReal) {
  400. depends.clear();
  401. commands.clear();
  402. depends.push_back(targetFullPathReal);
  403. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  404. targetFullPath, depends, commands,
  405. false);
  406. }
  407. // Write the main driver rule to build everything in this target.
  408. this->WriteTargetDriverRule(targetFullPath, relink);
  409. // Clean all the possible executable names and symlinks.
  410. this->CleanFiles.insert(this->CleanFiles.end(), exeCleanFiles.begin(),
  411. exeCleanFiles.end());
  412. }