cmMakefileExecutableTargetGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #include "cmake.h"
  21. //----------------------------------------------------------------------------
  22. cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator()
  23. {
  24. this->DriveCustomCommandsOnDepends = true;
  25. }
  26. //----------------------------------------------------------------------------
  27. void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
  28. {
  29. // create the build.make file and directory, put in the common blocks
  30. this->CreateRuleFile();
  31. // write rules used to help build object files
  32. this->WriteCommonCodeRules();
  33. // write in rules for object files and custom commands
  34. this->WriteTargetBuildRules();
  35. // write the per-target per-language flags
  36. this->WriteTargetLanguageFlags();
  37. // Write the dependency generation rule.
  38. this->WriteTargetDependRules();
  39. // write the link rules
  40. this->WriteExecutableRule(false);
  41. if(this->Target->NeedRelinkBeforeInstall())
  42. {
  43. // Write rules to link an installable version of the target.
  44. this->WriteExecutableRule(true);
  45. }
  46. // Write the requires target.
  47. this->WriteTargetRequiresRules();
  48. // Write clean target
  49. this->WriteTargetCleanRules();
  50. // close the streams
  51. this->CloseFileStreams();
  52. }
  53. //----------------------------------------------------------------------------
  54. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  55. {
  56. std::vector<std::string> commands;
  57. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  58. std::string objTarget;
  59. // Build list of dependencies.
  60. std::vector<std::string> depends;
  61. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  62. obj != this->Objects.end(); ++obj)
  63. {
  64. objTarget = relPath;
  65. // Handle extra content on Mac bundles
  66. if ( this->ExtraContent.find(*obj) != this->ExtraContent.end() )
  67. {
  68. objTarget = "";
  69. }
  70. objTarget += *obj;
  71. depends.push_back(objTarget);
  72. }
  73. // Add dependencies on targets that must be built first.
  74. this->AppendTargetDepends(depends);
  75. // Add a dependency on the rule file itself.
  76. this->LocalGenerator->AppendRuleDepend(depends,
  77. this->BuildFileNameFull.c_str());
  78. for(std::vector<std::string>::const_iterator obj =
  79. this->ExternalObjects.begin();
  80. obj != this->ExternalObjects.end(); ++obj)
  81. {
  82. depends.push_back(*obj);
  83. }
  84. // from here up is the same for exe or lib
  85. // Get the name of the executable to generate.
  86. std::string targetName;
  87. std::string targetNameReal;
  88. this->Target->GetExecutableNames
  89. (targetName, targetNameReal,
  90. this->LocalGenerator->ConfigurationName.c_str());
  91. // Construct the full path version of the names.
  92. std::string outpath = this->LocalGenerator->ExecutableOutputPath;
  93. if(outpath.length() == 0)
  94. {
  95. outpath = this->Makefile->GetStartOutputDirectory();
  96. outpath += "/";
  97. }
  98. #ifdef __APPLE__
  99. if(this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  100. {
  101. // Make bundle directories
  102. outpath += targetName;
  103. outpath += ".app/Contents/MacOS/";
  104. std::string f1 =
  105. this->Makefile->GetModulesFile("MacOSXBundleInfo.plist.in");
  106. if ( f1.size() == 0 )
  107. {
  108. cmSystemTools::Error("could not find Mac OSX bundle template file.");
  109. }
  110. std::string macdir =
  111. this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  112. if ( macdir.size() == 0 )
  113. {
  114. macdir = this->Makefile->GetCurrentOutputDirectory();
  115. }
  116. if(macdir.size() && macdir[macdir.size()-1] != '/')
  117. {
  118. macdir += "/";
  119. }
  120. macdir += targetName;
  121. macdir += ".app/Contents/";
  122. std::vector<cmSourceFile*>::iterator sourceIt;
  123. for ( sourceIt = this->Target->GetSourceFiles().begin();
  124. sourceIt != this->Target->GetSourceFiles().end();
  125. ++ sourceIt )
  126. {
  127. const char* subDir =
  128. (*sourceIt)->GetProperty("MACOSX_PACKAGE_LOCATION");
  129. if ( subDir )
  130. {
  131. std::string newDir = macdir;
  132. newDir += subDir;
  133. if ( !cmSystemTools::MakeDirectory(newDir.c_str()) )
  134. {
  135. cmSystemTools::Error("Cannot create a subdirectory for \"",
  136. newDir.c_str(), "\".");
  137. return;
  138. }
  139. }
  140. }
  141. // Configure the Info.plist file. Note that it needs the executable name
  142. // to be set.
  143. std::string f2 = macdir + "Info.plist";
  144. macdir += "MacOS";
  145. cmSystemTools::MakeDirectory(macdir.c_str());
  146. this->Makefile->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME",
  147. targetName.c_str());
  148. this->Makefile->ConfigureFile(f1.c_str(), f2.c_str(),
  149. false, false, false);
  150. }
  151. #endif
  152. if(relink)
  153. {
  154. outpath = this->Makefile->GetStartOutputDirectory();
  155. outpath += cmake::GetCMakeFilesDirectory();
  156. outpath += "/CMakeRelink.dir";
  157. cmSystemTools::MakeDirectory(outpath.c_str());
  158. outpath += "/";
  159. }
  160. std::string targetFullPath = outpath + targetName;
  161. std::string targetFullPathReal = outpath + targetNameReal;
  162. std::string targetFullPathPDB = outpath + this->Target->GetName();
  163. targetFullPathPDB += ".pdb";
  164. std::string targetOutPathPDB =
  165. this->Convert(targetFullPathPDB.c_str(),
  166. cmLocalGenerator::FULL,
  167. cmLocalGenerator::MAKEFILE);
  168. // Convert to the output path to use in constructing commands.
  169. std::string targetOutPath =
  170. this->Convert(targetFullPath.c_str(),
  171. cmLocalGenerator::START_OUTPUT,
  172. cmLocalGenerator::MAKEFILE);
  173. std::string targetOutPathReal =
  174. this->Convert(targetFullPathReal.c_str(),
  175. cmLocalGenerator::START_OUTPUT,
  176. cmLocalGenerator::MAKEFILE);
  177. // Get the language to use for linking this executable.
  178. const char* linkLanguage =
  179. this->Target->GetLinkerLanguage(this->GlobalGenerator);
  180. // Make sure we have a link language.
  181. if(!linkLanguage)
  182. {
  183. cmSystemTools::Error("Cannot determine link language for target \"",
  184. this->Target->GetName(), "\".");
  185. return;
  186. }
  187. // Add the link message.
  188. std::string buildEcho = "Linking ";
  189. buildEcho += linkLanguage;
  190. buildEcho += " executable ";
  191. buildEcho += targetOutPath;
  192. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  193. cmLocalUnixMakefileGenerator3::EchoLink);
  194. // Build a list of compiler flags and linker flags.
  195. std::string flags;
  196. std::string linkFlags;
  197. // Add flags to deal with shared libraries. Any library being
  198. // linked in might be shared, so always use shared flags for an
  199. // executable.
  200. this->LocalGenerator->AddSharedFlags(linkFlags, linkLanguage, true);
  201. // Add flags to create an executable.
  202. this->LocalGenerator->
  203. AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS",
  204. this->LocalGenerator->ConfigurationName.c_str());
  205. if(this->Target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  206. {
  207. this->LocalGenerator->AppendFlags
  208. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  209. }
  210. else
  211. {
  212. this->LocalGenerator->AppendFlags
  213. (linkFlags, this->Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  214. }
  215. // Add language-specific flags.
  216. this->LocalGenerator
  217. ->AddLanguageFlags(flags, linkLanguage,
  218. this->LocalGenerator->ConfigurationName.c_str());
  219. // Add target-specific linker flags.
  220. this->LocalGenerator->AppendFlags
  221. (linkFlags, this->Target->GetProperty("LINK_FLAGS"));
  222. std::string linkFlagsConfig = "LINK_FLAGS_";
  223. linkFlagsConfig +=
  224. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName.c_str());
  225. this->LocalGenerator->AppendFlags
  226. (linkFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  227. // Construct a list of files associated with this executable that
  228. // may need to be cleaned.
  229. std::vector<std::string> exeCleanFiles;
  230. {
  231. std::string cleanName;
  232. std::string cleanRealName;
  233. this->Target->GetExecutableCleanNames
  234. (cleanName, cleanRealName,
  235. this->LocalGenerator->ConfigurationName.c_str());
  236. std::string cleanFullName = outpath + cleanName;
  237. std::string cleanFullRealName = outpath + cleanRealName;
  238. exeCleanFiles.push_back(this->Convert(cleanFullName.c_str(),
  239. cmLocalGenerator::START_OUTPUT,
  240. cmLocalGenerator::UNCHANGED));
  241. #ifdef _WIN32
  242. // There may be a manifest file for this target. Add it to the
  243. // clean set just in case.
  244. exeCleanFiles.push_back(this->Convert((cleanFullName+".manifest").c_str(),
  245. cmLocalGenerator::START_OUTPUT,
  246. cmLocalGenerator::UNCHANGED));
  247. #endif
  248. if(cleanRealName != cleanName)
  249. {
  250. exeCleanFiles.push_back(this->Convert(cleanFullRealName.c_str(),
  251. cmLocalGenerator::START_OUTPUT,
  252. cmLocalGenerator::UNCHANGED));
  253. }
  254. }
  255. // Add a command to remove any existing files for this executable.
  256. std::vector<std::string> commands1;
  257. this->LocalGenerator->AppendCleanCommand(commands1, exeCleanFiles,
  258. *this->Target, "target");
  259. this->LocalGenerator->CreateCDCommand
  260. (commands1,
  261. this->Makefile->GetStartOutputDirectory(),
  262. this->Makefile->GetHomeOutputDirectory());
  263. commands.insert(commands.end(), commands1.begin(), commands1.end());
  264. commands1.clear();
  265. // Add the pre-build and pre-link rules building but not when relinking.
  266. if(!relink)
  267. {
  268. this->LocalGenerator
  269. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  270. this->LocalGenerator
  271. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  272. }
  273. // Construct the main link rule.
  274. std::string linkRuleVar = "CMAKE_";
  275. linkRuleVar += linkLanguage;
  276. linkRuleVar += "_LINK_EXECUTABLE";
  277. std::string linkRule =
  278. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  279. cmSystemTools::ExpandListArgument(linkRule, commands1);
  280. this->LocalGenerator->CreateCDCommand
  281. (commands1,
  282. this->Makefile->GetStartOutputDirectory(),
  283. this->Makefile->GetHomeOutputDirectory());
  284. commands.insert(commands.end(), commands1.begin(), commands1.end());
  285. // Add a rule to create necessary symlinks for the library.
  286. if(targetOutPath != targetOutPathReal)
  287. {
  288. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  289. symlink += targetOutPathReal;
  290. symlink += " ";
  291. symlink += targetOutPath;
  292. commands1.clear();
  293. commands1.push_back(symlink);
  294. this->LocalGenerator->CreateCDCommand(commands1,
  295. this->Makefile->GetStartOutputDirectory(),
  296. this->Makefile->GetHomeOutputDirectory());
  297. commands.insert(commands.end(), commands1.begin(), commands1.end());
  298. }
  299. // Add the post-build rules when building but not when relinking.
  300. if(!relink)
  301. {
  302. this->LocalGenerator->
  303. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  304. }
  305. // Collect up flags to link in needed libraries.
  306. cmOStringStream linklibs;
  307. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  308. // Construct object file lists that may be needed to expand the
  309. // rule.
  310. std::string variableName;
  311. std::string variableNameExternal;
  312. this->WriteObjectsVariable(variableName, variableNameExternal);
  313. std::string buildObjs = "$(";
  314. buildObjs += variableName;
  315. buildObjs += ") $(";
  316. buildObjs += variableNameExternal;
  317. buildObjs += ")";
  318. std::string cleanObjs = "$(";
  319. cleanObjs += variableName;
  320. cleanObjs += ")";
  321. cmLocalGenerator::RuleVariables vars;
  322. vars.Language = linkLanguage;
  323. vars.Objects = buildObjs.c_str();
  324. vars.Target = targetOutPathReal.c_str();
  325. vars.TargetPDB = targetOutPathPDB.c_str();
  326. // Setup the target version.
  327. std::string targetVersionMajor;
  328. std::string targetVersionMinor;
  329. {
  330. cmOStringStream majorStream;
  331. cmOStringStream minorStream;
  332. int major;
  333. int minor;
  334. this->Target->GetTargetVersion(major, minor);
  335. majorStream << major;
  336. minorStream << minor;
  337. targetVersionMajor = majorStream.str();
  338. targetVersionMinor = minorStream.str();
  339. }
  340. vars.TargetVersionMajor = targetVersionMajor.c_str();
  341. vars.TargetVersionMinor = targetVersionMinor.c_str();
  342. std::string linkString = linklibs.str();
  343. vars.LinkLibraries = linkString.c_str();
  344. vars.Flags = flags.c_str();
  345. vars.LinkFlags = linkFlags.c_str();
  346. // Expand placeholders in the commands.
  347. for(std::vector<std::string>::iterator i = commands.begin();
  348. i != commands.end(); ++i)
  349. {
  350. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  351. }
  352. // Write the build rule.
  353. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  354. 0,
  355. targetFullPathReal.c_str(),
  356. depends, commands, false);
  357. // The symlink name for the target should depend on the real target
  358. // so if the target version changes it rebuilds and recreates the
  359. // symlink.
  360. if(targetFullPath != targetFullPathReal)
  361. {
  362. depends.clear();
  363. commands.clear();
  364. depends.push_back(targetFullPathReal.c_str());
  365. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  366. targetFullPath.c_str(),
  367. depends, commands, false);
  368. }
  369. // Write the main driver rule to build everything in this target.
  370. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  371. // Clean all the possible executable names and symlinks and object files.
  372. this->CleanFiles.insert(this->CleanFiles.end(),
  373. exeCleanFiles.begin(),
  374. exeCleanFiles.end());
  375. this->CleanFiles.insert(this->CleanFiles.end(),
  376. this->Objects.begin(),
  377. this->Objects.end());
  378. }