cmake.h 21 KB

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