cmGlobalGenerator.h 20 KB

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