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):
29 create_makefile(false)
33 bool full_paths = false;
34 unsigned max_depth = 4;
38 bool no_externals = false;
41 list<string> log_channels;
45 getopt.add_option('a', "analyze", analyze_mode, GetOpt::REQUIRED_ARG).set_help("Perform dependency analysis.", "MODE");
46 getopt.add_option('b', "build", build, GetOpt::NO_ARG).set_help("Perform build even if also doing something else.");
47 getopt.add_option('c', "clean", clean, GetOpt::NO_ARG).set_help("Clean buildable targets.");
48 getopt.add_option('f', "file", build_file, GetOpt::REQUIRED_ARG).set_help("Read build instructions from FILE.", "FILE");
49 getopt.add_option('h', "help", help, GetOpt::NO_ARG).set_help("Print this message.");
50 getopt.add_option('j', "jobs", jobs, GetOpt::REQUIRED_ARG).set_help("Run up to NUM tasks in parallel.", "NUM");
51 getopt.add_option('l', "log", log_channels, GetOpt::REQUIRED_ARG).set_help("Enable listed log channels.", "LIST");
52 getopt.add_option('n', "dry-run", dry_run, GetOpt::NO_ARG).set_help("Show what would be done without actually doing it.");
53 getopt.add_option('s', "silent", silent, GetOpt::NO_ARG).set_help("Don't print any messages other than errors.");
54 getopt.add_option('t', "build-type", build_type, GetOpt::REQUIRED_ARG).set_help("Set build type.", "TYPE");
55 getopt.add_option('v', "verbose", verbose, GetOpt::NO_ARG).set_help("Print more information about what's going on.");
56 getopt.add_option('x', "no-externals",no_externals, GetOpt::NO_ARG).set_help("Do not load external source packages.");
57 getopt.add_option('A', "conf-all", conf_all, GetOpt::NO_ARG).set_help("Apply configuration to all packages.");
58 getopt.add_option('B', "build-all", build_all, GetOpt::NO_ARG).set_help("Build all targets unconditionally.");
59 getopt.add_option('C', "chdir", work_dir, GetOpt::REQUIRED_ARG).set_help("Change to DIR before doing anything else.", "DIR");
60 getopt.add_option('P', "progress", show_progress, GetOpt::NO_ARG).set_help("Display progress while building.");
61 getopt.add_option('W', "what-if", what_if, GetOpt::REQUIRED_ARG).set_help("Pretend that FILE has changed.", "FILE");
62 getopt.add_option( "arch", arch, GetOpt::REQUIRED_ARG).set_help("Build for architecture ARCH.", "ARCH");
63 getopt.add_option( "conf-only", conf_only, GetOpt::NO_ARG).set_help("Stop after configuring packages.");
64 getopt.add_option( "full-paths", full_paths, GetOpt::NO_ARG).set_help("Output full paths in analysis.");
65 getopt.add_option( "max-depth", max_depth, GetOpt::REQUIRED_ARG).set_help("Show up to NUM levels in analysis.", "NUM");
66 getopt.add_option( "prefix", prefix, GetOpt::REQUIRED_ARG).set_help("Install things to DIR.", "DIR");
67 getopt.add_option( "tempdir", tempdir, GetOpt::REQUIRED_ARG).set_help("Store temporary files in DIR.", "DIR");
68 getopt.add_argument("target", cmdline_targets, GetOpt::OPTIONAL_ARG).set_help("Target(s) to build");
73 helpmsg = "Usage:\n ";
74 helpmsg += getopt.generate_usage(argv[0], true);
76 helpmsg += getopt.generate_help();
83 logger.enable_channel("summary");
84 logger.enable_channel("tasks");
88 logger.enable_channel("environment");
89 logger.enable_channel("packages");
90 logger.enable_channel("commands");
94 logger.enable_channel("files");
95 logger.enable_channel("auxcommands");
97 for(list<string>::const_iterator i=log_channels.begin(); i!=log_channels.end(); ++i)
99 vector<string> parts = split(*i, ',');
100 for(vector<string>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
101 logger.enable_channel(*j);
103 builder.set_logger(&logger);
105 if(!analyze_mode.empty())
107 analyzer = new Analyzer(builder);
109 if(analyze_mode=="deps")
110 analyzer->set_mode(Analyzer::DEPS);
111 else if(analyze_mode=="alldeps")
112 analyzer->set_mode(Analyzer::ALLDEPS);
113 else if(analyze_mode=="rebuild")
114 analyzer->set_mode(Analyzer::REBUILD);
115 else if(analyze_mode=="rdeps")
116 analyzer->set_mode(Analyzer::RDEPS);
118 throw usage_error("Invalid analyze mode");
120 analyzer->set_max_depth(max_depth);
121 analyzer->set_full_paths(full_paths);
123 else if(!clean && !create_makefile)
126 for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); )
128 string::size_type equal = i->find('=');
129 if(equal!=string::npos)
131 cmdline_options.insert(Config::InputOptions::value_type(i->substr(0, equal), i->substr(equal+1)));
132 cmdline_targets.erase(i++);
138 if(!work_dir.empty())
143 PackageManager &package_manager = builder.get_package_manager();
145 package_manager.append_package_path(cwd);
146 package_manager.append_package_path(cwd/"..");
147 package_manager.append_binary_package_path(FS::get_sys_data_dir(argv[0], "builder"));
149 package_manager.set_no_externals(no_externals);
151 builder.set_architecture(arch);
153 list<FS::Path> start_files;
154 start_files.push_back(FS::get_sys_data_dir(argv[0], "builder")/"builderrc");
155 start_files.push_back(FS::get_user_data_dir("builder")/"rc");
156 for(list<FS::Path>::const_iterator i=start_files.begin(); i!=start_files.end(); ++i)
158 builder.load_build_file(*i);
161 builder.set_prefix(cwd/prefix);
164 builder.set_temp_directory(tempdir);
166 if(!build_type.empty())
167 builder.set_build_type(build_type);
169 builder.add_default_tools();
171 const Toolchain &toolchain = builder.get_toolchain();
172 for(Config::InputOptions::iterator i=cmdline_options.begin(); i!=cmdline_options.end(); )
174 if(toolchain.has_tool(i->first))
176 toolchain.get_tool(i->first).set_command(i->second);
177 cmdline_options.erase(i++);
184 BuilderCLI::~BuilderCLI()
189 int BuilderCLI::main()
191 FS::Path main_file = cwd/build_file;
192 if(FS::exists(main_file))
193 builder.load_build_file(main_file, &cmdline_options, conf_all);
196 IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
202 IO::print("Builder 1.0\n"
203 "Copyright © 2006-2013 Mikkosoft Productions, Mikko Rasa\n"
204 "Licensed under the GPL\n\n"
216 const Architecture &native_arch = builder.get_native_arch();
217 const Architecture ¤t_arch = builder.get_current_arch();
218 logger.log("environment", format("Building on %s, for %s%s", native_arch.get_name(),
219 current_arch.get_name(), (current_arch.is_native() ? " (native)" : "")));
220 logger.log("environment", format("Prefix is %s", builder.get_prefix()));
221 const FS::Path &tempdir = builder.get_temp_directory();
222 if(tempdir.is_absolute())
223 logger.log("environment", format("Temporary directory is %s", tempdir));
225 logger.log("environment", format("Using per-package temporary directory %s", tempdir));
226 const BuildType &build_type = builder.get_build_type();
227 logger.log("environment", format("Build type is %s", build_type.get_name()));
229 BuildGraph &build_graph = builder.get_build_graph();
230 PackageManager &package_manager = builder.get_package_manager();
231 const PackageManager::PackageMap &packages = package_manager.get_packages();
232 list<string> package_details;
233 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
235 if(!i->second->is_prepared())
238 string line = i->second->get_name();
239 if(dynamic_cast<SourcePackage *>(i->second))
244 unsigned to_be_built = 0;
245 const BuildGraph::TargetMap &targets = build_graph.get_targets();
246 for(BuildGraph::TargetMap::const_iterator j=targets.begin(); j!=targets.end(); ++j)
247 if(j->second->get_package()==i->second)
250 if(j->second->needs_rebuild())
255 line += format(" (%d targets", count);
257 line += format(", %d to be built", to_be_built);
262 package_details.push_back(line);
265 logger.log("summary", format("%d active packages, %d targets", package_details.size(), build_graph.get_targets().size()));
266 for(list<string>::const_iterator i=package_details.begin(); i!=package_details.end(); ++i)
267 logger.log("packages", *i);
272 if(build_graph.get_goals().is_broken())
274 list<string> problems = builder.collect_problems();
275 IO::print(IO::cerr, "The following problems were detected:\n");
276 for(list<string>::const_iterator i=problems.begin(); i!=problems.end(); ++i)
277 IO::print(IO::cerr, " %s\n", *i);
279 IO::print(IO::cerr, "Please fix them and try again.\n");
284 exit_code = builder.clean(clean>=2, dry_run);
286 exit_code = builder.build(jobs, dry_run, show_progress);
291 bool BuilderCLI::prepare_build()
293 /* XXX This is ugly; using the Builder class should not be this convoluted.
294 But the command line targets and what ifs need to be set at certain points
295 during preparation. */
296 BuildGraph &build_graph = builder.get_build_graph();
297 PackageManager &package_manager = builder.get_package_manager();
299 package_manager.get_main_package().prepare();
301 // Add targets from command line as goals
302 for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
304 Target *tgt = resolve_target(*i);
307 IO::print("I don't know anything about %s\n", *i);
311 build_graph.add_goal(*tgt);
314 build_graph.prepare();
317 for(NameList::iterator i=what_if.begin(); i!=what_if.end(); ++i)
319 FileTarget *tgt = dynamic_cast<FileTarget *>(resolve_target(*i));
322 IO::print(IO::cerr, "Unknown what-if target %s\n", *i);
329 build_graph.force_full_rebuild();
332 package_manager.save_all_caches();
337 Target *BuilderCLI::resolve_target(const string &name)
339 Target *tgt = builder.get_build_graph().get_target(name);
341 tgt = builder.get_vfs().get_target(cwd/name);
345 void BuilderCLI::package_help()
347 PackageManager &package_manager = builder.get_package_manager();
348 if(package_manager.get_packages().empty())
351 SourcePackage &main_pkg = dynamic_cast<SourcePackage &>(package_manager.get_main_package());
352 const Config &config = main_pkg.get_config();
353 const Config::OptionMap &options = config.get_options();
355 IO::print("\nRequired packages:\n ");
356 const Package::Requirements &requires = main_pkg.get_required_packages();
357 for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
359 if(i!=requires.begin())
361 IO::print((*i)->get_name());
367 IO::print("\nPackage configuration:\n");
368 for(Config::OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
370 const Config::Option &opt = i->second;
371 string line = format(" %s: %s (%s)", opt.name, opt.description, opt.value);
372 if(!opt.choices.empty())
375 for(list<string>::const_iterator j=opt.choices.begin(); j!=opt.choices.end(); ++j)
377 if(j!=opt.choices.begin())
383 else if(opt.value!=opt.default_value)
384 line += format(" [%s]", opt.default_value);
385 IO::print("%s\n", line);