cmake.h 19 KB

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