cmGlobalGenerator.h 20 KB

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