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