cmMakefileExecutableTargetGenerator.cxx 17 KB

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