cmMakefileExecutableTargetGenerator.cxx 17 KB

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