cmake.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * Handle a command line invocation of cmake.
  115. */
  116. int Run(const std::vector<std::string>&args)
  117. { return this->Run(args, false); }
  118. int Run(const std::vector<std::string>&args, bool noconfigure);
  119. /**
  120. * Run the global generator Generate step.
  121. */
  122. int Generate();
  123. /**
  124. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  125. * one has not already been set. It will then Call Configure on the
  126. * GlobalGenerator. This in turn will read in an process all the CMakeList
  127. * files for the tree. It will not produce any actual Makefiles, or
  128. * workspaces. Generate does that. */
  129. int Configure();
  130. int ActualConfigure();
  131. /**
  132. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  133. * one has not already been set. It will then Call Configure on the
  134. * GlobalGenerator. This in turn will read in an process all the CMakeList
  135. * files for the tree. It will not produce any actual Makefiles, or
  136. * workspaces. Generate does that. */
  137. int LoadCache();
  138. void PreLoadCMakeFiles();
  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 this->GlobalGenerator; }
  143. ///! Return the global generator assigned to this instance of cmake, const
  144. const cmGlobalGenerator* GetGlobalGenerator() const
  145. { return this->GlobalGenerator; }
  146. ///! Return the global generator assigned to this instance of cmake
  147. void SetGlobalGenerator(cmGlobalGenerator *);
  148. ///! Get the names of the current registered generators
  149. void GetRegisteredGenerators(std::vector<std::string>& names);
  150. ///! get the cmCachemManager used by this invocation of cmake
  151. cmCacheManager *GetCacheManager() { return this->CacheManager; }
  152. ///! set the cmake command this instance of cmake should use
  153. void SetCMakeCommand(const char* cmd) { this->CMakeCommand = cmd; }
  154. /**
  155. * Given a variable name, return its value (as a string).
  156. */
  157. const char* GetCacheDefinition(const char*) const;
  158. ///! Add an entry into the cache
  159. void AddCacheEntry(const char* key, const char* value,
  160. const char* helpString,
  161. int type);
  162. /**
  163. * Execute commands during the build process. Supports options such
  164. * as echo, remove file etc.
  165. */
  166. static int ExecuteCMakeCommand(std::vector<std::string>&);
  167. /**
  168. * Get the system information and write it to the file specified
  169. */
  170. int GetSystemInformation(std::vector<std::string>&);
  171. /**
  172. * Add a command to this cmake instance
  173. */
  174. void AddCommand(cmCommand* );
  175. void RenameCommand(const char* oldName, const char* newName);
  176. void RemoveCommand(const char* name);
  177. void RemoveUnscriptableCommands();
  178. /**
  179. * Get a command by its name
  180. */
  181. cmCommand *GetCommand(const char *name);
  182. /** Get list of all commands */
  183. RegisteredCommandsMap* GetCommands() { return &this->Commands; }
  184. /** Check if a command exists. */
  185. bool CommandExists(const char* name) const;
  186. ///! Parse command line arguments
  187. void SetArgs(const std::vector<std::string>&);
  188. ///! Is this cmake running as a result of a TRY_COMPILE command
  189. bool GetIsInTryCompile() { return this->InTryCompile; }
  190. ///! Is this cmake running as a result of a TRY_COMPILE command
  191. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  192. ///! Parse command line arguments that might set cache values
  193. bool SetCacheArgs(const std::vector<std::string>&);
  194. typedef void (*ProgressCallbackType)
  195. (const char*msg, float progress, void *);
  196. /**
  197. * Set the function used by GUI's to receive progress updates
  198. * Function gets passed: message as a const char*, a progress
  199. * amount ranging from 0 to 1.0 and client data. The progress
  200. * number provided may be negative in cases where a message is
  201. * to be displayed without any progress percentage.
  202. */
  203. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  204. ///! this is called by generators to update the progress
  205. void UpdateProgress(const char *msg, float prog);
  206. ///! get the cmake policies instance
  207. cmPolicies *GetPolicies() {return this->Policies;} ;
  208. ///! Get the variable watch object
  209. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  210. /** Get the documentation entries for the supported commands.
  211. * If withCurrentCommands is true, the documentation for the
  212. * recommended set of commands is included.
  213. * If withCompatCommands is true, the documentation for discouraged
  214. * (compatibility) commands is included.
  215. * You probably don't want to set both to false.
  216. */
  217. void GetCommandDocumentation(std::vector<cmDocumentationEntry>& entries,
  218. bool withCurrentCommands = true,
  219. bool withCompatCommands = true) const;
  220. void GetPropertiesDocumentation(std::map<std::string,
  221. cmDocumentationSection *>&);
  222. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  223. void GetPolicyDocumentation(std::vector<cmDocumentationEntry>& entries);
  224. ///! Set/Get a property of this target file
  225. void SetProperty(const char *prop, const char *value);
  226. void AppendProperty(const char *prop, const char *value);
  227. const char *GetProperty(const char *prop);
  228. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  229. bool GetPropertyAsBool(const char *prop);
  230. // Get the properties
  231. cmPropertyMap &GetProperties() { return this->Properties; };
  232. ///! Do all the checks before running configure
  233. int DoPreConfigureChecks();
  234. /**
  235. * Set and get the script mode option. In script mode there is no
  236. * generator and no cache. Also, language are not enabled, so
  237. * add_executable and things do not do anything.
  238. */
  239. void SetScriptMode(bool mode) { this->ScriptMode = mode; }
  240. bool GetScriptMode() { return this->ScriptMode; }
  241. ///! Debug the try compile stuff by not delelting the files
  242. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  243. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  244. /**
  245. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  246. */
  247. int AddCMakePaths();
  248. /**
  249. * Get the file comparison class
  250. */
  251. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  252. /**
  253. * Get the path to ctest
  254. */
  255. const char* GetCTestCommand();
  256. const char* GetCPackCommand();
  257. // Do we want debug output during the cmake run.
  258. bool GetDebugOutput() { return this->DebugOutput; }
  259. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  260. // Do we want trace output during the cmake run.
  261. bool GetTrace() { return this->Trace;}
  262. void SetTrace(bool b) { this->Trace = b;}
  263. bool GetWarnUninitialized() { return this->WarnUninitialized;}
  264. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b;}
  265. bool GetWarnUnused() { return this->WarnUnused;}
  266. void SetWarnUnused(bool b) { this->WarnUnused = b;}
  267. bool GetWarnUnusedCli() { return this->WarnUnusedCli;}
  268. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b;}
  269. void MarkCliAsUsed(const std::string& variable);
  270. // Define a property
  271. void DefineProperty(const char *name, cmProperty::ScopeType scope,
  272. const char *ShortDescription,
  273. const char *FullDescription,
  274. bool chain = false,
  275. const char *variableGroup = 0);
  276. // get property definition
  277. cmPropertyDefinition *GetPropertyDefinition
  278. (const char *name, cmProperty::ScopeType scope);
  279. // Is a property defined?
  280. bool IsPropertyDefined(const char *name, cmProperty::ScopeType scope);
  281. bool IsPropertyChained(const char *name, cmProperty::ScopeType scope);
  282. /** Get the list of configurations (in upper case) considered to be
  283. debugging configurations.*/
  284. std::vector<std::string> const& GetDebugConfigs();
  285. // record accesses of properties and variables
  286. void RecordPropertyAccess(const char *name, cmProperty::ScopeType scope);
  287. void ReportUndefinedPropertyAccesses(const char *filename);
  288. // Define the properties
  289. static void DefineProperties(cmake *cm);
  290. void SetCMakeEditCommand(const char* s)
  291. {
  292. this->CMakeEditCommand = s;
  293. }
  294. void SetSuppressDevWarnings(bool v)
  295. {
  296. this->SuppressDevWarnings = v;
  297. this->DoSuppressDevWarnings = true;
  298. }
  299. /** Display a message to the user. */
  300. void IssueMessage(cmake::MessageType t, std::string const& text,
  301. cmListFileBacktrace const& backtrace);
  302. // * run the --build option
  303. int Build(const std::string& dir,
  304. const std::string& target,
  305. const std::string& config,
  306. const std::vector<std::string>& nativeOptions,
  307. bool clean);
  308. protected:
  309. void InitializeProperties();
  310. int HandleDeleteCacheVariables(const char* var);
  311. cmPropertyMap Properties;
  312. std::set<std::pair<cmStdString,cmProperty::ScopeType> > AccessedProperties;
  313. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  314. PropertyDefinitions;
  315. typedef
  316. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  317. typedef std::map<cmStdString,
  318. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  319. typedef cmGlobalGenerator* (*CreateGeneratorFunctionType)();
  320. typedef std::map<cmStdString,
  321. CreateGeneratorFunctionType> RegisteredGeneratorsMap;
  322. RegisteredCommandsMap Commands;
  323. RegisteredGeneratorsMap Generators;
  324. RegisteredExtraGeneratorsMap ExtraGenerators;
  325. void AddDefaultCommands();
  326. void AddDefaultGenerators();
  327. void AddDefaultExtraGenerators();
  328. void AddExtraGenerator(const char* name,
  329. CreateExtraGeneratorFunctionType newFunction);
  330. cmPolicies *Policies;
  331. cmGlobalGenerator *GlobalGenerator;
  332. cmCacheManager *CacheManager;
  333. std::string cmHomeDirectory;
  334. std::string HomeOutputDirectory;
  335. std::string cmStartDirectory;
  336. std::string StartOutputDirectory;
  337. bool SuppressDevWarnings;
  338. bool DoSuppressDevWarnings;
  339. ///! read in a cmake list file to initialize the cache
  340. void ReadListFile(const char *path);
  341. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  342. /// If it is set, truncate it to 50kb
  343. void TruncateOutputLog(const char* fname);
  344. /**
  345. * Method called to check build system integrity at build time.
  346. * Returns 1 if CMake should rerun and 0 otherwise.
  347. */
  348. int CheckBuildSystem();
  349. void SetDirectoriesFromFile(const char* arg);
  350. //! Make sure all commands are what they say they are and there is no
  351. //macros.
  352. void CleanupCommandsAndMacros();
  353. void GenerateGraphViz(const char* fileName) const;
  354. static int SymlinkLibrary(std::vector<std::string>& args);
  355. static int SymlinkExecutable(std::vector<std::string>& args);
  356. static bool SymlinkInternal(std::string const& file,
  357. std::string const& link);
  358. static int ExecuteEchoColor(std::vector<std::string>& args);
  359. static int ExecuteLinkScript(std::vector<std::string>& args);
  360. static int VisualStudioLink(std::vector<std::string>& args, int type);
  361. static int VisualStudioLinkIncremental(std::vector<std::string>& args,
  362. int type,
  363. bool verbose);
  364. static int VisualStudioLinkNonIncremental(std::vector<std::string>& args,
  365. int type,
  366. bool hasManifest,
  367. bool verbose);
  368. static int ParseVisualStudioLinkCommand(std::vector<std::string>& args,
  369. std::vector<cmStdString>& command,
  370. std::string& targetName);
  371. static bool RunCommand(const char* comment,
  372. std::vector<cmStdString>& command,
  373. bool verbose,
  374. int* retCodeOut = 0);
  375. cmVariableWatch* VariableWatch;
  376. ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
  377. std::string FindCMakeProgram(const char* name) const;
  378. private:
  379. cmake(const cmake&); // Not implemented.
  380. void operator=(const cmake&); // Not implemented.
  381. ProgressCallbackType ProgressCallback;
  382. void* ProgressCallbackClientData;
  383. bool Verbose;
  384. bool InTryCompile;
  385. bool ScriptMode;
  386. bool DebugOutput;
  387. bool Trace;
  388. bool WarnUninitialized;
  389. bool WarnUnused;
  390. bool WarnUnusedCli;
  391. std::map<std::string, 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 makefile generator.", \
  431. "CMake may support multiple native build systems on certain platforms. " \
  432. "A makefile generator is responsible for generating a particular build " \
  433. "system. Possible generator names are specified in the Generators " \
  434. "section."},\
  435. {"-Wno-dev", "Suppress developer warnings.",\
  436. "Suppress warnings that are meant for the author"\
  437. " of the CMakeLists.txt files."},\
  438. {"-Wdev", "Enable developer warnings.",\
  439. "Enable warnings that are meant for the author"\
  440. " of the CMakeLists.txt files."}
  441. #define CMAKE_STANDARD_INTRODUCTION \
  442. {0, \
  443. "CMake is a cross-platform build system generator. Projects " \
  444. "specify their build process with platform-independent CMake listfiles " \
  445. "included in each directory of a source tree with the name " \
  446. "CMakeLists.txt. " \
  447. "Users build a project by using CMake to generate a build system " \
  448. "for a native tool on their platform.", 0}
  449. #endif