cmake.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 "cmSystemTools.h"
  13. #include "cmPropertyDefinitionMap.h"
  14. #include "cmPropertyMap.h"
  15. class cmGlobalGeneratorFactory;
  16. class cmGlobalGenerator;
  17. class cmLocalGenerator;
  18. class cmCacheManager;
  19. class cmMakefile;
  20. class cmCommand;
  21. class cmVariableWatch;
  22. class cmFileTimeComparison;
  23. class cmExternalMakefileProjectGenerator;
  24. class cmDocumentationSection;
  25. class cmPolicies;
  26. class cmListFileBacktrace;
  27. class cmTarget;
  28. class cmGeneratedFileStream;
  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 & Start 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 start & 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<cmStdString, cmCommand*> RegisteredCommandsMap;
  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. Remember that CMake processes
  97. * CMakeLists files by recursing up the tree starting at the StartDirectory
  98. * and going up until it reaches the HomeDirectory.
  99. */
  100. void SetHomeDirectory(const char* dir);
  101. const char* GetHomeDirectory() const
  102. {
  103. return this->cmHomeDirectory.c_str();
  104. }
  105. void SetHomeOutputDirectory(const char* lib);
  106. const char* GetHomeOutputDirectory() const
  107. {
  108. return this->HomeOutputDirectory.c_str();
  109. }
  110. //@}
  111. //@{
  112. /**
  113. * Set/Get the start directory (or output directory). The start directory
  114. * is the directory of the CMakeLists.txt file that started the current
  115. * round of processing. Remember that CMake processes CMakeLists files by
  116. * recursing up the tree starting at the StartDirectory and going up until
  117. * it reaches the HomeDirectory.
  118. */
  119. void SetStartDirectory(const char* dir)
  120. {
  121. this->cmStartDirectory = dir;
  122. cmSystemTools::ConvertToUnixSlashes(this->cmStartDirectory);
  123. }
  124. const char* GetStartDirectory() const
  125. {
  126. return this->cmStartDirectory.c_str();
  127. }
  128. void SetStartOutputDirectory(const char* lib)
  129. {
  130. this->StartOutputDirectory = lib;
  131. cmSystemTools::ConvertToUnixSlashes(this->StartOutputDirectory);
  132. }
  133. const char* GetStartOutputDirectory() const
  134. {
  135. return this->StartOutputDirectory.c_str();
  136. }
  137. //@}
  138. /**
  139. * Handle a command line invocation of cmake.
  140. */
  141. int Run(const std::vector<std::string>&args)
  142. { return this->Run(args, false); }
  143. int Run(const std::vector<std::string>&args, bool noconfigure);
  144. /**
  145. * Run the global generator Generate step.
  146. */
  147. int Generate();
  148. /**
  149. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  150. * one has not already been set. It will then Call Configure on the
  151. * GlobalGenerator. This in turn will read in an process all the CMakeList
  152. * files for the tree. It will not produce any actual Makefiles, or
  153. * workspaces. Generate does that. */
  154. int Configure();
  155. int ActualConfigure();
  156. int LoadCache();
  157. void PreLoadCMakeFiles();
  158. ///! Create a GlobalGenerator
  159. cmGlobalGenerator* CreateGlobalGenerator(const char* name);
  160. ///! Return the global generator assigned to this instance of cmake
  161. cmGlobalGenerator* GetGlobalGenerator() { return this->GlobalGenerator; }
  162. ///! Return the global generator assigned to this instance of cmake, const
  163. const cmGlobalGenerator* GetGlobalGenerator() const
  164. { return this->GlobalGenerator; }
  165. ///! Return the global generator assigned to this instance of cmake
  166. void SetGlobalGenerator(cmGlobalGenerator *);
  167. ///! Get the names of the current registered generators
  168. void GetRegisteredGenerators(std::vector<std::string>& names);
  169. ///! Set the name of the selected generator-specific toolset.
  170. void SetGeneratorToolset(std::string const& ts)
  171. { this->GeneratorToolset = ts; }
  172. ///! Get the name of the selected generator-specific toolset.
  173. std::string const& GetGeneratorToolset() const
  174. { return this->GeneratorToolset; }
  175. ///! get the cmCachemManager used by this invocation of cmake
  176. cmCacheManager *GetCacheManager() { return this->CacheManager; }
  177. /**
  178. * Given a variable name, return its value (as a string).
  179. */
  180. const char* GetCacheDefinition(const char*) const;
  181. ///! Add an entry into the cache
  182. void AddCacheEntry(const char* key, const char* value,
  183. const char* helpString,
  184. int type);
  185. /**
  186. * Get the system information and write it to the file specified
  187. */
  188. int GetSystemInformation(std::vector<std::string>&);
  189. /**
  190. * Add a command to this cmake instance
  191. */
  192. void AddCommand(cmCommand* );
  193. void RenameCommand(const char* oldName, const char* newName);
  194. void RemoveCommand(const char* name);
  195. void RemoveUnscriptableCommands();
  196. /**
  197. * Get a command by its name
  198. */
  199. cmCommand *GetCommand(const char *name);
  200. /** Get list of all commands */
  201. RegisteredCommandsMap* GetCommands() { return &this->Commands; }
  202. /** Check if a command exists. */
  203. bool CommandExists(const char* name) const;
  204. ///! Parse command line arguments
  205. void SetArgs(const std::vector<std::string>&,
  206. bool directoriesSetBefore = false);
  207. ///! Is this cmake running as a result of a TRY_COMPILE command
  208. bool GetIsInTryCompile() { return this->InTryCompile; }
  209. ///! Is this cmake running as a result of a TRY_COMPILE command
  210. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  211. ///! Parse command line arguments that might set cache values
  212. bool SetCacheArgs(const std::vector<std::string>&);
  213. typedef void (*ProgressCallbackType)
  214. (const char*msg, float progress, void *);
  215. /**
  216. * Set the function used by GUIs to receive progress updates
  217. * Function gets passed: message as a const char*, a progress
  218. * amount ranging from 0 to 1.0 and client data. The progress
  219. * number provided may be negative in cases where a message is
  220. * to be displayed without any progress percentage.
  221. */
  222. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  223. ///! this is called by generators to update the progress
  224. void UpdateProgress(const char *msg, float prog);
  225. ///! get the cmake policies instance
  226. cmPolicies *GetPolicies() {return this->Policies;} ;
  227. ///! Get the variable watch object
  228. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  229. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  230. ///! Set/Get a property of this target file
  231. void SetProperty(const std::string& prop, const char *value);
  232. void AppendProperty(const std::string& prop,
  233. const char *value,bool asString=false);
  234. const char *GetProperty(const std::string& prop);
  235. const char *GetProperty(const std::string& prop,
  236. cmProperty::ScopeType scope);
  237. bool GetPropertyAsBool(const std::string& prop);
  238. // Get the properties
  239. cmPropertyMap &GetProperties() { return this->Properties; };
  240. ///! Do all the checks before running configure
  241. int DoPreConfigureChecks();
  242. void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
  243. WorkingMode GetWorkingMode() { return this->CurrentWorkingMode; }
  244. ///! Debug the try compile stuff by not deleting the files
  245. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  246. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  247. /**
  248. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  249. */
  250. int AddCMakePaths();
  251. /**
  252. * Get the file comparison class
  253. */
  254. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  255. // Do we want debug output during the cmake run.
  256. bool GetDebugOutput() { return this->DebugOutput; }
  257. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  258. // Do we want trace output during the cmake run.
  259. bool GetTrace() { return this->Trace;}
  260. void SetTrace(bool b) { this->Trace = b;}
  261. bool GetWarnUninitialized() { return this->WarnUninitialized;}
  262. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b;}
  263. bool GetWarnUnused() { return this->WarnUnused;}
  264. void SetWarnUnused(bool b) { this->WarnUnused = b;}
  265. bool GetWarnUnusedCli() { return this->WarnUnusedCli;}
  266. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b;}
  267. bool GetCheckSystemVars() { return this->CheckSystemVars;}
  268. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b;}
  269. void MarkCliAsUsed(const std::string& variable);
  270. // Define a property
  271. void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
  272. const char *ShortDescription,
  273. const char *FullDescription,
  274. bool chain = false);
  275. // get property definition
  276. cmPropertyDefinition *GetPropertyDefinition
  277. (const std::string& name, cmProperty::ScopeType scope);
  278. // Is a property defined?
  279. bool IsPropertyDefined(const std::string& name, cmProperty::ScopeType scope);
  280. bool IsPropertyChained(const std::string& name, cmProperty::ScopeType scope);
  281. /** Get the list of configurations (in upper case) considered to be
  282. debugging configurations.*/
  283. std::vector<std::string> const& GetDebugConfigs();
  284. void SetCMakeEditCommand(std::string const& s)
  285. { this->CMakeEditCommand = s; }
  286. std::string const& GetCMakeEditCommand() const
  287. { return this->CMakeEditCommand; }
  288. void SetSuppressDevWarnings(bool v)
  289. {
  290. this->SuppressDevWarnings = v;
  291. this->DoSuppressDevWarnings = true;
  292. }
  293. /** Display a message to the user. */
  294. void IssueMessage(cmake::MessageType t, std::string const& text,
  295. cmListFileBacktrace const& backtrace);
  296. ///! run the --build option
  297. int Build(const std::string& dir,
  298. const std::string& target,
  299. const std::string& config,
  300. const std::vector<std::string>& nativeOptions,
  301. bool clean);
  302. void UnwatchUnusedCli(const char* var);
  303. void WatchUnusedCli(const char* var);
  304. protected:
  305. void RunCheckForUnusedVariables();
  306. void InitializeProperties();
  307. int HandleDeleteCacheVariables(const char* var);
  308. cmPropertyMap Properties;
  309. std::set<std::pair<cmStdString,cmProperty::ScopeType> > AccessedProperties;
  310. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  311. PropertyDefinitions;
  312. typedef
  313. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  314. typedef std::map<cmStdString,
  315. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  316. typedef std::vector<cmGlobalGeneratorFactory*> RegisteredGeneratorsVector;
  317. RegisteredCommandsMap Commands;
  318. RegisteredGeneratorsVector Generators;
  319. RegisteredExtraGeneratorsMap ExtraGenerators;
  320. void AddDefaultCommands();
  321. void AddDefaultGenerators();
  322. void AddDefaultExtraGenerators();
  323. void AddExtraGenerator(const char* name,
  324. CreateExtraGeneratorFunctionType newFunction);
  325. cmPolicies *Policies;
  326. cmGlobalGenerator *GlobalGenerator;
  327. cmCacheManager *CacheManager;
  328. std::string cmHomeDirectory;
  329. std::string HomeOutputDirectory;
  330. std::string cmStartDirectory;
  331. std::string StartOutputDirectory;
  332. bool SuppressDevWarnings;
  333. bool DoSuppressDevWarnings;
  334. std::string GeneratorToolset;
  335. ///! read in a cmake list file to initialize the cache
  336. void ReadListFile(const std::vector<std::string>& args, const char *path);
  337. bool FindPackage(const std::vector<std::string>& args);
  338. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  339. /// If it is set, truncate it to 50kb
  340. void TruncateOutputLog(const char* fname);
  341. /**
  342. * Method called to check build system integrity at build time.
  343. * Returns 1 if CMake should rerun and 0 otherwise.
  344. */
  345. int CheckBuildSystem();
  346. void SetDirectoriesFromFile(const char* arg);
  347. //! Make sure all commands are what they say they are and there is no
  348. /// macros.
  349. void CleanupCommandsAndMacros();
  350. void GenerateGraphViz(const char* fileName) const;
  351. cmVariableWatch* VariableWatch;
  352. private:
  353. cmake(const cmake&); // Not implemented.
  354. void operator=(const cmake&); // Not implemented.
  355. ProgressCallbackType ProgressCallback;
  356. void* ProgressCallbackClientData;
  357. bool Verbose;
  358. bool InTryCompile;
  359. WorkingMode CurrentWorkingMode;
  360. bool DebugOutput;
  361. bool Trace;
  362. bool WarnUninitialized;
  363. bool WarnUnused;
  364. bool WarnUnusedCli;
  365. bool CheckSystemVars;
  366. std::map<cmStdString, bool> UsedCliVariables;
  367. std::string CMakeEditCommand;
  368. std::string CXXEnvironment;
  369. std::string CCEnvironment;
  370. std::string CheckBuildSystemArgument;
  371. std::string CheckStampFile;
  372. std::string CheckStampList;
  373. std::string VSSolutionFile;
  374. bool ClearBuildSystem;
  375. bool DebugTryCompile;
  376. cmFileTimeComparison* FileComparison;
  377. std::string GraphVizFile;
  378. std::vector<std::string> DebugConfigs;
  379. void UpdateConversionPathTable();
  380. };
  381. #define CMAKE_STANDARD_OPTIONS_TABLE \
  382. {"-C <initial-cache>", "Pre-load a script to populate the cache."}, \
  383. {"-D <var>:<type>=<value>", "Create a cmake cache entry."}, \
  384. {"-U <globbing_expr>", "Remove matching entries from CMake cache."}, \
  385. {"-G <generator-name>", "Specify a build system generator."},\
  386. {"-T <toolset-name>", "Specify toolset name if supported by generator."}, \
  387. {"-Wno-dev", "Suppress developer warnings."},\
  388. {"-Wdev", "Enable developer warnings."}
  389. #endif