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>
8 #include "buildercli.h"
9 #include "filetarget.h"
10 #include "sourcepackage.h"
12 #include "toolchain.h"
17 BuilderCLI::BuilderCLI(int argc, char **argv):
18 RegisteredApplication<BuilderCLI>("builder"),
30 create_makefile(false)
34 bool full_paths = false;
35 unsigned max_depth = 4;
39 bool no_externals = false;
42 list<string> log_channels;
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");
74 helpmsg = "Usage:\n ";
75 helpmsg += getopt.generate_usage(argv[0], true);
77 helpmsg += getopt.generate_help();
84 logger.enable_channel("summary");
85 logger.enable_channel("tasks");
89 logger.enable_channel("environment");
90 logger.enable_channel("packages");
91 logger.enable_channel("commands");
95 logger.enable_channel("files");
96 logger.enable_channel("auxcommands");
98 for(const string &c: log_channels)
99 for(const string &p: split(c, ','))
100 logger.enable_channel(p);
101 builder.set_logger(&logger);
103 if(!analyze_mode.empty())
105 analyzer = new Analyzer(builder);
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);
116 throw usage_error("Invalid analyze mode");
118 analyzer->set_max_depth(max_depth);
119 analyzer->set_full_paths(full_paths);
121 else if(!clean && !create_makefile)
124 if(!work_dir.empty())
129 PackageManager &package_manager = builder.get_package_manager();
131 package_manager.append_package_path(cwd);
132 package_manager.append_package_path(cwd/"..");
133 package_manager.append_binary_package_path(FS::get_sys_data_dir()/"packages");
135 package_manager.set_no_externals(no_externals);
137 builder.set_architecture(tolower(arch));
139 list<FS::Path> start_files;
140 start_files.push_back(FS::get_sys_data_dir()/"builderrc");
141 start_files.push_back(FS::get_user_data_dir()/"rc");
142 for(const FS::Path &f: start_files)
144 builder.load_build_file(f);
147 builder.set_prefix(cwd/prefix);
150 builder.set_temp_directory(tempdir);
152 if(!build_type.empty())
153 builder.set_build_type(build_type);
155 builder.add_default_tools();
157 const Toolchain &toolchain = builder.get_toolchain();
158 for(auto i=cmdline_targets.begin(); i!=cmdline_targets.end(); )
160 string::size_type equal = i->find('=');
161 if(equal!=string::npos)
163 string key = i->substr(0, equal);
164 string value = i->substr(equal+1);
165 if(toolchain.has_tool(key))
166 toolchain.get_tool(key).set_command(value);
168 cmdline_options.insert({ key, value });
169 i = cmdline_targets.erase(i);
176 BuilderCLI::~BuilderCLI()
181 int BuilderCLI::main()
183 FS::Path main_file = cwd/build_file;
184 if(FS::exists(main_file))
186 builder.load_build_file(main_file, &cmdline_options, conf_all);
187 if(!dry_run && !cmdline_options.empty())
188 builder.save_caches();
192 IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
198 IO::print("Builder 1.0\n"
199 "Copyright © 2006-2013 Mikkosoft Productions, Mikko Rasa\n"
200 "Licensed under the GPL\n\n"
212 const Architecture &native_arch = builder.get_native_arch();
213 const Architecture ¤t_arch = builder.get_current_arch();
214 logger.log("environment", format("Building on %s, for %s%s", native_arch.get_name(),
215 current_arch.get_name(), (current_arch.is_native() ? " (native)" : "")));
216 logger.log("environment", format("Prefix is %s", builder.get_prefix()));
217 const FS::Path &tempdir = builder.get_temp_directory();
218 if(tempdir.is_absolute())
219 logger.log("environment", format("Temporary directory is %s", tempdir));
221 logger.log("environment", format("Using per-package temporary directory %s", tempdir));
222 const BuildType &build_type = builder.get_build_type();
223 logger.log("environment", format("Build type is %s", build_type.get_name()));
225 BuildGraph &build_graph = builder.get_build_graph();
226 PackageManager &package_manager = builder.get_package_manager();
227 list<string> package_details;
228 for(const auto &kvp: package_manager.get_packages())
230 if(!kvp.second->is_prepared())
233 string line = kvp.second->get_name();
234 if(dynamic_cast<SourcePackage *>(kvp.second))
239 unsigned to_be_built = 0;
240 for(const auto &kvp2: build_graph.get_targets())
241 if(kvp2.second->get_package()==kvp.second)
244 if(kvp2.second->needs_rebuild())
249 line += format(" (%d targets", count);
251 line += format(", %d to be built", to_be_built);
256 package_details.push_back(line);
259 logger.log("summary", format("%d active packages, %d targets", package_details.size(), build_graph.get_targets().size()));
260 for(const string &d: package_details)
261 logger.log("packages", d);
266 if(build_graph.get_goals().is_broken())
268 list<string> problems = builder.collect_problems();
269 IO::print(IO::cerr, "The following problems were detected:\n");
270 for(const string &p: problems)
271 IO::print(IO::cerr, " %s\n", p);
273 IO::print(IO::cerr, "Please fix them and try again.\n");
278 exit_code = builder.clean(clean>=2, dry_run);
280 exit_code = builder.build(jobs, dry_run, show_progress);
283 builder.save_caches();
288 bool BuilderCLI::prepare_build()
290 /* XXX This is ugly; using the Builder class should not be this convoluted.
291 But the command line targets and what ifs need to be set at certain points
292 during preparation. */
293 BuildGraph &build_graph = builder.get_build_graph();
294 PackageManager &package_manager = builder.get_package_manager();
296 package_manager.get_main_package().prepare();
298 // Add targets from command line as goals
299 for(const string &t: cmdline_targets)
301 Target *tgt = resolve_target(t);
304 IO::print("I don't know anything about %s\n", t);
308 build_graph.add_goal(*tgt);
311 build_graph.prepare();
314 for(const string &w: what_if)
316 FileTarget *tgt = dynamic_cast<FileTarget *>(resolve_target(w));
319 IO::print(IO::cerr, "Unknown what-if target %s\n", w);
326 build_graph.force_full_rebuild();
329 package_manager.save_all_caches();
334 Target *BuilderCLI::resolve_target(const string &name)
336 Target *tgt = builder.get_build_graph().get_target(name);
338 tgt = builder.get_vfs().get_target(cwd/name);
342 void BuilderCLI::package_help()
344 PackageManager &package_manager = builder.get_package_manager();
345 if(package_manager.get_packages().empty())
348 SourcePackage &main_pkg = dynamic_cast<SourcePackage &>(package_manager.get_main_package());
349 const Config &config = main_pkg.get_config();
350 const auto &options = config.get_options();
351 const Package::Requirements &requires = main_pkg.get_required_packages();
353 if(!requires.empty() || !options.empty())
354 IO::print("\nHelp for package %s:\n", main_pkg.get_name());
356 if(!requires.empty())
358 IO::print("\nRequired packages:\n ");
359 for(auto i=requires.begin(); i!=requires.end(); ++i)
361 if(i!=requires.begin())
363 IO::print((*i)->get_name());
370 IO::print("\nPackage configuration:\n");
371 for(const auto &kvp: options)
373 const Config::Option &opt = kvp.second;
374 string line = format(" %s: %s (%s)", opt.name, opt.description, opt.value);
375 if(!opt.choices.empty())
378 line += join(opt.choices.begin(), opt.choices.end(), " ");
381 else if(opt.value!=opt.default_value)
382 line += format(" [%s]", opt.default_value);
383 IO::print("%s\n", line);