cmake.h 19 KB

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