cmMakefileExecutableTargetGenerator.cxx 17 KB

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