cmMakefileExecutableTargetGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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->LocalGenerator->ConvertToOutputFormat(
  116. targetFullPathPDB, cmOutputConverter::SHELL);
  117. // Convert to the output path to use in constructing commands.
  118. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  119. this->LocalGenerator->ConvertToRelativePath(
  120. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  121. cmOutputConverter::SHELL);
  122. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  123. this->LocalGenerator->ConvertToRelativePath(
  124. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  125. cmOutputConverter::SHELL);
  126. std::string targetOutPathImport =
  127. this->LocalGenerator->ConvertToOutputFormat(
  128. this->LocalGenerator->ConvertToRelativePath(
  129. this->LocalGenerator->GetCurrentBinaryDirectory(),
  130. targetFullPathImport),
  131. cmOutputConverter::SHELL);
  132. // Get the language to use for linking this executable.
  133. std::string linkLanguage =
  134. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  135. // Make sure we have a link language.
  136. if (linkLanguage.empty()) {
  137. cmSystemTools::Error("Cannot determine link language for target \"",
  138. this->GeneratorTarget->GetName().c_str(), "\".");
  139. return;
  140. }
  141. this->NumberOfProgressActions++;
  142. if (!this->NoRuleMessages) {
  143. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  144. this->MakeEchoProgress(progress);
  145. // Add the link message.
  146. std::string buildEcho = "Linking ";
  147. buildEcho += linkLanguage;
  148. buildEcho += " executable ";
  149. buildEcho += targetOutPath;
  150. this->LocalGenerator->AppendEcho(
  151. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  152. }
  153. // Build a list of compiler flags and linker flags.
  154. std::string flags;
  155. std::string linkFlags;
  156. // Add flags to create an executable.
  157. this->LocalGenerator->AddConfigVariableFlags(
  158. linkFlags, "CMAKE_EXE_LINKER_FLAGS", this->ConfigName);
  159. if (this->GeneratorTarget->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  160. this->LocalGenerator->AppendFlags(
  161. linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  162. } else {
  163. this->LocalGenerator->AppendFlags(
  164. linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  165. }
  166. // Add symbol export flags if necessary.
  167. if (this->GeneratorTarget->IsExecutableWithExports()) {
  168. std::string export_flag_var = "CMAKE_EXE_EXPORTS_";
  169. export_flag_var += linkLanguage;
  170. export_flag_var += "_FLAG";
  171. this->LocalGenerator->AppendFlags(
  172. linkFlags, this->Makefile->GetDefinition(export_flag_var));
  173. }
  174. if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE")) {
  175. this->LocalGenerator->AppendFlags(linkFlags, " -Wl,--no-as-needed");
  176. }
  177. // Add language feature flags.
  178. this->AddFeatureFlags(flags, linkLanguage);
  179. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  180. linkLanguage, this->ConfigName);
  181. // Add target-specific linker flags.
  182. this->LocalGenerator->AppendFlags(
  183. linkFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  184. std::string linkFlagsConfig = "LINK_FLAGS_";
  185. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  186. this->LocalGenerator->AppendFlags(
  187. linkFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  188. this->AddModuleDefinitionFlag(linkFlags);
  189. // Construct a list of files associated with this executable that
  190. // may need to be cleaned.
  191. std::vector<std::string> exeCleanFiles;
  192. exeCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  193. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  194. #ifdef _WIN32
  195. // There may be a manifest file for this target. Add it to the
  196. // clean set just in case.
  197. exeCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  198. this->LocalGenerator->GetCurrentBinaryDirectory(),
  199. (targetFullPath + ".manifest").c_str()));
  200. #endif
  201. if (targetNameReal != targetName) {
  202. exeCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  203. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  204. }
  205. if (!targetNameImport.empty()) {
  206. exeCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  207. this->LocalGenerator->GetCurrentBinaryDirectory(),
  208. targetFullPathImport));
  209. std::string implib;
  210. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  211. implib)) {
  212. exeCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  213. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  214. }
  215. }
  216. // List the PDB for cleaning only when the whole target is
  217. // cleaned. We do not want to delete the .pdb file just before
  218. // linking the target.
  219. this->CleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
  220. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  221. // Add the pre-build and pre-link rules building but not when relinking.
  222. if (!relink) {
  223. this->LocalGenerator->AppendCustomCommands(
  224. commands, this->GeneratorTarget->GetPreBuildCommands(),
  225. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  226. this->LocalGenerator->AppendCustomCommands(
  227. commands, this->GeneratorTarget->GetPreLinkCommands(),
  228. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  229. }
  230. // Determine whether a link script will be used.
  231. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  232. // Construct the main link rule.
  233. std::vector<std::string> real_link_commands;
  234. std::string linkRuleVar = "CMAKE_";
  235. linkRuleVar += linkLanguage;
  236. linkRuleVar += "_LINK_EXECUTABLE";
  237. std::string linkRule = this->GetLinkRule(linkRuleVar);
  238. std::vector<std::string> commands1;
  239. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  240. if (this->GeneratorTarget->IsExecutableWithExports()) {
  241. // If a separate rule for creating an import library is specified
  242. // add it now.
  243. std::string implibRuleVar = "CMAKE_";
  244. implibRuleVar += linkLanguage;
  245. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  246. if (const char* rule = this->Makefile->GetDefinition(implibRuleVar)) {
  247. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  248. }
  249. }
  250. bool useResponseFileForObjects =
  251. this->CheckUseResponseFileForObjects(linkLanguage);
  252. bool const useResponseFileForLibs =
  253. this->CheckUseResponseFileForLibraries(linkLanguage);
  254. // Expand the rule variables.
  255. {
  256. bool useWatcomQuote =
  257. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  258. // Set path conversion for link script shells.
  259. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  260. // Collect up flags to link in needed libraries.
  261. std::string linkLibs;
  262. this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
  263. useWatcomQuote);
  264. // Construct object file lists that may be needed to expand the
  265. // rule.
  266. std::string buildObjs;
  267. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  268. buildObjs, depends, useWatcomQuote);
  269. // maybe create .def file from list of objects
  270. if (this->GeneratorTarget->IsExecutableWithExports() &&
  271. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  272. this->GenDefFile(real_link_commands, linkFlags);
  273. }
  274. std::string manifests = this->GetManifests();
  275. cmLocalGenerator::RuleVariables vars;
  276. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  277. vars.CMTarget = this->GeneratorTarget;
  278. vars.Language = linkLanguage.c_str();
  279. vars.Objects = buildObjs.c_str();
  280. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  281. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  282. this->LocalGenerator->ConvertToRelativePath(
  283. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  284. cmOutputConverter::SHELL);
  285. vars.ObjectDir = objectDir.c_str();
  286. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  287. ? cmOutputConverter::WATCOMQUOTE
  288. : cmOutputConverter::SHELL;
  289. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  290. this->LocalGenerator->ConvertToRelativePath(
  291. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  292. output);
  293. vars.Target = target.c_str();
  294. vars.TargetPDB = targetOutPathPDB.c_str();
  295. // Setup the target version.
  296. std::string targetVersionMajor;
  297. std::string targetVersionMinor;
  298. {
  299. std::ostringstream majorStream;
  300. std::ostringstream minorStream;
  301. int major;
  302. int minor;
  303. this->GeneratorTarget->GetTargetVersion(major, minor);
  304. majorStream << major;
  305. minorStream << minor;
  306. targetVersionMajor = majorStream.str();
  307. targetVersionMinor = minorStream.str();
  308. }
  309. vars.TargetVersionMajor = targetVersionMajor.c_str();
  310. vars.TargetVersionMinor = targetVersionMinor.c_str();
  311. vars.LinkLibraries = linkLibs.c_str();
  312. vars.Flags = flags.c_str();
  313. vars.LinkFlags = linkFlags.c_str();
  314. vars.Manifests = manifests.c_str();
  315. if (this->GeneratorTarget->GetProperty("LINK_WHAT_YOU_USE")) {
  316. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  317. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  318. cmakeCommand += " -E __run_iwyu --lwyu=";
  319. cmakeCommand += targetOutPathReal;
  320. real_link_commands.push_back(cmakeCommand);
  321. }
  322. // Expand placeholders in the commands.
  323. this->LocalGenerator->TargetImplib = targetOutPathImport;
  324. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  325. i != real_link_commands.end(); ++i) {
  326. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  327. }
  328. this->LocalGenerator->TargetImplib = "";
  329. // Restore path conversion to normal shells.
  330. this->LocalGenerator->SetLinkScriptShell(false);
  331. }
  332. // Optionally convert the build rule to use a script to avoid long
  333. // command lines in the make shell.
  334. if (useLinkScript) {
  335. // Use a link script.
  336. const char* name = (relink ? "relink.txt" : "link.txt");
  337. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  338. } else {
  339. // No link script. Just use the link rule directly.
  340. commands1 = real_link_commands;
  341. }
  342. this->LocalGenerator->CreateCDCommand(
  343. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  344. this->LocalGenerator->GetBinaryDirectory());
  345. commands.insert(commands.end(), commands1.begin(), commands1.end());
  346. commands1.clear();
  347. // Add a rule to create necessary symlinks for the library.
  348. if (targetOutPath != targetOutPathReal) {
  349. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  350. symlink += targetOutPathReal;
  351. symlink += " ";
  352. symlink += targetOutPath;
  353. commands1.push_back(symlink);
  354. this->LocalGenerator->CreateCDCommand(
  355. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  356. this->LocalGenerator->GetBinaryDirectory());
  357. commands.insert(commands.end(), commands1.begin(), commands1.end());
  358. commands1.clear();
  359. }
  360. // Add the post-build rules when building but not when relinking.
  361. if (!relink) {
  362. this->LocalGenerator->AppendCustomCommands(
  363. commands, this->GeneratorTarget->GetPostBuildCommands(),
  364. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  365. }
  366. // Write the build rule.
  367. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  368. targetFullPathReal, depends, commands,
  369. false);
  370. // The symlink name for the target should depend on the real target
  371. // so if the target version changes it rebuilds and recreates the
  372. // symlink.
  373. if (targetFullPath != targetFullPathReal) {
  374. depends.clear();
  375. commands.clear();
  376. depends.push_back(targetFullPathReal);
  377. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  378. targetFullPath, depends, commands,
  379. false);
  380. }
  381. // Write the main driver rule to build everything in this target.
  382. this->WriteTargetDriverRule(targetFullPath, relink);
  383. // Clean all the possible executable names and symlinks.
  384. this->CleanFiles.insert(this->CleanFiles.end(), exeCleanFiles.begin(),
  385. exeCleanFiles.end());
  386. }