cmMakefileExecutableTargetGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "cmTarget.h"
  17. #include "cmake.h"
  18. //----------------------------------------------------------------------------
  19. cmMakefileExecutableTargetGenerator
  20. ::cmMakefileExecutableTargetGenerator(cmGeneratorTarget* target):
  21. cmMakefileTargetGenerator(target)
  22. {
  23. this->CustomCommandDriver = OnDepends;
  24. this->GeneratorTarget->GetExecutableNames(
  25. this->TargetNameOut, this->TargetNameReal, this->TargetNameImport,
  26. this->TargetNamePDB, this->ConfigName);
  27. this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
  28. this->ConfigName);
  29. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  30. }
  31. //----------------------------------------------------------------------------
  32. cmMakefileExecutableTargetGenerator
  33. ::~cmMakefileExecutableTargetGenerator()
  34. {
  35. delete this->OSXBundleGenerator;
  36. }
  37. //----------------------------------------------------------------------------
  38. void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
  39. {
  40. // create the build.make file and directory, put in the common blocks
  41. this->CreateRuleFile();
  42. // write rules used to help build object files
  43. this->WriteCommonCodeRules();
  44. // write the per-target per-language flags
  45. this->WriteTargetLanguageFlags();
  46. // write in rules for object files and custom commands
  47. this->WriteTargetBuildRules();
  48. // write the link rules
  49. this->WriteExecutableRule(false);
  50. if(this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName))
  51. {
  52. // Write rules to link an installable version of the target.
  53. this->WriteExecutableRule(true);
  54. }
  55. // Write the requires target.
  56. this->WriteTargetRequiresRules();
  57. // Write clean target
  58. this->WriteTargetCleanRules();
  59. // Write the dependency generation rule. This must be done last so
  60. // that multiple output pair information is available.
  61. this->WriteTargetDependRules();
  62. // close the streams
  63. this->CloseFileStreams();
  64. }
  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
  78. (targetName, targetNameReal, 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->Target->IsAppBundleOnApple())
  83. {
  84. this->OSXBundleGenerator->CreateAppBundle(targetName, outpath);
  85. }
  86. outpath += "/";
  87. std::string outpathImp;
  88. if(relink)
  89. {
  90. outpath = this->Makefile->GetCurrentBinaryDirectory();
  91. outpath += cmake::GetCMakeFilesDirectory();
  92. outpath += "/CMakeRelink.dir";
  93. cmSystemTools::MakeDirectory(outpath.c_str());
  94. outpath += "/";
  95. if(!targetNameImport.empty())
  96. {
  97. outpathImp = outpath;
  98. }
  99. }
  100. else
  101. {
  102. cmSystemTools::MakeDirectory(outpath.c_str());
  103. if(!targetNameImport.empty())
  104. {
  105. outpathImp = this->GeneratorTarget->GetDirectory(this->ConfigName, true);
  106. cmSystemTools::MakeDirectory(outpathImp.c_str());
  107. outpathImp += "/";
  108. }
  109. }
  110. std::string compilePdbOutputPath =
  111. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  112. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  113. std::string pdbOutputPath =
  114. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  115. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  116. pdbOutputPath += "/";
  117. std::string targetFullPath = outpath + targetName;
  118. std::string targetFullPathReal = outpath + targetNameReal;
  119. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  120. std::string targetFullPathImport = outpathImp + targetNameImport;
  121. std::string targetOutPathPDB =
  122. this->Convert(targetFullPathPDB,
  123. cmLocalGenerator::NONE,
  124. cmLocalGenerator::SHELL);
  125. // Convert to the output path to use in constructing commands.
  126. std::string targetOutPath =
  127. this->Convert(targetFullPath,
  128. cmLocalGenerator::START_OUTPUT,
  129. cmLocalGenerator::SHELL);
  130. std::string targetOutPathReal =
  131. this->Convert(targetFullPathReal,
  132. cmLocalGenerator::START_OUTPUT,
  133. cmLocalGenerator::SHELL);
  134. std::string targetOutPathImport =
  135. this->Convert(targetFullPathImport,
  136. cmLocalGenerator::START_OUTPUT,
  137. cmLocalGenerator::SHELL);
  138. // Get the language to use for linking this executable.
  139. std::string linkLanguage =
  140. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  141. // Make sure we have a link language.
  142. if(linkLanguage.empty())
  143. {
  144. cmSystemTools::Error("Cannot determine link language for target \"",
  145. this->Target->GetName().c_str(), "\".");
  146. return;
  147. }
  148. this->NumberOfProgressActions++;
  149. if(!this->NoRuleMessages)
  150. {
  151. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  152. this->MakeEchoProgress(progress);
  153. // Add the link message.
  154. std::string buildEcho = "Linking ";
  155. buildEcho += linkLanguage;
  156. buildEcho += " executable ";
  157. buildEcho += targetOutPath;
  158. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  159. cmLocalUnixMakefileGenerator3::EchoLink,
  160. &progress);
  161. }
  162. // Build a list of compiler flags and linker flags.
  163. std::string flags;
  164. std::string linkFlags;
  165. // Add flags to create an executable.
  166. this->LocalGenerator->
  167. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS",
  168. this->ConfigName);
  169. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  170. {
  171. this->LocalGenerator->AppendFlags
  172. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  173. }
  174. else
  175. {
  176. this->LocalGenerator->AppendFlags
  177. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  178. }
  179. // Add symbol export flags if necessary.
  180. if(this->Target->IsExecutableWithExports())
  181. {
  182. std::string export_flag_var = "CMAKE_EXE_EXPORTS_";
  183. export_flag_var += linkLanguage;
  184. export_flag_var += "_FLAG";
  185. this->LocalGenerator->AppendFlags
  186. (linkFlags, this->Makefile->GetDefinition(export_flag_var));
  187. }
  188. // Add language feature flags.
  189. this->AddFeatureFlags(flags, linkLanguage);
  190. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  191. linkLanguage, this->ConfigName);
  192. // Add target-specific linker flags.
  193. this->LocalGenerator->AppendFlags
  194. (linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  195. std::string linkFlagsConfig = "LINK_FLAGS_";
  196. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  197. this->LocalGenerator->AppendFlags
  198. (linkFlags, this->Target->GetProperty(linkFlagsConfig));
  199. this->AddModuleDefinitionFlag(linkFlags);
  200. // Construct a list of files associated with this executable that
  201. // may need to be cleaned.
  202. std::vector<std::string> exeCleanFiles;
  203. exeCleanFiles.push_back(this->Convert(targetFullPath,
  204. cmLocalGenerator::START_OUTPUT,
  205. cmLocalGenerator::UNCHANGED));
  206. #ifdef _WIN32
  207. // There may be a manifest file for this target. Add it to the
  208. // clean set just in case.
  209. exeCleanFiles.push_back(this->Convert((targetFullPath+".manifest").c_str(),
  210. cmLocalGenerator::START_OUTPUT,
  211. cmLocalGenerator::UNCHANGED));
  212. #endif
  213. if(targetNameReal != targetName)
  214. {
  215. exeCleanFiles.push_back(this->Convert(targetFullPathReal,
  216. cmLocalGenerator::START_OUTPUT,
  217. cmLocalGenerator::UNCHANGED));
  218. }
  219. if(!targetNameImport.empty())
  220. {
  221. exeCleanFiles.push_back(this->Convert(targetFullPathImport,
  222. cmLocalGenerator::START_OUTPUT,
  223. cmLocalGenerator::UNCHANGED));
  224. std::string implib;
  225. if(this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport, implib))
  226. {
  227. exeCleanFiles.push_back(this->Convert(implib,
  228. cmLocalGenerator::START_OUTPUT,
  229. cmLocalGenerator::UNCHANGED));
  230. }
  231. }
  232. // List the PDB for cleaning only when the whole target is
  233. // cleaned. We do not want to delete the .pdb file just before
  234. // linking the target.
  235. this->CleanFiles.push_back
  236. (this->Convert(targetFullPathPDB,
  237. cmLocalGenerator::START_OUTPUT,
  238. cmLocalGenerator::UNCHANGED));
  239. // Add the pre-build and pre-link rules building but not when relinking.
  240. if(!relink)
  241. {
  242. this->LocalGenerator
  243. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  244. this->Target);
  245. this->LocalGenerator
  246. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  247. this->Target);
  248. }
  249. // Determine whether a link script will be used.
  250. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  251. // Construct the main link rule.
  252. std::vector<std::string> real_link_commands;
  253. std::string linkRuleVar = "CMAKE_";
  254. linkRuleVar += linkLanguage;
  255. linkRuleVar += "_LINK_EXECUTABLE";
  256. std::string linkRule = this->GetLinkRule(linkRuleVar);
  257. std::vector<std::string> commands1;
  258. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  259. if(this->Target->IsExecutableWithExports())
  260. {
  261. // If a separate rule for creating an import library is specified
  262. // add it now.
  263. std::string implibRuleVar = "CMAKE_";
  264. implibRuleVar += linkLanguage;
  265. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  266. if(const char* rule =
  267. this->Makefile->GetDefinition(implibRuleVar))
  268. {
  269. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  270. }
  271. }
  272. // Select whether to use a response file for objects.
  273. bool useResponseFileForObjects = false;
  274. {
  275. std::string responseVar = "CMAKE_";
  276. responseVar += linkLanguage;
  277. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  278. if(this->Makefile->IsOn(responseVar))
  279. {
  280. useResponseFileForObjects = true;
  281. }
  282. }
  283. // Select whether to use a response file for libraries.
  284. bool useResponseFileForLibs = false;
  285. {
  286. std::string responseVar = "CMAKE_";
  287. responseVar += linkLanguage;
  288. responseVar += "_USE_RESPONSE_FILE_FOR_LIBRARIES";
  289. if(this->Makefile->IsOn(responseVar))
  290. {
  291. useResponseFileForLibs = true;
  292. }
  293. }
  294. // Expand the rule variables.
  295. {
  296. bool useWatcomQuote = this->Makefile->IsOn(linkRuleVar+"_USE_WATCOM_QUOTE");
  297. // Set path conversion for link script shells.
  298. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  299. // Collect up flags to link in needed libraries.
  300. std::string linkLibs;
  301. this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
  302. useWatcomQuote);
  303. // Construct object file lists that may be needed to expand the
  304. // rule.
  305. std::string buildObjs;
  306. this->CreateObjectLists(useLinkScript, false,
  307. useResponseFileForObjects, buildObjs, depends,
  308. useWatcomQuote);
  309. std::string manifests = this->GetManifests();
  310. cmLocalGenerator::RuleVariables vars;
  311. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  312. vars.CMTarget = this->Target;
  313. vars.Language = linkLanguage.c_str();
  314. vars.Objects = buildObjs.c_str();
  315. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  316. objectDir = this->Convert(objectDir,
  317. cmLocalGenerator::START_OUTPUT,
  318. cmLocalGenerator::SHELL);
  319. vars.ObjectDir = objectDir.c_str();
  320. cmLocalGenerator::OutputFormat output = (useWatcomQuote) ?
  321. cmLocalGenerator::WATCOMQUOTE : cmLocalGenerator::SHELL;
  322. std::string target = this->Convert(targetFullPathReal,
  323. cmLocalGenerator::START_OUTPUT,
  324. output);
  325. vars.Target = target.c_str();
  326. vars.TargetPDB = targetOutPathPDB.c_str();
  327. // Setup the target version.
  328. std::string targetVersionMajor;
  329. std::string targetVersionMinor;
  330. {
  331. std::ostringstream majorStream;
  332. std::ostringstream minorStream;
  333. int major;
  334. int minor;
  335. this->GeneratorTarget->GetTargetVersion(major, minor);
  336. majorStream << major;
  337. minorStream << minor;
  338. targetVersionMajor = majorStream.str();
  339. targetVersionMinor = minorStream.str();
  340. }
  341. vars.TargetVersionMajor = targetVersionMajor.c_str();
  342. vars.TargetVersionMinor = targetVersionMinor.c_str();
  343. vars.LinkLibraries = linkLibs.c_str();
  344. vars.Flags = flags.c_str();
  345. vars.LinkFlags = linkFlags.c_str();
  346. vars.Manifests = manifests.c_str();
  347. // Expand placeholders in the commands.
  348. this->LocalGenerator->TargetImplib = targetOutPathImport;
  349. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  350. i != real_link_commands.end(); ++i)
  351. {
  352. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  353. }
  354. this->LocalGenerator->TargetImplib = "";
  355. // Restore path conversion to normal shells.
  356. this->LocalGenerator->SetLinkScriptShell(false);
  357. }
  358. // Optionally convert the build rule to use a script to avoid long
  359. // command lines in the make shell.
  360. if(useLinkScript)
  361. {
  362. // Use a link script.
  363. const char* name = (relink? "relink.txt" : "link.txt");
  364. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  365. }
  366. else
  367. {
  368. // No link script. Just use the link rule directly.
  369. commands1 = real_link_commands;
  370. }
  371. this->LocalGenerator->CreateCDCommand
  372. (commands1,
  373. this->Makefile->GetCurrentBinaryDirectory(),
  374. cmLocalGenerator::HOME_OUTPUT);
  375. commands.insert(commands.end(), commands1.begin(), commands1.end());
  376. commands1.clear();
  377. // Add a rule to create necessary symlinks for the library.
  378. if(targetOutPath != targetOutPathReal)
  379. {
  380. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  381. symlink += targetOutPathReal;
  382. symlink += " ";
  383. symlink += targetOutPath;
  384. commands1.push_back(symlink);
  385. this->LocalGenerator->CreateCDCommand(commands1,
  386. this->Makefile->GetCurrentBinaryDirectory(),
  387. cmLocalGenerator::HOME_OUTPUT);
  388. commands.insert(commands.end(), commands1.begin(), commands1.end());
  389. commands1.clear();
  390. }
  391. // Add the post-build rules when building but not when relinking.
  392. if(!relink)
  393. {
  394. this->LocalGenerator->
  395. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  396. this->Target);
  397. }
  398. // Write the build rule.
  399. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  400. 0,
  401. targetFullPathReal,
  402. depends, commands, false);
  403. // The symlink name for the target should depend on the real target
  404. // so if the target version changes it rebuilds and recreates the
  405. // symlink.
  406. if(targetFullPath != targetFullPathReal)
  407. {
  408. depends.clear();
  409. commands.clear();
  410. depends.push_back(targetFullPathReal);
  411. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  412. targetFullPath,
  413. depends, commands, false);
  414. }
  415. // Write the main driver rule to build everything in this target.
  416. this->WriteTargetDriverRule(targetFullPath, relink);
  417. // Clean all the possible executable names and symlinks.
  418. this->CleanFiles.insert(this->CleanFiles.end(),
  419. exeCleanFiles.begin(),
  420. exeCleanFiles.end());
  421. }