cmGlobalGenerator.h 20 KB

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