cmake.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #ifndef cmake_h
  2. #define cmake_h
  3. /*=========================================================================
  4. Program: CMake - Cross-Platform Makefile Generator
  5. Module: $RCSfile$
  6. Language: C++
  7. Date: $Date$
  8. Version: $Revision$
  9. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  10. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  11. This software is distributed WITHOUT ANY WARRANTY; without even
  12. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE. See the above copyright notices for more information.
  14. =========================================================================*/
  15. // This class represents a cmake invocation. It is the top level class when
  16. // running cmake. Most cmake based GUIS should primarily create an instance
  17. // of this class and communicate with it.
  18. //
  19. // The basic process for a GUI is as follows:
  20. //
  21. // 1) Create a cmake instance
  22. // 2) Set the Home & Start directories, generator, and cmake command. this
  23. // can be done using the Set methods or by using SetArgs and passing in
  24. // command line arguments.
  25. // 3) Load the cache by calling LoadCache (duh)
  26. // 4) if you are using command line arguments with -D or -C flags then
  27. // call SetCacheArgs (or if for some other reason you want to modify the
  28. // cache, do it now.
  29. // 5) Finally call Configure
  30. // 6) Let the user change values and go back to step 5
  31. // 7) call Generate
  32. //
  33. // If your GUI allows the user to change the start & home directories then
  34. // you must at a minimum redo steps 2 through 7.
  35. //
  36. #include "cmSystemTools.h"
  37. class cmGlobalGenerator;
  38. class cmLocalGenerator;
  39. class cmCacheManager;
  40. class cmMakefile;
  41. class cmCommand;
  42. class cmVariableWatch;
  43. class cmake
  44. {
  45. public:
  46. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  47. ///! construct an instance of cmake
  48. cmake();
  49. ///! destruct an instance of cmake
  50. ~cmake();
  51. /**
  52. * Return major and minor version numbers for cmake.
  53. */
  54. static unsigned int GetMajorVersion();
  55. static unsigned int GetMinorVersion();
  56. static const char *GetReleaseVersion();
  57. //@{
  58. /**
  59. * Set/Get the home directory (or output directory) in the project. The
  60. * home directory is the top directory of the project. It is where
  61. * cmake was run. Remember that CMake processes
  62. * CMakeLists files by recursing up the tree starting at the StartDirectory
  63. * and going up until it reaches the HomeDirectory.
  64. */
  65. void SetHomeDirectory(const char* dir);
  66. const char* GetHomeDirectory() const
  67. {
  68. return m_cmHomeDirectory.c_str();
  69. }
  70. void SetHomeOutputDirectory(const char* lib);
  71. const char* GetHomeOutputDirectory() const
  72. {
  73. return m_HomeOutputDirectory.c_str();
  74. }
  75. //@}
  76. //@{
  77. /**
  78. * Set/Get the start directory (or output directory). The start directory
  79. * is the directory of the CMakeLists.txt file that started the current
  80. * round of processing. Remember that CMake processes CMakeLists files by
  81. * recursing up the tree starting at the StartDirectory and going up until
  82. * it reaches the HomeDirectory.
  83. */
  84. void SetStartDirectory(const char* dir)
  85. {
  86. m_cmStartDirectory = dir;
  87. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  88. }
  89. const char* GetStartDirectory() const
  90. {
  91. return m_cmStartDirectory.c_str();
  92. }
  93. void SetStartOutputDirectory(const char* lib)
  94. {
  95. m_StartOutputDirectory = lib;
  96. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  97. }
  98. const char* GetStartOutputDirectory() const
  99. {
  100. return m_StartOutputDirectory.c_str();
  101. }
  102. //@}
  103. /**
  104. * Dump documentation to a file. If 0 is returned, the
  105. * operation failed.
  106. */
  107. int DumpDocumentationToFile(std::ostream&);
  108. /**
  109. * Handle a command line invocation of cmake.
  110. */
  111. int Run(const std::vector<std::string>&args)
  112. { return this->Run(args, false); }
  113. int Run(const std::vector<std::string>&args, bool noconfigure);
  114. /**
  115. * Generate the SourceFilesList from the SourceLists. This should only be
  116. * done once to be safe. The argument is a list of command line
  117. * arguments. The first argument should be the name or full path
  118. * to the command line version of cmake. For building a GUI,
  119. * you would pass in the following arguments:
  120. * /path/to/cmake -H/path/to/source -B/path/to/build
  121. * If you only want to parse the CMakeLists.txt files,
  122. * but not actually generate the makefiles, use buildMakefiles = false.
  123. */
  124. int Generate();
  125. /**
  126. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  127. * one has not already been set. It will then Call Configure on the
  128. * GlobalGenerator. This in turn will read in an process all the CMakeList
  129. * files for the tree. It will not produce any actual Makefiles, or
  130. * workspaces. Generate does that. */
  131. int Configure();
  132. /**
  133. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  134. * one has not already been set. It will then Call Configure on the
  135. * GlobalGenerator. This in turn will read in an process all the CMakeList
  136. * files for the tree. It will not produce any actual Makefiles, or
  137. * workspaces. Generate does that. */
  138. int LoadCache();
  139. ///! Create a GlobalGenerator
  140. cmGlobalGenerator* CreateGlobalGenerator(const char* name);
  141. ///! Return the global generator assigned to this instance of cmake
  142. cmGlobalGenerator* GetGlobalGenerator() { return m_GlobalGenerator; };
  143. ///! Return the global generator assigned to this instance of cmake
  144. void SetGlobalGenerator(cmGlobalGenerator *);
  145. ///! Get the names of the current registered generators
  146. void GetRegisteredGenerators(std::vector<std::string>& names);
  147. ///! get the cmCachemManager used by this invocation of cmake
  148. cmCacheManager *GetCacheManager() { return m_CacheManager; }
  149. ///! set the cmake command this instance of cmake should use
  150. void SetCMakeCommand(const char* cmd) { m_CMakeCommand = cmd; }
  151. /**
  152. * Given a variable name, return its value (as a string).
  153. */
  154. const char* GetCacheDefinition(const char*) const;
  155. ///! Add an entry into the cache
  156. void AddCacheEntry(const char* key, const char* value,
  157. const char* helpString,
  158. int type);
  159. /**
  160. * Execute commands during the build process. Supports options such
  161. * as echo, remove file etc.
  162. */
  163. static int CMakeCommand(std::vector<std::string>&);
  164. /**
  165. * Add a command to this cmake instance
  166. */
  167. void AddCommand(cmCommand* );
  168. /**
  169. * Get a command by its name
  170. */
  171. cmCommand *GetCommand(const char *name);
  172. /** Get list of all commands */
  173. RegisteredCommandsMap* GetCommands() { return &m_Commands; }
  174. /** Check if a command exists. */
  175. bool CommandExists(const char* name) const;
  176. /**
  177. * Is cmake in the process of a local cmake invocation. If so, we know the
  178. * cache is already configured and ready to go.
  179. */
  180. bool GetLocal() { return m_Local; }
  181. ///! Display command line useage
  182. void Usage(const char *program);
  183. ///! Parse command line arguments
  184. void SetArgs(const std::vector<std::string>&);
  185. ///! Is this cmake running as a result of a TRY_COMPILE command
  186. bool GetIsInTryCompile() { return m_InTryCompile; }
  187. ///! Is this cmake running as a result of a TRY_COMPILE command
  188. void SetIsInTryCompile(bool i) { m_InTryCompile = i; }
  189. ///! Parse command line arguments that might set cache values
  190. bool SetCacheArgs(const std::vector<std::string>&);
  191. typedef void (*ProgressCallback)(const char*msg, float progress, void *);
  192. /**
  193. * Set the function used by GUI's to receive progress updates
  194. * Function gets passed: message as a const char*, a progress
  195. * amount ranging from 0 to 1.0 and client data. The progress
  196. * number provided may be negative in cases where a message is
  197. * to be displayed without any progress percentage.
  198. */
  199. void SetProgressCallback(ProgressCallback f, void* clientData=0);
  200. ///! this is called by generators to update the progress
  201. void UpdateProgress(const char *msg, float prog);
  202. ///! Get the variable watch object
  203. cmVariableWatch* GetVariableWatch() { return m_VariableWatch; }
  204. void GetCommandDocumentation(std::vector<cmDocumentationEntry>&) const;
  205. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  206. ///! Do all the checks before running configure
  207. int DoPreConfigureChecks();
  208. /**
  209. * Set and get the script mode option. In script mode there is no generator
  210. * and no cache. Also, language are not enabled, so add_executable and things
  211. * do not do anything.
  212. */
  213. void SetScriptMode(bool mode) { m_ScriptMode = mode; }
  214. bool GetScriptMode() { return m_ScriptMode; }
  215. ///! Debug the try compile stuff by not delelting the files
  216. bool GetDebugTryCompile(){return m_DebugTryCompile;}
  217. void DebugTryCompileOn(){m_DebugTryCompile = true;}
  218. protected:
  219. typedef cmGlobalGenerator* (*CreateGeneratorFunctionType)();
  220. typedef std::map<cmStdString, CreateGeneratorFunctionType> RegisteredGeneratorsMap;
  221. RegisteredCommandsMap m_Commands;
  222. RegisteredGeneratorsMap m_Generators;
  223. void AddDefaultCommands();
  224. void AddDefaultGenerators();
  225. cmGlobalGenerator *m_GlobalGenerator;
  226. cmCacheManager *m_CacheManager;
  227. std::string m_cmHomeDirectory;
  228. std::string m_HomeOutputDirectory;
  229. std::string m_cmStartDirectory;
  230. std::string m_StartOutputDirectory;
  231. ///! return true if the same cmake was used to make the cache.
  232. bool CacheVersionMatches();
  233. ///! read in a cmake list file to initialize the cache
  234. void ReadListFile(const char *path);
  235. ///! used by Run
  236. int LocalGenerate();
  237. /**
  238. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  239. */
  240. int AddCMakePaths(const char *arg0);
  241. void SetDirectoriesFromFile(const char* arg);
  242. cmVariableWatch* m_VariableWatch;
  243. private:
  244. ProgressCallback m_ProgressCallback;
  245. void* m_ProgressCallbackClientData;
  246. bool m_Verbose;
  247. bool m_Local;
  248. bool m_InTryCompile;
  249. bool m_ScriptMode;
  250. std::string m_CMakeCommand;
  251. const char* m_CXXEnvironment;
  252. const char* m_CCEnvironment;
  253. bool m_DebugTryCompile;
  254. };
  255. #define CMAKE_STANDARD_OPTIONS_TABLE \
  256. {"-C<initial-cache>", "Pre-load cmake cache from given file.", \
  257. "When cmake is first run in an empty build tree, it creates a " \
  258. "CMakeCache.txt file and populates it with customizable settings " \
  259. "for the project. This option may be used to specify a file from " \
  260. "which to load cache entries before the first pass through " \
  261. "the project's cmake listfiles. The loaded entries take priority " \
  262. "over the project's default values."}, \
  263. {"-D<var>:<type>=<value>", "Create a cmake cache entry.", \
  264. "When cmake is first run in an empty build tree, it creates a " \
  265. "CMakeCache.txt file and populates it with customizable settings " \
  266. "for the project. This option may be used to specify a setting " \
  267. "that takes priority over the project's default value. The option " \
  268. "may be repeated for as many cache entries as desired."}, \
  269. {"-G<generator-name>", "Specify a makefile generator.", \
  270. "CMake may support multiple native build systems on certain platforms. " \
  271. "A makefile generator is responsible for generating a particular build " \
  272. "system. Possible generator names are specified in the Generators " \
  273. "section."}
  274. #define CMAKE_STANDARD_INTRODUCTION \
  275. {0, \
  276. "CMake is a cross-platform build system generator. Projects " \
  277. "specify their build process with platform-independent CMake listfiles " \
  278. "included in each directory of a source tree with the name CMakeLists.txt. " \
  279. "Users build a project by using CMake to generate a build system " \
  280. "for a native tool on their platform.", 0}
  281. #endif