1
0

cmMakefileExecutableTargetGenerator.cxx 16 KB

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