cmMakefileExecutableTargetGenerator.cxx 15 KB

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