cmake.h 17 KB

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