cmMakefileExecutableTargetGenerator.cxx 18 KB

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