cmMakefileExecutableTargetGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 += this->Target->GetName();
  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 += this->Target->GetName();
  107. macdir += ".app/Contents/";
  108. std::string f2 = macdir + "Info.plist";
  109. macdir += "MacOS";
  110. cmSystemTools::MakeDirectory(macdir.c_str());
  111. this->Makefile->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", this->Target->GetName());
  112. this->Makefile->ConfigureFile(f1.c_str(), f2.c_str(),
  113. false, false, false);
  114. }
  115. #endif
  116. if(relink)
  117. {
  118. outpath = this->Makefile->GetStartOutputDirectory();
  119. outpath += "/CMakeFiles/CMakeRelink.dir";
  120. cmSystemTools::MakeDirectory(outpath.c_str());
  121. outpath += "/";
  122. }
  123. std::string targetFullPath = outpath + targetName;
  124. std::string targetFullPathReal = outpath + targetNameReal;
  125. // Convert to the output path to use in constructing commands.
  126. std::string targetOutPath =
  127. this->Convert(targetFullPath.c_str(),
  128. cmLocalGenerator::START_OUTPUT,
  129. cmLocalGenerator::MAKEFILE);
  130. std::string targetOutPathReal =
  131. this->Convert(targetFullPathReal.c_str(),
  132. cmLocalGenerator::START_OUTPUT,
  133. cmLocalGenerator::MAKEFILE);
  134. // Get the language to use for linking this executable.
  135. const char* linkLanguage =
  136. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  137. // Make sure we have a link language.
  138. if(!linkLanguage)
  139. {
  140. cmSystemTools::Error("Cannot determine link language for target \"",
  141. this->Target->GetName(), "\".");
  142. return;
  143. }
  144. // Add the link message.
  145. std::string buildEcho = "Linking ";
  146. buildEcho += linkLanguage;
  147. buildEcho += " executable ";
  148. buildEcho += targetOutPath;
  149. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str());
  150. // Build a list of compiler flags and linker flags.
  151. std::string flags;
  152. std::string linkFlags;
  153. // Add flags to deal with shared libraries. Any library being
  154. // linked in might be shared, so always use shared flags for an
  155. // executable.
  156. this->LocalGenerator->AddSharedFlags(linkFlags, linkLanguage, true);
  157. // Add flags to create an executable.
  158. this->LocalGenerator->
  159. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  160. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  161. {
  162. this->LocalGenerator->AppendFlags(linkFlags,
  163. this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  164. }
  165. else
  166. {
  167. this->LocalGenerator->AppendFlags(linkFlags,
  168. this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  169. }
  170. // Add language-specific flags.
  171. this->LocalGenerator->AddLanguageFlags(flags, linkLanguage);
  172. // Add target-specific linker flags.
  173. this->LocalGenerator->AppendFlags(linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  174. // Construct a list of files associated with this executable that
  175. // may need to be cleaned.
  176. std::vector<std::string> exeCleanFiles;
  177. {
  178. std::string cleanName;
  179. std::string cleanRealName;
  180. this->Target->GetExecutableCleanNames(cleanName, cleanRealName,
  181. this->LocalGenerator->m_ConfigurationName.c_str());
  182. std::string cleanFullName = outpath + cleanName;
  183. std::string cleanFullRealName = outpath + cleanRealName;
  184. exeCleanFiles.push_back(this->Convert(cleanFullName.c_str(),
  185. cmLocalGenerator::START_OUTPUT,
  186. cmLocalGenerator::MAKEFILE));
  187. if(cleanRealName != cleanName)
  188. {
  189. exeCleanFiles.push_back(this->Convert(cleanFullRealName.c_str(),
  190. cmLocalGenerator::START_OUTPUT,
  191. cmLocalGenerator::MAKEFILE));
  192. }
  193. }
  194. // Add a command to remove any existing files for this executable.
  195. std::vector<std::string> commands1;
  196. this->LocalGenerator->AppendCleanCommand(commands1, exeCleanFiles);
  197. this->LocalGenerator->CreateCDCommand(commands1,
  198. this->Makefile->GetStartOutputDirectory(),
  199. this->Makefile->GetHomeOutputDirectory());
  200. commands.insert(commands.end(), commands1.begin(), commands1.end());
  201. commands1.clear();
  202. // Add the pre-build and pre-link rules building but not when relinking.
  203. if(!relink)
  204. {
  205. this->LocalGenerator
  206. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  207. this->LocalGenerator
  208. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  209. }
  210. // Construct the main link rule.
  211. std::string linkRuleVar = "CMAKE_";
  212. linkRuleVar += linkLanguage;
  213. linkRuleVar += "_LINK_EXECUTABLE";
  214. std::string linkRule =
  215. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  216. cmSystemTools::ExpandListArgument(linkRule, commands1);
  217. this->LocalGenerator->CreateCDCommand
  218. (commands1,
  219. this->Makefile->GetStartOutputDirectory(),
  220. this->Makefile->GetHomeOutputDirectory());
  221. commands.insert(commands.end(), commands1.begin(), commands1.end());
  222. // Add a rule to create necessary symlinks for the library.
  223. if(targetOutPath != targetOutPathReal)
  224. {
  225. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  226. symlink += targetOutPathReal;
  227. symlink += " ";
  228. symlink += targetOutPath;
  229. commands.push_back(symlink);
  230. }
  231. // Add the post-build rules when building but not when relinking.
  232. if(!relink)
  233. {
  234. this->LocalGenerator->
  235. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  236. }
  237. // Collect up flags to link in needed libraries.
  238. cmOStringStream linklibs;
  239. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  240. // Construct object file lists that may be needed to expand the
  241. // rule.
  242. std::string variableName;
  243. std::string variableNameExternal;
  244. this->WriteObjectsVariable(variableName, variableNameExternal);
  245. std::string buildObjs = "$(";
  246. buildObjs += variableName;
  247. buildObjs += ") $(";
  248. buildObjs += variableNameExternal;
  249. buildObjs += ")";
  250. std::string cleanObjs = "$(";
  251. cleanObjs += variableName;
  252. cleanObjs += ")";
  253. cmLocalGenerator::RuleVariables vars;
  254. vars.Language = linkLanguage;
  255. vars.Objects = buildObjs.c_str();
  256. vars.Target = targetOutPathReal.c_str();
  257. std::string linkString = linklibs.str();
  258. vars.LinkLibraries = linkString.c_str();
  259. vars.Flags = flags.c_str();
  260. vars.LinkFlags = linkFlags.c_str();
  261. // Expand placeholders in the commands.
  262. for(std::vector<std::string>::iterator i = commands.begin();
  263. i != commands.end(); ++i)
  264. {
  265. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  266. }
  267. // Write the build rule.
  268. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  269. 0,
  270. targetFullPathReal.c_str(),
  271. depends, commands, false);
  272. // The symlink name for the target should depend on the real target
  273. // so if the target version changes it rebuilds and recreates the
  274. // symlink.
  275. if(targetFullPath != targetFullPathReal)
  276. {
  277. depends.clear();
  278. commands.clear();
  279. depends.push_back(targetFullPathReal.c_str());
  280. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  281. targetFullPath.c_str(),
  282. depends, commands, false);
  283. }
  284. // Write convenience targets.
  285. std::string dir = this->Makefile->GetStartOutputDirectory();
  286. dir += "/";
  287. dir += this->LocalGenerator->GetTargetDirectory(*this->Target);
  288. std::string buildTargetRuleName = dir;
  289. buildTargetRuleName += relink?"/preinstall":"/build";
  290. buildTargetRuleName =
  291. this->Convert(buildTargetRuleName.c_str(),
  292. cmLocalGenerator::HOME_OUTPUT,
  293. cmLocalGenerator::MAKEFILE);
  294. this->LocalGenerator->WriteConvenienceRule(*this->BuildFileStream,
  295. targetFullPath.c_str(),
  296. buildTargetRuleName.c_str());
  297. // Clean all the possible executable names and symlinks and object files.
  298. this->CleanFiles.insert(this->CleanFiles.end(),
  299. exeCleanFiles.begin(),
  300. exeCleanFiles.end());
  301. this->CleanFiles.push_back(cleanObjs);
  302. }