cmGlobalXCodeGenerator.h 12 KB

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