cmMakefileExecutableTargetGenerator.cxx 16 KB

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