cmake.h 16 KB

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