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"
15 BuilderCLI::BuilderCLI(int argc, char **argv):
27 create_makefile(false)
31 bool full_paths = false;
32 unsigned max_depth = 4;
36 bool no_externals = false;
39 list<string> log_channels;
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();
74 logger.enable_channel("summary");
75 logger.enable_channel("tasks");
79 logger.enable_channel("environment");
80 logger.enable_channel("packages");
81 logger.enable_channel("commands");
85 logger.enable_channel("files");
86 logger.enable_channel("auxcommands");
88 for(list<string>::const_iterator i=log_channels.begin(); i!=log_channels.end(); ++i)
90 vector<string> parts = split(*i, ',');
91 for(vector<string>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
92 logger.enable_channel(*j);
94 builder.set_logger(&logger);
96 if(!analyze_mode.empty())
98 analyzer = new Analyzer(builder);
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);
109 throw usage_error("Invalid analyze mode");
111 analyzer->set_max_depth(max_depth);
112 analyzer->set_full_paths(full_paths);
114 else if(!clean && !create_makefile)
117 const vector<string> &args = getopt.get_args();
118 for(vector<string>::const_iterator i=args.begin(); i!=args.end(); ++i)
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)));
124 cmdline_targets.push_back(*i);
127 if(!work_dir.empty())
132 PackageManager &package_manager = builder.get_package_manager();
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"));
138 package_manager.set_no_externals(no_externals);
140 builder.set_architecture(arch);
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)
147 builder.load_build_file(*i);
150 builder.set_prefix(cwd/prefix);
153 builder.set_temp_directory(tempdir);
155 if(!build_type.empty())
156 builder.set_build_type(build_type);
158 builder.add_default_tools();
161 BuilderCLI::~BuilderCLI()
166 int BuilderCLI::main()
168 FS::Path main_file = cwd/build_file;
169 if(!FS::exists(main_file))
173 usage(0, "builder", false);
178 IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
183 builder.load_build_file(main_file, &cmdline_options, conf_all);
187 usage(0, "builder", false);
199 const Architecture &native_arch = builder.get_native_arch();
200 const Architecture ¤t_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));
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()));
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)
218 if(!i->second->is_prepared())
221 string line = i->second->get_name();
222 if(dynamic_cast<SourcePackage *>(i->second))
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)
233 if(j->second->needs_rebuild())
238 line += format(" (%d targets", count);
240 line += format(", %d to be built", to_be_built);
245 package_details.push_back(line);
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);
255 const Builder::ProblemList &problems = builder.get_problems();
256 if(!problems.empty())
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);
262 IO::print(IO::cerr, "Please fix them and try again.\n");
267 exit_code = builder.clean(clean>=2, dry_run);
269 exit_code = builder.build(jobs, dry_run, show_progress);
274 bool BuilderCLI::prepare_build()
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();
283 package_manager.get_main_package().prepare();
285 // Add targets from command line as goals
286 for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
288 Target *tgt = build_graph.get_target(*i);
290 tgt = vfs.get_target(*i);
292 tgt = vfs.get_target(cwd/ *i);
295 IO::print("I don't know anything about %s\n", *i);
299 build_graph.add_goal(*tgt);
302 build_graph.prepare();
305 for(NameList::iterator i=what_if.begin(); i!=what_if.end(); ++i)
307 FileTarget *tgt = vfs.get_target(cwd/ *i);
310 IO::print(IO::cerr, "Unknown what-if target %s\n", *i);
317 build_graph.force_full_rebuild();
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();
329 void BuilderCLI::usage(const char *reason, const char *argv0, bool brief)
332 IO::print(IO::cerr, "%s\n", reason);
335 IO::print(IO::cerr, "Usage: %s\n", usagemsg);
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);
345 void BuilderCLI::package_help()
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();
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)
356 if(i!=requires.begin())
358 IO::print((*i)->get_name());
360 IO::print("\n\nPackage configuration:\n");
361 for(Config::OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
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);
371 string BuilderCLI::usagemsg;
372 string BuilderCLI::helpmsg;