cmMakefileExecutableTargetGenerator.cxx 17 KB

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