cmake.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 cmake_h
  11. #define cmake_h
  12. #include "cmListFileCache.h"
  13. #include "cmSystemTools.h"
  14. #include "cmInstalledFile.h"
  15. #include "cmCacheManager.h"
  16. #include "cmState.h"
  17. class cmGlobalGeneratorFactory;
  18. class cmGlobalGenerator;
  19. class cmLocalGenerator;
  20. class cmMakefile;
  21. class cmVariableWatch;
  22. class cmFileTimeComparison;
  23. class cmExternalMakefileProjectGenerator;
  24. class cmDocumentationSection;
  25. class cmTarget;
  26. class cmGeneratedFileStream;
  27. /** \brief Represents a cmake invocation.
  28. *
  29. * This class represents a cmake invocation. It is the top level class when
  30. * running cmake. Most cmake based GUIs should primarily create an instance
  31. * of this class and communicate with it.
  32. *
  33. * The basic process for a GUI is as follows:
  34. *
  35. * -# Create a cmake instance
  36. * -# Set the Home directories, generator, and cmake command. this
  37. * can be done using the Set methods or by using SetArgs and passing in
  38. * command line arguments.
  39. * -# Load the cache by calling LoadCache (duh)
  40. * -# if you are using command line arguments with -D or -C flags then
  41. * call SetCacheArgs (or if for some other reason you want to modify the
  42. * cache), do it now.
  43. * -# Finally call Configure
  44. * -# Let the user change values and go back to step 5
  45. * -# call Generate
  46. * If your GUI allows the user to change the home directories then
  47. * you must at a minimum redo steps 2 through 7.
  48. */
  49. class cmake
  50. {
  51. public:
  52. enum MessageType
  53. { AUTHOR_WARNING,
  54. FATAL_ERROR,
  55. INTERNAL_ERROR,
  56. MESSAGE,
  57. WARNING,
  58. LOG,
  59. DEPRECATION_ERROR,
  60. DEPRECATION_WARNING
  61. };
  62. enum DiagLevel
  63. {
  64. DIAG_IGNORE,
  65. DIAG_WARN
  66. };
  67. /** \brief Describes the working modes of cmake */
  68. enum WorkingMode
  69. {
  70. NORMAL_MODE, ///< Cmake runs to create project files
  71. /** \brief Script mode (started by using -P).
  72. *
  73. * In script mode there is no generator and no cache. Also,
  74. * languages are not enabled, so add_executable and things do
  75. * nothing.
  76. */
  77. SCRIPT_MODE,
  78. /** \brief A pkg-config like mode
  79. *
  80. * In this mode cmake just searches for a package and prints the results to
  81. * stdout. This is similar to SCRIPT_MODE, but commands like add_library()
  82. * work too, since they may be used e.g. in exported target files. Started
  83. * via --find-package.
  84. */
  85. FIND_PACKAGE_MODE
  86. };
  87. struct GeneratorInfo
  88. {
  89. std::string name;
  90. bool supportsToolset;
  91. };
  92. typedef std::map<std::string, cmInstalledFile> InstalledFilesMap;
  93. /// Default constructor
  94. cmake();
  95. /// Destructor
  96. ~cmake();
  97. static const char *GetCMakeFilesDirectory() {return "/CMakeFiles";}
  98. static const char *GetCMakeFilesDirectoryPostSlash() {
  99. return "CMakeFiles/";}
  100. //@{
  101. /**
  102. * Set/Get the home directory (or output directory) in the project. The
  103. * home directory is the top directory of the project. It is the
  104. * path-to-source cmake was run with.
  105. */
  106. void SetHomeDirectory(const std::string& dir);
  107. const char* GetHomeDirectory() const;
  108. void SetHomeOutputDirectory(const std::string& dir);
  109. const char* GetHomeOutputDirectory() const;
  110. //@}
  111. /**
  112. * Handle a command line invocation of cmake.
  113. */
  114. int Run(const std::vector<std::string>&args)
  115. { return this->Run(args, false); }
  116. int Run(const std::vector<std::string>&args, bool noconfigure);
  117. /**
  118. * Run the global generator Generate step.
  119. */
  120. int Generate();
  121. /**
  122. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  123. * one has not already been set. It will then Call Configure on the
  124. * GlobalGenerator. This in turn will read in an process all the CMakeList
  125. * files for the tree. It will not produce any actual Makefiles, or
  126. * workspaces. Generate does that. */
  127. int Configure();
  128. int ActualConfigure();
  129. ///! Break up a line like VAR:type="value" into var, type and value
  130. static bool ParseCacheEntry(const std::string& entry,
  131. std::string& var,
  132. std::string& value,
  133. cmState::CacheEntryType& type);
  134. int LoadCache();
  135. bool LoadCache(const std::string& path);
  136. bool LoadCache(const std::string& path, bool internal,
  137. std::set<std::string>& excludes,
  138. std::set<std::string>& includes);
  139. bool SaveCache(const std::string& path);
  140. bool DeleteCache(const std::string& path);
  141. void PreLoadCMakeFiles();
  142. ///! Create a GlobalGenerator
  143. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name);
  144. ///! Return the global generator assigned to this instance of cmake
  145. cmGlobalGenerator* GetGlobalGenerator() { return this->GlobalGenerator; }
  146. ///! Return the global generator assigned to this instance of cmake, const
  147. const cmGlobalGenerator* GetGlobalGenerator() const
  148. { return this->GlobalGenerator; }
  149. ///! Return the global generator assigned to this instance of cmake
  150. void SetGlobalGenerator(cmGlobalGenerator *);
  151. ///! Get the names of the current registered generators
  152. void GetRegisteredGenerators(std::vector<GeneratorInfo>& generators);
  153. ///! Set the name of the selected generator-specific platform.
  154. void SetGeneratorPlatform(std::string const& ts)
  155. { this->GeneratorPlatform = ts; }
  156. ///! Get the name of the selected generator-specific platform.
  157. std::string const& GetGeneratorPlatform() const
  158. { return this->GeneratorPlatform; }
  159. ///! Set the name of the selected generator-specific toolset.
  160. void SetGeneratorToolset(std::string const& ts)
  161. { this->GeneratorToolset = ts; }
  162. ///! Get the name of the selected generator-specific toolset.
  163. std::string const& GetGeneratorToolset() const
  164. { return this->GeneratorToolset; }
  165. ///! get the cmCachemManager used by this invocation of cmake
  166. cmCacheManager *GetCacheManager() { return this->CacheManager; }
  167. const std::vector<std::string>& GetSourceExtensions() const
  168. {return this->SourceFileExtensions;}
  169. const std::vector<std::string>& GetHeaderExtensions() const
  170. {return this->HeaderFileExtensions;}
  171. /**
  172. * Given a variable name, return its value (as a string).
  173. */
  174. const char* GetCacheDefinition(const std::string&) const;
  175. ///! Add an entry into the cache
  176. void AddCacheEntry(const std::string& key, const char* value,
  177. const char* helpString,
  178. int type);
  179. /**
  180. * Get the system information and write it to the file specified
  181. */
  182. int GetSystemInformation(std::vector<std::string>&);
  183. ///! Parse command line arguments
  184. void SetArgs(const std::vector<std::string>&,
  185. bool directoriesSetBefore = false);
  186. ///! Is this cmake running as a result of a TRY_COMPILE command
  187. bool GetIsInTryCompile() const;
  188. void SetIsInTryCompile(bool b);
  189. ///! Parse command line arguments that might set cache values
  190. bool SetCacheArgs(const std::vector<std::string>&);
  191. typedef void (*ProgressCallbackType)
  192. (const char*msg, float progress, void *);
  193. /**
  194. * Set the function used by GUIs to receive progress updates
  195. * Function gets passed: message as a const char*, a progress
  196. * amount ranging from 0 to 1.0 and client data. The progress
  197. * number provided may be negative in cases where a message is
  198. * to be displayed without any progress percentage.
  199. */
  200. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  201. ///! this is called by generators to update the progress
  202. void UpdateProgress(const char *msg, float prog);
  203. ///! Get the variable watch object
  204. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  205. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  206. ///! Set/Get a property of this target file
  207. void SetProperty(const std::string& prop, const char *value);
  208. void AppendProperty(const std::string& prop,
  209. const char *value,bool asString=false);
  210. const char *GetProperty(const std::string& prop);
  211. bool GetPropertyAsBool(const std::string& prop);
  212. ///! Get or create an cmInstalledFile instance and return a pointer to it
  213. cmInstalledFile *GetOrCreateInstalledFile(
  214. cmMakefile* mf, const std::string& name);
  215. cmInstalledFile const* GetInstalledFile(const std::string& name) const;
  216. InstalledFilesMap const& GetInstalledFiles() const
  217. { return this->InstalledFiles; }
  218. ///! Do all the checks before running configure
  219. int DoPreConfigureChecks();
  220. void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
  221. WorkingMode GetWorkingMode() { return this->CurrentWorkingMode; }
  222. ///! Debug the try compile stuff by not deleting the files
  223. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  224. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  225. /**
  226. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  227. */
  228. int AddCMakePaths();
  229. /**
  230. * Get the file comparison class
  231. */
  232. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  233. // Do we want debug output during the cmake run.
  234. bool GetDebugOutput() { return this->DebugOutput; }
  235. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  236. // Do we want trace output during the cmake run.
  237. bool GetTrace() { return this->Trace;}
  238. void SetTrace(bool b) { this->Trace = b;}
  239. bool GetTraceExpand() { return this->TraceExpand;}
  240. void SetTraceExpand(bool b) { this->TraceExpand = b;}
  241. bool GetWarnUninitialized() { return this->WarnUninitialized;}
  242. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b;}
  243. bool GetWarnUnused() { return this->WarnUnused;}
  244. void SetWarnUnused(bool b) { this->WarnUnused = b;}
  245. bool GetWarnUnusedCli() { return this->WarnUnusedCli;}
  246. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b;}
  247. bool GetCheckSystemVars() { return this->CheckSystemVars;}
  248. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b;}
  249. void MarkCliAsUsed(const std::string& variable);
  250. /** Get the list of configurations (in upper case) considered to be
  251. debugging configurations.*/
  252. std::vector<std::string> GetDebugConfigs();
  253. void SetCMakeEditCommand(std::string const& s)
  254. { this->CMakeEditCommand = s; }
  255. std::string const& GetCMakeEditCommand() const
  256. { return this->CMakeEditCommand; }
  257. /*
  258. * Get the state of the suppression of developer (author) warnings.
  259. * Returns false, by default, if developer warnings should be shown, true
  260. * otherwise.
  261. */
  262. bool GetSuppressDevWarnings(cmMakefile const* mf = NULL);
  263. /*
  264. * Set the state of the suppression of developer (author) warnings.
  265. */
  266. void SetSuppressDevWarnings(bool v);
  267. /*
  268. * Get the state of the suppression of deprecated warnings.
  269. * Returns false, by default, if deprecated warnings should be shown, true
  270. * otherwise.
  271. */
  272. bool GetSuppressDeprecatedWarnings(cmMakefile const* mf = NULL);
  273. /*
  274. * Set the state of the suppression of deprecated warnings.
  275. */
  276. void SetSuppressDeprecatedWarnings(bool v);
  277. /** Display a message to the user. */
  278. void IssueMessage(cmake::MessageType t, std::string const& text,
  279. cmListFileBacktrace const& backtrace = cmListFileBacktrace(),
  280. bool force = false);
  281. void IssueMessage(cmake::MessageType t, std::string const& text,
  282. cmListFileContext const& lfc,
  283. bool force = false);
  284. ///! run the --build option
  285. int Build(const std::string& dir,
  286. const std::string& target,
  287. const std::string& config,
  288. const std::vector<std::string>& nativeOptions,
  289. bool clean);
  290. void UnwatchUnusedCli(const std::string& var);
  291. void WatchUnusedCli(const std::string& var);
  292. cmState* GetState() const { return this->State; }
  293. void SetCurrentSnapshot(cmState::Snapshot snapshot)
  294. { this->CurrentSnapshot = snapshot; }
  295. cmState::Snapshot GetCurrentSnapshot() const
  296. { return this->CurrentSnapshot; }
  297. protected:
  298. void RunCheckForUnusedVariables();
  299. void InitializeProperties();
  300. int HandleDeleteCacheVariables(const std::string& var);
  301. typedef
  302. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  303. typedef std::map<std::string,
  304. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  305. typedef std::vector<cmGlobalGeneratorFactory*> RegisteredGeneratorsVector;
  306. RegisteredGeneratorsVector Generators;
  307. RegisteredExtraGeneratorsMap ExtraGenerators;
  308. void AddDefaultCommands();
  309. void AddDefaultGenerators();
  310. void AddDefaultExtraGenerators();
  311. void AddExtraGenerator(const std::string& name,
  312. CreateExtraGeneratorFunctionType newFunction);
  313. cmGlobalGenerator *GlobalGenerator;
  314. cmCacheManager *CacheManager;
  315. std::map<std::string, DiagLevel> DiagLevels;
  316. std::string GeneratorPlatform;
  317. std::string GeneratorToolset;
  318. ///! read in a cmake list file to initialize the cache
  319. void ReadListFile(const std::vector<std::string>& args, const char *path);
  320. bool FindPackage(const std::vector<std::string>& args);
  321. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  322. /// If it is set, truncate it to 50kb
  323. void TruncateOutputLog(const char* fname);
  324. /**
  325. * Method called to check build system integrity at build time.
  326. * Returns 1 if CMake should rerun and 0 otherwise.
  327. */
  328. int CheckBuildSystem();
  329. void SetDirectoriesFromFile(const char* arg);
  330. //! Make sure all commands are what they say they are and there is no
  331. /// macros.
  332. void CleanupCommandsAndMacros();
  333. void GenerateGraphViz(const char* fileName) const;
  334. cmVariableWatch* VariableWatch;
  335. private:
  336. cmake(const cmake&); // Not implemented.
  337. void operator=(const cmake&); // Not implemented.
  338. ProgressCallbackType ProgressCallback;
  339. void* ProgressCallbackClientData;
  340. bool Verbose;
  341. bool InTryCompile;
  342. WorkingMode CurrentWorkingMode;
  343. bool DebugOutput;
  344. bool Trace;
  345. bool TraceExpand;
  346. bool WarnUninitialized;
  347. bool WarnUnused;
  348. bool WarnUnusedCli;
  349. bool CheckSystemVars;
  350. std::map<std::string, bool> UsedCliVariables;
  351. std::string CMakeEditCommand;
  352. std::string CXXEnvironment;
  353. std::string CCEnvironment;
  354. std::string CheckBuildSystemArgument;
  355. std::string CheckStampFile;
  356. std::string CheckStampList;
  357. std::string VSSolutionFile;
  358. std::vector<std::string> SourceFileExtensions;
  359. std::vector<std::string> HeaderFileExtensions;
  360. bool ClearBuildSystem;
  361. bool DebugTryCompile;
  362. cmFileTimeComparison* FileComparison;
  363. std::string GraphVizFile;
  364. InstalledFilesMap InstalledFiles;
  365. cmState* State;
  366. cmState::Snapshot CurrentSnapshot;
  367. void UpdateConversionPathTable();
  368. // Print a list of valid generators to stderr.
  369. void PrintGeneratorList();
  370. /*
  371. * Check if messages of this type should be output, based on the state of the
  372. * warning and error output CMake variables, in the cache.
  373. */
  374. bool IsMessageTypeVisible(cmake::MessageType t);
  375. bool PrintMessagePreamble(cmake::MessageType t, std::ostream& msg);
  376. };
  377. #define CMAKE_STANDARD_OPTIONS_TABLE \
  378. {"-C <initial-cache>", "Pre-load a script to populate the cache."}, \
  379. {"-D <var>[:<type>]=<value>", "Create a cmake cache entry."}, \
  380. {"-U <globbing_expr>", "Remove matching entries from CMake cache."}, \
  381. {"-G <generator-name>", "Specify a build system generator."},\
  382. {"-T <toolset-name>", "Specify toolset name if supported by generator."}, \
  383. {"-A <platform-name>", "Specify platform name if supported by generator."}, \
  384. {"-Wno-dev", "Suppress developer warnings."},\
  385. {"-Wdev", "Enable developer warnings."},\
  386. {"-Wdeprecated", "Enable deprecation warnings."},\
  387. {"-Wno-deprecated", "Suppress deprecation warnings."}
  388. #define FOR_EACH_C_FEATURE(F) \
  389. F(c_function_prototypes) \
  390. F(c_restrict) \
  391. F(c_static_assert) \
  392. F(c_variadic_macros)
  393. #define FOR_EACH_CXX_FEATURE(F) \
  394. F(cxx_aggregate_default_initializers) \
  395. F(cxx_alias_templates) \
  396. F(cxx_alignas) \
  397. F(cxx_alignof) \
  398. F(cxx_attributes) \
  399. F(cxx_attribute_deprecated) \
  400. F(cxx_auto_type) \
  401. F(cxx_binary_literals) \
  402. F(cxx_constexpr) \
  403. F(cxx_contextual_conversions) \
  404. F(cxx_decltype) \
  405. F(cxx_decltype_auto) \
  406. F(cxx_decltype_incomplete_return_types) \
  407. F(cxx_default_function_template_args) \
  408. F(cxx_defaulted_functions) \
  409. F(cxx_defaulted_move_initializers) \
  410. F(cxx_delegating_constructors) \
  411. F(cxx_deleted_functions) \
  412. F(cxx_digit_separators) \
  413. F(cxx_enum_forward_declarations) \
  414. F(cxx_explicit_conversions) \
  415. F(cxx_extended_friend_declarations) \
  416. F(cxx_extern_templates) \
  417. F(cxx_final) \
  418. F(cxx_func_identifier) \
  419. F(cxx_generalized_initializers) \
  420. F(cxx_generic_lambdas) \
  421. F(cxx_inheriting_constructors) \
  422. F(cxx_inline_namespaces) \
  423. F(cxx_lambdas) \
  424. F(cxx_lambda_init_captures) \
  425. F(cxx_local_type_template_args) \
  426. F(cxx_long_long_type) \
  427. F(cxx_noexcept) \
  428. F(cxx_nonstatic_member_init) \
  429. F(cxx_nullptr) \
  430. F(cxx_override) \
  431. F(cxx_range_for) \
  432. F(cxx_raw_string_literals) \
  433. F(cxx_reference_qualified_functions) \
  434. F(cxx_relaxed_constexpr) \
  435. F(cxx_return_type_deduction) \
  436. F(cxx_right_angle_brackets) \
  437. F(cxx_rvalue_references) \
  438. F(cxx_sizeof_member) \
  439. F(cxx_static_assert) \
  440. F(cxx_strong_enums) \
  441. F(cxx_template_template_parameters) \
  442. F(cxx_thread_local) \
  443. F(cxx_trailing_return_types) \
  444. F(cxx_unicode_literals) \
  445. F(cxx_uniform_initialization) \
  446. F(cxx_unrestricted_unions) \
  447. F(cxx_user_literals) \
  448. F(cxx_variable_templates) \
  449. F(cxx_variadic_macros) \
  450. F(cxx_variadic_templates)
  451. #endif