cmGlobalGenerator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmGlobalGenerator_h
  11. #define cmGlobalGenerator_h
  12. #include "cmStandardIncludes.h"
  13. #include "cmTarget.h" // For cmTargets
  14. #include "cmTargetDepend.h" // For cmTargetDependSet
  15. class cmake;
  16. class cmMakefile;
  17. class cmLocalGenerator;
  18. class cmExternalMakefileProjectGenerator;
  19. class cmTarget;
  20. class cmTargetExport;
  21. class cmInstallTargetGenerator;
  22. class cmInstallFilesGenerator;
  23. /** \class cmGlobalGenerator
  24. * \brief Responable for overseeing the generation process for the entire tree
  25. *
  26. * Subclasses of this class generate makefiles for various
  27. * platforms.
  28. */
  29. class cmGlobalGenerator
  30. {
  31. public:
  32. ///! Free any memory allocated with the GlobalGenerator
  33. cmGlobalGenerator();
  34. virtual ~cmGlobalGenerator();
  35. ///! Create a local generator appropriate to this Global Generator
  36. virtual cmLocalGenerator *CreateLocalGenerator();
  37. ///! Get the name for this generator
  38. virtual const char *GetName() const { return "Generic"; };
  39. /** Get the documentation entry for this generator. */
  40. virtual void GetDocumentation(cmDocumentationEntry& entry) const;
  41. /**
  42. * Create LocalGenerators and process the CMakeLists files. This does not
  43. * actually produce any makefiles, DSPs, etc.
  44. */
  45. virtual void Configure();
  46. /**
  47. * Generate the all required files for building this project/tree. This
  48. * basically creates a series of LocalGenerators for each directory and
  49. * requests that they Generate.
  50. */
  51. virtual void Generate();
  52. /**
  53. * Set/Get and Clear the enabled languages.
  54. */
  55. void SetLanguageEnabled(const char*, cmMakefile* mf);
  56. bool GetLanguageEnabled(const char*) const;
  57. void ClearEnabledLanguages();
  58. void GetEnabledLanguages(std::vector<std::string>& lang);
  59. /**
  60. * Try to determine system infomation such as shared library
  61. * extension, pthreads, byte order etc.
  62. */
  63. virtual void EnableLanguage(std::vector<std::string>const& languages,
  64. cmMakefile *, bool optional);
  65. /**
  66. * Try to determine system infomation, get it from another generator
  67. */
  68. virtual void EnableLanguagesFromGenerator(cmGlobalGenerator *gen);
  69. /**
  70. * Try running cmake and building a file. This is used for dynalically
  71. * loaded commands, not as part of the usual build process.
  72. */
  73. virtual int TryCompile(const char *srcdir, const char *bindir,
  74. const char *projectName, const char *targetName,
  75. bool fast, std::string *output, cmMakefile* mf);
  76. /**
  77. * Build a file given the following information. This is a more direct call
  78. * that is used by both CTest and TryCompile. If target name is NULL or
  79. * empty then all is assumed. clean indicates if a "make clean" should be
  80. * done first.
  81. */
  82. int Build(const char *srcdir, const char *bindir,
  83. const char *projectName, const char *targetName,
  84. std::string *output,
  85. const char *makeProgram, const char *config,
  86. bool clean, bool fast,
  87. double timeout, bool verbose=false,
  88. const char* extraOptions = 0,
  89. std::vector<std::string> const& nativeOptions =
  90. std::vector<std::string>());
  91. virtual std::string GenerateBuildCommand(
  92. const char* makeProgram,
  93. const char *projectName, const char* additionalOptions,
  94. const char *targetName,
  95. const char* config, bool ignoreErrors, bool fast);
  96. ///! Set the CMake instance
  97. void SetCMakeInstance(cmake *cm);
  98. ///! Get the CMake instance
  99. cmake *GetCMakeInstance() { return this->CMakeInstance; };
  100. void SetConfiguredFilesPath(cmGlobalGenerator* gen);
  101. const std::vector<cmLocalGenerator *>& GetLocalGenerators() const {
  102. return this->LocalGenerators;}
  103. cmLocalGenerator* GetCurrentLocalGenerator()
  104. {return this->CurrentLocalGenerator;}
  105. void SetCurrentLocalGenerator(cmLocalGenerator* lg)
  106. {this->CurrentLocalGenerator = lg;}
  107. void AddLocalGenerator(cmLocalGenerator *lg);
  108. ///! Set an generator for an "external makefile based project"
  109. void SetExternalMakefileProjectGenerator(
  110. cmExternalMakefileProjectGenerator *extraGenerator);
  111. const char* GetExtraGeneratorName() const;
  112. void AddInstallComponent(const char* component);
  113. const std::set<cmStdString>* GetInstallComponents() const
  114. { return &InstallComponents; }
  115. ///! Add one installed target to the sets of the exports
  116. void AddTargetToExports(const char* exportSet, cmTarget* target,
  117. cmInstallTargetGenerator* archive,
  118. cmInstallTargetGenerator* runTime,
  119. cmInstallTargetGenerator* library,
  120. cmInstallTargetGenerator* framework,
  121. cmInstallTargetGenerator* bundle,
  122. cmInstallFilesGenerator* publicHeaders);
  123. ///! Get the export target set with the given name
  124. const std::vector<cmTargetExport*>* GetExportSet(const char* name) const;
  125. /** Add a file to the manifest of generated targets for a configuration. */
  126. void AddToManifest(const char* config, std::string const& f);
  127. void EnableInstallTarget();
  128. int TryCompileTimeout;
  129. bool GetForceUnixPaths() {return this->ForceUnixPaths;}
  130. bool GetToolSupportsColor() { return this->ToolSupportsColor; }
  131. void SetToolSupportsColor(bool enable) { this->ToolSupportsColor = enable; }
  132. ///! return the language for the given extension
  133. const char* GetLanguageFromExtension(const char* ext);
  134. ///! is an extension to be ignored
  135. bool IgnoreFile(const char* ext);
  136. ///! What is the preference for linkers and this language (None or Prefered)
  137. int GetLinkerPreference(const char* lang);
  138. ///! What is the object file extension for a given source file?
  139. const char* GetLanguageOutputExtension(cmSourceFile const&);
  140. ///! What is the configurations directory variable called?
  141. virtual const char* GetCMakeCFGInitDirectory() { return "."; }
  142. /** Get whether the generator should use a script for link commands. */
  143. bool GetUseLinkScript() { return this->UseLinkScript; }
  144. /** Get whether the generator should produce special marks on rules
  145. producing symbolic (non-file) outputs. */
  146. bool GetNeedSymbolicMark() { return this->NeedSymbolicMark; }
  147. /*
  148. * Determine what program to use for building the project.
  149. */
  150. void FindMakeProgram(cmMakefile*);
  151. ///! Find a target by name by searching the local generators.
  152. cmTarget* FindTarget(const char* project, const char* name);
  153. /** Determine if a name resolves to a framework on disk or a built target
  154. that is a framework. */
  155. bool NameResolvesToFramework(const std::string& libname);
  156. /** If check to see if the target is linked to by any other
  157. target in the project */
  158. bool IsDependedOn(const char* project, cmTarget* target);
  159. ///! Find a local generator by its startdirectory
  160. cmLocalGenerator* FindLocalGenerator(const char* start_dir);
  161. /** Append the subdirectory for the given configuration. If anything is
  162. appended the given prefix and suffix will be appended around it, which
  163. is useful for leading or trailing slashes. */
  164. virtual void AppendDirectoryForConfig(const char* prefix,
  165. const char* config,
  166. const char* suffix,
  167. std::string& dir);
  168. /** Get the manifest of all targets that will be built for each
  169. configuration. This is valid during generation only. */
  170. cmTargetManifest const& GetTargetManifest() { return this->TargetManifest; }
  171. /** Get the content of a directory. Directory listings are loaded
  172. from disk at most once and cached. During the generation step
  173. the content will include the target files to be built even if
  174. they do not yet exist. */
  175. std::set<cmStdString> const& GetDirectoryContent(std::string const& dir,
  176. bool needDisk = true);
  177. void AddTarget(cmTargets::value_type &v);
  178. virtual const char* GetAllTargetName() { return "ALL_BUILD"; }
  179. virtual const char* GetInstallTargetName() { return "INSTALL"; }
  180. virtual const char* GetInstallLocalTargetName() { return 0; }
  181. virtual const char* GetInstallStripTargetName() { return 0; }
  182. virtual const char* GetPreinstallTargetName() { return 0; }
  183. virtual const char* GetTestTargetName() { return "RUN_TESTS"; }
  184. virtual const char* GetPackageTargetName() { return "PACKAGE"; }
  185. virtual const char* GetPackageSourceTargetName(){ return 0; }
  186. virtual const char* GetEditCacheTargetName() { return 0; }
  187. virtual const char* GetRebuildCacheTargetName() { return 0; }
  188. virtual const char* GetCleanTargetName() { return 0; }
  189. // Class to track a set of dependencies.
  190. typedef cmTargetDependSet TargetDependSet;
  191. // what targets does the specified target depend on directly
  192. // via a target_link_libraries or add_dependencies
  193. TargetDependSet const& GetTargetDirectDepends(cmTarget & target);
  194. const std::map<cmStdString, std::vector<cmLocalGenerator*> >& GetProjectMap()
  195. const {return this->ProjectMap;}
  196. // track files replaced during a Generate
  197. void FileReplacedDuringGenerate(const std::string& filename);
  198. void GetFilesReplacedDuringGenerate(std::vector<std::string>& filenames);
  199. void AddRuleHash(const std::vector<std::string>& outputs,
  200. std::string const& content);
  201. /** Return whether the given binary directory is unused. */
  202. bool BinaryDirectoryIsNew(const char* dir)
  203. {
  204. return this->BinaryDirectories.insert(dir).second;
  205. }
  206. /** Supported systems creates a GUID for the given name */
  207. virtual void CreateGUID(const char*) {}
  208. /** Return true if the generated build tree may contain multiple builds.
  209. i.e. "Can I build Debug and Release in the same tree?" */
  210. virtual bool IsMultiConfig() { return false; }
  211. protected:
  212. typedef std::vector<cmLocalGenerator*> GeneratorVector;
  213. // for a project collect all its targets by following depend
  214. // information, and also collect all the targets
  215. virtual void GetTargetSets(TargetDependSet& projectTargets,
  216. TargetDependSet& originalTargets,
  217. cmLocalGenerator* root, GeneratorVector const&);
  218. virtual bool IsRootOnlyTarget(cmTarget* target);
  219. void AddTargetDepends(cmTarget* target, TargetDependSet& projectTargets);
  220. void SetLanguageEnabledFlag(const char* l, cmMakefile* mf);
  221. void SetLanguageEnabledMaps(const char* l, cmMakefile* mf);
  222. void FillExtensionToLanguageMap(const char* l, cmMakefile* mf);
  223. virtual bool ComputeTargetDepends();
  224. virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS();
  225. bool CheckTargets();
  226. // Fill the ProjectMap, this must be called after LocalGenerators
  227. // has been populated.
  228. void FillProjectMap();
  229. void CheckLocalGenerators();
  230. bool IsExcluded(cmLocalGenerator* root, cmLocalGenerator* gen);
  231. bool IsExcluded(cmLocalGenerator* root, cmTarget& target);
  232. void FillLocalGeneratorToTargetMap();
  233. void CreateDefaultGlobalTargets(cmTargets* targets);
  234. cmTarget CreateGlobalTarget(const char* name, const char* message,
  235. const cmCustomCommandLines* commandLines,
  236. std::vector<std::string> depends, const char* workingDir,
  237. bool depends_on_all = false);
  238. bool NeedSymbolicMark;
  239. bool UseLinkScript;
  240. bool ForceUnixPaths;
  241. bool ToolSupportsColor;
  242. cmStdString FindMakeProgramFile;
  243. cmStdString ConfiguredFilesPath;
  244. cmake *CMakeInstance;
  245. std::vector<cmLocalGenerator *> LocalGenerators;
  246. cmLocalGenerator* CurrentLocalGenerator;
  247. // map from project name to vector of local generators in that project
  248. std::map<cmStdString, std::vector<cmLocalGenerator*> > ProjectMap;
  249. std::map<cmLocalGenerator*, std::set<cmTarget *> >
  250. LocalGeneratorToTargetMap;
  251. // Set of named installation components requested by the project.
  252. std::set<cmStdString> InstallComponents;
  253. bool InstallTargetEnabled;
  254. // Sets of named target exports
  255. std::map<cmStdString, std::vector<cmTargetExport*> > ExportSets;
  256. void ClearExportSets();
  257. // Manifest of all targets that will be built for each configuration.
  258. // This is computed just before local generators generate.
  259. cmTargetManifest TargetManifest;
  260. // All targets in the entire project.
  261. std::map<cmStdString,cmTarget *> TotalTargets;
  262. virtual const char* GetPredefinedTargetsFolder();
  263. virtual bool UseFolderProperty();
  264. private:
  265. float FirstTimeProgress;
  266. // If you add a new map here, make sure it is copied
  267. // in EnableLanguagesFromGenerator
  268. std::map<cmStdString, bool> IgnoreExtensions;
  269. std::map<cmStdString, bool> LanguageEnabled;
  270. std::map<cmStdString, cmStdString> OutputExtensions;
  271. std::map<cmStdString, cmStdString> LanguageToOutputExtension;
  272. std::map<cmStdString, cmStdString> ExtensionToLanguage;
  273. std::map<cmStdString, int> LanguageToLinkerPreference;
  274. // Record hashes for rules and outputs.
  275. struct RuleHash { char Data[32]; };
  276. std::map<cmStdString, RuleHash> RuleHashes;
  277. void CheckRuleHashes();
  278. void CheckRuleHashes(std::string const& pfile, std::string const& home);
  279. void WriteRuleHashes(std::string const& pfile);
  280. void WriteSummary();
  281. void WriteSummary(cmTarget* target);
  282. cmExternalMakefileProjectGenerator* ExtraGenerator;
  283. // track files replaced during a Generate
  284. std::vector<std::string> FilesReplacedDuringGenerate;
  285. // Store computed inter-target dependencies.
  286. typedef std::map<cmTarget *, TargetDependSet> TargetDependMap;
  287. TargetDependMap TargetDependencies;
  288. // Cache directory content and target files to be built.
  289. struct DirectoryContent: public std::set<cmStdString>
  290. {
  291. typedef std::set<cmStdString> derived;
  292. bool LoadedFromDisk;
  293. DirectoryContent(): LoadedFromDisk(false) {}
  294. DirectoryContent(DirectoryContent const& dc):
  295. derived(dc), LoadedFromDisk(dc.LoadedFromDisk) {}
  296. };
  297. std::map<cmStdString, DirectoryContent> DirectoryContentMap;
  298. // Set of binary directories on disk.
  299. std::set<cmStdString> BinaryDirectories;
  300. };
  301. #endif