cmMakefileExecutableTargetGenerator.cxx 14 KB

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