cmake.h 17 KB

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