cmMakefileExecutableTargetGenerator.cxx 16 KB

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