cmMakefileExecutableTargetGenerator.cxx 16 KB

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