cmGlobalXCodeGenerator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 UseEffectivePlatformName(cmMakefile* mf) const CM_OVERRIDE;
  78. bool ShouldStripResourcePath(cmMakefile*) const CM_OVERRIDE;
  79. bool SetGeneratorToolset(std::string const& ts, cmMakefile* mf) CM_OVERRIDE;
  80. void AppendFlag(std::string& flags, std::string const& flag);
  81. protected:
  82. void AddExtraIDETargets() CM_OVERRIDE;
  83. void Generate() CM_OVERRIDE;
  84. private:
  85. cmXCodeObject* CreateOrGetPBXGroup(cmGeneratorTarget* gtgt,
  86. cmSourceGroup* sg);
  87. cmXCodeObject* CreatePBXGroup(cmXCodeObject* parent, std::string name);
  88. bool CreateGroups(std::vector<cmLocalGenerator*>& generators);
  89. std::string XCodeEscapePath(const std::string& p);
  90. std::string RelativeToSource(const char* p);
  91. std::string RelativeToBinary(const char* p);
  92. std::string ConvertToRelativeForMake(const char* p);
  93. void CreateCustomCommands(cmXCodeObject* buildPhases,
  94. cmXCodeObject* sourceBuildPhase,
  95. cmXCodeObject* headerBuildPhase,
  96. cmXCodeObject* resourceBuildPhase,
  97. std::vector<cmXCodeObject*> contentBuildPhases,
  98. cmXCodeObject* frameworkBuildPhase,
  99. cmGeneratorTarget* gtgt);
  100. std::string ComputeInfoPListLocation(cmGeneratorTarget* target);
  101. void AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  102. cmGeneratorTarget* target,
  103. std::vector<cmCustomCommand> const& commands,
  104. const char* commandFileName);
  105. void CreateCustomRulesMakefile(const char* makefileBasename,
  106. cmGeneratorTarget* target,
  107. std::vector<cmCustomCommand> const& commands,
  108. const std::string& configName);
  109. cmXCodeObject* FindXCodeTarget(const cmGeneratorTarget*);
  110. std::string GetOrCreateId(const std::string& name, const std::string& id);
  111. // create cmXCodeObject from these functions so that memory can be managed
  112. // correctly. All objects created are stored in this->XCodeObjects.
  113. cmXCodeObject* CreateObject(cmXCodeObject::PBXType ptype);
  114. cmXCodeObject* CreateObject(cmXCodeObject::Type type);
  115. cmXCodeObject* CreateString(const std::string& s);
  116. cmXCodeObject* CreateObjectReference(cmXCodeObject*);
  117. cmXCodeObject* CreateFlatClone(cmXCodeObject*);
  118. cmXCodeObject* CreateXCodeTarget(cmGeneratorTarget* gtgt,
  119. cmXCodeObject* buildPhases);
  120. void ForceLinkerLanguages() CM_OVERRIDE;
  121. void ForceLinkerLanguage(cmGeneratorTarget* gtgt);
  122. const char* GetTargetLinkFlagsVar(const cmGeneratorTarget* target) const;
  123. const char* GetTargetFileType(cmGeneratorTarget* target);
  124. const char* GetTargetProductType(cmGeneratorTarget* target);
  125. std::string AddConfigurations(cmXCodeObject* target,
  126. cmGeneratorTarget* gtgt);
  127. void AppendOrAddBuildSetting(cmXCodeObject* settings, const char* attr,
  128. const char* value);
  129. void AppendBuildSettingAttribute(cmXCodeObject* target, const char* attr,
  130. const char* value,
  131. const std::string& configName);
  132. cmXCodeObject* CreateUtilityTarget(cmGeneratorTarget* gtgt);
  133. void AddDependAndLinkInformation(cmXCodeObject* target);
  134. void CreateBuildSettings(cmGeneratorTarget* gtgt,
  135. cmXCodeObject* buildSettings,
  136. const std::string& buildType);
  137. std::string ExtractFlag(const char* flag, std::string& flags);
  138. std::string ExtractFlagRegex(const char* exp, int matchIndex,
  139. std::string& flags);
  140. void FilterConfigurationAttribute(std::string const& configName,
  141. std::string& attribute);
  142. void SortXCodeObjects();
  143. // delete all objects in the this->XCodeObjects vector.
  144. void ClearXCodeObjects();
  145. bool CreateXCodeObjects(cmLocalGenerator* root,
  146. std::vector<cmLocalGenerator*>& generators);
  147. void OutputXCodeProject(cmLocalGenerator* root,
  148. std::vector<cmLocalGenerator*>& generators);
  149. // Write shared scheme files for all the native targets
  150. void OutputXCodeSharedSchemes(const std::string& xcProjDir);
  151. void OutputXCodeWorkspaceSettings(const std::string& xcProjDir);
  152. void WriteXCodePBXProj(std::ostream& fout, cmLocalGenerator* root,
  153. std::vector<cmLocalGenerator*>& generators);
  154. cmXCodeObject* CreateXCodeFileReferenceFromPath(const std::string& fullpath,
  155. cmGeneratorTarget* target,
  156. const std::string& lang,
  157. cmSourceFile* sf);
  158. cmXCodeObject* CreateXCodeSourceFileFromPath(const std::string& fullpath,
  159. cmGeneratorTarget* target,
  160. const std::string& lang,
  161. cmSourceFile* sf);
  162. cmXCodeObject* CreateXCodeFileReference(cmSourceFile* sf,
  163. cmGeneratorTarget* target);
  164. cmXCodeObject* CreateXCodeSourceFile(cmLocalGenerator* gen, cmSourceFile* sf,
  165. cmGeneratorTarget* gtgt);
  166. bool CreateXCodeTargets(cmLocalGenerator* gen, std::vector<cmXCodeObject*>&);
  167. bool IsHeaderFile(cmSourceFile*);
  168. void AddDependTarget(cmXCodeObject* target, cmXCodeObject* dependTarget);
  169. void CreateXCodeDependHackTarget(std::vector<cmXCodeObject*>& targets);
  170. bool SpecialTargetEmitted(std::string const& tname);
  171. void SetGenerationRoot(cmLocalGenerator* root);
  172. void AddExtraTargets(cmLocalGenerator* root,
  173. std::vector<cmLocalGenerator*>& gens);
  174. cmXCodeObject* CreateBuildPhase(const char* name, const char* name2,
  175. cmGeneratorTarget* target,
  176. const std::vector<cmCustomCommand>&);
  177. void CreateReRunCMakeFile(cmLocalGenerator* root,
  178. std::vector<cmLocalGenerator*> const& gens);
  179. std::string LookupFlags(const std::string& varNamePrefix,
  180. const std::string& varNameLang,
  181. const std::string& varNameSuffix,
  182. const std::string& default_flags);
  183. class Factory;
  184. class BuildObjectListOrString;
  185. friend class BuildObjectListOrString;
  186. void AppendDefines(BuildObjectListOrString& defs, const char* defines_list,
  187. bool dflag = false);
  188. void AppendDefines(BuildObjectListOrString& defs,
  189. std::vector<std::string> const& defines,
  190. bool dflag = false);
  191. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const CM_OVERRIDE;
  192. protected:
  193. const char* GetInstallTargetName() const CM_OVERRIDE { return "install"; }
  194. const char* GetPackageTargetName() const CM_OVERRIDE { return "package"; }
  195. unsigned int XcodeVersion;
  196. std::string VersionString;
  197. std::set<std::string> XCodeObjectIDs;
  198. std::vector<cmXCodeObject*> XCodeObjects;
  199. cmXCodeObject* RootObject;
  200. private:
  201. std::string const& GetXcodeBuildCommand();
  202. std::string FindXcodeBuildCommand();
  203. std::string XcodeBuildCommand;
  204. bool XcodeBuildCommandInitialized;
  205. void PrintCompilerAdvice(std::ostream&, std::string const&,
  206. const char*) const CM_OVERRIDE
  207. {
  208. }
  209. std::string GetObjectsNormalDirectory(const std::string& projName,
  210. const std::string& configName,
  211. const cmGeneratorTarget* t) const;
  212. void ComputeArchitectures(cmMakefile* mf);
  213. void ComputeObjectDirArch();
  214. void addObject(cmXCodeObject* obj);
  215. std::string PostBuildMakeTarget(std::string const& tName,
  216. std::string const& configName);
  217. cmXCodeObject* MainGroupChildren;
  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