cmMakefileExecutableTargetGenerator.cxx 14 KB

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