cmMakefileExecutableTargetGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 pdbOutputPath = this->Target->GetPDBDirectory();
  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.c_str(),
  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.c_str(),
  127. cmLocalGenerator::START_OUTPUT,
  128. cmLocalGenerator::SHELL);
  129. std::string targetOutPathReal =
  130. this->Convert(targetFullPathReal.c_str(),
  131. cmLocalGenerator::START_OUTPUT,
  132. cmLocalGenerator::SHELL);
  133. std::string targetOutPathImport =
  134. this->Convert(targetFullPathImport.c_str(),
  135. cmLocalGenerator::START_OUTPUT,
  136. cmLocalGenerator::SHELL);
  137. // Get the language to use for linking this executable.
  138. const char* linkLanguage =
  139. this->Target->GetLinkerLanguage(this->ConfigName);
  140. // Make sure we have a link language.
  141. if(!linkLanguage)
  142. {
  143. cmSystemTools::Error("Cannot determine link language for target \"",
  144. this->Target->GetName(), "\".");
  145. return;
  146. }
  147. if(!this->NoRuleMessages)
  148. {
  149. // Add the link message.
  150. std::string buildEcho = "Linking ";
  151. buildEcho += linkLanguage;
  152. buildEcho += " executable ";
  153. buildEcho += targetOutPath;
  154. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  155. cmLocalUnixMakefileGenerator3::EchoLink);
  156. }
  157. // Build a list of compiler flags and linker flags.
  158. std::string flags;
  159. std::string linkFlags;
  160. // Add flags to create an executable.
  161. this->LocalGenerator->
  162. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS",
  163. this->ConfigName);
  164. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  165. {
  166. this->LocalGenerator->AppendFlags
  167. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  168. }
  169. else
  170. {
  171. this->LocalGenerator->AppendFlags
  172. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  173. }
  174. // Add symbol export flags if necessary.
  175. if(this->Target->IsExecutableWithExports())
  176. {
  177. std::string export_flag_var = "CMAKE_EXE_EXPORTS_";
  178. export_flag_var += linkLanguage;
  179. export_flag_var += "_FLAG";
  180. this->LocalGenerator->AppendFlags
  181. (linkFlags, this->Makefile->GetDefinition(export_flag_var.c_str()));
  182. }
  183. // Add language feature flags.
  184. this->AddFeatureFlags(flags, linkLanguage);
  185. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  186. linkLanguage, this->ConfigName);
  187. // Add target-specific linker flags.
  188. this->LocalGenerator->AppendFlags
  189. (linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  190. std::string linkFlagsConfig = "LINK_FLAGS_";
  191. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  192. this->LocalGenerator->AppendFlags
  193. (linkFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  194. this->AddModuleDefinitionFlag(linkFlags);
  195. // Construct a list of files associated with this executable that
  196. // may need to be cleaned.
  197. std::vector<std::string> exeCleanFiles;
  198. exeCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  199. cmLocalGenerator::START_OUTPUT,
  200. cmLocalGenerator::UNCHANGED));
  201. #ifdef _WIN32
  202. // There may be a manifest file for this target. Add it to the
  203. // clean set just in case.
  204. exeCleanFiles.push_back(this->Convert((targetFullPath+".manifest").c_str(),
  205. cmLocalGenerator::START_OUTPUT,
  206. cmLocalGenerator::UNCHANGED));
  207. #endif
  208. if(targetNameReal != targetName)
  209. {
  210. exeCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  211. cmLocalGenerator::START_OUTPUT,
  212. cmLocalGenerator::UNCHANGED));
  213. }
  214. if(!targetNameImport.empty())
  215. {
  216. exeCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  217. cmLocalGenerator::START_OUTPUT,
  218. cmLocalGenerator::UNCHANGED));
  219. std::string implib;
  220. if(this->Target->GetImplibGNUtoMS(targetFullPathImport, implib))
  221. {
  222. exeCleanFiles.push_back(this->Convert(implib.c_str(),
  223. cmLocalGenerator::START_OUTPUT,
  224. cmLocalGenerator::UNCHANGED));
  225. }
  226. }
  227. // List the PDB for cleaning only when the whole target is
  228. // cleaned. We do not want to delete the .pdb file just before
  229. // linking the target.
  230. this->CleanFiles.push_back
  231. (this->Convert(targetFullPathPDB.c_str(),
  232. cmLocalGenerator::START_OUTPUT,
  233. cmLocalGenerator::UNCHANGED));
  234. // Add the pre-build and pre-link rules building but not when relinking.
  235. if(!relink)
  236. {
  237. this->LocalGenerator
  238. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  239. this->Target);
  240. this->LocalGenerator
  241. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  242. this->Target);
  243. }
  244. // Determine whether a link script will be used.
  245. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  246. // Construct the main link rule.
  247. std::vector<std::string> real_link_commands;
  248. std::string linkRuleVar = "CMAKE_";
  249. linkRuleVar += linkLanguage;
  250. linkRuleVar += "_LINK_EXECUTABLE";
  251. std::string linkRule = this->GetLinkRule(linkRuleVar.c_str());
  252. std::vector<std::string> commands1;
  253. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  254. if(this->Target->IsExecutableWithExports())
  255. {
  256. // If a separate rule for creating an import library is specified
  257. // add it now.
  258. std::string implibRuleVar = "CMAKE_";
  259. implibRuleVar += linkLanguage;
  260. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  261. if(const char* rule =
  262. this->Makefile->GetDefinition(implibRuleVar.c_str()))
  263. {
  264. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  265. }
  266. }
  267. // Select whether to use a response file for objects.
  268. bool useResponseFile = false;
  269. {
  270. std::string responseVar = "CMAKE_";
  271. responseVar += linkLanguage;
  272. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  273. if(this->Makefile->IsOn(responseVar.c_str()))
  274. {
  275. useResponseFile = true;
  276. }
  277. }
  278. // Expand the rule variables.
  279. {
  280. // Set path conversion for link script shells.
  281. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  282. // Collect up flags to link in needed libraries.
  283. std::string linkLibs;
  284. std::string frameworkPath;
  285. std::string linkPath;
  286. this->LocalGenerator->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  287. *this->GeneratorTarget,
  288. relink);
  289. linkLibs = frameworkPath + linkPath + linkLibs;
  290. // Construct object file lists that may be needed to expand the
  291. // rule.
  292. std::string buildObjs;
  293. this->CreateObjectLists(useLinkScript, false, useResponseFile,
  294. buildObjs, depends);
  295. cmLocalGenerator::RuleVariables vars;
  296. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  297. vars.CMTarget = this->Target;
  298. vars.Language = linkLanguage;
  299. vars.Objects = buildObjs.c_str();
  300. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  301. objdir += this->Target->GetName();
  302. objdir += ".dir";
  303. objdir = this->Convert(objdir.c_str(),
  304. cmLocalGenerator::START_OUTPUT,
  305. cmLocalGenerator::SHELL);
  306. vars.ObjectDir = objdir.c_str();
  307. vars.Target = targetOutPathReal.c_str();
  308. vars.TargetPDB = targetOutPathPDB.c_str();
  309. // Setup the target version.
  310. std::string targetVersionMajor;
  311. std::string targetVersionMinor;
  312. {
  313. cmOStringStream majorStream;
  314. cmOStringStream minorStream;
  315. int major;
  316. int minor;
  317. this->Target->GetTargetVersion(major, minor);
  318. majorStream << major;
  319. minorStream << minor;
  320. targetVersionMajor = majorStream.str();
  321. targetVersionMinor = minorStream.str();
  322. }
  323. vars.TargetVersionMajor = targetVersionMajor.c_str();
  324. vars.TargetVersionMinor = targetVersionMinor.c_str();
  325. vars.LinkLibraries = linkLibs.c_str();
  326. vars.Flags = flags.c_str();
  327. vars.LinkFlags = linkFlags.c_str();
  328. // Expand placeholders in the commands.
  329. this->LocalGenerator->TargetImplib = targetOutPathImport;
  330. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  331. i != real_link_commands.end(); ++i)
  332. {
  333. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  334. }
  335. this->LocalGenerator->TargetImplib = "";
  336. // Restore path conversion to normal shells.
  337. this->LocalGenerator->SetLinkScriptShell(false);
  338. }
  339. // Optionally convert the build rule to use a script to avoid long
  340. // command lines in the make shell.
  341. if(useLinkScript)
  342. {
  343. // Use a link script.
  344. const char* name = (relink? "relink.txt" : "link.txt");
  345. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  346. }
  347. else
  348. {
  349. // No link script. Just use the link rule directly.
  350. commands1 = real_link_commands;
  351. }
  352. this->LocalGenerator->CreateCDCommand
  353. (commands1,
  354. this->Makefile->GetStartOutputDirectory(),
  355. cmLocalGenerator::HOME_OUTPUT);
  356. commands.insert(commands.end(), commands1.begin(), commands1.end());
  357. commands1.clear();
  358. // Add a rule to create necessary symlinks for the library.
  359. if(targetOutPath != targetOutPathReal)
  360. {
  361. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  362. symlink += targetOutPathReal;
  363. symlink += " ";
  364. symlink += targetOutPath;
  365. commands1.push_back(symlink);
  366. this->LocalGenerator->CreateCDCommand(commands1,
  367. this->Makefile->GetStartOutputDirectory(),
  368. cmLocalGenerator::HOME_OUTPUT);
  369. commands.insert(commands.end(), commands1.begin(), commands1.end());
  370. commands1.clear();
  371. }
  372. // Add the post-build rules when building but not when relinking.
  373. if(!relink)
  374. {
  375. this->LocalGenerator->
  376. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  377. this->Target);
  378. }
  379. // Write the build rule.
  380. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  381. 0,
  382. targetFullPathReal.c_str(),
  383. depends, commands, false);
  384. // The symlink name for the target should depend on the real target
  385. // so if the target version changes it rebuilds and recreates the
  386. // symlink.
  387. if(targetFullPath != targetFullPathReal)
  388. {
  389. depends.clear();
  390. commands.clear();
  391. depends.push_back(targetFullPathReal.c_str());
  392. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  393. targetFullPath.c_str(),
  394. depends, commands, false);
  395. }
  396. // Write the main driver rule to build everything in this target.
  397. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  398. // Clean all the possible executable names and symlinks.
  399. this->CleanFiles.insert(this->CleanFiles.end(),
  400. exeCleanFiles.begin(),
  401. exeCleanFiles.end());
  402. }