]> git.tdb.fi Git - builder.git/blob - source/buildercli.cpp
c0a4f06c8f10c1b692b39c7a79798cf361a362e6
[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         getopt.add_argument("target", cmdline_targets, GetOpt::OPTIONAL_ARG).set_help("Target(s) to build");
67         getopt(argc, argv);
68
69         if(help)
70         {
71                 helpmsg = "Usage:\n  ";
72                 helpmsg += getopt.generate_usage(argv[0], true);
73                 helpmsg += "\n\n";
74                 helpmsg += getopt.generate_help();
75         }
76
77         if(silent)
78                 --verbose;
79         if(verbose>=1)
80         {
81                 logger.enable_channel("summary");
82                 logger.enable_channel("tasks");
83         }
84         if(verbose>=2)
85         {
86                 logger.enable_channel("environment");
87                 logger.enable_channel("packages");
88                 logger.enable_channel("commands");
89         }
90         if(verbose>=3)
91         {
92                 logger.enable_channel("files");
93                 logger.enable_channel("auxcommands");
94         }
95         for(list<string>::const_iterator i=log_channels.begin(); i!=log_channels.end(); ++i)
96         {
97                 vector<string> parts = split(*i, ',');
98                 for(vector<string>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
99                         logger.enable_channel(*j);
100         }
101         builder.set_logger(&logger);
102
103         if(!analyze_mode.empty())
104         {
105                 analyzer = new Analyzer(builder);
106
107                 if(analyze_mode=="deps")
108                         analyzer->set_mode(Analyzer::DEPS);
109                 else if(analyze_mode=="alldeps")
110                         analyzer->set_mode(Analyzer::ALLDEPS);
111                 else if(analyze_mode=="rebuild")
112                         analyzer->set_mode(Analyzer::REBUILD);
113                 else if(analyze_mode=="rdeps")
114                         analyzer->set_mode(Analyzer::RDEPS);
115                 else
116                         throw usage_error("Invalid analyze mode");
117
118                 analyzer->set_max_depth(max_depth);
119                 analyzer->set_full_paths(full_paths);
120         }
121         else if(!clean && !create_makefile)
122                 build = true;
123
124         for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); )
125         {
126                 string::size_type equal = i->find('=');
127                 if(equal!=string::npos)
128                 {
129                         cmdline_options.insert(Config::InputOptions::value_type(i->substr(0, equal), i->substr(equal+1)));
130                         cmdline_targets.erase(i++);
131                 }
132                 else
133                         ++i;
134         }
135
136         if(!work_dir.empty())
137                 FS::chdir(work_dir);
138
139         cwd = FS::getcwd();
140
141         PackageManager &package_manager = builder.get_package_manager();
142
143         package_manager.append_package_path(cwd);
144         package_manager.append_package_path(cwd/"..");
145         package_manager.append_binary_package_path(FS::get_sys_data_dir(argv[0], "builder"));
146
147         package_manager.set_no_externals(no_externals);
148
149         builder.set_architecture(arch);
150
151         list<FS::Path> start_files;
152         start_files.push_back(FS::get_sys_data_dir(argv[0], "builder")/"builderrc");
153         start_files.push_back(FS::get_user_data_dir("builder")/"rc");
154         for(list<FS::Path>::const_iterator i=start_files.begin(); i!=start_files.end(); ++i)
155                 if(FS::exists(*i))
156                         builder.load_build_file(*i);
157
158         if(!prefix.empty())
159                 builder.set_prefix(cwd/prefix);
160
161         if(!tempdir.empty())
162                 builder.set_temp_directory(tempdir);
163
164         if(!build_type.empty())
165                 builder.set_build_type(build_type);
166
167         builder.add_default_tools();
168 }
169
170 BuilderCLI::~BuilderCLI()
171 {
172         delete analyzer;
173 }
174
175 int BuilderCLI::main()
176 {
177         FS::Path main_file = cwd/build_file;
178         if(FS::exists(main_file))
179                 builder.load_build_file(main_file, &cmdline_options, conf_all);
180         else if(!help)
181         {
182                 IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
183                 return 1;
184         }
185
186         if(help)
187         {
188                 IO::print("Builder 1.0\n"
189                         "Copyright © 2006-2013  Mikkosoft Productions, Mikko Rasa\n"
190                         "Licensed under the GPL\n\n"
191                         "%s", helpmsg);
192                 package_help();
193                 return 0;
194         }
195
196         if(!prepare_build())
197                 return 1;
198
199         if(conf_only)
200                 return 0;
201
202         const Architecture &native_arch = builder.get_native_arch();
203         const Architecture &current_arch = builder.get_current_arch();
204         logger.log("environment", format("Building on %s, for %s%s", native_arch.get_name(),
205                 current_arch.get_name(), (current_arch.is_native() ? " (native)" : "")));
206         logger.log("environment", format("Prefix is %s", builder.get_prefix()));
207         const FS::Path &tempdir = builder.get_temp_directory();
208         if(tempdir.is_absolute())
209                 logger.log("environment", format("Temporary directory is %s", tempdir));
210         else
211                 logger.log("environment", format("Using per-package temporary directory %s", tempdir));
212         const BuildType &build_type = builder.get_build_type();
213         logger.log("environment", format("Build type is %s", build_type.get_name()));
214
215         BuildGraph &build_graph = builder.get_build_graph();
216         PackageManager &package_manager = builder.get_package_manager();
217         const PackageManager::PackageMap &packages = package_manager.get_packages();
218         list<string> package_details;
219         for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
220         {
221                 if(!i->second->is_prepared())
222                         continue;
223
224                 string line = i->second->get_name();
225                 if(dynamic_cast<SourcePackage *>(i->second))
226                 {
227                         line += '*';
228
229                         unsigned count = 0;
230                         unsigned to_be_built = 0;
231                         const BuildGraph::TargetMap &targets = build_graph.get_targets();
232                         for(BuildGraph::TargetMap::const_iterator j=targets.begin(); j!=targets.end(); ++j)
233                                 if(j->second->get_package()==i->second)
234                                 {
235                                         ++count;
236                                         if(j->second->needs_rebuild())
237                                                 ++to_be_built;
238                                 }
239                         if(count)
240                         {
241                                 line += format(" (%d targets", count);
242                                 if(to_be_built)
243                                         line += format(", %d to be built", to_be_built);
244                                 line += ')';
245                         }
246                 }
247
248                 package_details.push_back(line);
249         }
250
251         logger.log("summary", format("%d active packages, %d targets", package_details.size(), build_graph.get_targets().size()));
252         for(list<string>::const_iterator i=package_details.begin(); i!=package_details.end(); ++i)
253                 logger.log("packages", *i);
254
255         if(analyzer)
256                 analyzer->analyze();
257
258         if(build_graph.get_goals().is_broken())
259         {
260                 list<string> problems = builder.collect_problems();
261                 IO::print(IO::cerr, "The following problems were detected:\n");
262                 for(list<string>::const_iterator i=problems.begin(); i!=problems.end(); ++i)
263                         IO::print(IO::cerr, "  %s\n", *i);
264                 if(!analyzer)
265                         IO::print(IO::cerr, "Please fix them and try again.\n");
266                 return 1;
267         }
268
269         if(clean)
270                 exit_code = builder.clean(clean>=2, dry_run);
271         if(build)
272                 exit_code = builder.build(jobs, dry_run, show_progress);
273
274         return exit_code;
275 }
276
277 bool BuilderCLI::prepare_build()
278 {
279         /* XXX This is ugly; using the Builder class should not be this convoluted.
280         But the command line targets and what ifs need to be set at certain points
281         during preparation. */
282         BuildGraph &build_graph = builder.get_build_graph();
283         PackageManager &package_manager = builder.get_package_manager();
284
285         package_manager.get_main_package().prepare();
286
287         // Add targets from command line as goals
288         for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
289         {
290                 Target *tgt = resolve_target(*i);
291                 if(!tgt)
292                 {
293                         IO::print("I don't know anything about %s\n", *i);
294                         return false;
295                 }
296
297                 build_graph.add_goal(*tgt);
298         }
299
300         build_graph.prepare();
301
302         // Apply what-ifs
303         for(NameList::iterator i=what_if.begin(); i!=what_if.end(); ++i)
304         {
305                 FileTarget *tgt = dynamic_cast<FileTarget *>(resolve_target(*i));
306                 if(!tgt)
307                 {
308                         IO::print(IO::cerr, "Unknown what-if target %s\n", *i);
309                         return false;
310                 }
311                 tgt->touch();
312         }
313
314         if(build_all)
315                 build_graph.force_full_rebuild();
316
317         if(!dry_run)
318                 package_manager.save_all_caches();
319
320         return true;
321 }
322
323 Target *BuilderCLI::resolve_target(const string &name)
324 {
325         Target *tgt = builder.get_build_graph().get_target(name);
326         if(!tgt)
327                 tgt = builder.get_vfs().get_target(cwd/name);
328         return tgt;
329 }
330
331 void BuilderCLI::package_help()
332 {
333         PackageManager &package_manager = builder.get_package_manager();
334         if(package_manager.get_packages().empty())
335                 return;
336
337         SourcePackage &main_pkg = dynamic_cast<SourcePackage &>(package_manager.get_main_package());
338         const Config &config = main_pkg.get_config();
339         const Config::OptionMap &options = config.get_options();
340
341         IO::print("\nRequired packages:\n  ");
342         const Package::Requirements &requires = main_pkg.get_required_packages();
343         for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
344         {
345                 if(i!=requires.begin())
346                         IO::print(", ");
347                 IO::print((*i)->get_name());
348         }
349         IO::print("\n");
350
351         if(!options.empty())
352         {
353                 IO::print("\nPackage configuration:\n");
354                 for(Config::OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
355                 {
356                         const Config::Option &opt = i->second;
357                         string line = format("  %s: %s (%s)", opt.name, opt.description, opt.value);
358                         if(!opt.choices.empty())
359                         {
360                                 line += " {";
361                                 for(list<string>::const_iterator j=opt.choices.begin(); j!=opt.choices.end(); ++j)
362                                 {
363                                         if(j!=opt.choices.begin())
364                                                 line += ' ';
365                                         line += *j;
366                                 }
367                                 line += '}';
368                         }
369                         else if(opt.value!=opt.default_value)
370                                 line += format(" [%s]", opt.default_value);
371                         IO::print("%s\n", line);
372                 }
373         }
374 }