cmMakefileExecutableTargetGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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->GetStateSnapshot().GetDirectory()));
  189. this->AddModuleDefinitionFlag(linkLineComputer.get(), linkFlags);
  190. }
  191. // Construct a list of files associated with this executable that
  192. // may need to be cleaned.
  193. std::vector<std::string> exeCleanFiles;
  194. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  195. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  196. #ifdef _WIN32
  197. // There may be a manifest file for this target. Add it to the
  198. // clean set just in case.
  199. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  200. this->LocalGenerator->GetCurrentBinaryDirectory(),
  201. (targetFullPath + ".manifest").c_str()));
  202. #endif
  203. if (targetNameReal != targetName) {
  204. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  205. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  206. }
  207. if (!targetNameImport.empty()) {
  208. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  209. this->LocalGenerator->GetCurrentBinaryDirectory(),
  210. targetFullPathImport));
  211. std::string implib;
  212. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  213. implib)) {
  214. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  215. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  216. }
  217. }
  218. // List the PDB for cleaning only when the whole target is
  219. // cleaned. We do not want to delete the .pdb file just before
  220. // linking the target.
  221. this->CleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  222. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  223. // Add the pre-build and pre-link rules building but not when relinking.
  224. if (!relink) {
  225. this->LocalGenerator->AppendCustomCommands(
  226. commands, this->GeneratorTarget->GetPreBuildCommands(),
  227. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  228. this->LocalGenerator->AppendCustomCommands(
  229. commands, this->GeneratorTarget->GetPreLinkCommands(),
  230. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  231. }
  232. // Determine whether a link script will be used.
  233. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  234. // Construct the main link rule.
  235. std::vector<std::string> real_link_commands;
  236. std::string linkRuleVar = "CMAKE_";
  237. linkRuleVar += linkLanguage;
  238. linkRuleVar += "_LINK_EXECUTABLE";
  239. std::string linkRule = this->GetLinkRule(linkRuleVar);
  240. std::vector<std::string> commands1;
  241. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  242. if (this->GeneratorTarget->IsExecutableWithExports()) {
  243. // If a separate rule for creating an import library is specified
  244. // add it now.
  245. std::string implibRuleVar = "CMAKE_";
  246. implibRuleVar += linkLanguage;
  247. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  248. if (const char* rule = this->Makefile->GetDefinition(implibRuleVar)) {
  249. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  250. }
  251. }
  252. bool useResponseFileForObjects =
  253. this->CheckUseResponseFileForObjects(linkLanguage);
  254. bool const useResponseFileForLibs =
  255. this->CheckUseResponseFileForLibraries(linkLanguage);
  256. // Expand the rule variables.
  257. {
  258. bool useWatcomQuote =
  259. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  260. // Set path conversion for link script shells.
  261. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  262. // Collect up flags to link in needed libraries.
  263. std::string linkLibs;
  264. this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
  265. useWatcomQuote);
  266. // Construct object file lists that may be needed to expand the
  267. // rule.
  268. std::string buildObjs;
  269. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  270. buildObjs, depends, useWatcomQuote);
  271. // maybe create .def file from list of objects
  272. if (this->GeneratorTarget->IsExecutableWithExports() &&
  273. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  274. this->GenDefFile(real_link_commands, linkFlags);
  275. }
  276. std::string manifests = this->GetManifests();
  277. cmLocalGenerator::RuleVariables vars;
  278. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  279. vars.CMTarget = this->GeneratorTarget;
  280. vars.Language = linkLanguage.c_str();
  281. vars.Objects = buildObjs.c_str();
  282. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  283. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  284. this->LocalGenerator->MaybeConvertToRelativePath(
  285. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  286. cmOutputConverter::SHELL);
  287. vars.ObjectDir = objectDir.c_str();
  288. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  289. ? cmOutputConverter::WATCOMQUOTE
  290. : cmOutputConverter::SHELL;
  291. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  292. this->LocalGenerator->MaybeConvertToRelativePath(
  293. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  294. output);
  295. vars.Target = target.c_str();
  296. vars.TargetPDB = targetOutPathPDB.c_str();
  297. // Setup the target version.
  298. std::string targetVersionMajor;
  299. std::string targetVersionMinor;
  300. {
  301. std::ostringstream majorStream;
  302. std::ostringstream minorStream;
  303. int major;
  304. int minor;
  305. this->GeneratorTarget->GetTargetVersion(major, minor);
  306. majorStream << major;
  307. minorStream << minor;
  308. targetVersionMajor = majorStream.str();
  309. targetVersionMinor = minorStream.str();
  310. }
  311. vars.TargetVersionMajor = targetVersionMajor.c_str();
  312. vars.TargetVersionMinor = targetVersionMinor.c_str();
  313. vars.LinkLibraries = linkLibs.c_str();
  314. vars.Flags = flags.c_str();
  315. vars.LinkFlags = linkFlags.c_str();
  316. vars.Manifests = manifests.c_str();
  317. if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE")) {
  318. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  319. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  320. cmakeCommand += " -E __run_iwyu --lwyu=";
  321. cmakeCommand += targetOutPathReal;
  322. real_link_commands.push_back(cmakeCommand);
  323. }
  324. // Expand placeholders in the commands.
  325. this->LocalGenerator->TargetImplib = targetOutPathImport;
  326. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  327. i != real_link_commands.end(); ++i) {
  328. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  329. }
  330. this->LocalGenerator->TargetImplib = "";
  331. // Restore path conversion to normal shells.
  332. this->LocalGenerator->SetLinkScriptShell(false);
  333. }
  334. // Optionally convert the build rule to use a script to avoid long
  335. // command lines in the make shell.
  336. if (useLinkScript) {
  337. // Use a link script.
  338. const char* name = (relink ? "relink.txt" : "link.txt");
  339. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  340. } else {
  341. // No link script. Just use the link rule directly.
  342. commands1 = real_link_commands;
  343. }
  344. this->LocalGenerator->CreateCDCommand(
  345. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  346. this->LocalGenerator->GetBinaryDirectory());
  347. commands.insert(commands.end(), commands1.begin(), commands1.end());
  348. commands1.clear();
  349. // Add a rule to create necessary symlinks for the library.
  350. if (targetOutPath != targetOutPathReal) {
  351. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  352. symlink += targetOutPathReal;
  353. symlink += " ";
  354. symlink += targetOutPath;
  355. commands1.push_back(symlink);
  356. this->LocalGenerator->CreateCDCommand(
  357. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  358. this->LocalGenerator->GetBinaryDirectory());
  359. commands.insert(commands.end(), commands1.begin(), commands1.end());
  360. commands1.clear();
  361. }
  362. // Add the post-build rules when building but not when relinking.
  363. if (!relink) {
  364. this->LocalGenerator->AppendCustomCommands(
  365. commands, this->GeneratorTarget->GetPostBuildCommands(),
  366. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  367. }
  368. // Write the build rule.
  369. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  370. targetFullPathReal, depends, commands,
  371. false);
  372. // The symlink name for the target should depend on the real target
  373. // so if the target version changes it rebuilds and recreates the
  374. // symlink.
  375. if (targetFullPath != targetFullPathReal) {
  376. depends.clear();
  377. commands.clear();
  378. depends.push_back(targetFullPathReal);
  379. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  380. targetFullPath, depends, commands,
  381. false);
  382. }
  383. // Write the main driver rule to build everything in this target.
  384. this->WriteTargetDriverRule(targetFullPath, relink);
  385. // Clean all the possible executable names and symlinks.
  386. this->CleanFiles.insert(this->CleanFiles.end(), exeCleanFiles.begin(),
  387. exeCleanFiles.end());
  388. }