CVS resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmakemain.cxx
blobfc211c5b3c9c6f01ad380239e97aa23f942538a6
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmakemain.cxx,v $
5 Language: C++
6 Date: $Date: 2007/12/13 22:56:49 $
7 Version: $Revision: 1.76 $
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 {"--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 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 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 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 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 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 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 and plain text."},
141 {"--help-custom-modules [file]" , "Print help for all custom modules and "
142 "exit.",
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 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 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 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 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 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 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 and plain text."},
183 {0,0,0}
186 //----------------------------------------------------------------------------
187 static const char * cmDocumentationSeeAlso[][3] =
189 {0, "ccmake", 0},
190 {0, "ctest", 0},
191 {0, 0, 0}
194 //----------------------------------------------------------------------------
195 static const char * cmDocumentationNOTE[][3] =
198 "CMake no longer configures a project when run with no arguments. "
199 "In order to configure the project in the current directory, run\n"
200 " cmake .", 0},
201 {0,0,0}
203 #endif
205 int do_cmake(int ac, char** av);
207 static cmMakefile* cmakemainGetMakefile(void *clientdata)
209 cmake* cm = (cmake *)clientdata;
210 if(cm && cm->GetDebugOutput())
212 cmGlobalGenerator* gg=cm->GetGlobalGenerator();
213 if (gg)
215 cmLocalGenerator* lg=gg->GetCurrentLocalGenerator();
216 if (lg)
218 cmMakefile* mf = lg->GetMakefile();
219 return mf;
223 return 0;
226 static std::string cmakemainGetStack(void *clientdata)
228 std::string msg;
229 cmMakefile* mf=cmakemainGetMakefile(clientdata);
230 if (mf)
232 msg = mf->GetListFileStack();
233 if (!msg.empty())
235 msg = "\n Called from: " + msg;
239 return msg;
242 static void cmakemainErrorCallback(const char* m, const char*, bool&,
243 void *clientdata)
245 std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
248 static void cmakemainProgressCallback(const char *m, float prog,
249 void* clientdata)
251 cmMakefile* mf = cmakemainGetMakefile(clientdata);
252 std::string dir;
253 if ((mf) && (strstr(m, "Configuring")==m) && (prog<0))
255 dir = " ";
256 dir += mf->GetCurrentDirectory();
258 else if ((mf) && (strstr(m, "Generating")==m))
260 dir = " ";
261 dir += mf->GetCurrentOutputDirectory();
264 if ((prog < 0) || (!dir.empty()))
266 std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)<<std::endl;
269 std::cout.flush();
273 int main(int ac, char** av)
275 cmSystemTools::EnableMSVCDebugHook();
276 cmSystemTools::FindExecutableDirectory(av[0]);
277 int ret = do_cmake(ac, av);
278 #ifdef CMAKE_BUILD_WITH_CMAKE
279 cmDynamicLoader::FlushCache();
280 #endif
281 return ret;
284 int do_cmake(int ac, char** av)
286 #ifdef CMAKE_BUILD_WITH_CMAKE
287 cmDocumentation doc;
288 #endif
289 int nocwd = 0;
291 if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
293 std::cerr << "Current working directory cannot be established."
294 << std::endl;
295 nocwd = 1;
298 #ifdef CMAKE_BUILD_WITH_CMAKE
299 if(doc.CheckOptions(ac, av) || nocwd)
301 // Construct and print requested documentation.
302 cmake hcm;
303 hcm.AddCMakePaths();
304 doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
306 // the command line args are processed here so that you can do
307 // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
308 std::vector<std::string> args;
309 for(int i =0; i < ac; ++i)
311 args.push_back(av[i]);
313 hcm.SetCacheArgs(args);
314 const char* modulePath = hcm.GetCacheDefinition("CMAKE_MODULE_PATH");
315 if (modulePath)
317 doc.SetCMakeModulePath(modulePath);
320 std::vector<cmDocumentationEntry> commands;
321 std::vector<cmDocumentationEntry> compatCommands;
322 std::vector<cmDocumentationEntry> generators;
323 std::map<std::string,cmDocumentationSection *> propDocs;
325 hcm.GetCommandDocumentation(commands, true, false);
326 hcm.GetCommandDocumentation(compatCommands, false, true);
327 hcm.GetPropertiesDocumentation(propDocs);
328 hcm.GetGeneratorDocumentation(generators);
330 doc.SetName("cmake");
331 doc.SetSection("Name",cmDocumentationName);
332 doc.SetSection("Usage",cmDocumentationUsage);
333 doc.SetSection("Description",cmDocumentationDescription);
334 doc.AppendSection("Generators",generators);
335 doc.PrependSection("Options",cmDocumentationOptions);
336 doc.SetSection("Commands",commands);
337 doc.AppendSection("Compatibility Commands",compatCommands);
338 doc.SetSections(propDocs);
340 cmDocumentationEntry e;
341 e.Brief =
342 "variables defined by cmake, that give information about the project, "
343 "and cmake";
344 doc.PrependSection("Variables that Provide Information",e);
346 doc.SetSeeAlsoList(cmDocumentationSeeAlso);
347 int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
349 // If we were run with no arguments, but a CMakeLists.txt file
350 // exists, the user may have been trying to use the old behavior
351 // of cmake to build a project in-source. Print a message
352 // explaining the change to standard error and return an error
353 // condition in case the program is running from a script.
354 if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
356 doc.ClearSections();
357 doc.SetSection("NOTE", cmDocumentationNOTE);
358 doc.Print(cmDocumentation::UsageForm, std::cerr);
359 return 1;
361 return result;
363 #else
364 if ( nocwd || ac == 1 )
366 std::cout <<
367 "Bootstrap CMake should not be used outside CMake build process."
368 << std::endl;
369 return 0;
371 #endif
373 bool wiz = false;
374 bool sysinfo = false;
375 bool command = false;
376 bool list_cached = false;
377 bool list_all_cached = false;
378 bool list_help = false;
379 bool view_only = false;
380 bool script_mode = false;
381 std::vector<std::string> args;
382 for(int i =0; i < ac; ++i)
384 if(strcmp(av[i], "-i") == 0)
386 wiz = true;
388 else if(!command && strcmp(av[i], "--system-information") == 0)
390 sysinfo = true;
392 // if command has already been set, then
393 // do not eat the -E
394 else if (!command && strcmp(av[i], "-E") == 0)
396 command = true;
398 else if (strcmp(av[i], "-N") == 0)
400 view_only = true;
402 else if (strcmp(av[i], "-L") == 0)
404 list_cached = true;
406 else if (strcmp(av[i], "-LA") == 0)
408 list_all_cached = true;
410 else if (strcmp(av[i], "-LH") == 0)
412 list_cached = true;
413 list_help = true;
415 else if (strcmp(av[i], "-LAH") == 0)
417 list_all_cached = true;
418 list_help = true;
420 else if (strncmp(av[i], "-P", strlen("-P")) == 0)
422 if ( i == ac -1 )
424 cmSystemTools::Error("No script specified for argument -P");
426 else
428 script_mode = true;
429 args.push_back(av[i]);
430 i++;
431 args.push_back(av[i]);
434 else
436 args.push_back(av[i]);
440 if(command)
442 int ret = cmake::ExecuteCMakeCommand(args);
443 return ret;
445 if (wiz)
447 cmakewizard wizard;
448 return wizard.RunWizard(args);
450 if (sysinfo)
452 cmake cm;
453 int ret = cm.GetSystemInformation(args);
454 return ret;
456 cmake cm;
457 cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
458 cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
459 cm.SetScriptMode(script_mode);
461 int res = cm.Run(args, view_only);
462 if ( list_cached || list_all_cached )
464 cmCacheManager::CacheIterator it =
465 cm.GetCacheManager()->GetCacheIterator();
466 std::cout << "-- Cache values" << std::endl;
467 for ( it.Begin(); !it.IsAtEnd(); it.Next() )
469 cmCacheManager::CacheEntryType t = it.GetType();
470 if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
471 t != cmCacheManager::UNINITIALIZED )
473 bool advanced = it.PropertyExists("ADVANCED");
474 if ( list_all_cached || !advanced)
476 if ( list_help )
478 std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
480 std::cout << it.GetName() << ":" <<
481 cmCacheManager::TypeToString(it.GetType())
482 << "=" << it.GetValue() << std::endl;
483 if ( list_help )
485 std::cout << std::endl;
492 // Always return a non-negative value. Windows tools do not always
493 // interpret negative return values as errors.
494 if(res != 0)
496 return 1;
498 else
500 return 0;