cmake.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. bool directoriesSetBefore = false);
  191. ///! Is this cmake running as a result of a TRY_COMPILE command
  192. bool GetIsInTryCompile() { return this->InTryCompile; }
  193. ///! Is this cmake running as a result of a TRY_COMPILE command
  194. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  195. ///! Parse command line arguments that might set cache values
  196. bool SetCacheArgs(const std::vector<std::string>&);
  197. typedef void (*ProgressCallbackType)
  198. (const char*msg, float progress, void *);
  199. /**
  200. * Set the function used by GUI's to receive progress updates
  201. * Function gets passed: message as a const char*, a progress
  202. * amount ranging from 0 to 1.0 and client data. The progress
  203. * number provided may be negative in cases where a message is
  204. * to be displayed without any progress percentage.
  205. */
  206. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  207. ///! this is called by generators to update the progress
  208. void UpdateProgress(const char *msg, float prog);
  209. ///! get the cmake policies instance
  210. cmPolicies *GetPolicies() {return this->Policies;} ;
  211. ///! Get the variable watch object
  212. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  213. /** Get the documentation entries for the supported commands.
  214. * If withCurrentCommands is true, the documentation for the
  215. * recommended set of commands is included.
  216. * If withCompatCommands is true, the documentation for discouraged
  217. * (compatibility) commands is included.
  218. * You probably don't want to set both to false.
  219. */
  220. void GetCommandDocumentation(std::vector<cmDocumentationEntry>& entries,
  221. bool withCurrentCommands = true,
  222. bool withCompatCommands = true) const;
  223. void GetPropertiesDocumentation(std::map<std::string,
  224. cmDocumentationSection *>&);
  225. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  226. void GetPolicyDocumentation(std::vector<cmDocumentationEntry>& entries);
  227. ///! Set/Get a property of this target file
  228. void SetProperty(const char *prop, const char *value);
  229. void AppendProperty(const char *prop, const char *value);
  230. const char *GetProperty(const char *prop);
  231. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  232. bool GetPropertyAsBool(const char *prop);
  233. // Get the properties
  234. cmPropertyMap &GetProperties() { return this->Properties; };
  235. ///! Do all the checks before running configure
  236. int DoPreConfigureChecks();
  237. /**
  238. * Set and get the script mode option. In script mode there is no
  239. * generator and no cache. Also, language are not enabled, so
  240. * add_executable and things do not do anything.
  241. */
  242. void SetScriptMode(bool mode) { this->ScriptMode = mode; }
  243. bool GetScriptMode() { return this->ScriptMode; }
  244. ///! Debug the try compile stuff by not delelting 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. // Do we want debug output during the cmake run.
  261. bool GetDebugOutput() { return this->DebugOutput; }
  262. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  263. // Do we want trace output during the cmake run.
  264. bool GetTrace() { return this->Trace;}
  265. void SetTrace(bool b) { this->Trace = b;}
  266. bool GetWarnUninitialized() { return this->WarnUninitialized;}
  267. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b;}
  268. bool GetWarnUnused() { return this->WarnUnused;}
  269. void SetWarnUnused(bool b) { this->WarnUnused = b;}
  270. bool GetWarnUnusedCli() { return this->WarnUnusedCli;}
  271. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b;}
  272. bool GetCheckSystemVars() { return this->CheckSystemVars;}
  273. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b;}
  274. void MarkCliAsUsed(const std::string& variable);
  275. // Define a property
  276. void DefineProperty(const char *name, cmProperty::ScopeType scope,
  277. const char *ShortDescription,
  278. const char *FullDescription,
  279. bool chain = false,
  280. const char *variableGroup = 0);
  281. // get property definition
  282. cmPropertyDefinition *GetPropertyDefinition
  283. (const char *name, cmProperty::ScopeType scope);
  284. // Is a property defined?
  285. bool IsPropertyDefined(const char *name, cmProperty::ScopeType scope);
  286. bool IsPropertyChained(const char *name, cmProperty::ScopeType scope);
  287. /** Get the list of configurations (in upper case) considered to be
  288. debugging configurations.*/
  289. std::vector<std::string> const& GetDebugConfigs();
  290. // record accesses of properties and variables
  291. void RecordPropertyAccess(const char *name, cmProperty::ScopeType scope);
  292. void ReportUndefinedPropertyAccesses(const char *filename);
  293. // Define the properties
  294. static void DefineProperties(cmake *cm);
  295. void SetCMakeEditCommand(const char* s)
  296. {
  297. this->CMakeEditCommand = s;
  298. }
  299. void SetSuppressDevWarnings(bool v)
  300. {
  301. this->SuppressDevWarnings = v;
  302. this->DoSuppressDevWarnings = true;
  303. }
  304. /** Display a message to the user. */
  305. void IssueMessage(cmake::MessageType t, std::string const& text,
  306. cmListFileBacktrace const& backtrace);
  307. // * run the --build option
  308. int Build(const std::string& dir,
  309. const std::string& target,
  310. const std::string& config,
  311. const std::vector<std::string>& nativeOptions,
  312. bool clean);
  313. void UnwatchUnusedCli(const char* var);
  314. void WatchUnusedCli(const char* var);
  315. void RunCheckForUnusedVariables(const std::string& reason) const;
  316. protected:
  317. void InitializeProperties();
  318. int HandleDeleteCacheVariables(const char* var);
  319. cmPropertyMap Properties;
  320. std::set<std::pair<cmStdString,cmProperty::ScopeType> > AccessedProperties;
  321. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  322. PropertyDefinitions;
  323. typedef
  324. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  325. typedef std::map<cmStdString,
  326. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  327. typedef cmGlobalGenerator* (*CreateGeneratorFunctionType)();
  328. typedef std::map<cmStdString,
  329. CreateGeneratorFunctionType> RegisteredGeneratorsMap;
  330. RegisteredCommandsMap Commands;
  331. RegisteredGeneratorsMap Generators;
  332. RegisteredExtraGeneratorsMap ExtraGenerators;
  333. void AddDefaultCommands();
  334. void AddDefaultGenerators();
  335. void AddDefaultExtraGenerators();
  336. void AddExtraGenerator(const char* name,
  337. CreateExtraGeneratorFunctionType newFunction);
  338. cmPolicies *Policies;
  339. cmGlobalGenerator *GlobalGenerator;
  340. cmCacheManager *CacheManager;
  341. std::string cmHomeDirectory;
  342. std::string HomeOutputDirectory;
  343. std::string cmStartDirectory;
  344. std::string StartOutputDirectory;
  345. bool SuppressDevWarnings;
  346. bool DoSuppressDevWarnings;
  347. ///! read in a cmake list file to initialize the cache
  348. void ReadListFile(const char *path);
  349. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  350. /// If it is set, truncate it to 50kb
  351. void TruncateOutputLog(const char* fname);
  352. /**
  353. * Method called to check build system integrity at build time.
  354. * Returns 1 if CMake should rerun and 0 otherwise.
  355. */
  356. int CheckBuildSystem();
  357. void SetDirectoriesFromFile(const char* arg);
  358. //! Make sure all commands are what they say they are and there is no
  359. //macros.
  360. void CleanupCommandsAndMacros();
  361. void GenerateGraphViz(const char* fileName) const;
  362. static int SymlinkLibrary(std::vector<std::string>& args);
  363. static int SymlinkExecutable(std::vector<std::string>& args);
  364. static bool SymlinkInternal(std::string const& file,
  365. std::string const& link);
  366. static int ExecuteEchoColor(std::vector<std::string>& args);
  367. static int ExecuteLinkScript(std::vector<std::string>& args);
  368. static int VisualStudioLink(std::vector<std::string>& args, int type);
  369. static int VisualStudioLinkIncremental(std::vector<std::string>& args,
  370. int type,
  371. bool verbose);
  372. static int VisualStudioLinkNonIncremental(std::vector<std::string>& args,
  373. int type,
  374. bool hasManifest,
  375. bool verbose);
  376. static int ParseVisualStudioLinkCommand(std::vector<std::string>& args,
  377. std::vector<cmStdString>& command,
  378. std::string& targetName);
  379. static bool RunCommand(const char* comment,
  380. std::vector<cmStdString>& command,
  381. bool verbose,
  382. int* retCodeOut = 0);
  383. cmVariableWatch* VariableWatch;
  384. ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
  385. std::string FindCMakeProgram(const char* name) const;
  386. private:
  387. cmake(const cmake&); // Not implemented.
  388. void operator=(const cmake&); // Not implemented.
  389. ProgressCallbackType ProgressCallback;
  390. void* ProgressCallbackClientData;
  391. bool Verbose;
  392. bool InTryCompile;
  393. bool ScriptMode;
  394. bool DebugOutput;
  395. bool Trace;
  396. bool WarnUninitialized;
  397. bool WarnUnused;
  398. bool WarnUnusedCli;
  399. bool CheckSystemVars;
  400. std::map<std::string, bool> UsedCliVariables;
  401. std::string CMakeEditCommand;
  402. std::string CMakeCommand;
  403. std::string CXXEnvironment;
  404. std::string CCEnvironment;
  405. std::string CheckBuildSystemArgument;
  406. std::string CheckStampFile;
  407. std::string CheckStampList;
  408. std::string VSSolutionFile;
  409. std::string CTestCommand;
  410. std::string CPackCommand;
  411. bool ClearBuildSystem;
  412. bool DebugTryCompile;
  413. cmFileTimeComparison* FileComparison;
  414. std::string GraphVizFile;
  415. std::vector<std::string> DebugConfigs;
  416. void UpdateConversionPathTable();
  417. };
  418. #define CMAKE_STANDARD_OPTIONS_TABLE \
  419. {"-C <initial-cache>", "Pre-load a script to populate the cache.", \
  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 file from " \
  423. "which to load cache entries before the first pass through " \
  424. "the project's cmake listfiles. The loaded entries take priority " \
  425. "over the project's default values. The given file should be a CMake " \
  426. "script containing SET commands that use the CACHE option, " \
  427. "not a cache-format file."}, \
  428. {"-D <var>:<type>=<value>", "Create a cmake cache entry.", \
  429. "When cmake is first run in an empty build tree, it creates a " \
  430. "CMakeCache.txt file and populates it with customizable settings " \
  431. "for the project. This option may be used to specify a setting " \
  432. "that takes priority over the project's default value. The option " \
  433. "may be repeated for as many cache entries as desired."}, \
  434. {"-U <globbing_expr>", "Remove matching entries from CMake cache.", \
  435. "This option may be used to remove one or more variables from the " \
  436. "CMakeCache.txt file, globbing expressions using * and ? are supported. "\
  437. "The option may be repeated for as many cache entries as desired.\n" \
  438. "Use with care, you can make your CMakeCache.txt non-working."}, \
  439. {"-G <generator-name>", "Specify a makefile generator.", \
  440. "CMake may support multiple native build systems on certain platforms. " \
  441. "A makefile generator is responsible for generating a particular build " \
  442. "system. Possible generator names are specified in the Generators " \
  443. "section."},\
  444. {"-Wno-dev", "Suppress developer warnings.",\
  445. "Suppress warnings that are meant for the author"\
  446. " of the CMakeLists.txt files."},\
  447. {"-Wdev", "Enable developer warnings.",\
  448. "Enable warnings that are meant for the author"\
  449. " of the CMakeLists.txt files."}
  450. #define CMAKE_STANDARD_INTRODUCTION \
  451. {0, \
  452. "CMake is a cross-platform build system generator. Projects " \
  453. "specify their build process with platform-independent CMake listfiles " \
  454. "included in each directory of a source tree with the name " \
  455. "CMakeLists.txt. " \
  456. "Users build a project by using CMake to generate a build system " \
  457. "for a native tool on their platform.", 0}
  458. #endif