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