Introduce "generator expressions" to add_test()
[cmake.git] / Source / cmakemain.cxx
blob579bb3dfd87e0a36f81d1840574137573249ad41
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmakemain.cxx,v $
5 Language: C++
6 Date: $Date: 2009-04-19 16:48:30 $
7 Version: $Revision: 1.89 $
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 #define CMAKE_BUILD_OPTIONS \
65 " <dir> = Project binary directory to be built.\n" \
66 " --target <tgt> = Build <tgt> instead of default targets.\n" \
67 " --config <cfg> = For multi-configuration tools, choose <cfg>.\n" \
68 " --clean-first = Build target 'clean' first, then build.\n" \
69 " (To clean only, use --target 'clean'.)\n" \
70 " -- = Pass remaining options to the native tool.\n"
72 //----------------------------------------------------------------------------
73 static const char * cmDocumentationOptions[][3] =
75 CMAKE_STANDARD_OPTIONS_TABLE,
76 {"-E", "CMake command mode.",
77 "For true platform independence, CMake provides a list of commands "
78 "that can be used on all systems. Run with -E help for the usage "
79 "information. Commands available are: chdir, copy, copy_if_different "
80 "copy_directory, compare_files, echo, echo_append, environment, "
81 "make_directory, md5sum, remove_directory, remove, tar, time, "
82 "touch, touch_nocreate, write_regv, delete_regv, comspec, "
83 "create_symlink."},
84 {"-i", "Run in wizard mode.",
85 "Wizard mode runs cmake interactively without a GUI. The user is "
86 "prompted to answer questions about the project configuration. "
87 "The answers are used to set cmake cache values."},
88 {"-L[A][H]", "List non-advanced cached variables.",
89 "List cache variables will run CMake and list all the variables from the "
90 "CMake cache that are not marked as INTERNAL or ADVANCED. This will "
91 "effectively display current CMake settings, which can be then changed "
92 "with -D option. Changing some of the variable may result in more "
93 "variables being created. If A is specified, then it will display also "
94 "advanced variables. If H is specified, it will also display help for "
95 "each variable."},
96 {"--build <dir>", "Build a CMake-generated project binary tree.",
97 "This abstracts a native build tool's command-line interface with the "
98 "following options:\n"
99 CMAKE_BUILD_OPTIONS
100 "Run cmake --build with no options for quick help."},
101 {"-N", "View mode only.",
102 "Only load the cache. Do not actually run configure and generate steps."},
103 {"-P <file>", "Process script mode.",
104 "Process the given cmake file as a script written in the CMake language. "
105 "No configure or generate step is performed and the cache is not"
106 " modified. If variables are defined using -D, this must be done "
107 "before the -P argument."},
108 {"--graphviz=[file]", "Generate graphviz of dependencies.",
109 "Generate a graphviz input file that will contain all the library and "
110 "executable dependencies in the project."},
111 {"--system-information [file]", "Dump information about this system.",
112 "Dump a wide range of information about the current system. If run "
113 "from the top of a binary tree for a CMake project it will dump "
114 "additional information such as the cache, log files etc."},
115 {"--debug-trycompile", "Do not delete the try compile directories..",
116 "Do not delete the files and directories created for try_compile calls. "
117 "This is useful in debugging failed try_compiles. It may however "
118 "change the results of the try-compiles as old junk from a previous "
119 "try-compile may cause a different test to either pass or fail "
120 "incorrectly. This option is best used for one try-compile at a time, "
121 "and only when debugging." },
122 {"--debug-output", "Put cmake in a debug mode.",
123 "Print extra stuff during the cmake run like stack traces with "
124 "message(send_error ) calls."},
125 {"--trace", "Put cmake in trace mode.",
126 "Print a trace of all calls made and from where with "
127 "message(send_error ) calls."},
128 {"--help-command cmd [file]", "Print help for a single command and exit.",
129 "Full documentation specific to the given command 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-command-list [file]", "List available listfile commands and exit.",
134 "The list contains all commands for which help may be obtained by using "
135 "the --help-command argument followed by a command 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-commands [file]", "Print help for all commands and exit.",
140 "Full documentation specific for all current command 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-compatcommands [file]", "Print help for compatibility commands. ",
145 "Full documentation specific for all compatibility commands is displayed."
146 "If a file is specified, the documentation is written into and the output "
147 "format is determined depending on the filename suffix. Supported are man "
148 "page, HTML, DocBook and plain text."},
149 {"--help-module module [file]", "Print help for a single module and exit.",
150 "Full documentation specific to the given module is displayed."
151 "If a file is specified, the documentation is written into and the output "
152 "format is determined depending on the filename suffix. Supported are man "
153 "page, HTML, DocBook and plain text."},
154 {"--help-module-list [file]", "List available modules and exit.",
155 "The list contains all modules for which help may be obtained by using "
156 "the --help-module argument followed by a module name. "
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-modules [file]", "Print help for all modules and exit.",
161 "Full documentation for all modules 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-custom-modules [file]" , "Print help for all custom modules and "
166 "exit.",
167 "Full documentation for all custom modules 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-policy cmp [file]",
172 "Print help for a single policy and exit.",
173 "Full documentation specific to the given policy is displayed."
174 "If a file is specified, the documentation is written into and the output "
175 "format is determined depending on the filename suffix. Supported are man "
176 "page, HTML, DocBook and plain text."},
177 {"--help-policies [file]", "Print help for all policies and exit.",
178 "Full documentation for all policies is displayed."
179 "If a file is specified, the documentation is written into and the output "
180 "format is determined depending on the filename suffix. Supported are man "
181 "page, HTML, DocBook and plain text."},
182 {"--help-property prop [file]",
183 "Print help for a single property and exit.",
184 "Full documentation specific to the given property is displayed."
185 "If a file is specified, the documentation is written into and the output "
186 "format is determined depending on the filename suffix. Supported are man "
187 "page, HTML, DocBook and plain text."},
188 {"--help-property-list [file]", "List available properties and exit.",
189 "The list contains all properties for which help may be obtained by using "
190 "the --help-property argument followed by a property name. If a file is "
191 "specified, the help is written into it."
192 "If a file is specified, the documentation is written into and the output "
193 "format is determined depending on the filename suffix. Supported are man "
194 "page, HTML, DocBook and plain text."},
195 {"--help-properties [file]", "Print help for all properties and exit.",
196 "Full documentation for all properties is displayed."
197 "If a file is specified, the documentation is written into and the output "
198 "format is determined depending on the filename suffix. Supported are man "
199 "page, HTML, DocBook and plain text."},
200 {"--help-variable var [file]",
201 "Print help for a single variable and exit.",
202 "Full documentation specific to the given variable is displayed."
203 "If a file is specified, the documentation is written into and the output "
204 "format is determined depending on the filename suffix. Supported are man "
205 "page, HTML, DocBook and plain text."},
206 {"--help-variable-list [file]", "List documented variables and exit.",
207 "The list contains all variables for which help may be obtained by using "
208 "the --help-variable argument followed by a variable name. If a file is "
209 "specified, the help is written into it."
210 "If a file is specified, the documentation is written into and the output "
211 "format is determined depending on the filename suffix. Supported are man "
212 "page, HTML, DocBook and plain text."},
213 {"--help-variables [file]", "Print help for all variables and exit.",
214 "Full documentation for all variables is displayed."
215 "If a file is specified, the documentation is written into and the output "
216 "format is determined depending on the filename suffix. Supported are man "
217 "page, HTML, DocBook and plain text."},
218 {0,0,0}
221 //----------------------------------------------------------------------------
222 static const char * cmDocumentationSeeAlso[][3] =
224 {0, "ccmake", 0},
225 {0, "cpack", 0},
226 {0, "ctest", 0},
227 {0, "cmakecommands", 0},
228 {0, "cmakecompat", 0},
229 {0, "cmakemodules", 0},
230 {0, "cmakeprops", 0},
231 {0, "cmakevars", 0},
232 {0, 0, 0}
235 //----------------------------------------------------------------------------
236 static const char * cmDocumentationNOTE[][3] =
239 "CMake no longer configures a project when run with no arguments. "
240 "In order to configure the project in the current directory, run\n"
241 " cmake .", 0},
242 {0,0,0}
244 #endif
246 int do_cmake(int ac, char** av);
247 static int do_build(int ac, char** av);
249 static cmMakefile* cmakemainGetMakefile(void *clientdata)
251 cmake* cm = (cmake *)clientdata;
252 if(cm && cm->GetDebugOutput())
254 cmGlobalGenerator* gg=cm->GetGlobalGenerator();
255 if (gg)
257 cmLocalGenerator* lg=gg->GetCurrentLocalGenerator();
258 if (lg)
260 cmMakefile* mf = lg->GetMakefile();
261 return mf;
265 return 0;
268 static std::string cmakemainGetStack(void *clientdata)
270 std::string msg;
271 cmMakefile* mf=cmakemainGetMakefile(clientdata);
272 if (mf)
274 msg = mf->GetListFileStack();
275 if (!msg.empty())
277 msg = "\n Called from: " + msg;
281 return msg;
284 static void cmakemainErrorCallback(const char* m, const char*, bool&,
285 void *clientdata)
287 std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
290 static void cmakemainProgressCallback(const char *m, float prog,
291 void* clientdata)
293 cmMakefile* mf = cmakemainGetMakefile(clientdata);
294 std::string dir;
295 if ((mf) && (strstr(m, "Configuring")==m) && (prog<0))
297 dir = " ";
298 dir += mf->GetCurrentDirectory();
300 else if ((mf) && (strstr(m, "Generating")==m))
302 dir = " ";
303 dir += mf->GetCurrentOutputDirectory();
306 if ((prog < 0) || (!dir.empty()))
308 std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)<<std::endl;
311 std::cout.flush();
315 int main(int ac, char** av)
317 cmSystemTools::EnableMSVCDebugHook();
318 cmSystemTools::FindExecutableDirectory(av[0]);
319 if(ac > 1 && strcmp(av[1], "--build") == 0)
321 return do_build(ac, av);
323 int ret = do_cmake(ac, av);
324 #ifdef CMAKE_BUILD_WITH_CMAKE
325 cmDynamicLoader::FlushCache();
326 #endif
327 return ret;
330 int do_cmake(int ac, char** av)
332 int nocwd = 0;
334 if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
336 std::cerr << "Current working directory cannot be established."
337 << std::endl;
338 nocwd = 1;
341 #ifdef CMAKE_BUILD_WITH_CMAKE
342 cmDocumentation doc;
343 if(doc.CheckOptions(ac, av, "-E") || nocwd)
345 // Construct and print requested documentation.
346 cmake hcm;
347 hcm.AddCMakePaths();
348 doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
350 // the command line args are processed here so that you can do
351 // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
352 std::vector<std::string> args;
353 for(int i =0; i < ac; ++i)
355 args.push_back(av[i]);
357 hcm.SetCacheArgs(args);
358 const char* modulePath = hcm.GetCacheDefinition("CMAKE_MODULE_PATH");
359 if (modulePath)
361 doc.SetCMakeModulePath(modulePath);
364 std::vector<cmDocumentationEntry> commands;
365 std::vector<cmDocumentationEntry> policies;
366 std::vector<cmDocumentationEntry> compatCommands;
367 std::vector<cmDocumentationEntry> generators;
368 std::map<std::string,cmDocumentationSection *> propDocs;
370 hcm.GetPolicyDocumentation(policies);
371 hcm.GetCommandDocumentation(commands, true, false);
372 hcm.GetCommandDocumentation(compatCommands, false, true);
373 hcm.GetPropertiesDocumentation(propDocs);
374 hcm.GetGeneratorDocumentation(generators);
376 doc.SetName("cmake");
377 doc.SetSection("Name",cmDocumentationName);
378 doc.SetSection("Usage",cmDocumentationUsage);
379 doc.SetSection("Description",cmDocumentationDescription);
380 doc.AppendSection("Generators",generators);
381 doc.PrependSection("Options",cmDocumentationOptions);
382 doc.SetSection("Commands",commands);
383 doc.SetSection("Policies",policies);
384 doc.AppendSection("Compatibility Commands",compatCommands);
385 doc.SetSections(propDocs);
387 cmDocumentationEntry e;
388 e.Brief =
389 "variables defined by cmake, that give information about the project, "
390 "and cmake";
391 doc.PrependSection("Variables that Provide Information",e);
393 doc.SetSeeAlsoList(cmDocumentationSeeAlso);
394 int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
396 // If we were run with no arguments, but a CMakeLists.txt file
397 // exists, the user may have been trying to use the old behavior
398 // of cmake to build a project in-source. Print a message
399 // explaining the change to standard error and return an error
400 // condition in case the program is running from a script.
401 if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
403 doc.ClearSections();
404 doc.SetSection("NOTE", cmDocumentationNOTE);
405 doc.Print(cmDocumentation::UsageForm, std::cerr);
406 return 1;
408 return result;
410 #else
411 if ( nocwd || ac == 1 )
413 std::cout <<
414 "Bootstrap CMake should not be used outside CMake build process."
415 << std::endl;
416 return 0;
418 #endif
420 bool wiz = false;
421 bool sysinfo = false;
422 bool command = false;
423 bool list_cached = false;
424 bool list_all_cached = false;
425 bool list_help = false;
426 bool view_only = false;
427 bool script_mode = false;
428 std::vector<std::string> args;
429 for(int i =0; i < ac; ++i)
431 if(strcmp(av[i], "-i") == 0)
433 wiz = true;
435 else if(!command && strcmp(av[i], "--system-information") == 0)
437 sysinfo = true;
439 // if command has already been set, then
440 // do not eat the -E
441 else if (!command && strcmp(av[i], "-E") == 0)
443 command = true;
445 else if (!command && strcmp(av[i], "-N") == 0)
447 view_only = true;
449 else if (!command && strcmp(av[i], "-L") == 0)
451 list_cached = true;
453 else if (!command && strcmp(av[i], "-LA") == 0)
455 list_all_cached = true;
457 else if (!command && strcmp(av[i], "-LH") == 0)
459 list_cached = true;
460 list_help = true;
462 else if (!command && strcmp(av[i], "-LAH") == 0)
464 list_all_cached = true;
465 list_help = true;
467 else if (!command && strncmp(av[i], "-P", strlen("-P")) == 0)
469 if ( i == ac -1 )
471 cmSystemTools::Error("No script specified for argument -P");
473 else
475 script_mode = true;
476 args.push_back(av[i]);
477 i++;
478 args.push_back(av[i]);
481 else
483 args.push_back(av[i]);
486 if(command)
488 int ret = cmake::ExecuteCMakeCommand(args);
489 return ret;
491 if (wiz)
493 cmakewizard wizard;
494 return wizard.RunWizard(args);
496 if (sysinfo)
498 cmake cm;
499 int ret = cm.GetSystemInformation(args);
500 return ret;
502 cmake cm;
503 cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
504 cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
505 cm.SetScriptMode(script_mode);
507 int res = cm.Run(args, view_only);
508 if ( list_cached || list_all_cached )
510 cmCacheManager::CacheIterator it =
511 cm.GetCacheManager()->GetCacheIterator();
512 std::cout << "-- Cache values" << std::endl;
513 for ( it.Begin(); !it.IsAtEnd(); it.Next() )
515 cmCacheManager::CacheEntryType t = it.GetType();
516 if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
517 t != cmCacheManager::UNINITIALIZED )
519 bool advanced = it.PropertyExists("ADVANCED");
520 if ( list_all_cached || !advanced)
522 if ( list_help )
524 std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
526 std::cout << it.GetName() << ":" <<
527 cmCacheManager::TypeToString(it.GetType())
528 << "=" << it.GetValue() << std::endl;
529 if ( list_help )
531 std::cout << std::endl;
538 // Always return a non-negative value. Windows tools do not always
539 // interpret negative return values as errors.
540 if(res != 0)
542 return 1;
544 else
546 return 0;
550 //----------------------------------------------------------------------------
551 static int do_build(int ac, char** av)
553 #ifndef CMAKE_BUILD_WITH_CMAKE
554 std::cerr << "This cmake does not support --build\n";
555 return -1;
556 #else
557 std::string target;
558 std::string config = "Debug";
559 std::string dir;
560 std::vector<std::string> nativeOptions;
561 bool clean = false;
563 enum Doing { DoingNone, DoingDir, DoingTarget, DoingConfig, DoingNative};
564 Doing doing = DoingDir;
565 for(int i=2; i < ac; ++i)
567 if(doing == DoingNative)
569 nativeOptions.push_back(av[i]);
571 else if(strcmp(av[i], "--target") == 0)
573 doing = DoingTarget;
575 else if(strcmp(av[i], "--config") == 0)
577 doing = DoingConfig;
579 else if(strcmp(av[i], "--clean-first") == 0)
581 clean = true;
582 doing = DoingNone;
584 else if(strcmp(av[i], "--") == 0)
586 doing = DoingNative;
588 else
590 switch (doing)
592 case DoingDir:
593 dir = av[i];
594 doing = DoingNone;
595 break;
596 case DoingTarget:
597 target = av[i];
598 doing = DoingNone;
599 break;
600 case DoingConfig:
601 config = av[i];
602 doing = DoingNone;
603 break;
604 default:
605 std::cerr << "Unknown argument " << av[i] << std::endl;
606 dir = "";
607 break;
611 if(dir.empty())
613 std::cerr <<
614 "Usage: cmake --build <dir> [options] [-- [native-options]]\n"
615 "Options:\n"
616 CMAKE_BUILD_OPTIONS
618 return 1;
621 // Hack for vs6 that passes ".\Debug" as "$(IntDir)" value:
623 if (cmSystemTools::StringStartsWith(config.c_str(), ".\\"))
625 config = config.substr(2);
628 cmake cm;
629 return cm.Build(dir, target, config, nativeOptions, clean);
630 #endif