cmGlobalXCodeGenerator.h 15 KB

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