]> git.tdb.fi Git - builder.git/blob - source/cli/buildercli.cpp
Rename some variables to avoid conflicts with C++20 keywords
[builder.git] / source / cli / buildercli.cpp
1 #include <msp/builder/filetarget.h>
2 #include <msp/builder/sourcepackage.h>
3 #include <msp/builder/tool.h>
4 #include <msp/builder/toolchain.h>
5 #include <msp/core/getopt.h>
6 #include <msp/fs/dir.h>
7 #include <msp/fs/stat.h>
8 #include <msp/io/print.h>
9 #include <msp/strings/format.h>
10 #include <msp/strings/utils.h>
11 #include "analyzer.h"
12 #include "buildercli.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 BuilderCLI::BuilderCLI(int argc, char **argv):
18         RegisteredApplication<BuilderCLI>("builder")
19 {
20         string analyze_mode;
21         string work_dir;
22         bool full_paths = false;
23         unsigned max_depth = 4;
24         string prefix;
25         string tempdir;
26         string arch;
27         bool no_externals = false;
28         unsigned verbose = 1;
29         bool silent = false;
30         vector<string> log_channels;
31         string build_type;
32
33         GetOpt getopt;
34         getopt.add_option('a', "analyze",    analyze_mode,  GetOpt::REQUIRED_ARG).set_help("Perform dependency analysis.", "MODE");
35         getopt.add_option('b', "build",      build,         GetOpt::NO_ARG).set_help("Perform build even if also doing something else.");
36         getopt.add_option('c', "clean",      clean,         GetOpt::NO_ARG).set_help("Clean buildable targets.");
37         getopt.add_option('f', "file",       build_file,    GetOpt::REQUIRED_ARG).set_help("Read build instructions from FILE.", "FILE");
38         getopt.add_option('h', "help",       help,          GetOpt::NO_ARG).set_help("Print this message.");
39         getopt.add_option('j', "jobs",       jobs,          GetOpt::REQUIRED_ARG).set_help("Run up to NUM tasks in parallel.", "NUM");
40         getopt.add_option('l', "log",        log_channels,  GetOpt::REQUIRED_ARG).set_help("Enable listed log channels.", "LIST");
41         getopt.add_option('n', "dry-run",    dry_run,       GetOpt::NO_ARG).set_help("Show what would be done without actually doing it.");
42         getopt.add_option('s', "silent",     silent,        GetOpt::NO_ARG).set_help("Don't print any messages other than errors.");
43         getopt.add_option('t', "build-type", build_type,    GetOpt::REQUIRED_ARG).set_help("Set build type.", "TYPE");
44         getopt.add_option('v', "verbose",    verbose,       GetOpt::NO_ARG).set_help("Print more information about what's going on.");
45         getopt.add_option('x', "no-externals",no_externals, GetOpt::NO_ARG).set_help("Do not load external source packages.");
46         getopt.add_option('A', "conf-all",   conf_all,      GetOpt::NO_ARG).set_help("Apply configuration to all packages.");
47         getopt.add_option('B', "build-all",  build_all,     GetOpt::NO_ARG).set_help("Build all targets unconditionally.");
48         getopt.add_option('C', "chdir",      work_dir,      GetOpt::REQUIRED_ARG).set_help("Change to DIR before doing anything else.", "DIR");
49         getopt.add_option('P', "progress",   show_progress, GetOpt::NO_ARG).set_help("Display progress while building.");
50         getopt.add_option('W', "what-if",    what_if,       GetOpt::REQUIRED_ARG).set_help("Pretend that FILE has changed.", "FILE");
51         getopt.add_option(     "arch",       arch,          GetOpt::REQUIRED_ARG).set_help("Build for architecture ARCH.", "ARCH");
52         getopt.add_option(     "conf-only",  conf_only,     GetOpt::NO_ARG).set_help("Stop after configuring packages.");
53         getopt.add_option(     "full-paths", full_paths,    GetOpt::NO_ARG).set_help("Output full paths in analysis.");
54         getopt.add_option(     "max-depth",  max_depth,     GetOpt::REQUIRED_ARG).set_help("Show up to NUM levels in analysis.", "NUM");
55         getopt.add_option(     "prefix",     prefix,        GetOpt::REQUIRED_ARG).set_help("Install things to DIR.", "DIR");
56         getopt.add_option(     "tempdir",    tempdir,       GetOpt::REQUIRED_ARG).set_help("Store temporary files in DIR.", "DIR");
57         getopt.add_argument("target", cmdline_targets, GetOpt::OPTIONAL_ARG).set_help("Target(s) to build");
58         getopt(argc, argv);
59
60         if(help)
61         {
62                 helpmsg = "Usage:\n  ";
63                 helpmsg += getopt.generate_usage(argv[0], true);
64                 helpmsg += "\n\n";
65                 helpmsg += getopt.generate_help();
66         }
67
68         if(silent)
69                 --verbose;
70         if(verbose>=1)
71         {
72                 logger.enable_channel("summary");
73                 logger.enable_channel("tasks");
74         }
75         if(verbose>=2)
76         {
77                 logger.enable_channel("environment");
78                 logger.enable_channel("packages");
79                 logger.enable_channel("commands");
80         }
81         if(verbose>=3)
82         {
83                 logger.enable_channel("files");
84                 logger.enable_channel("auxcommands");
85         }
86         for(const string &c: log_channels)
87                 for(const string &p: split(c, ','))
88                         logger.enable_channel(p);
89         builder.set_logger(&logger);
90         builder.load_plugins();
91
92         if(!analyze_mode.empty())
93         {
94                 analyzer = new Analyzer(builder);
95
96                 if(analyze_mode=="deps")
97                         analyzer->set_mode(Analyzer::DEPS);
98                 else if(analyze_mode=="alldeps")
99                         analyzer->set_mode(Analyzer::ALLDEPS);
100                 else if(analyze_mode=="rebuild")
101                         analyzer->set_mode(Analyzer::REBUILD);
102                 else if(analyze_mode=="rdeps")
103                         analyzer->set_mode(Analyzer::RDEPS);
104                 else
105                         throw usage_error("Invalid analyze mode");
106
107                 analyzer->set_max_depth(max_depth);
108                 analyzer->set_full_paths(full_paths);
109         }
110         else if(!clean && !create_makefile)
111                 build = true;
112
113         if(!work_dir.empty())
114                 FS::chdir(work_dir);
115
116         cwd = FS::getcwd();
117
118         PackageManager &package_manager = builder.get_package_manager();
119
120         package_manager.append_package_path(cwd);
121         package_manager.append_package_path(cwd/"..");
122         package_manager.append_binary_package_path(FS::get_sys_data_dir()/"packages");
123
124         package_manager.set_no_externals(no_externals);
125
126         builder.set_architecture(tolower(arch));
127
128         vector<FS::Path> start_files;
129         start_files.push_back(FS::get_sys_data_dir()/"builderrc");
130         start_files.push_back(FS::get_user_data_dir()/"rc");
131         for(const FS::Path &f: start_files)
132                 if(FS::exists(f))
133                         builder.load_build_file(f);
134
135         if(!prefix.empty())
136                 builder.set_prefix(cwd/prefix);
137
138         if(!tempdir.empty())
139                 builder.set_temp_directory(tempdir);
140
141         if(!build_type.empty())
142                 builder.set_build_type(build_type);
143
144         builder.add_default_tools();
145
146         const Toolchain &toolchain = builder.get_toolchain();
147         for(auto i=cmdline_targets.begin(); i!=cmdline_targets.end(); )
148         {
149                 string::size_type equal = i->find('=');
150                 if(equal!=string::npos)
151                 {
152                         string key = i->substr(0, equal);
153                         string value = i->substr(equal+1);
154                         if(toolchain.has_tool(key))
155                                 toolchain.get_tool(key).set_command(value);
156                         else
157                                 cmdline_options.insert({ key, value });
158                         i = cmdline_targets.erase(i);
159                 }
160                 else
161                         ++i;
162         }
163 }
164
165 BuilderCLI::~BuilderCLI()
166 {
167         delete analyzer;
168 }
169
170 int BuilderCLI::main()
171 {
172         FS::Path main_file = cwd/build_file;
173         if(FS::exists(main_file))
174         {
175                 builder.load_build_file(main_file, &cmdline_options, conf_all);
176                 if(!dry_run && !cmdline_options.empty())
177                         builder.save_caches();
178         }
179         else if(!help)
180         {
181                 IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
182                 return 1;
183         }
184
185         if(help)
186         {
187                 IO::print("Builder 3.0\n"
188                         "Copyright © 2006-2022  Mikkosoft Productions, Mikko Rasa\n"
189                         "Licensed under the GPL\n\n"
190                         "%s", helpmsg);
191                 package_help();
192                 return 0;
193         }
194
195         const Architecture &native_arch = builder.get_native_arch();
196         const Architecture &current_arch = builder.get_current_arch();
197         logger.log("environment", "Building on %s, for %s%s", native_arch.get_name(),
198                 current_arch.get_name(), (current_arch.is_native() ? " (native)" : ""));
199         logger.log("environment", "Prefix is %s", builder.get_prefix());
200         const FS::Path &tempdir = builder.get_temp_directory();
201         if(tempdir.is_absolute())
202                 logger.log("environment", "Temporary directory is %s", tempdir);
203         else
204                 logger.log("environment", "Using per-package temporary directory %s", tempdir);
205         const BuildType &build_type = builder.get_build_type();
206         logger.log("environment", "Build type is %s", build_type.get_name());
207
208         if(!prepare_build())
209                 return 1;
210
211         if(conf_only)
212                 return 0;
213
214         BuildGraph &build_graph = builder.get_build_graph();
215         PackageManager &package_manager = builder.get_package_manager();
216         vector<string> package_details;
217         for(const auto &kvp: package_manager.get_packages())
218         {
219                 if(!kvp.second->is_prepared())
220                         continue;
221
222                 string line = kvp.second->get_name();
223                 if(dynamic_cast<SourcePackage *>(kvp.second))
224                 {
225                         line += '*';
226
227                         unsigned count = 0;
228                         unsigned to_be_built = 0;
229                         for(const auto &kvp2: build_graph.get_targets())
230                                 if(kvp2.second->get_package()==kvp.second)
231                                 {
232                                         ++count;
233                                         if(kvp2.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", "%d active packages, %d targets", package_details.size(), build_graph.get_targets().size());
249         for(const string &d: package_details)
250                 logger.log("packages", d);
251
252         if(analyzer)
253                 analyzer->analyze();
254
255         if(build_graph.get_goals().is_broken())
256         {
257                 vector<string> problems = builder.collect_problems();
258                 IO::print(IO::cerr, "The following problems were detected:\n");
259                 for(const string &p: problems)
260                         IO::print(IO::cerr, "  %s\n", p);
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         if(!dry_run)
272                 builder.save_caches();
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(const string &t: cmdline_targets)
289         {
290                 Target *tgt = resolve_target(t);
291                 if(!tgt)
292                 {
293                         IO::print("I don't know anything about %s\n", t);
294                         return false;
295                 }
296
297                 build_graph.add_goal(*tgt);
298         }
299
300         build_graph.prepare();
301
302         // Apply what-ifs
303         for(const string &w: what_if)
304         {
305                 FileTarget *tgt = dynamic_cast<FileTarget *>(resolve_target(w));
306                 if(!tgt)
307                 {
308                         IO::print(IO::cerr, "Unknown what-if target %s\n", w);
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 auto &options = config.get_options();
340         const Package::Requirements &required_pkgs = main_pkg.get_required_packages();
341
342         if(!required_pkgs.empty() || !options.empty())
343                 IO::print("\nHelp for package %s:\n", main_pkg.get_name());
344
345         if(!required_pkgs.empty())
346         {
347                 IO::print("\nRequired packages:\n  ");
348                 for(auto i=required_pkgs.begin(); i!=required_pkgs.end(); ++i)
349                 {
350                         if(i!=required_pkgs.begin())
351                                 IO::print(", ");
352                         IO::print((*i)->get_name());
353                 }
354                 IO::print("\n");
355         }
356
357         if(!options.empty())
358         {
359                 IO::print("\nPackage configuration:\n");
360                 for(const auto &kvp: options)
361                 {
362                         const Config::Option &opt = kvp.second;
363                         string line = format("  %s: %s (%s)", opt.name, opt.description, opt.value);
364                         if(!opt.choices.empty())
365                         {
366                                 line += " {";
367                                 line += join(opt.choices.begin(), opt.choices.end(), " ");
368                                 line += '}';
369                         }
370                         else if(opt.value!=opt.default_value)
371                                 line += format(" [%s]", opt.default_value);
372                         IO::print("%s\n", line);
373                 }
374         }
375 }