cmake.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. /** Get the documentation entries for the supported commands.
  232. * If withCurrentCommands is true, the documentation for the
  233. * recommended set of commands is included.
  234. * If withCompatCommands is true, the documentation for discouraged
  235. * (compatibility) commands is included.
  236. * You probably don't want to set both to false.
  237. */
  238. void GetCommandDocumentation(std::vector<cmDocumentationEntry>& entries,
  239. bool withCurrentCommands = true,
  240. bool withCompatCommands = true) const;
  241. void GetPropertiesDocumentation(std::map<std::string,
  242. cmDocumentationSection *>&);
  243. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  244. void GetPolicyDocumentation(std::vector<cmDocumentationEntry>& entries);
  245. ///! Set/Get a property of this target file
  246. void SetProperty(const char *prop, const char *value);
  247. void AppendProperty(const char *prop, const char *value,bool asString=false);
  248. const char *GetProperty(const char *prop);
  249. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  250. bool GetPropertyAsBool(const char *prop);
  251. // Get the properties
  252. cmPropertyMap &GetProperties() { return this->Properties; };
  253. ///! Do all the checks before running configure
  254. int DoPreConfigureChecks();
  255. void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
  256. WorkingMode GetWorkingMode() { return this->CurrentWorkingMode; }
  257. ///! Debug the try compile stuff by not deleting the files
  258. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  259. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  260. /**
  261. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  262. */
  263. int AddCMakePaths();
  264. /**
  265. * Get the file comparison class
  266. */
  267. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  268. /**
  269. * Get the path to ctest
  270. */
  271. const char* GetCTestCommand();
  272. const char* GetCPackCommand();
  273. const char* GetCMakeCommand();
  274. // Do we want debug output during the cmake run.
  275. bool GetDebugOutput() { return this->DebugOutput; }
  276. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  277. // Do we want trace output during the cmake run.
  278. bool GetTrace() { return this->Trace;}
  279. void SetTrace(bool b) { this->Trace = b;}
  280. bool GetWarnUninitialized() { return this->WarnUninitialized;}
  281. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b;}
  282. bool GetWarnUnused() { return this->WarnUnused;}
  283. void SetWarnUnused(bool b) { this->WarnUnused = b;}
  284. bool GetWarnUnusedCli() { return this->WarnUnusedCli;}
  285. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b;}
  286. bool GetCheckSystemVars() { return this->CheckSystemVars;}
  287. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b;}
  288. void MarkCliAsUsed(const std::string& variable);
  289. // Define a property
  290. void DefineProperty(const char *name, cmProperty::ScopeType scope,
  291. const char *ShortDescription,
  292. const char *FullDescription,
  293. bool chain = false,
  294. const char *variableGroup = 0);
  295. // get property definition
  296. cmPropertyDefinition *GetPropertyDefinition
  297. (const char *name, cmProperty::ScopeType scope);
  298. // Is a property defined?
  299. bool IsPropertyDefined(const char *name, cmProperty::ScopeType scope);
  300. bool IsPropertyChained(const char *name, cmProperty::ScopeType scope);
  301. /** Get the list of configurations (in upper case) considered to be
  302. debugging configurations.*/
  303. std::vector<std::string> const& GetDebugConfigs();
  304. // Define the properties
  305. static void DefineProperties(cmake *cm);
  306. void SetCMakeEditCommand(const char* s)
  307. {
  308. this->CMakeEditCommand = s;
  309. }
  310. void SetSuppressDevWarnings(bool v)
  311. {
  312. this->SuppressDevWarnings = v;
  313. this->DoSuppressDevWarnings = true;
  314. }
  315. /** Display a message to the user. */
  316. void IssueMessage(cmake::MessageType t, std::string const& text,
  317. cmListFileBacktrace const& backtrace);
  318. ///! run the --build option
  319. int Build(const std::string& dir,
  320. const std::string& target,
  321. const std::string& config,
  322. const std::vector<std::string>& nativeOptions,
  323. bool clean,
  324. cmSystemTools::OutputOption outputflag);
  325. void UnwatchUnusedCli(const char* var);
  326. void WatchUnusedCli(const char* var);
  327. protected:
  328. void RunCheckForUnusedVariables();
  329. void InitializeProperties();
  330. int HandleDeleteCacheVariables(const char* var);
  331. cmPropertyMap Properties;
  332. std::set<std::pair<cmStdString,cmProperty::ScopeType> > AccessedProperties;
  333. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  334. PropertyDefinitions;
  335. typedef
  336. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  337. typedef std::map<cmStdString,
  338. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  339. typedef std::vector<cmGlobalGeneratorFactory*> RegisteredGeneratorsVector;
  340. RegisteredCommandsMap Commands;
  341. RegisteredGeneratorsVector Generators;
  342. RegisteredExtraGeneratorsMap ExtraGenerators;
  343. void AddDefaultCommands();
  344. void AddDefaultGenerators();
  345. void AddDefaultExtraGenerators();
  346. void AddExtraGenerator(const char* name,
  347. CreateExtraGeneratorFunctionType newFunction);
  348. cmPolicies *Policies;
  349. cmGlobalGenerator *GlobalGenerator;
  350. cmCacheManager *CacheManager;
  351. std::string cmHomeDirectory;
  352. std::string HomeOutputDirectory;
  353. std::string cmStartDirectory;
  354. std::string StartOutputDirectory;
  355. bool SuppressDevWarnings;
  356. bool DoSuppressDevWarnings;
  357. std::string GeneratorToolset;
  358. ///! read in a cmake list file to initialize the cache
  359. void ReadListFile(const std::vector<std::string>& args, const char *path);
  360. bool FindPackage(const std::vector<std::string>& args);
  361. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  362. /// If it is set, truncate it to 50kb
  363. void TruncateOutputLog(const char* fname);
  364. /**
  365. * Method called to check build system integrity at build time.
  366. * Returns 1 if CMake should rerun and 0 otherwise.
  367. */
  368. int CheckBuildSystem();
  369. void SetDirectoriesFromFile(const char* arg);
  370. //! Make sure all commands are what they say they are and there is no
  371. /// macros.
  372. void CleanupCommandsAndMacros();
  373. void GenerateGraphViz(const char* fileName) const;
  374. cmVariableWatch* VariableWatch;
  375. ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
  376. std::string FindCMakeProgram(const char* name) const;
  377. private:
  378. cmake(const cmake&); // Not implemented.
  379. void operator=(const cmake&); // Not implemented.
  380. ProgressCallbackType ProgressCallback;
  381. void* ProgressCallbackClientData;
  382. bool Verbose;
  383. bool InTryCompile;
  384. WorkingMode CurrentWorkingMode;
  385. bool DebugOutput;
  386. bool Trace;
  387. bool WarnUninitialized;
  388. bool WarnUnused;
  389. bool WarnUnusedCli;
  390. bool CheckSystemVars;
  391. std::map<cmStdString, bool> UsedCliVariables;
  392. std::string CMakeEditCommand;
  393. std::string CMakeCommand;
  394. std::string CXXEnvironment;
  395. std::string CCEnvironment;
  396. std::string CheckBuildSystemArgument;
  397. std::string CheckStampFile;
  398. std::string CheckStampList;
  399. std::string VSSolutionFile;
  400. std::string CTestCommand;
  401. std::string CPackCommand;
  402. bool ClearBuildSystem;
  403. bool DebugTryCompile;
  404. cmFileTimeComparison* FileComparison;
  405. std::string GraphVizFile;
  406. std::vector<std::string> DebugConfigs;
  407. void UpdateConversionPathTable();
  408. };
  409. #define CMAKE_STANDARD_OPTIONS_TABLE \
  410. {"-C <initial-cache>", "Pre-load a script to populate the cache.", \
  411. "When cmake is first run in an empty build tree, it creates a " \
  412. "CMakeCache.txt file and populates it with customizable settings " \
  413. "for the project. This option may be used to specify a file from " \
  414. "which to load cache entries before the first pass through " \
  415. "the project's cmake listfiles. The loaded entries take priority " \
  416. "over the project's default values. The given file should be a CMake " \
  417. "script containing SET commands that use the CACHE option, " \
  418. "not a cache-format file."}, \
  419. {"-D <var>:<type>=<value>", "Create a cmake cache entry.", \
  420. "When cmake is first run in an empty build tree, it creates a " \
  421. "CMakeCache.txt file and populates it with customizable settings " \
  422. "for the project. This option may be used to specify a setting " \
  423. "that takes priority over the project's default value. The option " \
  424. "may be repeated for as many cache entries as desired."}, \
  425. {"-U <globbing_expr>", "Remove matching entries from CMake cache.", \
  426. "This option may be used to remove one or more variables from the " \
  427. "CMakeCache.txt file, globbing expressions using * and ? are supported. "\
  428. "The option may be repeated for as many cache entries as desired.\n" \
  429. "Use with care, you can make your CMakeCache.txt non-working."}, \
  430. {"-G <generator-name>", "Specify a build system generator.", \
  431. "CMake may support multiple native build systems on certain platforms. " \
  432. "A generator is responsible for generating a particular build " \
  433. "system. Possible generator names are specified in the Generators " \
  434. "section."},\
  435. {"-T <toolset-name>", "Specify toolset name if supported by generator.", \
  436. "Some CMake generators support a toolset name to be given to the " \
  437. "native build system to choose a compiler. " \
  438. "This is supported only on specific generators:\n" \
  439. " Visual Studio >= 10\n" \
  440. " Xcode >= 3.0\n" \
  441. "See native build system documentation for allowed toolset names."}, \
  442. {"-Wno-dev", "Suppress developer warnings.",\
  443. "Suppress warnings that are meant for the author"\
  444. " of the CMakeLists.txt files."},\
  445. {"-Wdev", "Enable developer warnings.",\
  446. "Enable warnings that are meant for the author"\
  447. " of the CMakeLists.txt files."}
  448. #define CMAKE_STANDARD_INTRODUCTION \
  449. {0, \
  450. "CMake is a cross-platform build system generator. Projects " \
  451. "specify their build process with platform-independent CMake listfiles " \
  452. "included in each directory of a source tree with the name " \
  453. "CMakeLists.txt. " \
  454. "Users build a project by using CMake to generate a build system " \
  455. "for a native tool on their platform.", 0}
  456. #endif