cmGlobalGenerator.h 20 KB

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