1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmake.h,v $
6 Date: $Date: 2008-04-02 21:29:25 $
7 Version: $Revision: 1.113 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 // This class represents a cmake invocation. It is the top level class when
18 // running cmake. Most cmake based GUIS should primarily create an instance
19 // of this class and communicate with it.
21 // The basic process for a GUI is as follows:
23 // 1) Create a cmake instance
24 // 2) Set the Home & Start directories, generator, and cmake command. this
25 // can be done using the Set methods or by using SetArgs and passing in
26 // command line arguments.
27 // 3) Load the cache by calling LoadCache (duh)
28 // 4) if you are using command line arguments with -D or -C flags then
29 // call SetCacheArgs (or if for some other reason you want to modify the
31 // 5) Finally call Configure
32 // 6) Let the user change values and go back to step 5
35 // If your GUI allows the user to change the start & home directories then
36 // you must at a minimum redo steps 2 through 7.
43 #include "cmSystemTools.h"
44 #include "cmPropertyDefinitionMap.h"
45 #include "cmPropertyMap.h"
47 class cmGlobalGenerator
;
48 class cmLocalGenerator
;
52 class cmVariableWatch
;
53 class cmFileTimeComparison
;
54 class cmExternalMakefileProjectGenerator
;
55 class cmDocumentationSection
;
57 class cmListFileBacktrace
;
70 typedef std::map
<cmStdString
, cmCommand
*> RegisteredCommandsMap
;
72 ///! construct an instance of cmake
74 ///! destruct an instance of cmake
77 ///! construct an instance of cmake
78 static const char *GetCMakeFilesDirectory() {return "/CMakeFiles";};
79 static const char *GetCMakeFilesDirectoryPostSlash() {
80 return "CMakeFiles/";};
84 * Set/Get the home directory (or output directory) in the project. The
85 * home directory is the top directory of the project. It is where
86 * cmake was run. Remember that CMake processes
87 * CMakeLists files by recursing up the tree starting at the StartDirectory
88 * and going up until it reaches the HomeDirectory.
90 void SetHomeDirectory(const char* dir
);
91 const char* GetHomeDirectory() const
93 return this->cmHomeDirectory
.c_str();
95 void SetHomeOutputDirectory(const char* lib
);
96 const char* GetHomeOutputDirectory() const
98 return this->HomeOutputDirectory
.c_str();
104 * Set/Get the start directory (or output directory). The start directory
105 * is the directory of the CMakeLists.txt file that started the current
106 * round of processing. Remember that CMake processes CMakeLists files by
107 * recursing up the tree starting at the StartDirectory and going up until
108 * it reaches the HomeDirectory.
110 void SetStartDirectory(const char* dir
)
112 this->cmStartDirectory
= dir
;
113 cmSystemTools::ConvertToUnixSlashes(this->cmStartDirectory
);
115 const char* GetStartDirectory() const
117 return this->cmStartDirectory
.c_str();
119 void SetStartOutputDirectory(const char* lib
)
121 this->StartOutputDirectory
= lib
;
122 cmSystemTools::ConvertToUnixSlashes(this->StartOutputDirectory
);
124 const char* GetStartOutputDirectory() const
126 return this->StartOutputDirectory
.c_str();
131 * Dump documentation to a file. If 0 is returned, the
134 int DumpDocumentationToFile(std::ostream
&);
137 * Handle a command line invocation of cmake.
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
);
144 * Run the global generator Generate step.
149 * Configure the cmMakefiles. This routine will create a GlobalGenerator if
150 * one has not already been set. It will then Call Configure on the
151 * GlobalGenerator. This in turn will read in an process all the CMakeList
152 * files for the tree. It will not produce any actual Makefiles, or
153 * workspaces. Generate does that. */
155 int ActualConfigure();
158 * Configure the cmMakefiles. This routine will create a GlobalGenerator if
159 * one has not already been set. It will then Call Configure on the
160 * GlobalGenerator. This in turn will read in an process all the CMakeList
161 * files for the tree. It will not produce any actual Makefiles, or
162 * workspaces. Generate does that. */
164 void PreLoadCMakeFiles();
166 ///! Create a GlobalGenerator
167 cmGlobalGenerator
* CreateGlobalGenerator(const char* name
);
169 ///! Return the global generator assigned to this instance of cmake
170 cmGlobalGenerator
* GetGlobalGenerator() { return this->GlobalGenerator
; }
171 ///! Return the global generator assigned to this instance of cmake, const
172 const cmGlobalGenerator
* GetGlobalGenerator() const
173 { return this->GlobalGenerator
; }
175 ///! Return the global generator assigned to this instance of cmake
176 void SetGlobalGenerator(cmGlobalGenerator
*);
178 ///! Get the names of the current registered generators
179 void GetRegisteredGenerators(std::vector
<std::string
>& names
);
181 ///! get the cmCachemManager used by this invocation of cmake
182 cmCacheManager
*GetCacheManager() { return this->CacheManager
; }
184 ///! set the cmake command this instance of cmake should use
185 void SetCMakeCommand(const char* cmd
) { this->CMakeCommand
= cmd
; }
188 * Given a variable name, return its value (as a string).
190 const char* GetCacheDefinition(const char*) const;
191 ///! Add an entry into the cache
192 void AddCacheEntry(const char* key
, const char* value
,
193 const char* helpString
,
196 * Execute commands during the build process. Supports options such
197 * as echo, remove file etc.
199 static int ExecuteCMakeCommand(std::vector
<std::string
>&);
202 * Get the system information and write it to the file specified
204 int GetSystemInformation(std::vector
<std::string
>&);
207 * Add a command to this cmake instance
209 void AddCommand(cmCommand
* );
210 void RenameCommand(const char* oldName
, const char* newName
);
211 void RemoveCommand(const char* name
);
212 void RemoveUnscriptableCommands();
215 * Get a command by its name
217 cmCommand
*GetCommand(const char *name
);
219 /** Get list of all commands */
220 RegisteredCommandsMap
* GetCommands() { return &this->Commands
; }
222 /** Check if a command exists. */
223 bool CommandExists(const char* name
) const;
225 ///! Parse command line arguments
226 void SetArgs(const std::vector
<std::string
>&);
228 ///! Is this cmake running as a result of a TRY_COMPILE command
229 bool GetIsInTryCompile() { return this->InTryCompile
; }
231 ///! Is this cmake running as a result of a TRY_COMPILE command
232 void SetIsInTryCompile(bool i
) { this->InTryCompile
= i
; }
234 ///! Parse command line arguments that might set cache values
235 bool SetCacheArgs(const std::vector
<std::string
>&);
237 typedef void (*ProgressCallbackType
)
238 (const char*msg
, float progress
, void *);
240 * Set the function used by GUI's to receive progress updates
241 * Function gets passed: message as a const char*, a progress
242 * amount ranging from 0 to 1.0 and client data. The progress
243 * number provided may be negative in cases where a message is
244 * to be displayed without any progress percentage.
246 void SetProgressCallback(ProgressCallbackType f
, void* clientData
=0);
248 ///! this is called by generators to update the progress
249 void UpdateProgress(const char *msg
, float prog
);
251 ///! get the cmake policies instance
252 cmPolicies
*GetPolicies() {return this->Policies
;} ;
254 ///! Get the variable watch object
255 cmVariableWatch
* GetVariableWatch() { return this->VariableWatch
; }
257 /** Get the documentation entries for the supported commands.
258 * If withCurrentCommands is true, the documentation for the
259 * recommended set of commands is included.
260 * If withCompatCommands is true, the documentation for discouraged
261 * (compatibility) commands is included.
262 * You probably don't want to set both to false.
264 void GetCommandDocumentation(std::vector
<cmDocumentationEntry
>& entries
,
265 bool withCurrentCommands
= true,
266 bool withCompatCommands
= true) const;
267 void GetPropertiesDocumentation(std::map
<std::string
,
268 cmDocumentationSection
*>&);
269 void GetGeneratorDocumentation(std::vector
<cmDocumentationEntry
>&);
270 void GetPolicyDocumentation(std::vector
<cmDocumentationEntry
>& entries
);
272 ///! Set/Get a property of this target file
273 void SetProperty(const char *prop
, const char *value
);
274 void AppendProperty(const char *prop
, const char *value
);
275 const char *GetProperty(const char *prop
);
276 const char *GetProperty(const char *prop
, cmProperty::ScopeType scope
);
277 bool GetPropertyAsBool(const char *prop
);
279 // Get the properties
280 cmPropertyMap
&GetProperties() { return this->Properties
; };
282 ///! Do all the checks before running configure
283 int DoPreConfigureChecks();
286 * Set and get the script mode option. In script mode there is no
287 * generator and no cache. Also, language are not enabled, so
288 * add_executable and things do not do anything.
290 void SetScriptMode(bool mode
) { this->ScriptMode
= mode
; }
291 bool GetScriptMode() { return this->ScriptMode
; }
293 ///! Debug the try compile stuff by not delelting the files
294 bool GetDebugTryCompile(){return this->DebugTryCompile
;}
295 void DebugTryCompileOn(){this->DebugTryCompile
= true;}
297 ///! Get the list of files written by CMake using FILE(WRITE / WRITE_FILE
298 void AddWrittenFile(const char* file
);
299 bool HasWrittenFile(const char* file
);
300 void CleanupWrittenFiles();
303 * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
308 * Get the file comparison class
310 cmFileTimeComparison
* GetFileComparison() { return this->FileComparison
; }
313 * Get the path to ctest
315 const char* GetCTestCommand();
316 const char* GetCPackCommand();
318 // Do we want debug output during the cmake run.
319 bool GetDebugOutput() { return this->DebugOutput
; }
320 void SetDebugOutputOn(bool b
) { this->DebugOutput
= b
;}
323 void DefineProperty(const char *name
, cmProperty::ScopeType scope
,
324 const char *ShortDescription
,
325 const char *FullDescription
,
327 const char *variableGroup
= 0);
329 // get property definition
330 cmPropertyDefinition
*GetPropertyDefinition
331 (const char *name
, cmProperty::ScopeType scope
);
333 // Is a property defined?
334 bool IsPropertyDefined(const char *name
, cmProperty::ScopeType scope
);
335 bool IsPropertyChained(const char *name
, cmProperty::ScopeType scope
);
337 // record accesses of properties and variables
338 void RecordPropertyAccess(const char *name
, cmProperty::ScopeType scope
);
339 void ReportUndefinedPropertyAccesses(const char *filename
);
341 // Define the properties
342 static void DefineProperties(cmake
*cm
);
344 void SetCMakeEditCommand(const char* s
)
346 this->CMakeEditCommand
= s
;
348 void SetSuppressDevWarnings(bool v
)
350 this->SuppressDevWarnings
= v
;
351 this->DoSuppressDevWarnings
= true;
354 /** Display a message to the user. */
355 void IssueMessage(cmake::MessageType t
, std::string
const& text
,
356 cmListFileBacktrace
const& backtrace
);
358 void InitializeProperties();
359 int HandleDeleteCacheVariables(const char* var
);
360 cmPropertyMap Properties
;
361 std::set
<std::pair
<cmStdString
,cmProperty::ScopeType
> > AccessedProperties
;
363 std::map
<cmProperty::ScopeType
, cmPropertyDefinitionMap
>
367 cmExternalMakefileProjectGenerator
* (*CreateExtraGeneratorFunctionType
)();
368 typedef std::map
<cmStdString
,
369 CreateExtraGeneratorFunctionType
> RegisteredExtraGeneratorsMap
;
371 typedef cmGlobalGenerator
* (*CreateGeneratorFunctionType
)();
372 typedef std::map
<cmStdString
,
373 CreateGeneratorFunctionType
> RegisteredGeneratorsMap
;
374 RegisteredCommandsMap Commands
;
375 RegisteredGeneratorsMap Generators
;
376 RegisteredExtraGeneratorsMap ExtraGenerators
;
377 void AddDefaultCommands();
378 void AddDefaultGenerators();
379 void AddDefaultExtraGenerators();
380 void AddExtraGenerator(const char* name
,
381 CreateExtraGeneratorFunctionType newFunction
);
383 cmPolicies
*Policies
;
384 cmGlobalGenerator
*GlobalGenerator
;
385 cmCacheManager
*CacheManager
;
386 std::string cmHomeDirectory
;
387 std::string HomeOutputDirectory
;
388 std::string cmStartDirectory
;
389 std::string StartOutputDirectory
;
390 bool SuppressDevWarnings
;
391 bool DoSuppressDevWarnings
;
392 std::set
<cmStdString
> WrittenFiles
;
394 ///! return true if the same cmake was used to make the cache.
395 bool CacheVersionMatches();
396 ///! read in a cmake list file to initialize the cache
397 void ReadListFile(const char *path
);
399 ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
400 /// If it is set, truncate it to 50kb
401 void TruncateOutputLog(const char* fname
);
404 * Method called to check build system integrity at build time.
405 * Returns 1 if CMake should rerun and 0 otherwise.
407 int CheckBuildSystem();
409 void SetDirectoriesFromFile(const char* arg
);
411 //! Make sure all commands are what they say they are and there is no
413 void CleanupCommandsAndMacros();
415 void GenerateGraphViz(const char* fileName
) const;
417 static int ExecuteEchoColor(std::vector
<std::string
>& args
);
418 static int ExecuteLinkScript(std::vector
<std::string
>& args
);
419 static int VisualStudioLink(std::vector
<std::string
>& args
, int type
);
420 static int VisualStudioLinkIncremental(std::vector
<std::string
>& args
,
423 static int VisualStudioLinkNonIncremental(std::vector
<std::string
>& args
,
426 static int ParseVisualStudioLinkCommand(std::vector
<std::string
>& args
,
427 std::vector
<cmStdString
>& command
,
428 std::string
& targetName
);
429 static bool RunCommand(const char* comment
,
430 std::vector
<cmStdString
>& command
,
432 int* retCodeOut
= 0);
433 cmVariableWatch
* VariableWatch
;
435 ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
436 std::string
FindCMakeProgram(const char* name
) const;
438 ProgressCallbackType ProgressCallback
;
439 void* ProgressCallbackClientData
;
444 std::string CMakeEditCommand
;
445 std::string CMakeCommand
;
446 std::string CXXEnvironment
;
447 std::string CCEnvironment
;
448 std::string CheckBuildSystemArgument
;
449 std::string CheckStampFile
;
450 std::string CheckStampList
;
451 std::string VSSolutionFile
;
452 std::string CTestCommand
;
453 std::string CPackCommand
;
454 bool ClearBuildSystem
;
455 bool DebugTryCompile
;
456 cmFileTimeComparison
* FileComparison
;
457 std::string GraphVizFile
;
459 void UpdateConversionPathTable();
462 #define CMAKE_STANDARD_OPTIONS_TABLE \
463 {"-C <initial-cache>", "Pre-load a script to populate the cache.", \
464 "When cmake is first run in an empty build tree, it creates a " \
465 "CMakeCache.txt file and populates it with customizable settings " \
466 "for the project. This option may be used to specify a file from " \
467 "which to load cache entries before the first pass through " \
468 "the project's cmake listfiles. The loaded entries take priority " \
469 "over the project's default values. The given file should be a CMake " \
470 "script containing SET commands that use the CACHE option, " \
471 "not a cache-format file."}, \
472 {"-D <var>:<type>=<value>", "Create a cmake cache entry.", \
473 "When cmake is first run in an empty build tree, it creates a " \
474 "CMakeCache.txt file and populates it with customizable settings " \
475 "for the project. This option may be used to specify a setting " \
476 "that takes priority over the project's default value. The option " \
477 "may be repeated for as many cache entries as desired."}, \
478 {"-U <globbing_expr>", "Remove matching entries from CMake cache.", \
479 "This option may be used to remove one or more variables from the " \
480 "CMakeCache.txt file, globbing expressions using * and ? are supported. "\
481 "The option may be repeated for as many cache entries as desired.\n" \
482 "Use with care, you can make your CMakeCache.txt non-working."}, \
483 {"-G <generator-name>", "Specify a makefile generator.", \
484 "CMake may support multiple native build systems on certain platforms. " \
485 "A makefile generator is responsible for generating a particular build " \
486 "system. Possible generator names are specified in the Generators " \
488 {"-Wno-dev", "Suppress developer warnings.",\
489 "Suppress warnings that are meant for the author"\
490 " of the CMakeLists.txt files."},\
491 {"-Wdev", "Enable developer warnings.",\
492 "Enable warnings that are meant for the author"\
493 " of the CMakeLists.txt files."}
496 #define CMAKE_STANDARD_INTRODUCTION \
498 "CMake is a cross-platform build system generator. Projects " \
499 "specify their build process with platform-independent CMake listfiles " \
500 "included in each directory of a source tree with the name " \
502 "Users build a project by using CMake to generate a build system " \
503 "for a native tool on their platform.", 0}