]> git.tdb.fi Git - builder.git/blob - source/buildercli.cpp
Separate the command-line interface into its own class
[builder.git] / source / buildercli.cpp
1 #include <msp/core/getopt.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/io/print.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "analyzer.h"
8 #include "buildercli.h"
9 #include "filetarget.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 BuilderCLI::BuilderCLI(int argc, char **argv):
16         analyzer(0),
17         build(false),
18         clean(0),
19         dry_run(false),
20         help(false),
21         show_progress(false),
22         build_file("Build"),
23         jobs(1),
24         conf_all(false),
25         conf_only(false),
26         build_all(false),
27         create_makefile(false)
28 {
29         string analyze_mode;
30         string work_dir;
31         bool full_paths = false;
32         unsigned max_depth = 4;
33         string prefix;
34         string tempdir;
35         string arch;
36         bool no_externals = false;
37         unsigned verbose = 1;
38         bool silent = false;
39         list<string> log_channels;
40         string build_type;
41
42         GetOpt getopt;
43         getopt.add_option('a', "analyze",    analyze_mode,  GetOpt::REQUIRED_ARG).set_help("Perform dependency analysis.", "MODE");
44         getopt.add_option('b', "build",      build,         GetOpt::NO_ARG).set_help("Perform build even if also doing something else.");
45         getopt.add_option('c', "clean",      clean,         GetOpt::NO_ARG).set_help("Clean buildable targets.");
46         getopt.add_option('f', "file",       build_file,    GetOpt::REQUIRED_ARG).set_help("Read build instructions from FILE.", "FILE");
47         getopt.add_option('h', "help",       help,          GetOpt::NO_ARG).set_help("Print this message.");
48         getopt.add_option('j', "jobs",       jobs,          GetOpt::REQUIRED_ARG).set_help("Run up to NUM tasks in parallel.", "NUM");
49         getopt.add_option('l', "log",        log_channels,  GetOpt::REQUIRED_ARG).set_help("Enable listed log channels.", "LIST");
50         getopt.add_option('n', "dry-run",    dry_run,       GetOpt::NO_ARG).set_help("Show what would be done without actually doing it.");
51         getopt.add_option('s', "silent",     silent,        GetOpt::NO_ARG).set_help("Don't print any messages other than errors.");
52         getopt.add_option('t', "build-type", build_type,    GetOpt::REQUIRED_ARG).set_help("Set build type.", "TYPE");
53         getopt.add_option('v', "verbose",    verbose,       GetOpt::NO_ARG).set_help("Print more information about what's going on.");
54         getopt.add_option('x', "no-externals",no_externals, GetOpt::NO_ARG).set_help("Do not load external source packages.");
55         getopt.add_option('A', "conf-all",   conf_all,      GetOpt::NO_ARG).set_help("Apply configuration to all packages.");
56         getopt.add_option('B', "build-all",  build_all,     GetOpt::NO_ARG).set_help("Build all targets unconditionally.");
57         getopt.add_option('C', "chdir",      work_dir,      GetOpt::REQUIRED_ARG).set_help("Change to DIR before doing anything else.", "DIR");
58         getopt.add_option('P', "progress",   show_progress, GetOpt::NO_ARG).set_help("Display progress while building.");
59         getopt.add_option('W', "what-if",    what_if,       GetOpt::REQUIRED_ARG).set_help("Pretend that FILE has changed.", "FILE");
60         getopt.add_option(     "arch",       arch,          GetOpt::REQUIRED_ARG).set_help("Build for architecture ARCH.", "ARCH");
61         getopt.add_option(     "conf-only",  conf_only,     GetOpt::NO_ARG).set_help("Stop after configuring packages.");
62         getopt.add_option(     "full-paths", full_paths,    GetOpt::NO_ARG).set_help("Output full paths in analysis.");
63         getopt.add_option(     "max-depth",  max_depth,     GetOpt::REQUIRED_ARG).set_help("Show up to NUM levels in analysis.", "NUM");
64         getopt.add_option(     "prefix",     prefix,        GetOpt::REQUIRED_ARG).set_help("Install things to DIR.", "DIR");
65         getopt.add_option(     "tempdir",    tempdir,       GetOpt::REQUIRED_ARG).set_help("Store temporary files in DIR.", "DIR");
66         usagemsg = getopt.generate_usage(argv[0])+" [<target> ...]";
67         helpmsg = getopt.generate_help();
68         getopt(argc, argv);
69
70         if(silent)
71                 --verbose;
72         if(verbose>=1)
73         {
74                 logger.enable_channel("summary");
75                 logger.enable_channel("tasks");
76         }
77         if(verbose>=2)
78         {
79                 logger.enable_channel("environment");
80                 logger.enable_channel("packages");
81                 logger.enable_channel("commands");
82         }
83         if(verbose>=3)
84         {
85                 logger.enable_channel("files");
86                 logger.enable_channel("auxcommands");
87         }
88         for(list<string>::const_iterator i=log_channels.begin(); i!=log_channels.end(); ++i)
89         {
90                 vector<string> parts = split(*i, ',');
91                 for(vector<string>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
92                         logger.enable_channel(*j);
93         }
94         builder.set_logger(&logger);
95
96         if(!analyze_mode.empty())
97         {
98                 analyzer = new Analyzer(builder);
99
100                 if(analyze_mode=="deps")
101                         analyzer->set_mode(Analyzer::DEPS);
102                 else if(analyze_mode=="alldeps")
103                         analyzer->set_mode(Analyzer::ALLDEPS);
104                 else if(analyze_mode=="rebuild")
105                         analyzer->set_mode(Analyzer::REBUILD);
106                 else if(analyze_mode=="rdeps")
107                         analyzer->set_mode(Analyzer::RDEPS);
108                 else
109                         throw usage_error("Invalid analyze mode");
110
111                 analyzer->set_max_depth(max_depth);
112                 analyzer->set_full_paths(full_paths);
113         }
114         else if(!clean && !create_makefile)
115                 build = true;
116
117         const vector<string> &args = getopt.get_args();
118         for(vector<string>::const_iterator i=args.begin(); i!=args.end(); ++i)
119         {
120                 string::size_type equal = i->find('=');
121                 if(equal!=string::npos)
122                         cmdline_options.insert(Config::InputOptions::value_type(i->substr(0, equal), i->substr(equal+1)));
123                 else
124                         cmdline_targets.push_back(*i);
125         }
126
127         if(!work_dir.empty())
128                 FS::chdir(work_dir);
129
130         cwd = FS::getcwd();
131
132         PackageManager &package_manager = builder.get_package_manager();
133
134         package_manager.append_package_path(cwd);
135         package_manager.append_package_path(cwd/"..");
136         package_manager.append_binary_package_path(FS::get_sys_data_dir(argv[0], "builder"));
137
138         package_manager.set_no_externals(no_externals);
139
140         builder.set_architecture(arch);
141
142         list<FS::Path> start_files;
143         start_files.push_back(FS::get_sys_data_dir(argv[0], "builder")/"builderrc");
144         start_files.push_back(FS::get_user_data_dir("builder")/"rc");
145         for(list<FS::Path>::const_iterator i=start_files.begin(); i!=start_files.end(); ++i)
146                 if(FS::exists(*i))
147                         builder.load_build_file(*i);
148
149         if(!prefix.empty())
150                 builder.set_prefix(cwd/prefix);
151
152         if(!tempdir.empty())
153                 builder.set_temp_directory(tempdir);
154
155         if(!build_type.empty())
156                 builder.set_build_type(build_type);
157
158         builder.add_default_tools();
159 }
160
161 BuilderCLI::~BuilderCLI()
162 {
163         delete analyzer;
164 }
165
166 int BuilderCLI::main()
167 {
168         FS::Path main_file = cwd/build_file;
169         if(!FS::exists(main_file))
170         {
171                 if(help)
172                 {
173                         usage(0, "builder", false);
174                         return 0;
175                 }
176                 else
177                 {
178                         IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
179                         return 1;
180                 }
181         }
182
183         builder.load_build_file(main_file, &cmdline_options, conf_all);
184
185         if(help)
186         {
187                 usage(0, "builder", false);
188                 IO::print("\n");
189                 package_help();
190                 return 0;
191         }
192
193         if(!prepare_build())
194                 return 1;
195
196         if(conf_only)
197                 return 0;
198
199         const Architecture &native_arch = builder.get_native_arch();
200         const Architecture &current_arch = builder.get_current_arch();
201         logger.log("environment", format("Building on %s, for %s%s", native_arch.get_name(),
202                 current_arch.get_name(), (current_arch.is_native() ? " (native)" : "")));
203         logger.log("environment", format("Prefix is %s", builder.get_prefix()));
204         const FS::Path &tempdir = builder.get_temp_directory();
205         if(tempdir.is_absolute())
206                 logger.log("environment", format("Temporary directory is %s", tempdir));
207         else
208                 logger.log("environment", format("Using per-package temporary directory %s", tempdir));
209         const BuildType &build_type = builder.get_build_type();
210         logger.log("environment", format("Build type is %s", build_type.get_name()));
211
212         BuildGraph &build_graph = builder.get_build_graph();
213         PackageManager &package_manager = builder.get_package_manager();
214         const PackageManager::PackageMap &packages = package_manager.get_packages();
215         list<string> package_details;
216         for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
217         {
218                 if(!i->second->is_prepared())
219                         continue;
220
221                 string line = i->second->get_name();
222                 if(dynamic_cast<SourcePackage *>(i->second))
223                 {
224                         line += '*';
225
226                         unsigned count = 0;
227                         unsigned to_be_built = 0;
228                         const BuildGraph::TargetMap &targets = build_graph.get_targets();
229                         for(BuildGraph::TargetMap::const_iterator j=targets.begin(); j!=targets.end(); ++j)
230                                 if(j->second->get_package()==i->second)
231                                 {
232                                         ++count;
233                                         if(j->second->needs_rebuild())
234                                                 ++to_be_built;
235                                 }
236                         if(count)
237                         {
238                                 line += format(" (%d targets", count);
239                                 if(to_be_built)
240                                         line += format(", %d to be built", to_be_built);
241                                 line += ')';
242                         }
243                 }
244
245                 package_details.push_back(line);
246         }
247
248         logger.log("summary", format("%d active packages, %d targets", package_details.size(), build_graph.get_targets().size()));
249         for(list<string>::const_iterator i=package_details.begin(); i!=package_details.end(); ++i)
250                 logger.log("packages", *i);
251
252         if(analyzer)
253                 analyzer->analyze();
254
255         const Builder::ProblemList &problems = builder.get_problems();
256         if(!problems.empty())
257         {
258                 IO::print(IO::cerr, "The following problems were detected:\n");
259                 for(Builder::ProblemList::const_iterator i=problems.begin(); i!=problems.end(); ++i)
260                         IO::print(IO::cerr, "  %s: %s\n", i->package, i->descr);
261                 if(!analyzer)
262                         IO::print(IO::cerr, "Please fix them and try again.\n");
263                 return 1;
264         }
265
266         if(clean)
267                 exit_code = builder.clean(clean>=2, dry_run);
268         if(build)
269                 exit_code = builder.build(jobs, dry_run, show_progress);
270
271         return exit_code;
272 }
273
274 bool BuilderCLI::prepare_build()
275 {
276         /* XXX This is ugly; using the Builder class should not be this convoluted.
277         But the command line targets and what ifs need to be set at certain points
278         during preparation. */
279         BuildGraph &build_graph = builder.get_build_graph();
280         PackageManager &package_manager = builder.get_package_manager();
281         VirtualFileSystem &vfs = builder.get_vfs();
282
283         package_manager.get_main_package().prepare();
284
285         // Add targets from command line as goals
286         for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
287         {
288                 Target *tgt = build_graph.get_target(*i);
289                 if(!tgt)
290                         tgt = vfs.get_target(*i);
291                 if(!tgt)
292                         tgt = vfs.get_target(cwd/ *i);
293                 if(!tgt)
294                 {
295                         IO::print("I don't know anything about %s\n", *i);
296                         return false;
297                 }
298
299                 build_graph.add_goal(*tgt);
300         }
301
302         build_graph.prepare();
303
304         // Apply what-ifs
305         for(NameList::iterator i=what_if.begin(); i!=what_if.end(); ++i)
306         {
307                 FileTarget *tgt = vfs.get_target(cwd/ *i);
308                 if(!tgt)
309                 {
310                         IO::print(IO::cerr, "Unknown what-if target %s\n", *i);
311                         return false;
312                 }
313                 tgt->touch();
314         }
315
316         if(build_all)
317                 build_graph.force_full_rebuild();
318
319         if(!dry_run)
320         {
321                 const PackageManager::PackageMap &packages = package_manager.get_packages();
322                 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
323                         i->second->save_caches();
324         }
325
326         return true;
327 }
328
329 void BuilderCLI::usage(const char *reason, const char *argv0, bool brief)
330 {
331         if(reason)
332                 IO::print(IO::cerr, "%s\n", reason);
333
334         if(brief)
335                 IO::print(IO::cerr, "Usage: %s\n", usagemsg);
336         else
337         {
338                 IO::print(IO::cerr, "Builder 1.0\n\n");
339                 IO::print(IO::cerr, "Usage: %s [options] [<target> ...]\n\n", argv0);
340                 IO::print(IO::cerr, "Options:\n");
341                 IO::print(IO::cerr, helpmsg);
342         }
343 }
344
345 void BuilderCLI::package_help()
346 {
347         PackageManager &package_manager = builder.get_package_manager();
348         SourcePackage &main_pkg = dynamic_cast<SourcePackage &>(package_manager.get_main_package());
349         const Config &config = main_pkg.get_config();
350         const Config::OptionMap &options = config.get_options();
351
352         IO::print("Required packages:\n  ");
353         const Package::Requirements &requires = main_pkg.get_required_packages();
354         for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
355         {
356                 if(i!=requires.begin())
357                         IO::print(", ");
358                 IO::print((*i)->get_name());
359         }
360         IO::print("\n\nPackage configuration:\n");
361         for(Config::OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
362         {
363                 const Config::Option &opt = i->second;
364                 IO::print("  %s: %s (%s)", opt.name, opt.description, opt.value);
365                 if(opt.value!=opt.default_value)
366                         IO::print(" [%s]", opt.default_value);
367                 IO::print("\n");
368         }
369 }
370
371 string BuilderCLI::usagemsg;
372 string BuilderCLI::helpmsg;