cmMakefileExecutableTargetGenerator.cxx 17 KB

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