cmGlobalXCodeGenerator.h 12 KB

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