You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

470 lines
16 KiB

  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*>::const_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 the pre-build and pre-link rules building but not when relinking.
  289. if(!relink)
  290. {
  291. this->LocalGenerator
  292. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  293. this->LocalGenerator
  294. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  295. }
  296. // Construct the main link rule.
  297. std::string linkRuleVar = "CMAKE_";
  298. linkRuleVar += linkLanguage;
  299. linkRuleVar += "_LINK_EXECUTABLE";
  300. std::string linkRule =
  301. this->Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  302. std::vector<std::string> commands1;
  303. cmSystemTools::ExpandListArgument(linkRule, commands1);
  304. if(this->Target->GetPropertyAsBool("ENABLE_EXPORTS"))
  305. {
  306. // If a separate rule for creating an import library is specified
  307. // add it now.
  308. std::string implibRuleVar = "CMAKE_";
  309. implibRuleVar += linkLanguage;
  310. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  311. if(const char* rule =
  312. this->Makefile->GetDefinition(implibRuleVar.c_str()))
  313. {
  314. cmSystemTools::ExpandListArgument(rule, commands1);
  315. }
  316. }
  317. this->LocalGenerator->CreateCDCommand
  318. (commands1,
  319. this->Makefile->GetStartOutputDirectory(),
  320. this->Makefile->GetHomeOutputDirectory());
  321. commands.insert(commands.end(), commands1.begin(), commands1.end());
  322. // Add a rule to create necessary symlinks for the library.
  323. if(targetOutPath != targetOutPathReal)
  324. {
  325. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  326. symlink += targetOutPathReal;
  327. symlink += " ";
  328. symlink += targetOutPath;
  329. commands1.clear();
  330. commands1.push_back(symlink);
  331. this->LocalGenerator->CreateCDCommand(commands1,
  332. this->Makefile->GetStartOutputDirectory(),
  333. this->Makefile->GetHomeOutputDirectory());
  334. commands.insert(commands.end(), commands1.begin(), commands1.end());
  335. }
  336. // Add the post-build rules when building but not when relinking.
  337. if(!relink)
  338. {
  339. this->LocalGenerator->
  340. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  341. }
  342. // Collect up flags to link in needed libraries.
  343. cmOStringStream linklibs;
  344. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  345. // Construct object file lists that may be needed to expand the
  346. // rule.
  347. std::string variableName;
  348. std::string variableNameExternal;
  349. this->WriteObjectsVariable(variableName, variableNameExternal);
  350. std::string buildObjs = "$(";
  351. buildObjs += variableName;
  352. buildObjs += ") $(";
  353. buildObjs += variableNameExternal;
  354. buildObjs += ")";
  355. std::string cleanObjs = "$(";
  356. cleanObjs += variableName;
  357. cleanObjs += ")";
  358. cmLocalGenerator::RuleVariables vars;
  359. vars.Language = linkLanguage;
  360. vars.Objects = buildObjs.c_str();
  361. vars.Target = targetOutPathReal.c_str();
  362. vars.TargetPDB = targetOutPathPDB.c_str();
  363. // Setup the target version.
  364. std::string targetVersionMajor;
  365. std::string targetVersionMinor;
  366. {
  367. cmOStringStream majorStream;
  368. cmOStringStream minorStream;
  369. int major;
  370. int minor;
  371. this->Target->GetTargetVersion(major, minor);
  372. majorStream << major;
  373. minorStream << minor;
  374. targetVersionMajor = majorStream.str();
  375. targetVersionMinor = minorStream.str();
  376. }
  377. vars.TargetVersionMajor = targetVersionMajor.c_str();
  378. vars.TargetVersionMinor = targetVersionMinor.c_str();
  379. std::string linkString = linklibs.str();
  380. vars.LinkLibraries = linkString.c_str();
  381. vars.Flags = flags.c_str();
  382. vars.LinkFlags = linkFlags.c_str();
  383. // Expand placeholders in the commands.
  384. this->LocalGenerator->TargetImplib = targetOutPathImport;
  385. for(std::vector<std::string>::iterator i = commands.begin();
  386. i != commands.end(); ++i)
  387. {
  388. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  389. }
  390. this->LocalGenerator->TargetImplib = "";
  391. // Write the build rule.
  392. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream,
  393. 0,
  394. targetFullPathReal.c_str(),
  395. depends, commands, false);
  396. // The symlink name for the target should depend on the real target
  397. // so if the target version changes it rebuilds and recreates the
  398. // symlink.
  399. if(targetFullPath != targetFullPathReal)
  400. {
  401. depends.clear();
  402. commands.clear();
  403. depends.push_back(targetFullPathReal.c_str());
  404. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  405. targetFullPath.c_str(),
  406. depends, commands, false);
  407. }
  408. // Write the main driver rule to build everything in this target.
  409. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  410. // Clean all the possible executable names and symlinks.
  411. this->CleanFiles.insert(this->CleanFiles.end(),
  412. exeCleanFiles.begin(),
  413. exeCleanFiles.end());
  414. }