cmGlobalGenerator.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #include "cmSystemTools.h" // for cmSystemTools::OutputOption
  16. #include "cmExportSetMap.h" // For cmExportSetMap
  17. #include "cmGeneratorTarget.h"
  18. #include "cmGeneratorExpression.h"
  19. #if defined(CMAKE_BUILD_WITH_CMAKE)
  20. # include <cmsys/hash_map.hxx>
  21. #endif
  22. class cmake;
  23. class cmGeneratorTarget;
  24. class cmGeneratorExpressionEvaluationFile;
  25. class cmMakefile;
  26. class cmLocalGenerator;
  27. class cmExternalMakefileProjectGenerator;
  28. class cmTarget;
  29. class cmInstallTargetGenerator;
  30. class cmInstallFilesGenerator;
  31. class cmExportBuildFileGenerator;
  32. class cmQtAutoGenerators;
  33. /** \class cmGlobalGenerator
  34. * \brief Responable for overseeing the generation process for the entire tree
  35. *
  36. * Subclasses of this class generate makefiles for various
  37. * platforms.
  38. */
  39. class cmGlobalGenerator
  40. {
  41. public:
  42. ///! Free any memory allocated with the GlobalGenerator
  43. cmGlobalGenerator();
  44. virtual ~cmGlobalGenerator();
  45. ///! Create a local generator appropriate to this Global Generator
  46. virtual cmLocalGenerator *CreateLocalGenerator();
  47. ///! Get the name for this generator
  48. virtual std::string GetName() const { return "Generic"; }
  49. /** Check whether the given name matches the current generator. */
  50. virtual bool MatchesGeneratorName(const std::string& name) const
  51. { return this->GetName() == name; }
  52. /** Set the generator-specific toolset name. Returns true if toolset
  53. is supported and false otherwise. */
  54. virtual bool SetGeneratorToolset(std::string const& ts);
  55. /**
  56. * Create LocalGenerators and process the CMakeLists files. This does not
  57. * actually produce any makefiles, DSPs, etc.
  58. */
  59. virtual void Configure();
  60. /**
  61. * Generate the all required files for building this project/tree. This
  62. * basically creates a series of LocalGenerators for each directory and
  63. * requests that they Generate.
  64. */
  65. virtual void Generate();
  66. /**
  67. * Set/Get and Clear the enabled languages.
  68. */
  69. void SetLanguageEnabled(const std::string&, cmMakefile* mf);
  70. bool GetLanguageEnabled(const std::string&) const;
  71. void ClearEnabledLanguages();
  72. void GetEnabledLanguages(std::vector<std::string>& lang) const;
  73. /**
  74. * Try to determine system infomation such as shared library
  75. * extension, pthreads, byte order etc.
  76. */
  77. virtual void EnableLanguage(std::vector<std::string>const& languages,
  78. cmMakefile *, bool optional);
  79. /**
  80. * Resolve the CMAKE_<lang>_COMPILER setting for the given language.
  81. * Intended to be called from EnableLanguage.
  82. */
  83. void ResolveLanguageCompiler(const std::string &lang, cmMakefile *mf,
  84. bool optional) const;
  85. /**
  86. * Try to determine system infomation, get it from another generator
  87. */
  88. virtual void EnableLanguagesFromGenerator(cmGlobalGenerator *gen,
  89. cmMakefile* mf);
  90. /**
  91. * Try running cmake and building a file. This is used for dynamically
  92. * loaded commands, not as part of the usual build process.
  93. */
  94. virtual int TryCompile(const std::string& srcdir, const std::string& bindir,
  95. const std::string& projectName,
  96. const std::string& targetName,
  97. bool fast, std::string *output, cmMakefile* mf);
  98. /**
  99. * Build a file given the following information. This is a more direct call
  100. * that is used by both CTest and TryCompile. If target name is NULL or
  101. * empty then all is assumed. clean indicates if a "make clean" should be
  102. * done first.
  103. */
  104. int Build(const std::string& srcdir, const std::string& bindir,
  105. const std::string& projectName, const std::string& targetName,
  106. std::string *output,
  107. const std::string& makeProgram, const std::string& config,
  108. bool clean, bool fast,
  109. double timeout,
  110. cmSystemTools::OutputOption outputflag=cmSystemTools::OUTPUT_NONE,
  111. std::vector<std::string> const& nativeOptions =
  112. std::vector<std::string>());
  113. virtual void GenerateBuildCommand(
  114. std::vector<std::string>& makeCommand,
  115. const std::string& makeProgram,
  116. const std::string& projectName, const std::string& projectDir,
  117. const std::string& targetName, const std::string& config, bool fast,
  118. std::vector<std::string> const& makeOptions = std::vector<std::string>()
  119. );
  120. /** Generate a "cmake --build" call for a given target and config. */
  121. std::string GenerateCMakeBuildCommand(const std::string& target,
  122. const std::string& config,
  123. const std::string& native,
  124. bool ignoreErrors);
  125. ///! Set the CMake instance
  126. void SetCMakeInstance(cmake *cm);
  127. ///! Get the CMake instance
  128. cmake *GetCMakeInstance() const { return this->CMakeInstance; }
  129. void SetConfiguredFilesPath(cmGlobalGenerator* gen);
  130. const std::vector<cmLocalGenerator *>& GetLocalGenerators() const {
  131. return this->LocalGenerators;}
  132. cmLocalGenerator* GetCurrentLocalGenerator()
  133. {return this->CurrentLocalGenerator;}
  134. void SetCurrentLocalGenerator(cmLocalGenerator* lg)
  135. {this->CurrentLocalGenerator = lg;}
  136. void AddLocalGenerator(cmLocalGenerator *lg);
  137. ///! Set an generator for an "external makefile based project"
  138. void SetExternalMakefileProjectGenerator(
  139. cmExternalMakefileProjectGenerator *extraGenerator);
  140. std::string GetExtraGeneratorName() const;
  141. void AddInstallComponent(const char* component);
  142. const std::set<std::string>* GetInstallComponents() const
  143. { return &this->InstallComponents; }
  144. cmExportSetMap& GetExportSets() {return this->ExportSets;}
  145. /** Add a file to the manifest of generated targets for a configuration. */
  146. void AddToManifest(const std::string& config, std::string const& f);
  147. void EnableInstallTarget();
  148. int TryCompileTimeout;
  149. bool GetForceUnixPaths() const { return this->ForceUnixPaths; }
  150. bool GetToolSupportsColor() const { return this->ToolSupportsColor; }
  151. ///! return the language for the given extension
  152. std::string GetLanguageFromExtension(const char* ext) const;
  153. ///! is an extension to be ignored
  154. bool IgnoreFile(const char* ext) const;
  155. ///! What is the preference for linkers and this language (None or Prefered)
  156. int GetLinkerPreference(const std::string& lang) const;
  157. ///! What is the object file extension for a given source file?
  158. std::string GetLanguageOutputExtension(cmSourceFile const&) const;
  159. ///! What is the configurations directory variable called?
  160. virtual const char* GetCMakeCFGIntDir() const { return "."; }
  161. ///! expand CFGIntDir for a configuration
  162. virtual std::string ExpandCFGIntDir(const std::string& str,
  163. const std::string& config) const;
  164. /** Get whether the generator should use a script for link commands. */
  165. bool GetUseLinkScript() const { return this->UseLinkScript; }
  166. /** Get whether the generator should produce special marks on rules
  167. producing symbolic (non-file) outputs. */
  168. bool GetNeedSymbolicMark() const { return this->NeedSymbolicMark; }
  169. /*
  170. * Determine what program to use for building the project.
  171. */
  172. virtual void FindMakeProgram(cmMakefile*);
  173. ///! Find a target by name by searching the local generators.
  174. cmTarget* FindTarget(const std::string& name,
  175. bool excludeAliases = false) const;
  176. void AddAlias(const std::string& name, cmTarget *tgt);
  177. bool IsAlias(const std::string& name) const;
  178. /** Determine if a name resolves to a framework on disk or a built target
  179. that is a framework. */
  180. bool NameResolvesToFramework(const std::string& libname) const;
  181. /** If check to see if the target is linked to by any other
  182. target in the project */
  183. bool IsDependedOn(const std::string& project, cmTarget const* target);
  184. ///! Find a local generator by its startdirectory
  185. cmLocalGenerator* FindLocalGenerator(const std::string& start_dir) const;
  186. /** Append the subdirectory for the given configuration. If anything is
  187. appended the given prefix and suffix will be appended around it, which
  188. is useful for leading or trailing slashes. */
  189. virtual void AppendDirectoryForConfig(const std::string& prefix,
  190. const std::string& config,
  191. const std::string& suffix,
  192. std::string& dir);
  193. /** Get the manifest of all targets that will be built for each
  194. configuration. This is valid during generation only. */
  195. cmTargetManifest const& GetTargetManifest() const
  196. { return this->TargetManifest; }
  197. /** Get the content of a directory. Directory listings are loaded
  198. from disk at most once and cached. During the generation step
  199. the content will include the target files to be built even if
  200. they do not yet exist. */
  201. std::set<std::string> const& GetDirectoryContent(std::string const& dir,
  202. bool needDisk = true);
  203. void AddTarget(cmTarget* t);
  204. static bool IsReservedTarget(std::string const& name);
  205. virtual const char* GetAllTargetName() const { return "ALL_BUILD"; }
  206. virtual const char* GetInstallTargetName() const { return "INSTALL"; }
  207. virtual const char* GetInstallLocalTargetName() const { return 0; }
  208. virtual const char* GetInstallStripTargetName() const { return 0; }
  209. virtual const char* GetPreinstallTargetName() const { return 0; }
  210. virtual const char* GetTestTargetName() const { return "RUN_TESTS"; }
  211. virtual const char* GetPackageTargetName() const { return "PACKAGE"; }
  212. virtual const char* GetPackageSourceTargetName() const { return 0; }
  213. virtual const char* GetEditCacheTargetName() const { return 0; }
  214. virtual const char* GetRebuildCacheTargetName() const { return 0; }
  215. virtual const char* GetCleanTargetName() const { return 0; }
  216. // Lookup edit_cache target command preferred by this generator.
  217. virtual std::string GetEditCacheCommand() const { return ""; }
  218. // Class to track a set of dependencies.
  219. typedef cmTargetDependSet TargetDependSet;
  220. // what targets does the specified target depend on directly
  221. // via a target_link_libraries or add_dependencies
  222. TargetDependSet const& GetTargetDirectDepends(cmTarget const& target);
  223. /** Get per-target generator information. */
  224. cmGeneratorTarget* GetGeneratorTarget(cmTarget const*) const;
  225. const std::map<std::string, std::vector<cmLocalGenerator*> >& GetProjectMap()
  226. const {return this->ProjectMap;}
  227. // track files replaced during a Generate
  228. void FileReplacedDuringGenerate(const std::string& filename);
  229. void GetFilesReplacedDuringGenerate(std::vector<std::string>& filenames);
  230. void AddRuleHash(const std::vector<std::string>& outputs,
  231. std::string const& content);
  232. /** Return whether the given binary directory is unused. */
  233. bool BinaryDirectoryIsNew(const std::string& dir)
  234. {
  235. return this->BinaryDirectories.insert(dir).second;
  236. }
  237. /** Supported systems creates a GUID for the given name */
  238. virtual void CreateGUID(const std::string&) {}
  239. /** Return true if the generated build tree may contain multiple builds.
  240. i.e. "Can I build Debug and Release in the same tree?" */
  241. virtual bool IsMultiConfig() { return false; }
  242. std::string GetSharedLibFlagsForLanguage(std::string const& lang) const;
  243. /** Generate an <output>.rule file path for a given command output. */
  244. virtual std::string GenerateRuleFile(std::string const& output) const;
  245. static std::string EscapeJSON(const std::string& s);
  246. void AddEvaluationFile(const std::string &inputFile,
  247. cmsys::auto_ptr<cmCompiledGeneratorExpression> outputName,
  248. cmMakefile *makefile,
  249. cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
  250. bool inputIsContent);
  251. void ProcessEvaluationFiles();
  252. std::map<std::string, cmExportBuildFileGenerator*>& GetBuildExportSets()
  253. {return this->BuildExportSets;}
  254. void AddBuildExportSet(cmExportBuildFileGenerator*);
  255. void AddBuildExportExportSet(cmExportBuildFileGenerator*);
  256. bool IsExportedTargetsFile(const std::string &filename) const;
  257. bool GenerateImportFile(const std::string &file);
  258. cmExportBuildFileGenerator*
  259. GetExportedTargetsFile(const std::string &filename) const;
  260. void AddCMP0042WarnTarget(const std::string& target);
  261. virtual void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const;
  262. protected:
  263. typedef std::vector<cmLocalGenerator*> GeneratorVector;
  264. // for a project collect all its targets by following depend
  265. // information, and also collect all the targets
  266. virtual void GetTargetSets(TargetDependSet& projectTargets,
  267. TargetDependSet& originalTargets,
  268. cmLocalGenerator* root, GeneratorVector const&);
  269. bool IsRootOnlyTarget(cmTarget* target) const;
  270. void AddTargetDepends(cmTarget const* target,
  271. TargetDependSet& projectTargets);
  272. void SetLanguageEnabledFlag(const std::string& l, cmMakefile* mf);
  273. void SetLanguageEnabledMaps(const std::string& l, cmMakefile* mf);
  274. void FillExtensionToLanguageMap(const std::string& l, cmMakefile* mf);
  275. virtual bool ComputeTargetDepends();
  276. virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const;
  277. typedef std::vector<std::pair<cmQtAutoGenerators,
  278. cmTarget const*> > AutogensType;
  279. void CreateQtAutoGeneratorsTargets(AutogensType& autogens);
  280. std::string SelectMakeProgram(const std::string& makeProgram,
  281. const std::string& makeDefault = "") const;
  282. // Fill the ProjectMap, this must be called after LocalGenerators
  283. // has been populated.
  284. void FillProjectMap();
  285. void CheckLocalGenerators();
  286. bool IsExcluded(cmLocalGenerator* root, cmLocalGenerator* gen) const;
  287. bool IsExcluded(cmLocalGenerator* root, cmTarget const& target) const;
  288. void FillLocalGeneratorToTargetMap();
  289. void CreateDefaultGlobalTargets(cmTargets* targets);
  290. cmTarget CreateGlobalTarget(const std::string& name, const char* message,
  291. const cmCustomCommandLines* commandLines,
  292. std::vector<std::string> depends, const char* workingDir);
  293. bool NeedSymbolicMark;
  294. bool UseLinkScript;
  295. bool ForceUnixPaths;
  296. bool ToolSupportsColor;
  297. std::string FindMakeProgramFile;
  298. std::string ConfiguredFilesPath;
  299. cmake *CMakeInstance;
  300. std::vector<cmLocalGenerator *> LocalGenerators;
  301. cmLocalGenerator* CurrentLocalGenerator;
  302. // map from project name to vector of local generators in that project
  303. std::map<std::string, std::vector<cmLocalGenerator*> > ProjectMap;
  304. std::map<cmLocalGenerator*, std::set<cmTarget const*> >
  305. LocalGeneratorToTargetMap;
  306. // Set of named installation components requested by the project.
  307. std::set<std::string> InstallComponents;
  308. bool InstallTargetEnabled;
  309. // Sets of named target exports
  310. cmExportSetMap ExportSets;
  311. std::map<std::string, cmExportBuildFileGenerator*> BuildExportSets;
  312. std::map<std::string, cmExportBuildFileGenerator*> BuildExportExportSets;
  313. // Manifest of all targets that will be built for each configuration.
  314. // This is computed just before local generators generate.
  315. cmTargetManifest TargetManifest;
  316. // All targets in the entire project.
  317. #if defined(CMAKE_BUILD_WITH_CMAKE)
  318. typedef cmsys::hash_map<std::string, cmTarget*> TargetMap;
  319. #else
  320. typedef std::map<std::string,cmTarget *> TargetMap;
  321. #endif
  322. TargetMap TotalTargets;
  323. TargetMap AliasTargets;
  324. TargetMap ImportedTargets;
  325. std::vector<cmGeneratorExpressionEvaluationFile*> EvaluationFiles;
  326. virtual const char* GetPredefinedTargetsFolder();
  327. virtual bool UseFolderProperty();
  328. void EnableMinGWLanguage(cmMakefile *mf);
  329. private:
  330. cmMakefile* TryCompileOuterMakefile;
  331. float FirstTimeProgress;
  332. // If you add a new map here, make sure it is copied
  333. // in EnableLanguagesFromGenerator
  334. std::map<std::string, bool> IgnoreExtensions;
  335. std::map<std::string, bool> LanguageEnabled;
  336. std::set<std::string> LanguagesReady; // Ready for try_compile
  337. std::map<std::string, std::string> OutputExtensions;
  338. std::map<std::string, std::string> LanguageToOutputExtension;
  339. std::map<std::string, std::string> ExtensionToLanguage;
  340. std::map<std::string, int> LanguageToLinkerPreference;
  341. std::map<std::string, std::string> LanguageToOriginalSharedLibFlags;
  342. // Record hashes for rules and outputs.
  343. struct RuleHash { char Data[32]; };
  344. std::map<std::string, RuleHash> RuleHashes;
  345. void CheckRuleHashes();
  346. void CheckRuleHashes(std::string const& pfile, std::string const& home);
  347. void WriteRuleHashes(std::string const& pfile);
  348. void WriteSummary();
  349. void WriteSummary(cmTarget* target);
  350. void FinalizeTargetCompileInfo();
  351. virtual void ForceLinkerLanguages();
  352. virtual void PrintCompilerAdvice(std::ostream& os, std::string const& lang,
  353. const char* envVar) const;
  354. void CheckCompilerIdCompatibility(cmMakefile* mf,
  355. std::string const& lang) const;
  356. cmExternalMakefileProjectGenerator* ExtraGenerator;
  357. // track files replaced during a Generate
  358. std::vector<std::string> FilesReplacedDuringGenerate;
  359. // Store computed inter-target dependencies.
  360. typedef std::map<cmTarget const*, TargetDependSet> TargetDependMap;
  361. TargetDependMap TargetDependencies;
  362. // Per-target generator information.
  363. cmGeneratorTargetsType GeneratorTargets;
  364. friend class cmake;
  365. void CreateGeneratorTargets(cmMakefile* mf);
  366. void CreateGeneratorTargets();
  367. void ClearGeneratorMembers();
  368. virtual const char* GetBuildIgnoreErrorsFlag() const { return 0; }
  369. // Cache directory content and target files to be built.
  370. struct DirectoryContent: public std::set<std::string>
  371. {
  372. typedef std::set<std::string> derived;
  373. bool LoadedFromDisk;
  374. DirectoryContent(): LoadedFromDisk(false) {}
  375. DirectoryContent(DirectoryContent const& dc):
  376. derived(dc), LoadedFromDisk(dc.LoadedFromDisk) {}
  377. };
  378. std::map<std::string, DirectoryContent> DirectoryContentMap;
  379. // Set of binary directories on disk.
  380. std::set<std::string> BinaryDirectories;
  381. // track targets to issue CMP0042 warning for.
  382. std::set<std::string> CMP0042WarnTargets;
  383. };
  384. #endif