cmGlobalXCodeGenerator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. std::vector<GeneratedMakeCommand> GenerateBuildCommand(
  59. const std::string& makeProgram, const std::string& projectName,
  60. const std::string& projectDir, std::vector<std::string> const& targetNames,
  61. const std::string& config, bool fast, int jobs, bool verbose,
  62. std::vector<std::string> const& makeOptions =
  63. std::vector<std::string>()) override;
  64. /** Append the subdirectory for the given configuration. */
  65. void AppendDirectoryForConfig(const std::string& prefix,
  66. const std::string& config,
  67. const std::string& suffix,
  68. std::string& dir) override;
  69. bool FindMakeProgram(cmMakefile*) override;
  70. //! What is the configurations directory variable called?
  71. const char* GetCMakeCFGIntDir() const override;
  72. //! expand CFGIntDir
  73. std::string ExpandCFGIntDir(const std::string& str,
  74. const std::string& config) const override;
  75. void SetCurrentLocalGenerator(cmLocalGenerator*);
  76. /** Return true if the generated build tree may contain multiple builds.
  77. i.e. "Can I build Debug and Release in the same tree?" */
  78. bool IsMultiConfig() const override;
  79. bool IsXcode() const override { return true; }
  80. bool HasKnownObjectFileLocation(std::string* reason) const override;
  81. bool IsIPOSupported() const override { return true; }
  82. bool UseEffectivePlatformName(cmMakefile* mf) const override;
  83. bool ShouldStripResourcePath(cmMakefile*) const override;
  84. bool SetGeneratorToolset(std::string const& ts, bool build,
  85. cmMakefile* mf) override;
  86. void AppendFlag(std::string& flags, std::string const& flag) const;
  87. protected:
  88. void AddExtraIDETargets() override;
  89. void ComputeTargetOrder();
  90. void ComputeTargetOrder(cmGeneratorTarget const* gt, size_t& index);
  91. void Generate() override;
  92. private:
  93. cmXCodeObject* CreateOrGetPBXGroup(cmGeneratorTarget* gtgt,
  94. cmSourceGroup* sg);
  95. cmXCodeObject* CreatePBXGroup(cmXCodeObject* parent,
  96. const std::string& name);
  97. bool CreateGroups(std::vector<cmLocalGenerator*>& generators);
  98. std::string XCodeEscapePath(const std::string& p);
  99. std::string RelativeToSource(const std::string& p);
  100. std::string RelativeToBinary(const std::string& p);
  101. std::string ConvertToRelativeForMake(std::string const& p);
  102. void CreateCustomCommands(
  103. cmXCodeObject* buildPhases, cmXCodeObject* sourceBuildPhase,
  104. cmXCodeObject* headerBuildPhase, cmXCodeObject* resourceBuildPhase,
  105. std::vector<cmXCodeObject*> const& contentBuildPhases,
  106. cmXCodeObject* frameworkBuildPhase, cmGeneratorTarget* gtgt);
  107. std::string ComputeInfoPListLocation(cmGeneratorTarget* target);
  108. void AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  109. cmGeneratorTarget* target,
  110. std::vector<cmCustomCommand> const& commands,
  111. const char* commandFileName);
  112. void CreateCustomRulesMakefile(const char* makefileBasename,
  113. cmGeneratorTarget* target,
  114. std::vector<cmCustomCommand> const& commands,
  115. const std::string& configName);
  116. cmXCodeObject* FindXCodeTarget(const cmGeneratorTarget*);
  117. std::string GetOrCreateId(const std::string& name, const std::string& id);
  118. // create cmXCodeObject from these functions so that memory can be managed
  119. // correctly. All objects created are stored in this->XCodeObjects.
  120. cmXCodeObject* CreateObject(cmXCodeObject::PBXType ptype);
  121. cmXCodeObject* CreateObject(cmXCodeObject::Type type);
  122. cmXCodeObject* CreateString(const std::string& s);
  123. cmXCodeObject* CreateObjectReference(cmXCodeObject*);
  124. cmXCodeObject* CreateFlatClone(cmXCodeObject*);
  125. cmXCodeObject* CreateXCodeTarget(cmGeneratorTarget* gtgt,
  126. cmXCodeObject* buildPhases);
  127. void ForceLinkerLanguages() override;
  128. void ForceLinkerLanguage(cmGeneratorTarget* gtgt);
  129. const char* GetTargetLinkFlagsVar(const cmGeneratorTarget* target) const;
  130. const char* GetTargetFileType(cmGeneratorTarget* target);
  131. const char* GetTargetProductType(cmGeneratorTarget* target);
  132. std::string AddConfigurations(cmXCodeObject* target,
  133. cmGeneratorTarget* gtgt);
  134. void AppendOrAddBuildSetting(cmXCodeObject* settings, const char* attr,
  135. const char* value);
  136. void AppendBuildSettingAttribute(cmXCodeObject* target, const char* attr,
  137. const char* value,
  138. const std::string& configName);
  139. cmXCodeObject* CreateUtilityTarget(cmGeneratorTarget* gtgt);
  140. void AddDependAndLinkInformation(cmXCodeObject* target);
  141. void AddPositionIndependentLinkAttribute(cmGeneratorTarget* target,
  142. cmXCodeObject* buildSettings,
  143. const std::string& configName);
  144. void CreateBuildSettings(cmGeneratorTarget* gtgt,
  145. cmXCodeObject* buildSettings,
  146. const std::string& buildType);
  147. std::string ExtractFlag(const char* flag, std::string& flags);
  148. std::string ExtractFlagRegex(const char* exp, int matchIndex,
  149. std::string& flags);
  150. void FilterConfigurationAttribute(std::string const& configName,
  151. std::string& attribute);
  152. void SortXCodeObjects();
  153. // delete all objects in the this->XCodeObjects vector.
  154. void ClearXCodeObjects();
  155. bool CreateXCodeObjects(cmLocalGenerator* root,
  156. std::vector<cmLocalGenerator*>& generators);
  157. void OutputXCodeProject(cmLocalGenerator* root,
  158. std::vector<cmLocalGenerator*>& generators);
  159. // Write shared scheme files for all the native targets
  160. // return true if any were written
  161. bool OutputXCodeSharedSchemes(const std::string& xcProjDir,
  162. cmLocalGenerator* root);
  163. void OutputXCodeWorkspaceSettings(const std::string& xcProjDir,
  164. bool hasGeneratedSchemes);
  165. void WriteXCodePBXProj(std::ostream& fout, cmLocalGenerator* root,
  166. std::vector<cmLocalGenerator*>& generators);
  167. cmXCodeObject* CreateXCodeFileReferenceFromPath(const std::string& fullpath,
  168. cmGeneratorTarget* target,
  169. const std::string& lang,
  170. cmSourceFile* sf);
  171. cmXCodeObject* CreateXCodeSourceFileFromPath(const std::string& fullpath,
  172. cmGeneratorTarget* target,
  173. const std::string& lang,
  174. cmSourceFile* sf);
  175. cmXCodeObject* CreateXCodeFileReference(cmSourceFile* sf,
  176. cmGeneratorTarget* target);
  177. cmXCodeObject* CreateXCodeSourceFile(cmLocalGenerator* gen, cmSourceFile* sf,
  178. cmGeneratorTarget* gtgt);
  179. void AddXCodeProjBuildRule(cmGeneratorTarget* target,
  180. std::vector<cmSourceFile*>& sources) const;
  181. bool CreateXCodeTargets(cmLocalGenerator* gen, std::vector<cmXCodeObject*>&);
  182. bool CreateXCodeTarget(cmGeneratorTarget* gtgt,
  183. std::vector<cmXCodeObject*>&);
  184. bool IsHeaderFile(cmSourceFile*);
  185. void AddDependTarget(cmXCodeObject* target, cmXCodeObject* dependTarget);
  186. void CreateXCodeDependHackTarget(std::vector<cmXCodeObject*>& targets);
  187. bool SpecialTargetEmitted(std::string const& tname);
  188. void SetGenerationRoot(cmLocalGenerator* root);
  189. void AddExtraTargets(cmLocalGenerator* root,
  190. std::vector<cmLocalGenerator*>& gens);
  191. cmXCodeObject* CreateBuildPhase(const char* name, const char* name2,
  192. cmGeneratorTarget* target,
  193. const std::vector<cmCustomCommand>&);
  194. void CreateReRunCMakeFile(cmLocalGenerator* root,
  195. std::vector<cmLocalGenerator*> const& gens);
  196. std::string LookupFlags(const std::string& varNamePrefix,
  197. const std::string& varNameLang,
  198. const std::string& varNameSuffix,
  199. const std::string& default_flags);
  200. class Factory;
  201. class BuildObjectListOrString;
  202. friend class BuildObjectListOrString;
  203. void AppendDefines(BuildObjectListOrString& defs, const char* defines_list,
  204. bool dflag = false);
  205. void AppendDefines(BuildObjectListOrString& defs,
  206. std::vector<std::string> const& defines,
  207. bool dflag = false);
  208. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
  209. protected:
  210. const char* GetInstallTargetName() const override { return "install"; }
  211. const char* GetPackageTargetName() const override { return "package"; }
  212. unsigned int XcodeVersion;
  213. std::string VersionString;
  214. std::set<std::string> XCodeObjectIDs;
  215. std::vector<cmXCodeObject*> XCodeObjects;
  216. cmXCodeObject* RootObject;
  217. private:
  218. std::string const& GetXcodeBuildCommand();
  219. std::string FindXcodeBuildCommand();
  220. std::string XcodeBuildCommand;
  221. bool XcodeBuildCommandInitialized;
  222. void PrintCompilerAdvice(std::ostream&, std::string const&,
  223. const char*) const override
  224. {
  225. }
  226. std::string GetObjectsDirectory(const std::string& projName,
  227. const std::string& configName,
  228. const cmGeneratorTarget* t,
  229. const std::string& variant) const;
  230. static std::string GetDeploymentPlatform(const cmMakefile* mf);
  231. void ComputeArchitectures(cmMakefile* mf);
  232. void ComputeObjectDirArch(cmMakefile* mf);
  233. void addObject(cmXCodeObject* obj);
  234. std::string PostBuildMakeTarget(std::string const& tName,
  235. std::string const& configName);
  236. cmXCodeObject* MainGroupChildren;
  237. cmMakefile* CurrentMakefile;
  238. cmLocalGenerator* CurrentLocalGenerator;
  239. std::vector<std::string> CurrentConfigurationTypes;
  240. std::string CurrentReRunCMakeMakefile;
  241. std::string CurrentXCodeHackMakefile;
  242. std::string CurrentProject;
  243. std::set<std::string> TargetDoneSet;
  244. std::vector<std::string> ProjectSourceDirectoryComponents;
  245. std::vector<std::string> ProjectOutputDirectoryComponents;
  246. std::map<std::string, cmXCodeObject*> GroupMap;
  247. std::map<std::string, cmXCodeObject*> GroupNameMap;
  248. std::map<std::string, cmXCodeObject*> TargetGroup;
  249. std::map<std::string, cmXCodeObject*> FileRefs;
  250. std::map<cmGeneratorTarget const*, cmXCodeObject*> XCodeObjectMap;
  251. std::vector<std::string> Architectures;
  252. std::string ObjectDirArchDefault;
  253. std::string ObjectDirArch;
  254. std::string GeneratorToolset;
  255. std::map<cmGeneratorTarget const*, size_t> TargetOrderIndex;
  256. };
  257. #endif