cmGlobalXCodeGenerator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmGlobalXCodeGenerator_h
  4. #define cmGlobalXCodeGenerator_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. #include "cmGlobalGenerator.h"
  13. #include "cmXCodeObject.h"
  14. class cmCustomCommand;
  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 UNIX 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. protected:
  94. void AddExtraIDETargets() override;
  95. void ComputeTargetOrder();
  96. void ComputeTargetOrder(cmGeneratorTarget const* gt, size_t& index);
  97. void Generate() override;
  98. private:
  99. cmXCodeObject* CreateOrGetPBXGroup(cmGeneratorTarget* gtgt,
  100. cmSourceGroup* sg);
  101. cmXCodeObject* CreatePBXGroup(cmXCodeObject* parent,
  102. const std::string& name);
  103. bool CreateGroups(std::vector<cmLocalGenerator*>& generators);
  104. std::string XCodeEscapePath(const std::string& p);
  105. std::string RelativeToSource(const std::string& p);
  106. std::string RelativeToBinary(const std::string& p);
  107. std::string ConvertToRelativeForMake(std::string const& p);
  108. void CreateCustomCommands(
  109. cmXCodeObject* buildPhases, cmXCodeObject* sourceBuildPhase,
  110. cmXCodeObject* headerBuildPhase, cmXCodeObject* resourceBuildPhase,
  111. std::vector<cmXCodeObject*> const& contentBuildPhases,
  112. cmXCodeObject* frameworkBuildPhase, cmGeneratorTarget* gtgt);
  113. std::string ComputeInfoPListLocation(cmGeneratorTarget* target);
  114. void AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  115. cmGeneratorTarget* target,
  116. std::vector<cmCustomCommand> const& commands,
  117. const char* commandFileName);
  118. void CreateCustomRulesMakefile(const char* makefileBasename,
  119. cmGeneratorTarget* target,
  120. std::vector<cmCustomCommand> const& commands,
  121. const std::string& configName);
  122. cmXCodeObject* FindXCodeTarget(const cmGeneratorTarget*);
  123. std::string GetOrCreateId(const std::string& name, const std::string& id);
  124. // create cmXCodeObject from these functions so that memory can be managed
  125. // correctly. All objects created are stored in this->XCodeObjects.
  126. cmXCodeObject* CreateObject(cmXCodeObject::PBXType ptype);
  127. cmXCodeObject* CreateObject(cmXCodeObject::Type type);
  128. cmXCodeObject* CreateString(const std::string& s);
  129. cmXCodeObject* CreateObjectReference(cmXCodeObject*);
  130. cmXCodeObject* CreateFlatClone(cmXCodeObject*);
  131. cmXCodeObject* CreateXCodeTarget(cmGeneratorTarget* gtgt,
  132. cmXCodeObject* buildPhases);
  133. void ForceLinkerLanguages() override;
  134. void ForceLinkerLanguage(cmGeneratorTarget* gtgt);
  135. const char* GetTargetLinkFlagsVar(const cmGeneratorTarget* target) const;
  136. const char* GetTargetFileType(cmGeneratorTarget* target);
  137. const char* GetTargetProductType(cmGeneratorTarget* target);
  138. std::string AddConfigurations(cmXCodeObject* target,
  139. cmGeneratorTarget* gtgt);
  140. void AppendOrAddBuildSetting(cmXCodeObject* settings, const char* attr,
  141. const char* value);
  142. void AppendBuildSettingAttribute(cmXCodeObject* target, const char* attr,
  143. const char* value,
  144. const std::string& configName);
  145. cmXCodeObject* CreateUtilityTarget(cmGeneratorTarget* gtgt);
  146. void AddDependAndLinkInformation(cmXCodeObject* target);
  147. void AddPositionIndependentLinkAttribute(cmGeneratorTarget* target,
  148. cmXCodeObject* buildSettings,
  149. const std::string& configName);
  150. void CreateBuildSettings(cmGeneratorTarget* gtgt,
  151. cmXCodeObject* buildSettings,
  152. const std::string& buildType);
  153. std::string ExtractFlag(const char* flag, std::string& flags);
  154. std::string ExtractFlagRegex(const char* exp, int matchIndex,
  155. std::string& flags);
  156. void FilterConfigurationAttribute(std::string const& configName,
  157. std::string& attribute);
  158. void SortXCodeObjects();
  159. // delete all objects in the this->XCodeObjects vector.
  160. void ClearXCodeObjects();
  161. bool CreateXCodeObjects(cmLocalGenerator* root,
  162. std::vector<cmLocalGenerator*>& generators);
  163. void OutputXCodeProject(cmLocalGenerator* root,
  164. std::vector<cmLocalGenerator*>& generators);
  165. // Write shared scheme files for all the native targets
  166. // return true if any were written
  167. bool OutputXCodeSharedSchemes(const std::string& xcProjDir,
  168. cmLocalGenerator* root);
  169. void OutputXCodeWorkspaceSettings(const std::string& xcProjDir,
  170. bool hasGeneratedSchemes);
  171. void WriteXCodePBXProj(std::ostream& fout, cmLocalGenerator* root,
  172. std::vector<cmLocalGenerator*>& generators);
  173. cmXCodeObject* CreateXCodeFileReferenceFromPath(const std::string& fullpath,
  174. cmGeneratorTarget* target,
  175. const std::string& lang,
  176. cmSourceFile* sf);
  177. cmXCodeObject* CreateXCodeSourceFileFromPath(const std::string& fullpath,
  178. cmGeneratorTarget* target,
  179. const std::string& lang,
  180. cmSourceFile* sf);
  181. cmXCodeObject* CreateXCodeFileReference(cmSourceFile* sf,
  182. cmGeneratorTarget* target);
  183. cmXCodeObject* CreateXCodeSourceFile(cmLocalGenerator* gen, cmSourceFile* sf,
  184. cmGeneratorTarget* gtgt);
  185. void AddXCodeProjBuildRule(cmGeneratorTarget* target,
  186. std::vector<cmSourceFile*>& sources) const;
  187. bool CreateXCodeTargets(cmLocalGenerator* gen, std::vector<cmXCodeObject*>&);
  188. bool CreateXCodeTarget(cmGeneratorTarget* gtgt,
  189. std::vector<cmXCodeObject*>&);
  190. bool IsHeaderFile(cmSourceFile*);
  191. void AddDependTarget(cmXCodeObject* target, cmXCodeObject* dependTarget);
  192. void CreateXCodeDependHackTarget(std::vector<cmXCodeObject*>& targets);
  193. bool SpecialTargetEmitted(std::string const& tname);
  194. void SetGenerationRoot(cmLocalGenerator* root);
  195. void AddExtraTargets(cmLocalGenerator* root,
  196. std::vector<cmLocalGenerator*>& gens);
  197. cmXCodeObject* CreateBuildPhase(const char* name, const char* name2,
  198. cmGeneratorTarget* target,
  199. const std::vector<cmCustomCommand>&);
  200. void CreateReRunCMakeFile(cmLocalGenerator* root,
  201. std::vector<cmLocalGenerator*> const& gens);
  202. std::string LookupFlags(const std::string& varNamePrefix,
  203. const std::string& varNameLang,
  204. const std::string& varNameSuffix,
  205. const std::string& default_flags);
  206. class Factory;
  207. class BuildObjectListOrString;
  208. friend class BuildObjectListOrString;
  209. void AppendDefines(BuildObjectListOrString& defs, const char* defines_list,
  210. bool dflag = false);
  211. void AppendDefines(BuildObjectListOrString& defs,
  212. std::vector<std::string> const& defines,
  213. bool dflag = false);
  214. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
  215. protected:
  216. const char* GetInstallTargetName() const override { return "install"; }
  217. const char* GetPackageTargetName() const override { return "package"; }
  218. unsigned int XcodeVersion;
  219. std::string VersionString;
  220. std::set<std::string> XCodeObjectIDs;
  221. std::vector<std::unique_ptr<cmXCodeObject>> XCodeObjects;
  222. cmXCodeObject* RootObject;
  223. private:
  224. std::string const& GetXcodeBuildCommand();
  225. std::string FindXcodeBuildCommand();
  226. std::string XcodeBuildCommand;
  227. bool XcodeBuildCommandInitialized;
  228. void PrintCompilerAdvice(std::ostream&, std::string const&,
  229. const char*) const override
  230. {
  231. }
  232. std::string GetObjectsDirectory(const std::string& projName,
  233. const std::string& configName,
  234. const cmGeneratorTarget* t,
  235. const std::string& variant) const;
  236. static std::string GetDeploymentPlatform(const cmMakefile* mf);
  237. void ComputeArchitectures(cmMakefile* mf);
  238. void ComputeObjectDirArch(cmMakefile* mf);
  239. void addObject(std::unique_ptr<cmXCodeObject> obj);
  240. std::string PostBuildMakeTarget(std::string const& tName,
  241. std::string const& configName);
  242. cmXCodeObject* MainGroupChildren;
  243. cmMakefile* CurrentMakefile;
  244. cmLocalGenerator* CurrentLocalGenerator;
  245. std::vector<std::string> CurrentConfigurationTypes;
  246. std::string CurrentReRunCMakeMakefile;
  247. std::string CurrentXCodeHackMakefile;
  248. std::string CurrentProject;
  249. std::set<std::string> TargetDoneSet;
  250. std::vector<std::string> ProjectSourceDirectoryComponents;
  251. std::vector<std::string> ProjectOutputDirectoryComponents;
  252. std::map<std::string, cmXCodeObject*> GroupMap;
  253. std::map<std::string, cmXCodeObject*> GroupNameMap;
  254. std::map<std::string, cmXCodeObject*> TargetGroup;
  255. std::map<std::string, cmXCodeObject*> FileRefs;
  256. std::map<cmGeneratorTarget const*, cmXCodeObject*> XCodeObjectMap;
  257. std::vector<std::string> Architectures;
  258. std::string ObjectDirArchDefault;
  259. std::string ObjectDirArch;
  260. std::string SystemName;
  261. std::string GeneratorToolset;
  262. std::map<cmGeneratorTarget const*, size_t> TargetOrderIndex;
  263. };
  264. #endif