cmMakefileExecutableTargetGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 link rules
  30. this->WriteExecutableRule();
  31. // Write the requires target.
  32. this->WriteTargetRequiresRules();
  33. // Write clean target
  34. this->WriteTargetCleanRules();
  35. // close the streams
  36. this->CloseFileStreams();
  37. }
  38. //----------------------------------------------------------------------------
  39. void cmMakefileExecutableTargetGenerator::WriteExecutableRule()
  40. {
  41. // Write the dependency generation rule.
  42. this->WriteTargetDependRules();
  43. std::vector<std::string> commands;
  44. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  45. std::string objTarget;
  46. // Build list of dependencies.
  47. std::vector<std::string> depends;
  48. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  49. obj != this->Objects.end(); ++obj)
  50. {
  51. objTarget = relPath;
  52. objTarget += *obj;
  53. depends.push_back(objTarget);
  54. }
  55. // Add dependencies on targets that must be built first.
  56. this->AppendTargetDepends(depends);
  57. // Add a dependency on the rule file itself.
  58. this->LocalGenerator->AppendRuleDepend(depends,
  59. this->BuildFileNameFull.c_str());
  60. for(std::vector<std::string>::const_iterator obj =
  61. this->ExternalObjects.begin();
  62. obj != this->ExternalObjects.end(); ++obj)
  63. {
  64. depends.push_back(*obj);
  65. }
  66. // from here up is the same for exe or lib
  67. // Get the name of the executable to generate.
  68. std::string targetName;
  69. std::string targetNameReal;
  70. this->Target->GetExecutableNames(targetName, targetNameReal,
  71. this->LocalGenerator->m_ConfigurationName.c_str());
  72. // Construct the full path version of the names.
  73. std::string outpath = this->LocalGenerator->m_ExecutableOutputPath;
  74. if(outpath.length() == 0)
  75. {
  76. outpath = this->Makefile->GetStartOutputDirectory();
  77. outpath += "/";
  78. }
  79. #ifdef __APPLE__
  80. if(this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  81. {
  82. // Make bundle directories
  83. outpath += this->Target->GetName();
  84. outpath += ".app/Contents/MacOS/";
  85. std::string f1 =
  86. this->Makefile->GetModulesFile("MacOSXBundleInfo.plist.in");
  87. if ( f1.size() == 0 )
  88. {
  89. cmSystemTools::Error("could not find Mac OSX bundle template file.");
  90. }
  91. std::string macdir =
  92. this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  93. if ( macdir.size() == 0 )
  94. {
  95. macdir = this->Makefile->GetCurrentOutputDirectory();
  96. }
  97. if(macdir.size() && macdir[macdir.size()-1] != '/')
  98. {
  99. macdir += "/";
  100. }
  101. macdir += this->Target->GetName();
  102. macdir += ".app/Contents/";
  103. std::string f2 = macdir + "Info.plist";
  104. macdir += "MacOS";
  105. cmSystemTools::MakeDirectory(macdir.c_str());
  106. this->Makefile->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", this->Target->GetName());
  107. this->Makefile->ConfigureFile(f1.c_str(), f2.c_str(),
  108. false, false, false);
  109. }
  110. #endif
  111. std::string targetFullPath = outpath + targetName;
  112. std::string targetFullPathReal = outpath + targetNameReal;
  113. // Convert to the output path to use in constructing commands.
  114. std::string targetOutPath =
  115. this->Convert(targetFullPath.c_str(),
  116. cmLocalGenerator::START_OUTPUT,
  117. cmLocalGenerator::MAKEFILE);
  118. std::string targetOutPathReal =
  119. this->Convert(targetFullPathReal.c_str(),
  120. cmLocalGenerator::START_OUTPUT,
  121. cmLocalGenerator::MAKEFILE);
  122. // Get the language to use for linking this executable.
  123. const char* linkLanguage =
  124. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  125. // Make sure we have a link language.
  126. if(!linkLanguage)
  127. {
  128. cmSystemTools::Error("Cannot determine link language for target \"",
  129. this->Target->GetName(), "\".");
  130. return;
  131. }
  132. // Add the link message.
  133. std::string buildEcho = "Linking ";
  134. buildEcho += linkLanguage;
  135. buildEcho += " executable ";
  136. buildEcho += targetOutPath;
  137. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str());
  138. // Build a list of compiler flags and linker flags.
  139. std::string flags;
  140. std::string linkFlags;
  141. // Add flags to deal with shared libraries. Any library being
  142. // linked in might be shared, so always use shared flags for an
  143. // executable.
  144. this->LocalGenerator->AddSharedFlags(linkFlags, linkLanguage, true);
  145. // Add flags to create an executable.
  146. this->LocalGenerator->
  147. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  148. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  149. {
  150. this->LocalGenerator->AppendFlags(linkFlags,
  151. this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  152. }
  153. else
  154. {
  155. this->LocalGenerator->AppendFlags(linkFlags,
  156. this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  157. }
  158. // Add language-specific flags.
  159. this->LocalGenerator->AddLanguageFlags(flags, linkLanguage);
  160. // Add target-specific linker flags.
  161. this->LocalGenerator->AppendFlags(linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  162. // Construct a list of files associated with this executable that
  163. // may need to be cleaned.
  164. std::vector<std::string> exeCleanFiles;
  165. {
  166. std::string cleanName;
  167. std::string cleanRealName;
  168. this->Target->GetExecutableCleanNames(cleanName, cleanRealName,
  169. this->LocalGenerator->m_ConfigurationName.c_str());
  170. std::string cleanFullName = outpath + cleanName;
  171. std::string cleanFullRealName = outpath + cleanRealName;
  172. exeCleanFiles.push_back(this->Convert(cleanFullName.c_str(),
  173. cmLocalGenerator::START_OUTPUT,
  174. cmLocalGenerator::MAKEFILE));
  175. if(cleanRealName != cleanName)
  176. {
  177. exeCleanFiles.push_back(this->Convert(cleanFullRealName.c_str(),
  178. cmLocalGenerator::START_OUTPUT,
  179. cmLocalGenerator::MAKEFILE));
  180. }
  181. }
  182. // Add a command to remove any existing files for this executable.
  183. std::vector<std::string> commands1;
  184. this->LocalGenerator->AppendCleanCommand(commands1, exeCleanFiles);
  185. this->LocalGenerator->CreateCDCommand(commands1,
  186. this->Makefile->GetStartOutputDirectory(),
  187. this->Makefile->GetHomeOutputDirectory());
  188. commands.insert(commands.end(), commands1.begin(), commands1.end());
  189. commands1.clear();
  190. // Add the pre-build and pre-link rules.
  191. this->LocalGenerator->
  192. AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  193. this->LocalGenerator->
  194. AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  195. // Construct the main link rule.
  196. std::string linkRuleVar = "CMAKE_";
  197. linkRuleVar += linkLanguage;
  198. linkRuleVar += "_LINK_EXECUTABLE";
  199. std::string linkRule =
  200. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  201. cmSystemTools::ExpandListArgument(linkRule, commands1);
  202. this->LocalGenerator->CreateCDCommand
  203. (commands1,
  204. this->Makefile->GetStartOutputDirectory(),
  205. this->Makefile->GetHomeOutputDirectory());
  206. commands.insert(commands.end(), commands1.begin(), commands1.end());
  207. // Add a rule to create necessary symlinks for the library.
  208. if(targetOutPath != targetOutPathReal)
  209. {
  210. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  211. symlink += targetOutPathReal;
  212. symlink += " ";
  213. symlink += targetOutPath;
  214. commands.push_back(symlink);
  215. }
  216. // Add the post-build rules.
  217. this->LocalGenerator->AppendCustomCommands
  218. (commands, this->Target->GetPostBuildCommands());
  219. // Collect up flags to link in needed libraries.
  220. cmOStringStream linklibs;
  221. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target);
  222. // Construct object file lists that may be needed to expand the
  223. // rule.
  224. std::string variableName;
  225. std::string variableNameExternal;
  226. this->WriteObjectsVariable(variableName, variableNameExternal);
  227. std::string buildObjs = "$(";
  228. buildObjs += variableName;
  229. buildObjs += ") $(";
  230. buildObjs += variableNameExternal;
  231. buildObjs += ")";
  232. std::string cleanObjs = "$(";
  233. cleanObjs += variableName;
  234. cleanObjs += ")";
  235. // Expand placeholders in the commands.
  236. for(std::vector<std::string>::iterator i = commands.begin();
  237. i != commands.end(); ++i)
  238. {
  239. this->LocalGenerator->ExpandRuleVariables(*i,
  240. linkLanguage,
  241. buildObjs.c_str(),
  242. targetOutPathReal.c_str(),
  243. linklibs.str().c_str(),
  244. 0,
  245. 0,
  246. flags.c_str(),
  247. 0,
  248. 0,
  249. linkFlags.c_str());
  250. }
  251. // Write the build rule.
  252. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  253. 0,
  254. targetFullPathReal.c_str(),
  255. depends, commands);
  256. // The symlink name for the target should depend on the real target
  257. // so if the target version changes it rebuilds and recreates the
  258. // symlink.
  259. if(targetFullPath != targetFullPathReal)
  260. {
  261. depends.clear();
  262. commands.clear();
  263. depends.push_back(targetFullPathReal.c_str());
  264. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  265. targetFullPath.c_str(),
  266. depends, commands);
  267. }
  268. // Write convenience targets.
  269. std::string dir = this->Makefile->GetStartOutputDirectory();
  270. dir += "/";
  271. dir += this->LocalGenerator->GetTargetDirectory(*this->Target);
  272. std::string buildTargetRuleName = dir;
  273. buildTargetRuleName += "/build";
  274. buildTargetRuleName =
  275. this->Convert(buildTargetRuleName.c_str(),
  276. cmLocalGenerator::HOME_OUTPUT,
  277. cmLocalGenerator::MAKEFILE);
  278. this->LocalGenerator->WriteConvenienceRule(*this->BuildFileStream,
  279. targetFullPath.c_str(),
  280. buildTargetRuleName.c_str());
  281. // Clean all the possible executable names and symlinks and object files.
  282. this->CleanFiles.insert(this->CleanFiles.end(),
  283. exeCleanFiles.begin(),
  284. exeCleanFiles.end());
  285. this->CleanFiles.push_back(cleanObjs);
  286. }