cmGlobalXCodeGenerator.h 13 KB

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