cmake.h 19 KB

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