cmMakefileExecutableTargetGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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->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(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->Target->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->Target->GetExecutableNames
  78. (targetName, targetNameReal, targetNameImport, targetNamePDB,
  79. this->ConfigName);
  80. // Construct the full path version of the names.
  81. std::string outpath = this->Target->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->GetStartOutputDirectory();
  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->Target->GetDirectory(this->ConfigName, true);
  106. cmSystemTools::MakeDirectory(outpathImp.c_str());
  107. outpathImp += "/";
  108. }
  109. }
  110. std::string pdbOutputPath = this->Target->GetPDBDirectory();
  111. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  112. pdbOutputPath += "/";
  113. std::string targetFullPath = outpath + targetName;
  114. std::string targetFullPathReal = outpath + targetNameReal;
  115. std::string targetFullPathPDB = pdbOutputPath + 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. std::string linkLibs;
  281. std::string frameworkPath;
  282. std::string linkPath;
  283. this->LocalGenerator->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  284. *this->GeneratorTarget,
  285. relink);
  286. linkLibs = frameworkPath + linkPath + linkLibs;
  287. // Construct object file lists that may be needed to expand the
  288. // rule.
  289. std::string buildObjs;
  290. this->CreateObjectLists(useLinkScript, false, useResponseFile,
  291. buildObjs, depends);
  292. cmLocalGenerator::RuleVariables vars;
  293. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  294. vars.CMTarget = this->Target;
  295. vars.Language = linkLanguage;
  296. vars.Objects = buildObjs.c_str();
  297. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  298. objdir += this->Target->GetName();
  299. objdir += ".dir";
  300. objdir = this->Convert(objdir.c_str(),
  301. cmLocalGenerator::START_OUTPUT,
  302. cmLocalGenerator::SHELL);
  303. vars.ObjectDir = objdir.c_str();
  304. vars.Target = targetOutPathReal.c_str();
  305. vars.TargetPDB = targetOutPathPDB.c_str();
  306. // Setup the target version.
  307. std::string targetVersionMajor;
  308. std::string targetVersionMinor;
  309. {
  310. cmOStringStream majorStream;
  311. cmOStringStream minorStream;
  312. int major;
  313. int minor;
  314. this->Target->GetTargetVersion(major, minor);
  315. majorStream << major;
  316. minorStream << minor;
  317. targetVersionMajor = majorStream.str();
  318. targetVersionMinor = minorStream.str();
  319. }
  320. vars.TargetVersionMajor = targetVersionMajor.c_str();
  321. vars.TargetVersionMinor = targetVersionMinor.c_str();
  322. vars.LinkLibraries = linkLibs.c_str();
  323. vars.Flags = flags.c_str();
  324. vars.LinkFlags = linkFlags.c_str();
  325. // Expand placeholders in the commands.
  326. this->LocalGenerator->TargetImplib = targetOutPathImport;
  327. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  328. i != real_link_commands.end(); ++i)
  329. {
  330. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  331. }
  332. this->LocalGenerator->TargetImplib = "";
  333. // Restore path conversion to normal shells.
  334. this->LocalGenerator->SetLinkScriptShell(false);
  335. }
  336. // Optionally convert the build rule to use a script to avoid long
  337. // command lines in the make shell.
  338. if(useLinkScript)
  339. {
  340. // Use a link script.
  341. const char* name = (relink? "relink.txt" : "link.txt");
  342. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  343. }
  344. else
  345. {
  346. // No link script. Just use the link rule directly.
  347. commands1 = real_link_commands;
  348. }
  349. this->LocalGenerator->CreateCDCommand
  350. (commands1,
  351. this->Makefile->GetStartOutputDirectory(),
  352. cmLocalGenerator::HOME_OUTPUT);
  353. commands.insert(commands.end(), commands1.begin(), commands1.end());
  354. commands1.clear();
  355. // Add a rule to create necessary symlinks for the library.
  356. if(targetOutPath != targetOutPathReal)
  357. {
  358. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  359. symlink += targetOutPathReal;
  360. symlink += " ";
  361. symlink += targetOutPath;
  362. commands1.push_back(symlink);
  363. this->LocalGenerator->CreateCDCommand(commands1,
  364. this->Makefile->GetStartOutputDirectory(),
  365. cmLocalGenerator::HOME_OUTPUT);
  366. commands.insert(commands.end(), commands1.begin(), commands1.end());
  367. commands1.clear();
  368. }
  369. // Add the post-build rules when building but not when relinking.
  370. if(!relink)
  371. {
  372. this->LocalGenerator->
  373. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  374. this->Target);
  375. }
  376. // Write the build rule.
  377. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  378. 0,
  379. targetFullPathReal.c_str(),
  380. depends, commands, false);
  381. // The symlink name for the target should depend on the real target
  382. // so if the target version changes it rebuilds and recreates the
  383. // symlink.
  384. if(targetFullPath != targetFullPathReal)
  385. {
  386. depends.clear();
  387. commands.clear();
  388. depends.push_back(targetFullPathReal.c_str());
  389. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  390. targetFullPath.c_str(),
  391. depends, commands, false);
  392. }
  393. // Write the main driver rule to build everything in this target.
  394. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  395. // Clean all the possible executable names and symlinks.
  396. this->CleanFiles.insert(this->CleanFiles.end(),
  397. exeCleanFiles.begin(),
  398. exeCleanFiles.end());
  399. }