cmake.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. ///! set the cmake command this instance of cmake should use
  178. void SetCMakeCommand(const char* cmd) { this->CMakeCommand = cmd; }
  179. /**
  180. * Given a variable name, return its value (as a string).
  181. */
  182. const char* GetCacheDefinition(const char*) const;
  183. ///! Add an entry into the cache
  184. void AddCacheEntry(const char* key, const char* value,
  185. const char* helpString,
  186. int type);
  187. /**
  188. * Get the system information and write it to the file specified
  189. */
  190. int GetSystemInformation(std::vector<std::string>&);
  191. /**
  192. * Add a command to this cmake instance
  193. */
  194. void AddCommand(cmCommand* );
  195. void RenameCommand(const char* oldName, const char* newName);
  196. void RemoveCommand(const char* name);
  197. void RemoveUnscriptableCommands();
  198. /**
  199. * Get a command by its name
  200. */
  201. cmCommand *GetCommand(const char *name);
  202. /** Get list of all commands */
  203. RegisteredCommandsMap* GetCommands() { return &this->Commands; }
  204. /** Check if a command exists. */
  205. bool CommandExists(const char* name) const;
  206. ///! Parse command line arguments
  207. void SetArgs(const std::vector<std::string>&,
  208. bool directoriesSetBefore = false);
  209. ///! Is this cmake running as a result of a TRY_COMPILE command
  210. bool GetIsInTryCompile() { return this->InTryCompile; }
  211. ///! Is this cmake running as a result of a TRY_COMPILE command
  212. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  213. ///! Parse command line arguments that might set cache values
  214. bool SetCacheArgs(const std::vector<std::string>&);
  215. typedef void (*ProgressCallbackType)
  216. (const char*msg, float progress, void *);
  217. /**
  218. * Set the function used by GUIs to receive progress updates
  219. * Function gets passed: message as a const char*, a progress
  220. * amount ranging from 0 to 1.0 and client data. The progress
  221. * number provided may be negative in cases where a message is
  222. * to be displayed without any progress percentage.
  223. */
  224. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  225. ///! this is called by generators to update the progress
  226. void UpdateProgress(const char *msg, float prog);
  227. ///! get the cmake policies instance
  228. cmPolicies *GetPolicies() {return this->Policies;} ;
  229. ///! Get the variable watch object
  230. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  231. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  232. ///! Set/Get a property of this target file
  233. void SetProperty(const char *prop, const char *value);
  234. void AppendProperty(const char *prop, const char *value,bool asString=false);
  235. const char *GetProperty(const char *prop);
  236. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  237. bool GetPropertyAsBool(const char *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. /**
  256. * Get the path to ctest
  257. */
  258. const char* GetCTestCommand();
  259. const char* GetCPackCommand();
  260. const char* GetCMakeCommand();
  261. // Do we want debug output during the cmake run.
  262. bool GetDebugOutput() { return this->DebugOutput; }
  263. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  264. // Do we want trace output during the cmake run.
  265. bool GetTrace() { return this->Trace;}
  266. void SetTrace(bool b) { this->Trace = b;}
  267. bool GetWarnUninitialized() { return this->WarnUninitialized;}
  268. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b;}
  269. bool GetWarnUnused() { return this->WarnUnused;}
  270. void SetWarnUnused(bool b) { this->WarnUnused = b;}
  271. bool GetWarnUnusedCli() { return this->WarnUnusedCli;}
  272. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b;}
  273. bool GetCheckSystemVars() { return this->CheckSystemVars;}
  274. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b;}
  275. void MarkCliAsUsed(const std::string& variable);
  276. // Define a property
  277. void DefineProperty(const char *name, cmProperty::ScopeType scope,
  278. const char *ShortDescription,
  279. const char *FullDescription,
  280. bool chain = false,
  281. const char *variableGroup = 0);
  282. // get property definition
  283. cmPropertyDefinition *GetPropertyDefinition
  284. (const char *name, cmProperty::ScopeType scope);
  285. // Is a property defined?
  286. bool IsPropertyDefined(const char *name, cmProperty::ScopeType scope);
  287. bool IsPropertyChained(const char *name, cmProperty::ScopeType scope);
  288. /** Get the list of configurations (in upper case) considered to be
  289. debugging configurations.*/
  290. std::vector<std::string> const& GetDebugConfigs();
  291. void SetCMakeEditCommand(const char* s)
  292. {
  293. this->CMakeEditCommand = s;
  294. }
  295. void SetSuppressDevWarnings(bool v)
  296. {
  297. this->SuppressDevWarnings = v;
  298. this->DoSuppressDevWarnings = true;
  299. }
  300. /** Display a message to the user. */
  301. void IssueMessage(cmake::MessageType t, std::string const& text,
  302. cmListFileBacktrace const& backtrace);
  303. ///! run the --build option
  304. int Build(const std::string& dir,
  305. const std::string& target,
  306. const std::string& config,
  307. const std::vector<std::string>& nativeOptions,
  308. bool clean,
  309. cmSystemTools::OutputOption outputflag);
  310. void UnwatchUnusedCli(const char* var);
  311. void WatchUnusedCli(const char* var);
  312. protected:
  313. void RunCheckForUnusedVariables();
  314. void InitializeProperties();
  315. int HandleDeleteCacheVariables(const char* var);
  316. cmPropertyMap Properties;
  317. std::set<std::pair<cmStdString,cmProperty::ScopeType> > AccessedProperties;
  318. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  319. PropertyDefinitions;
  320. typedef
  321. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  322. typedef std::map<cmStdString,
  323. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  324. typedef std::vector<cmGlobalGeneratorFactory*> RegisteredGeneratorsVector;
  325. RegisteredCommandsMap Commands;
  326. RegisteredGeneratorsVector Generators;
  327. RegisteredExtraGeneratorsMap ExtraGenerators;
  328. void AddDefaultCommands();
  329. void AddDefaultGenerators();
  330. void AddDefaultExtraGenerators();
  331. void AddExtraGenerator(const char* name,
  332. CreateExtraGeneratorFunctionType newFunction);
  333. cmPolicies *Policies;
  334. cmGlobalGenerator *GlobalGenerator;
  335. cmCacheManager *CacheManager;
  336. std::string cmHomeDirectory;
  337. std::string HomeOutputDirectory;
  338. std::string cmStartDirectory;
  339. std::string StartOutputDirectory;
  340. bool SuppressDevWarnings;
  341. bool DoSuppressDevWarnings;
  342. std::string GeneratorToolset;
  343. ///! read in a cmake list file to initialize the cache
  344. void ReadListFile(const std::vector<std::string>& args, const char *path);
  345. bool FindPackage(const std::vector<std::string>& args);
  346. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  347. /// If it is set, truncate it to 50kb
  348. void TruncateOutputLog(const char* fname);
  349. /**
  350. * Method called to check build system integrity at build time.
  351. * Returns 1 if CMake should rerun and 0 otherwise.
  352. */
  353. int CheckBuildSystem();
  354. void SetDirectoriesFromFile(const char* arg);
  355. //! Make sure all commands are what they say they are and there is no
  356. /// macros.
  357. void CleanupCommandsAndMacros();
  358. void GenerateGraphViz(const char* fileName) const;
  359. cmVariableWatch* VariableWatch;
  360. ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
  361. std::string FindCMakeProgram(const char* name) const;
  362. private:
  363. cmake(const cmake&); // Not implemented.
  364. void operator=(const cmake&); // Not implemented.
  365. ProgressCallbackType ProgressCallback;
  366. void* ProgressCallbackClientData;
  367. bool Verbose;
  368. bool InTryCompile;
  369. WorkingMode CurrentWorkingMode;
  370. bool DebugOutput;
  371. bool Trace;
  372. bool WarnUninitialized;
  373. bool WarnUnused;
  374. bool WarnUnusedCli;
  375. bool CheckSystemVars;
  376. std::map<cmStdString, bool> UsedCliVariables;
  377. std::string CMakeEditCommand;
  378. std::string CMakeCommand;
  379. std::string CXXEnvironment;
  380. std::string CCEnvironment;
  381. std::string CheckBuildSystemArgument;
  382. std::string CheckStampFile;
  383. std::string CheckStampList;
  384. std::string VSSolutionFile;
  385. std::string CTestCommand;
  386. std::string CPackCommand;
  387. bool ClearBuildSystem;
  388. bool DebugTryCompile;
  389. cmFileTimeComparison* FileComparison;
  390. std::string GraphVizFile;
  391. std::vector<std::string> DebugConfigs;
  392. void UpdateConversionPathTable();
  393. };
  394. #define CMAKE_STANDARD_OPTIONS_TABLE \
  395. {"-C <initial-cache>", "Pre-load a script to populate the cache.", \
  396. "When cmake is first run in an empty build tree, it creates a " \
  397. "CMakeCache.txt file and populates it with customizable settings " \
  398. "for the project. This option may be used to specify a file from " \
  399. "which to load cache entries before the first pass through " \
  400. "the project's cmake listfiles. The loaded entries take priority " \
  401. "over the project's default values. The given file should be a CMake " \
  402. "script containing SET commands that use the CACHE option, " \
  403. "not a cache-format file."}, \
  404. {"-D <var>:<type>=<value>", "Create a cmake cache entry.", \
  405. "When cmake is first run in an empty build tree, it creates a " \
  406. "CMakeCache.txt file and populates it with customizable settings " \
  407. "for the project. This option may be used to specify a setting " \
  408. "that takes priority over the project's default value. The option " \
  409. "may be repeated for as many cache entries as desired."}, \
  410. {"-U <globbing_expr>", "Remove matching entries from CMake cache.", \
  411. "This option may be used to remove one or more variables from the " \
  412. "CMakeCache.txt file, globbing expressions using * and ? are supported. "\
  413. "The option may be repeated for as many cache entries as desired.\n" \
  414. "Use with care, you can make your CMakeCache.txt non-working."}, \
  415. {"-G <generator-name>", "Specify a build system generator.", \
  416. "CMake may support multiple native build systems on certain platforms. " \
  417. "A generator is responsible for generating a particular build " \
  418. "system. Possible generator names are specified in the Generators " \
  419. "section."},\
  420. {"-T <toolset-name>", "Specify toolset name if supported by generator.", \
  421. "Some CMake generators support a toolset name to be given to the " \
  422. "native build system to choose a compiler. " \
  423. "This is supported only on specific generators:\n" \
  424. " Visual Studio >= 10\n" \
  425. " Xcode >= 3.0\n" \
  426. "See native build system documentation for allowed toolset names."}, \
  427. {"-Wno-dev", "Suppress developer warnings.",\
  428. "Suppress warnings that are meant for the author"\
  429. " of the CMakeLists.txt files."},\
  430. {"-Wdev", "Enable developer warnings.",\
  431. "Enable warnings that are meant for the author"\
  432. " of the CMakeLists.txt files."}
  433. #endif