cmMakefileExecutableTargetGenerator.cxx 16 KB

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