cmMakefileExecutableTargetGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 feature flags.
  200. this->AddFeatureFlags(flags, linkLanguage);
  201. // Add target-specific linker flags.
  202. this->LocalGenerator->AppendFlags
  203. (linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  204. std::string linkFlagsConfig = "LINK_FLAGS_";
  205. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  206. this->LocalGenerator->AppendFlags
  207. (linkFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  208. this->AddModuleDefinitionFlag(linkFlags);
  209. // Construct a list of files associated with this executable that
  210. // may need to be cleaned.
  211. std::vector<std::string> exeCleanFiles;
  212. exeCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  213. cmLocalGenerator::START_OUTPUT,
  214. cmLocalGenerator::UNCHANGED));
  215. #ifdef _WIN32
  216. // There may be a manifest file for this target. Add it to the
  217. // clean set just in case.
  218. exeCleanFiles.push_back(this->Convert((targetFullPath+".manifest").c_str(),
  219. cmLocalGenerator::START_OUTPUT,
  220. cmLocalGenerator::UNCHANGED));
  221. #endif
  222. if(targetNameReal != targetName)
  223. {
  224. exeCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  225. cmLocalGenerator::START_OUTPUT,
  226. cmLocalGenerator::UNCHANGED));
  227. }
  228. if(!targetNameImport.empty())
  229. {
  230. exeCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  231. cmLocalGenerator::START_OUTPUT,
  232. cmLocalGenerator::UNCHANGED));
  233. }
  234. // List the PDB for cleaning only when the whole target is
  235. // cleaned. We do not want to delete the .pdb file just before
  236. // linking the target.
  237. this->CleanFiles.push_back
  238. (this->Convert(targetFullPathPDB.c_str(),
  239. cmLocalGenerator::START_OUTPUT,
  240. cmLocalGenerator::UNCHANGED));
  241. // Add the pre-build and pre-link rules building but not when relinking.
  242. if(!relink)
  243. {
  244. this->LocalGenerator
  245. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  246. this->Target);
  247. this->LocalGenerator
  248. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  249. this->Target);
  250. }
  251. // Determine whether a link script will be used.
  252. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  253. // Construct the main link rule.
  254. std::vector<std::string> real_link_commands;
  255. std::string linkRuleVar = "CMAKE_";
  256. linkRuleVar += linkLanguage;
  257. linkRuleVar += "_LINK_EXECUTABLE";
  258. std::string linkRule =
  259. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  260. std::vector<std::string> commands1;
  261. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  262. if(this->Target->IsExecutableWithExports())
  263. {
  264. // If a separate rule for creating an import library is specified
  265. // add it now.
  266. std::string implibRuleVar = "CMAKE_";
  267. implibRuleVar += linkLanguage;
  268. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  269. if(const char* rule =
  270. this->Makefile->GetDefinition(implibRuleVar.c_str()))
  271. {
  272. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  273. }
  274. }
  275. // Select whether to use a response file for objects.
  276. bool useResponseFile = false;
  277. {
  278. std::string responseVar = "CMAKE_";
  279. responseVar += linkLanguage;
  280. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  281. if(this->Makefile->IsOn(responseVar.c_str()))
  282. {
  283. useResponseFile = true;
  284. }
  285. }
  286. // Expand the rule variables.
  287. {
  288. // Set path conversion for link script shells.
  289. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  290. // Collect up flags to link in needed libraries.
  291. cmOStringStream linklibs;
  292. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  293. // Construct object file lists that may be needed to expand the
  294. // rule.
  295. std::string buildObjs;
  296. this->CreateObjectLists(useLinkScript, false, useResponseFile,
  297. buildObjs, depends);
  298. cmLocalGenerator::RuleVariables vars;
  299. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  300. vars.CMTarget = this->Target;
  301. vars.Language = linkLanguage;
  302. vars.Objects = buildObjs.c_str();
  303. vars.Target = targetOutPathReal.c_str();
  304. vars.TargetPDB = targetOutPathPDB.c_str();
  305. // Setup the target version.
  306. std::string targetVersionMajor;
  307. std::string targetVersionMinor;
  308. {
  309. cmOStringStream majorStream;
  310. cmOStringStream minorStream;
  311. int major;
  312. int minor;
  313. this->Target->GetTargetVersion(major, minor);
  314. majorStream << major;
  315. minorStream << minor;
  316. targetVersionMajor = majorStream.str();
  317. targetVersionMinor = minorStream.str();
  318. }
  319. vars.TargetVersionMajor = targetVersionMajor.c_str();
  320. vars.TargetVersionMinor = targetVersionMinor.c_str();
  321. std::string linkString = linklibs.str();
  322. vars.LinkLibraries = linkString.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. }
  400. //----------------------------------------------------------------------------
  401. void
  402. cmMakefileExecutableTargetGenerator::CreateAppBundle(std::string& targetName,
  403. std::string& outpath)
  404. {
  405. // Compute bundle directory names.
  406. outpath = this->MacContentDirectory;
  407. outpath += "MacOS";
  408. cmSystemTools::MakeDirectory(outpath.c_str());
  409. this->Makefile->AddCMakeOutputFile(outpath.c_str());
  410. outpath += "/";
  411. // Configure the Info.plist file. Note that it needs the executable name
  412. // to be set.
  413. std::string plist = this->MacContentDirectory + "Info.plist";
  414. this->LocalGenerator->GenerateAppleInfoPList(this->Target,
  415. targetName.c_str(),
  416. plist.c_str());
  417. this->Makefile->AddCMakeOutputFile(plist.c_str());
  418. }