cmMakefileExecutableTargetGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMakefileExecutableTargetGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmTarget.h"
  20. //----------------------------------------------------------------------------
  21. void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
  22. {
  23. // create the build.make file and directory, put in the common blocks
  24. this->CreateRuleFile();
  25. // Add in any rules for custom commands
  26. this->WriteCustomCommandsForTarget();
  27. // write in rules for object files
  28. this->WriteCommonCodeRules();
  29. // Write the dependency generation rule.
  30. this->WriteTargetDependRules();
  31. // write the link rules
  32. this->WriteExecutableRule(false);
  33. if(this->Target->NeedRelinkBeforeInstall())
  34. {
  35. // Write rules to link an installable version of the target.
  36. this->WriteExecutableRule(true);
  37. }
  38. // Write the requires target.
  39. this->WriteTargetRequiresRules();
  40. // Write clean target
  41. this->WriteTargetCleanRules();
  42. // close the streams
  43. this->CloseFileStreams();
  44. }
  45. //----------------------------------------------------------------------------
  46. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  47. {
  48. std::vector<std::string> commands;
  49. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  50. std::string objTarget;
  51. // Build list of dependencies.
  52. std::vector<std::string> depends;
  53. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  54. obj != this->Objects.end(); ++obj)
  55. {
  56. objTarget = relPath;
  57. objTarget += *obj;
  58. depends.push_back(objTarget);
  59. }
  60. // Add dependencies on targets that must be built first.
  61. this->AppendTargetDepends(depends);
  62. // Add a dependency on the rule file itself.
  63. this->LocalGenerator->AppendRuleDepend(depends,
  64. this->BuildFileNameFull.c_str());
  65. for(std::vector<std::string>::const_iterator obj =
  66. this->ExternalObjects.begin();
  67. obj != this->ExternalObjects.end(); ++obj)
  68. {
  69. depends.push_back(*obj);
  70. }
  71. // from here up is the same for exe or lib
  72. // Get the name of the executable to generate.
  73. std::string targetName;
  74. std::string targetNameReal;
  75. this->Target->GetExecutableNames(targetName, targetNameReal,
  76. this->LocalGenerator->m_ConfigurationName.c_str());
  77. // Construct the full path version of the names.
  78. std::string outpath = this->LocalGenerator->m_ExecutableOutputPath;
  79. if(outpath.length() == 0)
  80. {
  81. outpath = this->Makefile->GetStartOutputDirectory();
  82. outpath += "/";
  83. }
  84. #ifdef __APPLE__
  85. if(this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  86. {
  87. // Make bundle directories
  88. outpath += targetName;
  89. outpath += ".app/Contents/MacOS/";
  90. std::string f1 =
  91. this->Makefile->GetModulesFile("MacOSXBundleInfo.plist.in");
  92. if ( f1.size() == 0 )
  93. {
  94. cmSystemTools::Error("could not find Mac OSX bundle template file.");
  95. }
  96. std::string macdir =
  97. this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  98. if ( macdir.size() == 0 )
  99. {
  100. macdir = this->Makefile->GetCurrentOutputDirectory();
  101. }
  102. if(macdir.size() && macdir[macdir.size()-1] != '/')
  103. {
  104. macdir += "/";
  105. }
  106. macdir += targetName;
  107. macdir += ".app/Contents/";
  108. // Configure the Info.plist file. Note that it needs the executable name
  109. // to be set.
  110. std::string f2 = macdir + "Info.plist";
  111. macdir += "MacOS";
  112. cmSystemTools::MakeDirectory(macdir.c_str());
  113. this->Makefile->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME",
  114. targetName.c_str());
  115. this->Makefile->ConfigureFile(f1.c_str(), f2.c_str(), false, false, false);
  116. }
  117. #endif
  118. if(relink)
  119. {
  120. outpath = this->Makefile->GetStartOutputDirectory();
  121. outpath += "/CMakeFiles/CMakeRelink.dir";
  122. cmSystemTools::MakeDirectory(outpath.c_str());
  123. outpath += "/";
  124. }
  125. std::string targetFullPath = outpath + targetName;
  126. std::string targetFullPathReal = outpath + targetNameReal;
  127. // Convert to the output path to use in constructing commands.
  128. std::string targetOutPath =
  129. this->Convert(targetFullPath.c_str(),
  130. cmLocalGenerator::START_OUTPUT,
  131. cmLocalGenerator::MAKEFILE);
  132. std::string targetOutPathReal =
  133. this->Convert(targetFullPathReal.c_str(),
  134. cmLocalGenerator::START_OUTPUT,
  135. cmLocalGenerator::MAKEFILE);
  136. // Get the language to use for linking this executable.
  137. const char* linkLanguage =
  138. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  139. // Make sure we have a link language.
  140. if(!linkLanguage)
  141. {
  142. cmSystemTools::Error("Cannot determine link language for target \"",
  143. this->Target->GetName(), "\".");
  144. return;
  145. }
  146. // Add the link message.
  147. std::string buildEcho = "Linking ";
  148. buildEcho += linkLanguage;
  149. buildEcho += " executable ";
  150. buildEcho += targetOutPath;
  151. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str());
  152. // Build a list of compiler flags and linker flags.
  153. std::string flags;
  154. std::string linkFlags;
  155. // Add flags to deal with shared libraries. Any library being
  156. // linked in might be shared, so always use shared flags for an
  157. // executable.
  158. this->LocalGenerator->AddSharedFlags(linkFlags, linkLanguage, true);
  159. // Add flags to create an executable.
  160. this->LocalGenerator->
  161. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS",
  162. this->LocalGenerator->m_ConfigurationName.c_str());
  163. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  164. {
  165. this->LocalGenerator->AppendFlags(linkFlags,
  166. this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  167. }
  168. else
  169. {
  170. this->LocalGenerator->AppendFlags(linkFlags,
  171. this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  172. }
  173. // Add language-specific flags.
  174. this->LocalGenerator
  175. ->AddLanguageFlags(flags, linkLanguage,
  176. this->LocalGenerator->m_ConfigurationName.c_str());
  177. // Add target-specific linker flags.
  178. this->LocalGenerator->AppendFlags(linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  179. // Construct a list of files associated with this executable that
  180. // may need to be cleaned.
  181. std::vector<std::string> exeCleanFiles;
  182. {
  183. std::string cleanName;
  184. std::string cleanRealName;
  185. this->Target->GetExecutableCleanNames(cleanName, cleanRealName,
  186. this->LocalGenerator->m_ConfigurationName.c_str());
  187. std::string cleanFullName = outpath + cleanName;
  188. std::string cleanFullRealName = outpath + cleanRealName;
  189. exeCleanFiles.push_back(this->Convert(cleanFullName.c_str(),
  190. cmLocalGenerator::START_OUTPUT,
  191. cmLocalGenerator::UNCHANGED));
  192. if(cleanRealName != cleanName)
  193. {
  194. exeCleanFiles.push_back(this->Convert(cleanFullRealName.c_str(),
  195. cmLocalGenerator::START_OUTPUT,
  196. cmLocalGenerator::UNCHANGED));
  197. }
  198. }
  199. // Add a command to remove any existing files for this executable.
  200. std::vector<std::string> commands1;
  201. this->LocalGenerator->AppendCleanCommand(commands1, exeCleanFiles,
  202. *this->Target, "target");
  203. this->LocalGenerator->CreateCDCommand(commands1,
  204. this->Makefile->GetStartOutputDirectory(),
  205. this->Makefile->GetHomeOutputDirectory());
  206. commands.insert(commands.end(), commands1.begin(), commands1.end());
  207. commands1.clear();
  208. // Add the pre-build and pre-link rules building but not when relinking.
  209. if(!relink)
  210. {
  211. this->LocalGenerator
  212. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  213. this->LocalGenerator
  214. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  215. }
  216. // Construct the main link rule.
  217. std::string linkRuleVar = "CMAKE_";
  218. linkRuleVar += linkLanguage;
  219. linkRuleVar += "_LINK_EXECUTABLE";
  220. std::string linkRule =
  221. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  222. cmSystemTools::ExpandListArgument(linkRule, commands1);
  223. this->LocalGenerator->CreateCDCommand
  224. (commands1,
  225. this->Makefile->GetStartOutputDirectory(),
  226. this->Makefile->GetHomeOutputDirectory());
  227. commands.insert(commands.end(), commands1.begin(), commands1.end());
  228. // Add a rule to create necessary symlinks for the library.
  229. if(targetOutPath != targetOutPathReal)
  230. {
  231. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  232. symlink += targetOutPathReal;
  233. symlink += " ";
  234. symlink += targetOutPath;
  235. commands.push_back(symlink);
  236. }
  237. // Add the post-build rules when building but not when relinking.
  238. if(!relink)
  239. {
  240. this->LocalGenerator->
  241. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  242. }
  243. // Collect up flags to link in needed libraries.
  244. cmOStringStream linklibs;
  245. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  246. // Construct object file lists that may be needed to expand the
  247. // rule.
  248. std::string variableName;
  249. std::string variableNameExternal;
  250. this->WriteObjectsVariable(variableName, variableNameExternal);
  251. std::string buildObjs = "$(";
  252. buildObjs += variableName;
  253. buildObjs += ") $(";
  254. buildObjs += variableNameExternal;
  255. buildObjs += ")";
  256. std::string cleanObjs = "$(";
  257. cleanObjs += variableName;
  258. cleanObjs += ")";
  259. cmLocalGenerator::RuleVariables vars;
  260. vars.Language = linkLanguage;
  261. vars.Objects = buildObjs.c_str();
  262. vars.Target = targetOutPathReal.c_str();
  263. std::string linkString = linklibs.str();
  264. vars.LinkLibraries = linkString.c_str();
  265. vars.Flags = flags.c_str();
  266. vars.LinkFlags = linkFlags.c_str();
  267. // Expand placeholders in the commands.
  268. for(std::vector<std::string>::iterator i = commands.begin();
  269. i != commands.end(); ++i)
  270. {
  271. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  272. }
  273. // Write the build rule.
  274. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  275. 0,
  276. targetFullPathReal.c_str(),
  277. depends, commands, false);
  278. // The symlink name for the target should depend on the real target
  279. // so if the target version changes it rebuilds and recreates the
  280. // symlink.
  281. if(targetFullPath != targetFullPathReal)
  282. {
  283. depends.clear();
  284. commands.clear();
  285. depends.push_back(targetFullPathReal.c_str());
  286. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  287. targetFullPath.c_str(),
  288. depends, commands, false);
  289. }
  290. // Write convenience targets.
  291. std::string dir = this->Makefile->GetStartOutputDirectory();
  292. dir += "/";
  293. dir += this->LocalGenerator->GetTargetDirectory(*this->Target);
  294. std::string buildTargetRuleName = dir;
  295. buildTargetRuleName += relink?"/preinstall":"/build";
  296. buildTargetRuleName =
  297. this->Convert(buildTargetRuleName.c_str(),
  298. cmLocalGenerator::HOME_OUTPUT,
  299. cmLocalGenerator::MAKEFILE);
  300. this->LocalGenerator->WriteConvenienceRule(*this->BuildFileStream,
  301. targetFullPath.c_str(),
  302. buildTargetRuleName.c_str());
  303. // Clean all the possible executable names and symlinks and object files.
  304. this->CleanFiles.insert(this->CleanFiles.end(),
  305. exeCleanFiles.begin(),
  306. exeCleanFiles.end());
  307. this->CleanFiles.insert(this->CleanFiles.end(),
  308. this->Objects.begin(),
  309. this->Objects.end());
  310. }