cmake.h 20 KB

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