cmMakefileExecutableTargetGenerator.cxx 13 KB

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