STYLE: Nightly Date Stamp
[cmake.git] / Source / cmakemain.cxx
blobbf96c118cbbbbf12f91ba7a0eccd7c22a8226e45
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmakemain.cxx,v $
5 Language: C++
6 Date: $Date: 2008-08-19 19:42:48 $
7 Version: $Revision: 1.84 $
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"
22 #endif
24 #include "cmake.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] =
37 {0,
38 " cmake - Cross-Platform Makefile Generator.", 0},
39 {0,0,0}
42 //----------------------------------------------------------------------------
43 static const char * cmDocumentationUsage[][3] =
45 {0,
46 " cmake [options] <path-to-source>\n"
47 " cmake [options] <path-to-existing-build>", 0},
48 {0,0,0}
51 //----------------------------------------------------------------------------
52 static const char * cmDocumentationDescription[][3] =
54 {0,
55 "The \"cmake\" executable is the CMake command-line interface. It may "
56 "be used to configure projects in scripts. Project configuration "
57 "settings "
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,
61 {0,0,0}
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 "
71 "information."},
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 "
83 "each variable."},
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 {"--trace", "Put cmake in trace mode.",
105 "Print a trace of all calls made and from where with "
106 "message(send_error ) calls."},
107 {"--help-command cmd [file]", "Print help for a single command and exit.",
108 "Full documentation specific to the given command is displayed. "
109 "If a file is specified, the documentation is written into and the output "
110 "format is determined depending on the filename suffix. Supported are man "
111 "page, HTML, DocBook and plain text."},
112 {"--help-command-list [file]", "List available listfile commands and exit.",
113 "The list contains all commands for which help may be obtained by using "
114 "the --help-command argument followed by a command name. "
115 "If a file is specified, the documentation is written into and the output "
116 "format is determined depending on the filename suffix. Supported are man "
117 "page, HTML, DocBook and plain text."},
118 {"--help-commands [file]", "Print help for all commands and exit.",
119 "Full documentation specific for all current command is displayed."
120 "If a file is specified, the documentation is written into and the output "
121 "format is determined depending on the filename suffix. Supported are man "
122 "page, HTML, DocBook and plain text."},
123 {"--help-compatcommands [file]", "Print help for compatibility commands. ",
124 "Full documentation specific for all compatibility commands is displayed."
125 "If a file is specified, the documentation is written into and the output "
126 "format is determined depending on the filename suffix. Supported are man "
127 "page, HTML, DocBook and plain text."},
128 {"--help-module module [file]", "Print help for a single module and exit.",
129 "Full documentation specific to the given module is displayed."
130 "If a file is specified, the documentation is written into and the output "
131 "format is determined depending on the filename suffix. Supported are man "
132 "page, HTML, DocBook and plain text."},
133 {"--help-module-list [file]", "List available modules and exit.",
134 "The list contains all modules for which help may be obtained by using "
135 "the --help-module argument followed by a module name. "
136 "If a file is specified, the documentation is written into and the output "
137 "format is determined depending on the filename suffix. Supported are man "
138 "page, HTML, DocBook and plain text."},
139 {"--help-modules [file]", "Print help for all modules and exit.",
140 "Full documentation for all modules is displayed. "
141 "If a file is specified, the documentation is written into and the output "
142 "format is determined depending on the filename suffix. Supported are man "
143 "page, HTML, DocBook and plain text."},
144 {"--help-custom-modules [file]" , "Print help for all custom modules and "
145 "exit.",
146 "Full documentation for all custom modules is displayed. "
147 "If a file is specified, the documentation is written into and the output "
148 "format is determined depending on the filename suffix. Supported are man "
149 "page, HTML, DocBook and plain text."},
150 {"--help-policy cmp [file]",
151 "Print help for a single policy and exit.",
152 "Full documentation specific to the given policy is displayed."
153 "If a file is specified, the documentation is written into and the output "
154 "format is determined depending on the filename suffix. Supported are man "
155 "page, HTML, DocBook and plain text."},
156 {"--help-policies [file]", "Print help for all policies and exit.",
157 "Full documentation for all policies is displayed."
158 "If a file is specified, the documentation is written into and the output "
159 "format is determined depending on the filename suffix. Supported are man "
160 "page, HTML, DocBook and plain text."},
161 {"--help-property prop [file]",
162 "Print help for a single property and exit.",
163 "Full documentation specific to the given property is displayed."
164 "If a file is specified, the documentation is written into and the output "
165 "format is determined depending on the filename suffix. Supported are man "
166 "page, HTML, DocBook and plain text."},
167 {"--help-property-list [file]", "List available properties and exit.",
168 "The list contains all properties for which help may be obtained by using "
169 "the --help-property argument followed by a property name. If a file is "
170 "specified, the help is written into it."
171 "If a file is specified, the documentation is written into and the output "
172 "format is determined depending on the filename suffix. Supported are man "
173 "page, HTML, DocBook and plain text."},
174 {"--help-properties [file]", "Print help for all properties and exit.",
175 "Full documentation for all properties is displayed."
176 "If a file is specified, the documentation is written into and the output "
177 "format is determined depending on the filename suffix. Supported are man "
178 "page, HTML, DocBook and plain text."},
179 {"--help-variable var [file]",
180 "Print help for a single variable and exit.",
181 "Full documentation specific to the given variable is displayed."
182 "If a file is specified, the documentation is written into and the output "
183 "format is determined depending on the filename suffix. Supported are man "
184 "page, HTML, DocBook and plain text."},
185 {"--help-variable-list [file]", "List documented variables and exit.",
186 "The list contains all variables for which help may be obtained by using "
187 "the --help-variable argument followed by a variable name. If a file is "
188 "specified, the help is written into it."
189 "If a file is specified, the documentation is written into and the output "
190 "format is determined depending on the filename suffix. Supported are man "
191 "page, HTML, DocBook and plain text."},
192 {"--help-variables [file]", "Print help for all variables and exit.",
193 "Full documentation for all variables is displayed."
194 "If a file is specified, the documentation is written into and the output "
195 "format is determined depending on the filename suffix. Supported are man "
196 "page, HTML, DocBook and plain text."},
197 {0,0,0}
200 //----------------------------------------------------------------------------
201 static const char * cmDocumentationSeeAlso[][3] =
203 {0, "ccmake", 0},
204 {0, "cpack", 0},
205 {0, "ctest", 0},
206 {0, "cmakecommands", 0},
207 {0, "cmakecompat", 0},
208 {0, "cmakemodules", 0},
209 {0, "cmakeprops", 0},
210 {0, "cmakevars", 0},
211 {0, 0, 0}
214 //----------------------------------------------------------------------------
215 static const char * cmDocumentationNOTE[][3] =
218 "CMake no longer configures a project when run with no arguments. "
219 "In order to configure the project in the current directory, run\n"
220 " cmake .", 0},
221 {0,0,0}
223 #endif
225 int do_cmake(int ac, char** av);
227 static cmMakefile* cmakemainGetMakefile(void *clientdata)
229 cmake* cm = (cmake *)clientdata;
230 if(cm && cm->GetDebugOutput())
232 cmGlobalGenerator* gg=cm->GetGlobalGenerator();
233 if (gg)
235 cmLocalGenerator* lg=gg->GetCurrentLocalGenerator();
236 if (lg)
238 cmMakefile* mf = lg->GetMakefile();
239 return mf;
243 return 0;
246 static std::string cmakemainGetStack(void *clientdata)
248 std::string msg;
249 cmMakefile* mf=cmakemainGetMakefile(clientdata);
250 if (mf)
252 msg = mf->GetListFileStack();
253 if (!msg.empty())
255 msg = "\n Called from: " + msg;
259 return msg;
262 static void cmakemainErrorCallback(const char* m, const char*, bool&,
263 void *clientdata)
265 std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
268 static void cmakemainProgressCallback(const char *m, float prog,
269 void* clientdata)
271 cmMakefile* mf = cmakemainGetMakefile(clientdata);
272 std::string dir;
273 if ((mf) && (strstr(m, "Configuring")==m) && (prog<0))
275 dir = " ";
276 dir += mf->GetCurrentDirectory();
278 else if ((mf) && (strstr(m, "Generating")==m))
280 dir = " ";
281 dir += mf->GetCurrentOutputDirectory();
284 if ((prog < 0) || (!dir.empty()))
286 std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)<<std::endl;
289 std::cout.flush();
293 int main(int ac, char** av)
295 cmSystemTools::EnableMSVCDebugHook();
296 cmSystemTools::FindExecutableDirectory(av[0]);
297 int ret = do_cmake(ac, av);
298 #ifdef CMAKE_BUILD_WITH_CMAKE
299 cmDynamicLoader::FlushCache();
300 #endif
301 return ret;
304 int do_cmake(int ac, char** av)
306 int nocwd = 0;
308 if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
310 std::cerr << "Current working directory cannot be established."
311 << std::endl;
312 nocwd = 1;
315 #ifdef CMAKE_BUILD_WITH_CMAKE
316 cmDocumentation doc;
317 if(doc.CheckOptions(ac, av, "-E") || nocwd)
319 // Construct and print requested documentation.
320 cmake hcm;
321 hcm.AddCMakePaths();
322 doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
324 // the command line args are processed here so that you can do
325 // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
326 std::vector<std::string> args;
327 for(int i =0; i < ac; ++i)
329 args.push_back(av[i]);
331 hcm.SetCacheArgs(args);
332 const char* modulePath = hcm.GetCacheDefinition("CMAKE_MODULE_PATH");
333 if (modulePath)
335 doc.SetCMakeModulePath(modulePath);
338 std::vector<cmDocumentationEntry> commands;
339 std::vector<cmDocumentationEntry> policies;
340 std::vector<cmDocumentationEntry> compatCommands;
341 std::vector<cmDocumentationEntry> generators;
342 std::map<std::string,cmDocumentationSection *> propDocs;
344 hcm.GetPolicyDocumentation(policies);
345 hcm.GetCommandDocumentation(commands, true, false);
346 hcm.GetCommandDocumentation(compatCommands, false, true);
347 hcm.GetPropertiesDocumentation(propDocs);
348 hcm.GetGeneratorDocumentation(generators);
350 doc.SetName("cmake");
351 doc.SetSection("Name",cmDocumentationName);
352 doc.SetSection("Usage",cmDocumentationUsage);
353 doc.SetSection("Description",cmDocumentationDescription);
354 doc.AppendSection("Generators",generators);
355 doc.PrependSection("Options",cmDocumentationOptions);
356 doc.SetSection("Commands",commands);
357 doc.SetSection("Policies",policies);
358 doc.AppendSection("Compatibility Commands",compatCommands);
359 doc.SetSections(propDocs);
361 cmDocumentationEntry e;
362 e.Brief =
363 "variables defined by cmake, that give information about the project, "
364 "and cmake";
365 doc.PrependSection("Variables that Provide Information",e);
367 doc.SetSeeAlsoList(cmDocumentationSeeAlso);
368 int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
370 // If we were run with no arguments, but a CMakeLists.txt file
371 // exists, the user may have been trying to use the old behavior
372 // of cmake to build a project in-source. Print a message
373 // explaining the change to standard error and return an error
374 // condition in case the program is running from a script.
375 if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
377 doc.ClearSections();
378 doc.SetSection("NOTE", cmDocumentationNOTE);
379 doc.Print(cmDocumentation::UsageForm, std::cerr);
380 return 1;
382 return result;
384 #else
385 if ( nocwd || ac == 1 )
387 std::cout <<
388 "Bootstrap CMake should not be used outside CMake build process."
389 << std::endl;
390 return 0;
392 #endif
394 bool wiz = false;
395 bool sysinfo = false;
396 bool command = false;
397 bool list_cached = false;
398 bool list_all_cached = false;
399 bool list_help = false;
400 bool view_only = false;
401 bool script_mode = false;
402 std::vector<std::string> args;
403 for(int i =0; i < ac; ++i)
405 if(strcmp(av[i], "-i") == 0)
407 wiz = true;
409 else if(!command && strcmp(av[i], "--system-information") == 0)
411 sysinfo = true;
413 // if command has already been set, then
414 // do not eat the -E
415 else if (!command && strcmp(av[i], "-E") == 0)
417 command = true;
419 else if (!command && strcmp(av[i], "-N") == 0)
421 view_only = true;
423 else if (!command && strcmp(av[i], "-L") == 0)
425 list_cached = true;
427 else if (!command && strcmp(av[i], "-LA") == 0)
429 list_all_cached = true;
431 else if (!command && strcmp(av[i], "-LH") == 0)
433 list_cached = true;
434 list_help = true;
436 else if (!command && strcmp(av[i], "-LAH") == 0)
438 list_all_cached = true;
439 list_help = true;
441 else if (!command && strncmp(av[i], "-P", strlen("-P")) == 0)
443 if ( i == ac -1 )
445 cmSystemTools::Error("No script specified for argument -P");
447 else
449 script_mode = true;
450 args.push_back(av[i]);
451 i++;
452 args.push_back(av[i]);
455 else
457 args.push_back(av[i]);
460 if(command)
462 int ret = cmake::ExecuteCMakeCommand(args);
463 return ret;
465 if (wiz)
467 cmakewizard wizard;
468 return wizard.RunWizard(args);
470 if (sysinfo)
472 cmake cm;
473 int ret = cm.GetSystemInformation(args);
474 return ret;
476 cmake cm;
477 cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
478 cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
479 cm.SetScriptMode(script_mode);
481 int res = cm.Run(args, view_only);
482 if ( list_cached || list_all_cached )
484 cmCacheManager::CacheIterator it =
485 cm.GetCacheManager()->GetCacheIterator();
486 std::cout << "-- Cache values" << std::endl;
487 for ( it.Begin(); !it.IsAtEnd(); it.Next() )
489 cmCacheManager::CacheEntryType t = it.GetType();
490 if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
491 t != cmCacheManager::UNINITIALIZED )
493 bool advanced = it.PropertyExists("ADVANCED");
494 if ( list_all_cached || !advanced)
496 if ( list_help )
498 std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
500 std::cout << it.GetName() << ":" <<
501 cmCacheManager::TypeToString(it.GetType())
502 << "=" << it.GetValue() << std::endl;
503 if ( list_help )
505 std::cout << std::endl;
512 // Always return a non-negative value. Windows tools do not always
513 // interpret negative return values as errors.
514 if(res != 0)
516 return 1;
518 else
520 return 0;