1
0

cmGlobalXCodeGenerator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <iosfwd>
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "cmGlobalGenerator.h"
  12. #include "cmXCodeObject.h"
  13. class cmCustomCommand;
  14. class cmCustomCommandGenerator;
  15. class cmGeneratorTarget;
  16. class cmGlobalGeneratorFactory;
  17. class cmLocalGenerator;
  18. class cmMakefile;
  19. class cmSourceFile;
  20. class cmSourceGroup;
  21. class cmake;
  22. struct cmDocumentationEntry;
  23. /** \class cmGlobalXCodeGenerator
  24. * \brief Write a Unix makefiles.
  25. *
  26. * cmGlobalXCodeGenerator manages Xcode build process for a tree
  27. */
  28. class cmGlobalXCodeGenerator : public cmGlobalGenerator
  29. {
  30. public:
  31. cmGlobalXCodeGenerator(cmake* cm, std::string const& version_string,
  32. unsigned int version_number);
  33. static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory();
  34. cmGlobalXCodeGenerator(const cmGlobalXCodeGenerator&) = delete;
  35. const cmGlobalXCodeGenerator& operator=(const cmGlobalXCodeGenerator&) =
  36. delete;
  37. //! Get the name for the generator.
  38. std::string GetName() const override
  39. {
  40. return cmGlobalXCodeGenerator::GetActualName();
  41. }
  42. static std::string GetActualName() { return "Xcode"; }
  43. /** Get the documentation entry for this generator. */
  44. static void GetDocumentation(cmDocumentationEntry& entry);
  45. //! Create a local generator appropriate to this Global Generator
  46. std::unique_ptr<cmLocalGenerator> CreateLocalGenerator(
  47. cmMakefile* mf) override;
  48. /**
  49. * Try to determine system information such as shared library
  50. * extension, pthreads, byte order etc.
  51. */
  52. void EnableLanguage(std::vector<std::string> const& languages, cmMakefile*,
  53. bool optional) override;
  54. /**
  55. * Open a generated IDE project given the following information.
  56. */
  57. bool Open(const std::string& bindir, const std::string& projectName,
  58. bool dryRun) override;
  59. /**
  60. * Try running cmake and building a file. This is used for dynalically
  61. * loaded commands, not as part of the usual build process.
  62. */
  63. std::vector<GeneratedMakeCommand> GenerateBuildCommand(
  64. const std::string& makeProgram, const std::string& projectName,
  65. const std::string& projectDir, std::vector<std::string> const& targetNames,
  66. const std::string& config, bool fast, int jobs, bool verbose,
  67. std::vector<std::string> const& makeOptions =
  68. std::vector<std::string>()) override;
  69. /** Append the subdirectory for the given configuration. */
  70. void AppendDirectoryForConfig(const std::string& prefix,
  71. const std::string& config,
  72. const std::string& suffix,
  73. std::string& dir) override;
  74. bool FindMakeProgram(cmMakefile*) override;
  75. //! What is the configurations directory variable called?
  76. const char* GetCMakeCFGIntDir() const override;
  77. //! expand CFGIntDir
  78. std::string ExpandCFGIntDir(const std::string& str,
  79. const std::string& config) const override;
  80. void SetCurrentLocalGenerator(cmLocalGenerator*);
  81. /** Return true if the generated build tree may contain multiple builds.
  82. i.e. "Can I build Debug and Release in the same tree?" */
  83. bool IsMultiConfig() const override;
  84. bool IsXcode() const override { return true; }
  85. bool HasKnownObjectFileLocation(std::string* reason) const override;
  86. bool IsIPOSupported() const override { return true; }
  87. bool UseEffectivePlatformName(cmMakefile* mf) const override;
  88. bool ShouldStripResourcePath(cmMakefile*) const override;
  89. bool SetSystemName(std::string const& s, cmMakefile* mf) override;
  90. bool SetGeneratorToolset(std::string const& ts, bool build,
  91. cmMakefile* mf) override;
  92. void AppendFlag(std::string& flags, std::string const& flag) const;
  93. enum class BuildSystem
  94. {
  95. One = 1,
  96. Twelve = 12,
  97. };
  98. protected:
  99. void AddExtraIDETargets() override;
  100. void Generate() override;
  101. private:
  102. bool ParseGeneratorToolset(std::string const& ts, cmMakefile* mf);
  103. bool ProcessGeneratorToolsetField(std::string const& key,
  104. std::string const& value, cmMakefile* mf);
  105. cmXCodeObject* CreateOrGetPBXGroup(cmGeneratorTarget* gtgt,
  106. cmSourceGroup* sg);
  107. cmXCodeObject* CreatePBXGroup(cmXCodeObject* parent,
  108. const std::string& name);
  109. bool CreateGroups(std::vector<cmLocalGenerator*>& generators);
  110. std::string XCodeEscapePath(const std::string& p);
  111. std::string RelativeToSource(const std::string& p);
  112. std::string RelativeToBinary(const std::string& p);
  113. std::string ConvertToRelativeForMake(std::string const& p);
  114. void CreateCustomCommands(
  115. cmXCodeObject* buildPhases, cmXCodeObject* sourceBuildPhase,
  116. cmXCodeObject* headerBuildPhase, cmXCodeObject* resourceBuildPhase,
  117. std::vector<cmXCodeObject*> const& contentBuildPhases,
  118. cmXCodeObject* frameworkBuildPhase, cmGeneratorTarget* gtgt);
  119. std::string ComputeInfoPListLocation(cmGeneratorTarget* target);
  120. void AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  121. cmGeneratorTarget* target,
  122. std::vector<cmCustomCommand> const& commands,
  123. const char* commandFileName);
  124. void CreateCustomRulesMakefile(const char* makefileBasename,
  125. cmGeneratorTarget* target,
  126. std::vector<cmCustomCommand> const& commands,
  127. const std::string& configName);
  128. cmXCodeObject* FindXCodeTarget(const cmGeneratorTarget*);
  129. std::string GetOrCreateId(const std::string& name, const std::string& id);
  130. // create cmXCodeObject from these functions so that memory can be managed
  131. // correctly. All objects created are stored in this->XCodeObjects.
  132. cmXCodeObject* CreateObject(cmXCodeObject::PBXType ptype);
  133. cmXCodeObject* CreateObject(cmXCodeObject::Type type);
  134. cmXCodeObject* CreateString(const std::string& s);
  135. cmXCodeObject* CreateObjectReference(cmXCodeObject*);
  136. cmXCodeObject* CreateFlatClone(cmXCodeObject*);
  137. cmXCodeObject* CreateXCodeTarget(cmGeneratorTarget* gtgt,
  138. cmXCodeObject* buildPhases);
  139. void ForceLinkerLanguages() override;
  140. void ForceLinkerLanguage(cmGeneratorTarget* gtgt);
  141. const char* GetTargetLinkFlagsVar(const cmGeneratorTarget* target) const;
  142. const char* GetTargetFileType(cmGeneratorTarget* target);
  143. const char* GetTargetProductType(cmGeneratorTarget* target);
  144. std::string AddConfigurations(cmXCodeObject* target,
  145. cmGeneratorTarget* gtgt);
  146. void AppendOrAddBuildSetting(cmXCodeObject* settings, const char* attr,
  147. cmXCodeObject* value);
  148. void AppendBuildSettingAttribute(cmXCodeObject* target, const char* attr,
  149. cmXCodeObject* value,
  150. const std::string& configName);
  151. cmXCodeObject* CreateUtilityTarget(cmGeneratorTarget* gtgt);
  152. void AddDependAndLinkInformation(cmXCodeObject* target);
  153. void AddPositionIndependentLinkAttribute(cmGeneratorTarget* target,
  154. cmXCodeObject* buildSettings,
  155. const std::string& configName);
  156. void CreateBuildSettings(cmGeneratorTarget* gtgt,
  157. cmXCodeObject* buildSettings,
  158. const std::string& buildType);
  159. std::string ExtractFlag(const char* flag, std::string& flags);
  160. std::string ExtractFlagRegex(const char* exp, int matchIndex,
  161. std::string& flags);
  162. void FilterConfigurationAttribute(std::string const& configName,
  163. std::string& attribute);
  164. void SortXCodeObjects();
  165. // delete all objects in the this->XCodeObjects vector.
  166. void ClearXCodeObjects();
  167. bool CreateXCodeObjects(cmLocalGenerator* root,
  168. std::vector<cmLocalGenerator*>& generators);
  169. void OutputXCodeProject(cmLocalGenerator* root,
  170. std::vector<cmLocalGenerator*>& generators);
  171. // Write shared scheme files for all the native targets
  172. // return true if any were written
  173. bool OutputXCodeSharedSchemes(const std::string& xcProjDir,
  174. cmLocalGenerator* root);
  175. void OutputXCodeWorkspaceSettings(const std::string& xcProjDir,
  176. bool hasGeneratedSchemes);
  177. void WriteXCodePBXProj(std::ostream& fout, cmLocalGenerator* root,
  178. std::vector<cmLocalGenerator*>& generators);
  179. cmXCodeObject* CreateXCodeFileReferenceFromPath(const std::string& fullpath,
  180. cmGeneratorTarget* target,
  181. const std::string& lang,
  182. cmSourceFile* sf);
  183. cmXCodeObject* CreateXCodeBuildFileFromPath(const std::string& fullpath,
  184. cmGeneratorTarget* target,
  185. const std::string& lang,
  186. cmSourceFile* sf);
  187. cmXCodeObject* CreateXCodeFileReference(cmSourceFile* sf,
  188. cmGeneratorTarget* target);
  189. cmXCodeObject* CreateXCodeSourceFile(cmLocalGenerator* gen, cmSourceFile* sf,
  190. cmGeneratorTarget* gtgt);
  191. void AddXCodeProjBuildRule(cmGeneratorTarget* target,
  192. std::vector<cmSourceFile*>& sources) const;
  193. bool CreateXCodeTargets(cmLocalGenerator* gen, std::vector<cmXCodeObject*>&);
  194. bool CreateXCodeTarget(cmGeneratorTarget* gtgt,
  195. std::vector<cmXCodeObject*>&);
  196. bool IsHeaderFile(cmSourceFile*);
  197. void AddDependTarget(cmXCodeObject* target, cmXCodeObject* dependTarget);
  198. void CreateXCodeDependHackTarget(std::vector<cmXCodeObject*>& targets);
  199. bool SpecialTargetEmitted(std::string const& tname);
  200. void SetGenerationRoot(cmLocalGenerator* root);
  201. void AddExtraTargets(cmLocalGenerator* root,
  202. std::vector<cmLocalGenerator*>& gens);
  203. cmXCodeObject* CreateLegacyRunScriptBuildPhase(
  204. const char* name, const char* name2, cmGeneratorTarget* target,
  205. const std::vector<cmCustomCommand>&);
  206. void CreateRunScriptBuildPhases(cmXCodeObject* buildPhases,
  207. cmGeneratorTarget const* gt);
  208. void CreateRunScriptBuildPhases(cmXCodeObject* buildPhases,
  209. cmSourceFile const* sf,
  210. cmGeneratorTarget const* gt,
  211. std::set<cmSourceFile const*>& visited);
  212. cmXCodeObject* CreateRunScriptBuildPhase(cmSourceFile const* sf,
  213. cmGeneratorTarget const* gt,
  214. cmCustomCommand const& cc);
  215. cmXCodeObject* CreateRunScriptBuildPhase(
  216. std::string const& name, std::vector<cmCustomCommand> const& commands);
  217. std::string ConstructScript(cmCustomCommandGenerator const& ccg);
  218. void CreateReRunCMakeFile(cmLocalGenerator* root,
  219. std::vector<cmLocalGenerator*> const& gens);
  220. std::string LookupFlags(const std::string& varNamePrefix,
  221. const std::string& varNameLang,
  222. const std::string& varNameSuffix,
  223. const std::string& default_flags);
  224. class Factory;
  225. class BuildObjectListOrString;
  226. friend class BuildObjectListOrString;
  227. void AppendDefines(BuildObjectListOrString& defs, const char* defines_list,
  228. bool dflag = false);
  229. void AppendDefines(BuildObjectListOrString& defs,
  230. std::vector<std::string> const& defines,
  231. bool dflag = false);
  232. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
  233. protected:
  234. const char* GetInstallTargetName() const override { return "install"; }
  235. const char* GetPackageTargetName() const override { return "package"; }
  236. unsigned int XcodeVersion;
  237. std::string VersionString;
  238. std::set<std::string> XCodeObjectIDs;
  239. std::vector<std::unique_ptr<cmXCodeObject>> XCodeObjects;
  240. cmXCodeObject* RootObject;
  241. BuildSystem XcodeBuildSystem = BuildSystem::One;
  242. private:
  243. std::string const& GetXcodeBuildCommand();
  244. std::string FindXcodeBuildCommand();
  245. std::string XcodeBuildCommand;
  246. bool XcodeBuildCommandInitialized;
  247. void PrintCompilerAdvice(std::ostream&, std::string const&,
  248. const char*) const override
  249. {
  250. }
  251. std::string GetObjectsDirectory(const std::string& projName,
  252. const std::string& configName,
  253. const cmGeneratorTarget* t,
  254. const std::string& variant) const;
  255. static std::string GetDeploymentPlatform(const cmMakefile* mf);
  256. void ComputeArchitectures(cmMakefile* mf);
  257. void ComputeObjectDirArch(cmMakefile* mf);
  258. void addObject(std::unique_ptr<cmXCodeObject> obj);
  259. std::string PostBuildMakeTarget(std::string const& tName,
  260. std::string const& configName);
  261. cmXCodeObject* MainGroupChildren;
  262. cmXCodeObject* FrameworkGroup;
  263. cmMakefile* CurrentMakefile;
  264. cmLocalGenerator* CurrentLocalGenerator;
  265. std::vector<std::string> CurrentConfigurationTypes;
  266. std::string CurrentReRunCMakeMakefile;
  267. std::string CurrentXCodeHackMakefile;
  268. std::string CurrentProject;
  269. std::set<std::string> TargetDoneSet;
  270. std::vector<std::string> ProjectSourceDirectoryComponents;
  271. std::vector<std::string> ProjectOutputDirectoryComponents;
  272. std::map<std::string, cmXCodeObject*> GroupMap;
  273. std::map<std::string, cmXCodeObject*> GroupNameMap;
  274. std::map<std::string, cmXCodeObject*> TargetGroup;
  275. std::map<std::string, cmXCodeObject*> FileRefs;
  276. std::map<std::string, cmXCodeObject*> ExternalLibRefs;
  277. std::map<cmGeneratorTarget const*, cmXCodeObject*> XCodeObjectMap;
  278. std::map<cmXCodeObject*, cmXCodeObject*> FileRefToBuildFileMap;
  279. std::vector<std::string> Architectures;
  280. std::string ObjectDirArchDefault;
  281. std::string ObjectDirArch;
  282. std::string SystemName;
  283. std::string GeneratorToolset;
  284. std::vector<std::string> EnabledLangs;
  285. std::map<cmGeneratorTarget const*, std::set<cmSourceFile const*>>
  286. CommandsVisited;
  287. std::map<cmSourceFile const*, std::set<cmGeneratorTarget const*>>
  288. CustomCommandRoots;
  289. };