cmMakefileExecutableTargetGenerator.cxx 16 KB

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