cmMakefileExecutableTargetGenerator.cxx 16 KB

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