cmake.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. // This class represents a cmake invocation. It is the top level class when
  11. // running cmake. Most cmake based GUIS should primarily create an instance
  12. // of this class and communicate with it.
  13. //
  14. // The basic process for a GUI is as follows:
  15. //
  16. // 1) Create a cmake instance
  17. // 2) Set the Home & Start directories, generator, and cmake command. this
  18. // can be done using the Set methods or by using SetArgs and passing in
  19. // command line arguments.
  20. // 3) Load the cache by calling LoadCache (duh)
  21. // 4) if you are using command line arguments with -D or -C flags then
  22. // call SetCacheArgs (or if for some other reason you want to modify the
  23. // cache, do it now.
  24. // 5) Finally call Configure
  25. // 6) Let the user change values and go back to step 5
  26. // 7) call Generate
  27. //
  28. // If your GUI allows the user to change the start & home directories then
  29. // you must at a minimum redo steps 2 through 7.
  30. //
  31. #ifndef cmake_h
  32. #define cmake_h
  33. #include "cmSystemTools.h"
  34. #include "cmPropertyDefinitionMap.h"
  35. #include "cmPropertyMap.h"
  36. class cmGlobalGenerator;
  37. class cmLocalGenerator;
  38. class cmCacheManager;
  39. class cmMakefile;
  40. class cmCommand;
  41. class cmVariableWatch;
  42. class cmFileTimeComparison;
  43. class cmExternalMakefileProjectGenerator;
  44. class cmDocumentationSection;
  45. class cmPolicies;
  46. class cmListFileBacktrace;
  47. class cmake
  48. {
  49. public:
  50. enum MessageType
  51. { AUTHOR_WARNING,
  52. FATAL_ERROR,
  53. INTERNAL_ERROR,
  54. MESSAGE,
  55. WARNING,
  56. LOG
  57. };
  58. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  59. ///! construct an instance of cmake
  60. cmake();
  61. ///! destruct an instance of cmake
  62. ~cmake();
  63. ///! construct an instance of cmake
  64. static const char *GetCMakeFilesDirectory() {return "/CMakeFiles";};
  65. static const char *GetCMakeFilesDirectoryPostSlash() {
  66. return "CMakeFiles/";};
  67. //@{
  68. /**
  69. * Set/Get the home directory (or output directory) in the project. The
  70. * home directory is the top directory of the project. It is where
  71. * cmake was run. Remember that CMake processes
  72. * CMakeLists files by recursing up the tree starting at the StartDirectory
  73. * and going up until it reaches the HomeDirectory.
  74. */
  75. void SetHomeDirectory(const char* dir);
  76. const char* GetHomeDirectory() const
  77. {
  78. return this->cmHomeDirectory.c_str();
  79. }
  80. void SetHomeOutputDirectory(const char* lib);
  81. const char* GetHomeOutputDirectory() const
  82. {
  83. return this->HomeOutputDirectory.c_str();
  84. }
  85. //@}
  86. //@{
  87. /**
  88. * Set/Get the start directory (or output directory). The start directory
  89. * is the directory of the CMakeLists.txt file that started the current
  90. * round of processing. Remember that CMake processes CMakeLists files by
  91. * recursing up the tree starting at the StartDirectory and going up until
  92. * it reaches the HomeDirectory.
  93. */
  94. void SetStartDirectory(const char* dir)
  95. {
  96. this->cmStartDirectory = dir;
  97. cmSystemTools::ConvertToUnixSlashes(this->cmStartDirectory);
  98. }
  99. const char* GetStartDirectory() const
  100. {
  101. return this->cmStartDirectory.c_str();
  102. }
  103. void SetStartOutputDirectory(const char* lib)
  104. {
  105. this->StartOutputDirectory = lib;
  106. cmSystemTools::ConvertToUnixSlashes(this->StartOutputDirectory);
  107. }
  108. const char* GetStartOutputDirectory() const
  109. {
  110. return this->StartOutputDirectory.c_str();
  111. }
  112. //@}
  113. /**
  114. * Dump documentation to a file. If 0 is returned, the
  115. * operation failed.
  116. */
  117. int DumpDocumentationToFile(std::ostream&);
  118. /**
  119. * Handle a command line invocation of cmake.
  120. */
  121. int Run(const std::vector<std::string>&args)
  122. { return this->Run(args, false); }
  123. int Run(const std::vector<std::string>&args, bool noconfigure);
  124. /**
  125. * Run the global generator Generate step.
  126. */
  127. int Generate();
  128. /**
  129. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  130. * one has not already been set. It will then Call Configure on the
  131. * GlobalGenerator. This in turn will read in an process all the CMakeList
  132. * files for the tree. It will not produce any actual Makefiles, or
  133. * workspaces. Generate does that. */
  134. int Configure();
  135. int ActualConfigure();
  136. /**
  137. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  138. * one has not already been set. It will then Call Configure on the
  139. * GlobalGenerator. This in turn will read in an process all the CMakeList
  140. * files for the tree. It will not produce any actual Makefiles, or
  141. * workspaces. Generate does that. */
  142. int LoadCache();
  143. void PreLoadCMakeFiles();
  144. ///! Create a GlobalGenerator
  145. cmGlobalGenerator* CreateGlobalGenerator(const char* name);
  146. ///! Return the global generator assigned to this instance of cmake
  147. cmGlobalGenerator* GetGlobalGenerator() { return this->GlobalGenerator; }
  148. ///! Return the global generator assigned to this instance of cmake, const
  149. const cmGlobalGenerator* GetGlobalGenerator() const
  150. { return this->GlobalGenerator; }
  151. ///! Return the global generator assigned to this instance of cmake
  152. void SetGlobalGenerator(cmGlobalGenerator *);
  153. ///! Get the names of the current registered generators
  154. void GetRegisteredGenerators(std::vector<std::string>& names);
  155. ///! get the cmCachemManager used by this invocation of cmake
  156. cmCacheManager *GetCacheManager() { return this->CacheManager; }
  157. ///! set the cmake command this instance of cmake should use
  158. void SetCMakeCommand(const char* cmd) { this->CMakeCommand = cmd; }
  159. /**
  160. * Given a variable name, return its value (as a string).
  161. */
  162. const char* GetCacheDefinition(const char*) const;
  163. ///! Add an entry into the cache
  164. void AddCacheEntry(const char* key, const char* value,
  165. const char* helpString,
  166. int type);
  167. /**
  168. * Execute commands during the build process. Supports options such
  169. * as echo, remove file etc.
  170. */
  171. static int ExecuteCMakeCommand(std::vector<std::string>&);
  172. /**
  173. * Get the system information and write it to the file specified
  174. */
  175. int GetSystemInformation(std::vector<std::string>&);
  176. /**
  177. * Add a command to this cmake instance
  178. */
  179. void AddCommand(cmCommand* );
  180. void RenameCommand(const char* oldName, const char* newName);
  181. void RemoveCommand(const char* name);
  182. void RemoveUnscriptableCommands();
  183. /**
  184. * Get a command by its name
  185. */
  186. cmCommand *GetCommand(const char *name);
  187. /** Get list of all commands */
  188. RegisteredCommandsMap* GetCommands() { return &this->Commands; }
  189. /** Check if a command exists. */
  190. bool CommandExists(const char* name) const;
  191. ///! Parse command line arguments
  192. void SetArgs(const std::vector<std::string>&);
  193. ///! Is this cmake running as a result of a TRY_COMPILE command
  194. bool GetIsInTryCompile() { return this->InTryCompile; }
  195. ///! Is this cmake running as a result of a TRY_COMPILE command
  196. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  197. ///! Parse command line arguments that might set cache values
  198. bool SetCacheArgs(const std::vector<std::string>&);
  199. typedef void (*ProgressCallbackType)
  200. (const char*msg, float progress, void *);
  201. /**
  202. * Set the function used by GUI's to receive progress updates
  203. * Function gets passed: message as a const char*, a progress
  204. * amount ranging from 0 to 1.0 and client data. The progress
  205. * number provided may be negative in cases where a message is
  206. * to be displayed without any progress percentage.
  207. */
  208. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  209. ///! this is called by generators to update the progress
  210. void UpdateProgress(const char *msg, float prog);
  211. ///! get the cmake policies instance
  212. cmPolicies *GetPolicies() {return this->Policies;} ;
  213. ///! Get the variable watch object
  214. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  215. /** Get the documentation entries for the supported commands.
  216. * If withCurrentCommands is true, the documentation for the
  217. * recommended set of commands is included.
  218. * If withCompatCommands is true, the documentation for discouraged
  219. * (compatibility) commands is included.
  220. * You probably don't want to set both to false.
  221. */
  222. void GetCommandDocumentation(std::vector<cmDocumentationEntry>& entries,
  223. bool withCurrentCommands = true,
  224. bool withCompatCommands = true) const;
  225. void GetPropertiesDocumentation(std::map<std::string,
  226. cmDocumentationSection *>&);
  227. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  228. void GetPolicyDocumentation(std::vector<cmDocumentationEntry>& entries);
  229. ///! Set/Get a property of this target file
  230. void SetProperty(const char *prop, const char *value);
  231. void AppendProperty(const char *prop, const char *value);
  232. const char *GetProperty(const char *prop);
  233. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  234. bool GetPropertyAsBool(const char *prop);
  235. // Get the properties
  236. cmPropertyMap &GetProperties() { return this->Properties; };
  237. ///! Do all the checks before running configure
  238. int DoPreConfigureChecks();
  239. /**
  240. * Set and get the script mode option. In script mode there is no
  241. * generator and no cache. Also, language are not enabled, so
  242. * add_executable and things do not do anything.
  243. */
  244. void SetScriptMode(bool mode) { this->ScriptMode = mode; }
  245. bool GetScriptMode() { return this->ScriptMode; }
  246. ///! Debug the try compile stuff by not delelting the files
  247. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  248. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  249. /**
  250. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  251. */
  252. int AddCMakePaths();
  253. /**
  254. * Get the file comparison class
  255. */
  256. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  257. /**
  258. * Get the path to ctest
  259. */
  260. const char* GetCTestCommand();
  261. const char* GetCPackCommand();
  262. // Do we want debug output during the cmake run.
  263. bool GetDebugOutput() { return this->DebugOutput; }
  264. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  265. // Do we want trace output during the cmake run.
  266. bool GetTrace() { return this->Trace;}
  267. void SetTrace(bool b) { this->Trace = b;}
  268. // Define a property
  269. void DefineProperty(const char *name, cmProperty::ScopeType scope,
  270. const char *ShortDescription,
  271. const char *FullDescription,
  272. bool chain = false,
  273. const char *variableGroup = 0);
  274. // get property definition
  275. cmPropertyDefinition *GetPropertyDefinition
  276. (const char *name, cmProperty::ScopeType scope);
  277. // Is a property defined?
  278. bool IsPropertyDefined(const char *name, cmProperty::ScopeType scope);
  279. bool IsPropertyChained(const char *name, cmProperty::ScopeType scope);
  280. /** Get the list of configurations (in upper case) considered to be
  281. debugging configurations.*/
  282. std::vector<std::string> const& GetDebugConfigs();
  283. // record accesses of properties and variables
  284. void RecordPropertyAccess(const char *name, cmProperty::ScopeType scope);
  285. void ReportUndefinedPropertyAccesses(const char *filename);
  286. // Define the properties
  287. static void DefineProperties(cmake *cm);
  288. void SetCMakeEditCommand(const char* s)
  289. {
  290. this->CMakeEditCommand = s;
  291. }
  292. void SetSuppressDevWarnings(bool v)
  293. {
  294. this->SuppressDevWarnings = v;
  295. this->DoSuppressDevWarnings = true;
  296. }
  297. /** Display a message to the user. */
  298. void IssueMessage(cmake::MessageType t, std::string const& text,
  299. cmListFileBacktrace const& backtrace);
  300. // * run the --build option
  301. int Build(const std::string& dir,
  302. const std::string& target,
  303. const std::string& config,
  304. const std::vector<std::string>& nativeOptions,
  305. bool clean);
  306. protected:
  307. void InitializeProperties();
  308. int HandleDeleteCacheVariables(const char* var);
  309. cmPropertyMap Properties;
  310. std::set<std::pair<cmStdString,cmProperty::ScopeType> > AccessedProperties;
  311. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  312. PropertyDefinitions;
  313. typedef
  314. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  315. typedef std::map<cmStdString,
  316. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  317. typedef cmGlobalGenerator* (*CreateGeneratorFunctionType)();
  318. typedef std::map<cmStdString,
  319. CreateGeneratorFunctionType> RegisteredGeneratorsMap;
  320. RegisteredCommandsMap Commands;
  321. RegisteredGeneratorsMap Generators;
  322. RegisteredExtraGeneratorsMap ExtraGenerators;
  323. void AddDefaultCommands();
  324. void AddDefaultGenerators();
  325. void AddDefaultExtraGenerators();
  326. void AddExtraGenerator(const char* name,
  327. CreateExtraGeneratorFunctionType newFunction);
  328. cmPolicies *Policies;
  329. cmGlobalGenerator *GlobalGenerator;
  330. cmCacheManager *CacheManager;
  331. std::string cmHomeDirectory;
  332. std::string HomeOutputDirectory;
  333. std::string cmStartDirectory;
  334. std::string StartOutputDirectory;
  335. bool SuppressDevWarnings;
  336. bool DoSuppressDevWarnings;
  337. ///! read in a cmake list file to initialize the cache
  338. void ReadListFile(const char *path);
  339. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  340. /// If it is set, truncate it to 50kb
  341. void TruncateOutputLog(const char* fname);
  342. /**
  343. * Method called to check build system integrity at build time.
  344. * Returns 1 if CMake should rerun and 0 otherwise.
  345. */
  346. int CheckBuildSystem();
  347. void SetDirectoriesFromFile(const char* arg);
  348. //! Make sure all commands are what they say they are and there is no
  349. //macros.
  350. void CleanupCommandsAndMacros();
  351. void GenerateGraphViz(const char* fileName) const;
  352. static int SymlinkLibrary(std::vector<std::string>& args);
  353. static int SymlinkExecutable(std::vector<std::string>& args);
  354. static bool SymlinkInternal(std::string const& file,
  355. std::string const& link);
  356. static int ExecuteEchoColor(std::vector<std::string>& args);
  357. static int ExecuteLinkScript(std::vector<std::string>& args);
  358. static int VisualStudioLink(std::vector<std::string>& args, int type);
  359. static int VisualStudioLinkIncremental(std::vector<std::string>& args,
  360. int type,
  361. bool verbose);
  362. static int VisualStudioLinkNonIncremental(std::vector<std::string>& args,
  363. int type,
  364. bool hasManifest,
  365. bool verbose);
  366. static int ParseVisualStudioLinkCommand(std::vector<std::string>& args,
  367. std::vector<cmStdString>& command,
  368. std::string& targetName);
  369. static bool RunCommand(const char* comment,
  370. std::vector<cmStdString>& command,
  371. bool verbose,
  372. int* retCodeOut = 0);
  373. cmVariableWatch* VariableWatch;
  374. ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
  375. std::string FindCMakeProgram(const char* name) const;
  376. private:
  377. cmake(const cmake&); // Not implemented.
  378. void operator=(const cmake&); // Not implemented.
  379. ProgressCallbackType ProgressCallback;
  380. void* ProgressCallbackClientData;
  381. bool Verbose;
  382. bool InTryCompile;
  383. bool ScriptMode;
  384. bool DebugOutput;
  385. bool Trace;
  386. std::string CMakeEditCommand;
  387. std::string CMakeCommand;
  388. std::string CXXEnvironment;
  389. std::string CCEnvironment;
  390. std::string CheckBuildSystemArgument;
  391. std::string CheckStampFile;
  392. std::string CheckStampList;
  393. std::string VSSolutionFile;
  394. std::string CTestCommand;
  395. std::string CPackCommand;
  396. bool ClearBuildSystem;
  397. bool DebugTryCompile;
  398. cmFileTimeComparison* FileComparison;
  399. std::string GraphVizFile;
  400. std::vector<std::string> DebugConfigs;
  401. void UpdateConversionPathTable();
  402. };
  403. #define CMAKE_STANDARD_OPTIONS_TABLE \
  404. {"-C <initial-cache>", "Pre-load a script to populate the cache.", \
  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 file from " \
  408. "which to load cache entries before the first pass through " \
  409. "the project's cmake listfiles. The loaded entries take priority " \
  410. "over the project's default values. The given file should be a CMake " \
  411. "script containing SET commands that use the CACHE option, " \
  412. "not a cache-format file."}, \
  413. {"-D <var>:<type>=<value>", "Create a cmake cache entry.", \
  414. "When cmake is first run in an empty build tree, it creates a " \
  415. "CMakeCache.txt file and populates it with customizable settings " \
  416. "for the project. This option may be used to specify a setting " \
  417. "that takes priority over the project's default value. The option " \
  418. "may be repeated for as many cache entries as desired."}, \
  419. {"-U <globbing_expr>", "Remove matching entries from CMake cache.", \
  420. "This option may be used to remove one or more variables from the " \
  421. "CMakeCache.txt file, globbing expressions using * and ? are supported. "\
  422. "The option may be repeated for as many cache entries as desired.\n" \
  423. "Use with care, you can make your CMakeCache.txt non-working."}, \
  424. {"-G <generator-name>", "Specify a makefile generator.", \
  425. "CMake may support multiple native build systems on certain platforms. " \
  426. "A makefile generator is responsible for generating a particular build " \
  427. "system. Possible generator names are specified in the Generators " \
  428. "section."},\
  429. {"-Wno-dev", "Suppress developer warnings.",\
  430. "Suppress warnings that are meant for the author"\
  431. " of the CMakeLists.txt files."},\
  432. {"-Wdev", "Enable developer warnings.",\
  433. "Enable warnings that are meant for the author"\
  434. " of the CMakeLists.txt files."}
  435. #define CMAKE_STANDARD_INTRODUCTION \
  436. {0, \
  437. "CMake is a cross-platform build system generator. Projects " \
  438. "specify their build process with platform-independent CMake listfiles " \
  439. "included in each directory of a source tree with the name " \
  440. "CMakeLists.txt. " \
  441. "Users build a project by using CMake to generate a build system " \
  442. "for a native tool on their platform.", 0}
  443. #endif