1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmakemain.cxx,v $
6 Date: $Date: 2008-03-11 20:02:10 $
7 Version: $Revision: 1.80 $
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 // include these first, otherwise there will be problems on Windows
18 // with GetCurrentDirectory() being redefined
19 #ifdef CMAKE_BUILD_WITH_CMAKE
20 #include "cmDynamicLoader.h"
21 #include "cmDocumentation.h"
25 #include "cmCacheManager.h"
26 #include "cmListFileCache.h"
27 #include "cmakewizard.h"
28 #include "cmSourceFile.h"
29 #include "cmGlobalGenerator.h"
30 #include "cmLocalGenerator.h"
31 #include "cmMakefile.h"
33 #ifdef CMAKE_BUILD_WITH_CMAKE
34 //----------------------------------------------------------------------------
35 static const char * cmDocumentationName
[][3] =
38 " cmake - Cross-Platform Makefile Generator.", 0},
42 //----------------------------------------------------------------------------
43 static const char * cmDocumentationUsage
[][3] =
46 " cmake [options] <path-to-source>\n"
47 " cmake [options] <path-to-existing-build>", 0},
51 //----------------------------------------------------------------------------
52 static const char * cmDocumentationDescription
[][3] =
55 "The \"cmake\" executable is the CMake command-line interface. It may "
56 "be used to configure projects in scripts. Project configuration "
58 "may be specified on the command line with the -D option. The -i option "
59 "will cause cmake to interactively prompt for such settings.", 0},
60 CMAKE_STANDARD_INTRODUCTION
,
64 //----------------------------------------------------------------------------
65 static const char * cmDocumentationOptions
[][3] =
67 CMAKE_STANDARD_OPTIONS_TABLE
,
68 {"-E", "CMake command mode.",
69 "For true platform independence, CMake provides a list of commands "
70 "that can be used on all systems. Run with -E help for the usage "
72 {"-i", "Run in wizard mode.",
73 "Wizard mode runs cmake interactively without a GUI. The user is "
74 "prompted to answer questions about the project configuration. "
75 "The answers are used to set cmake cache values."},
76 {"-L[A][H]", "List non-advanced cached variables.",
77 "List cache variables will run CMake and list all the variables from the "
78 "CMake cache that are not marked as INTERNAL or ADVANCED. This will "
79 "effectively display current CMake settings, which can be then changed "
80 "with -D option. Changing some of the variable may result in more "
81 "variables being created. If A is specified, then it will display also "
82 "advanced variables. If H is specified, it will also display help for "
84 {"-N", "View mode only.",
85 "Only load the cache. Do not actually run configure and generate steps."},
86 {"-P <file>", "Process script mode.",
87 "Process the given cmake file as a script written in the CMake language. "
88 "No configure or generate step is performed and the cache is not"
89 " modified. If variables are defined using -D, this must be done "
90 "before the -P argument."},
91 {"--graphviz=[file]", "Generate graphviz of dependencies.",
92 "Generate a graphviz input file that will contain all the library and "
93 "executable dependencies in the project."},
94 {"--system-information [file]", "Dump information about this system.",
95 "Dump a wide range of information about the current system. If run "
96 "from the top of a binary tree for a CMake project it will dump "
97 "additional information such as the cache, log files etc."},
98 {"--debug-trycompile", "Do not delete the try compile directories..",
99 "Do not delete the files and directories created for try_compile calls. "
100 "This is useful in debugging failed try_compiles."},
101 {"--debug-output", "Put cmake in a debug mode.",
102 "Print extra stuff during the cmake run like stack traces with "
103 "message(send_error ) calls."},
104 {"--help-command cmd [file]", "Print help for a single command and exit.",
105 "Full documentation specific to the given command is displayed. "
106 "If a file is specified, the documentation is written into and the output "
107 "format is determined depending on the filename suffix. Supported are man "
108 "page, HTML, DocBook and plain text."},
109 {"--help-command-list [file]", "List available listfile commands and exit.",
110 "The list contains all commands for which help may be obtained by using "
111 "the --help-command argument followed by a command name. "
112 "If a file is specified, the documentation is written into and the output "
113 "format is determined depending on the filename suffix. Supported are man "
114 "page, HTML, DocBook and plain text."},
115 {"--help-commands [file]", "Print help for all commands and exit.",
116 "Full documentation specific for all current command is displayed."
117 "If a file is specified, the documentation is written into and the output "
118 "format is determined depending on the filename suffix. Supported are man "
119 "page, HTML, DocBook and plain text."},
120 {"--help-compatcommands [file]", "Print help for compatibility commands. ",
121 "Full documentation specific for all compatibility commands is displayed."
122 "If a file is specified, the documentation is written into and the output "
123 "format is determined depending on the filename suffix. Supported are man "
124 "page, HTML, DocBook and plain text."},
125 {"--help-module module [file]", "Print help for a single module and exit.",
126 "Full documentation specific to the given module is displayed."
127 "If a file is specified, the documentation is written into and the output "
128 "format is determined depending on the filename suffix. Supported are man "
129 "page, HTML, DocBook and plain text."},
130 {"--help-module-list [file]", "List available modules and exit.",
131 "The list contains all modules for which help may be obtained by using "
132 "the --help-module argument followed by a module name. "
133 "If a file is specified, the documentation is written into and the output "
134 "format is determined depending on the filename suffix. Supported are man "
135 "page, HTML, DocBook and plain text."},
136 {"--help-modules [file]", "Print help for all modules and exit.",
137 "Full documentation for all modules is displayed. "
138 "If a file is specified, the documentation is written into and the output "
139 "format is determined depending on the filename suffix. Supported are man "
140 "page, HTML, DocBook and plain text."},
141 {"--help-custom-modules [file]" , "Print help for all custom modules and "
143 "Full documentation for all custom modules is displayed. "
144 "If a file is specified, the documentation is written into and the output "
145 "format is determined depending on the filename suffix. Supported are man "
146 "page, HTML, DocBook and plain text."},
147 {"--help-property prop [file]",
148 "Print help for a single property and exit.",
149 "Full documentation specific to the given property is displayed."
150 "If a file is specified, the documentation is written into and the output "
151 "format is determined depending on the filename suffix. Supported are man "
152 "page, HTML, DocBook and plain text."},
153 {"--help-property-list [file]", "List available properties and exit.",
154 "The list contains all properties for which help may be obtained by using "
155 "the --help-property argument followed by a property name. If a file is "
156 "specified, the help is written into it."
157 "If a file is specified, the documentation is written into and the output "
158 "format is determined depending on the filename suffix. Supported are man "
159 "page, HTML, DocBook and plain text."},
160 {"--help-properties [file]", "Print help for all properties and exit.",
161 "Full documentation for all properties is displayed."
162 "If a file is specified, the documentation is written into and the output "
163 "format is determined depending on the filename suffix. Supported are man "
164 "page, HTML, DocBook and plain text."},
165 {"--help-variable var [file]",
166 "Print help for a single variable and exit.",
167 "Full documentation specific to the given variable is displayed."
168 "If a file is specified, the documentation is written into and the output "
169 "format is determined depending on the filename suffix. Supported are man "
170 "page, HTML, DocBook and plain text."},
171 {"--help-variable-list [file]", "List documented variables and exit.",
172 "The list contains all variables for which help may be obtained by using "
173 "the --help-variable argument followed by a variable name. If a file is "
174 "specified, the help is written into it."
175 "If a file is specified, the documentation is written into and the output "
176 "format is determined depending on the filename suffix. Supported are man "
177 "page, HTML, DocBook and plain text."},
178 {"--help-variables [file]", "Print help for all variables and exit.",
179 "Full documentation for all variables is displayed."
180 "If a file is specified, the documentation is written into and the output "
181 "format is determined depending on the filename suffix. Supported are man "
182 "page, HTML, DocBook and plain text."},
186 //----------------------------------------------------------------------------
187 static const char * cmDocumentationSeeAlso
[][3] =
192 {0, "cmakecommands", 0},
193 {0, "cmakecompat", 0},
194 {0, "cmakemodules", 0},
195 {0, "cmakeprops", 0},
200 //----------------------------------------------------------------------------
201 static const char * cmDocumentationNOTE
[][3] =
204 "CMake no longer configures a project when run with no arguments. "
205 "In order to configure the project in the current directory, run\n"
211 int do_cmake(int ac
, char** av
);
213 static cmMakefile
* cmakemainGetMakefile(void *clientdata
)
215 cmake
* cm
= (cmake
*)clientdata
;
216 if(cm
&& cm
->GetDebugOutput())
218 cmGlobalGenerator
* gg
=cm
->GetGlobalGenerator();
221 cmLocalGenerator
* lg
=gg
->GetCurrentLocalGenerator();
224 cmMakefile
* mf
= lg
->GetMakefile();
232 static std::string
cmakemainGetStack(void *clientdata
)
235 cmMakefile
* mf
=cmakemainGetMakefile(clientdata
);
238 msg
= mf
->GetListFileStack();
241 msg
= "\n Called from: " + msg
;
248 static void cmakemainErrorCallback(const char* m
, const char*, bool&,
251 std::cerr
<< m
<< cmakemainGetStack(clientdata
) << std::endl
<< std::flush
;
254 static void cmakemainProgressCallback(const char *m
, float prog
,
257 cmMakefile
* mf
= cmakemainGetMakefile(clientdata
);
259 if ((mf
) && (strstr(m
, "Configuring")==m
) && (prog
<0))
262 dir
+= mf
->GetCurrentDirectory();
264 else if ((mf
) && (strstr(m
, "Generating")==m
))
267 dir
+= mf
->GetCurrentOutputDirectory();
270 if ((prog
< 0) || (!dir
.empty()))
272 std::cout
<< "-- " << m
<< dir
<< cmakemainGetStack(clientdata
)<<std::endl
;
279 int main(int ac
, char** av
)
281 cmSystemTools::EnableMSVCDebugHook();
282 cmSystemTools::FindExecutableDirectory(av
[0]);
283 int ret
= do_cmake(ac
, av
);
284 #ifdef CMAKE_BUILD_WITH_CMAKE
285 cmDynamicLoader::FlushCache();
290 int do_cmake(int ac
, char** av
)
292 #ifdef CMAKE_BUILD_WITH_CMAKE
297 if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
299 std::cerr
<< "Current working directory cannot be established."
304 #ifdef CMAKE_BUILD_WITH_CMAKE
305 if(doc
.CheckOptions(ac
, av
) || nocwd
)
307 // Construct and print requested documentation.
310 doc
.SetCMakeRoot(hcm
.GetCacheDefinition("CMAKE_ROOT"));
312 // the command line args are processed here so that you can do
313 // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
314 std::vector
<std::string
> args
;
315 for(int i
=0; i
< ac
; ++i
)
317 args
.push_back(av
[i
]);
319 hcm
.SetCacheArgs(args
);
320 const char* modulePath
= hcm
.GetCacheDefinition("CMAKE_MODULE_PATH");
323 doc
.SetCMakeModulePath(modulePath
);
326 std::vector
<cmDocumentationEntry
> commands
;
327 std::vector
<cmDocumentationEntry
> policies
;
328 std::vector
<cmDocumentationEntry
> compatCommands
;
329 std::vector
<cmDocumentationEntry
> generators
;
330 std::map
<std::string
,cmDocumentationSection
*> propDocs
;
332 hcm
.GetPolicyDocumentation(policies
);
333 hcm
.GetCommandDocumentation(commands
, true, false);
334 hcm
.GetCommandDocumentation(compatCommands
, false, true);
335 hcm
.GetPropertiesDocumentation(propDocs
);
336 hcm
.GetGeneratorDocumentation(generators
);
338 doc
.SetName("cmake");
339 doc
.SetSection("Name",cmDocumentationName
);
340 doc
.SetSection("Usage",cmDocumentationUsage
);
341 doc
.SetSection("Description",cmDocumentationDescription
);
342 doc
.AppendSection("Generators",generators
);
343 doc
.PrependSection("Options",cmDocumentationOptions
);
344 doc
.SetSection("Commands",commands
);
345 doc
.SetSection("Policies",policies
);
346 doc
.AppendSection("Compatibility Commands",compatCommands
);
347 doc
.SetSections(propDocs
);
349 cmDocumentationEntry e
;
351 "variables defined by cmake, that give information about the project, "
353 doc
.PrependSection("Variables that Provide Information",e
);
355 doc
.SetSeeAlsoList(cmDocumentationSeeAlso
);
356 int result
= doc
.PrintRequestedDocumentation(std::cout
)? 0:1;
358 // If we were run with no arguments, but a CMakeLists.txt file
359 // exists, the user may have been trying to use the old behavior
360 // of cmake to build a project in-source. Print a message
361 // explaining the change to standard error and return an error
362 // condition in case the program is running from a script.
363 if((ac
== 1) && cmSystemTools::FileExists("CMakeLists.txt"))
366 doc
.SetSection("NOTE", cmDocumentationNOTE
);
367 doc
.Print(cmDocumentation::UsageForm
, std::cerr
);
373 if ( nocwd
|| ac
== 1 )
376 "Bootstrap CMake should not be used outside CMake build process."
383 bool sysinfo
= false;
384 bool command
= false;
385 bool list_cached
= false;
386 bool list_all_cached
= false;
387 bool list_help
= false;
388 bool view_only
= false;
389 bool script_mode
= false;
390 std::vector
<std::string
> args
;
391 for(int i
=0; i
< ac
; ++i
)
393 if(strcmp(av
[i
], "-i") == 0)
397 else if(!command
&& strcmp(av
[i
], "--system-information") == 0)
401 // if command has already been set, then
403 else if (!command
&& strcmp(av
[i
], "-E") == 0)
407 else if (strcmp(av
[i
], "-N") == 0)
411 else if (strcmp(av
[i
], "-L") == 0)
415 else if (strcmp(av
[i
], "-LA") == 0)
417 list_all_cached
= true;
419 else if (strcmp(av
[i
], "-LH") == 0)
424 else if (strcmp(av
[i
], "-LAH") == 0)
426 list_all_cached
= true;
429 else if (strncmp(av
[i
], "-P", strlen("-P")) == 0)
433 cmSystemTools::Error("No script specified for argument -P");
438 args
.push_back(av
[i
]);
440 args
.push_back(av
[i
]);
445 args
.push_back(av
[i
]);
451 int ret
= cmake::ExecuteCMakeCommand(args
);
457 return wizard
.RunWizard(args
);
462 int ret
= cm
.GetSystemInformation(args
);
466 cmSystemTools::SetErrorCallback(cmakemainErrorCallback
, (void *)&cm
);
467 cm
.SetProgressCallback(cmakemainProgressCallback
, (void *)&cm
);
468 cm
.SetScriptMode(script_mode
);
470 int res
= cm
.Run(args
, view_only
);
471 if ( list_cached
|| list_all_cached
)
473 cmCacheManager::CacheIterator it
=
474 cm
.GetCacheManager()->GetCacheIterator();
475 std::cout
<< "-- Cache values" << std::endl
;
476 for ( it
.Begin(); !it
.IsAtEnd(); it
.Next() )
478 cmCacheManager::CacheEntryType t
= it
.GetType();
479 if ( t
!= cmCacheManager::INTERNAL
&& t
!= cmCacheManager::STATIC
&&
480 t
!= cmCacheManager::UNINITIALIZED
)
482 bool advanced
= it
.PropertyExists("ADVANCED");
483 if ( list_all_cached
|| !advanced
)
487 std::cout
<< "// " << it
.GetProperty("HELPSTRING") << std::endl
;
489 std::cout
<< it
.GetName() << ":" <<
490 cmCacheManager::TypeToString(it
.GetType())
491 << "=" << it
.GetValue() << std::endl
;
494 std::cout
<< std::endl
;
501 // Always return a non-negative value. Windows tools do not always
502 // interpret negative return values as errors.