cmMakefileExecutableTargetGenerator.cxx 15 KB

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