#include "buildercli.h"
#include "filetarget.h"
#include "sourcepackage.h"
+#include "tool.h"
+#include "toolchain.h"
using namespace std;
using namespace Msp;
getopt.add_option( "max-depth", max_depth, GetOpt::REQUIRED_ARG).set_help("Show up to NUM levels in analysis.", "NUM");
getopt.add_option( "prefix", prefix, GetOpt::REQUIRED_ARG).set_help("Install things to DIR.", "DIR");
getopt.add_option( "tempdir", tempdir, GetOpt::REQUIRED_ARG).set_help("Store temporary files in DIR.", "DIR");
- usagemsg = getopt.generate_usage(argv[0])+" [<target> ...]";
- helpmsg = getopt.generate_help();
+ getopt.add_argument("target", cmdline_targets, GetOpt::OPTIONAL_ARG).set_help("Target(s) to build");
getopt(argc, argv);
+ if(help)
+ {
+ helpmsg = "Usage:\n ";
+ helpmsg += getopt.generate_usage(argv[0], true);
+ helpmsg += "\n\n";
+ helpmsg += getopt.generate_help();
+ }
+
if(silent)
--verbose;
if(verbose>=1)
else if(!clean && !create_makefile)
build = true;
- const vector<string> &args = getopt.get_args();
- for(vector<string>::const_iterator i=args.begin(); i!=args.end(); ++i)
+ for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); )
{
string::size_type equal = i->find('=');
if(equal!=string::npos)
+ {
cmdline_options.insert(Config::InputOptions::value_type(i->substr(0, equal), i->substr(equal+1)));
+ cmdline_targets.erase(i++);
+ }
else
- cmdline_targets.push_back(*i);
+ ++i;
}
if(!work_dir.empty())
package_manager.append_package_path(cwd);
package_manager.append_package_path(cwd/"..");
- package_manager.append_binary_package_path(FS::get_sys_data_dir(argv[0], "builder"));
+ package_manager.append_binary_package_path(FS::get_sys_data_dir(argv[0], "builder")/"packages");
package_manager.set_no_externals(no_externals);
builder.set_build_type(build_type);
builder.add_default_tools();
+
+ const Toolchain &toolchain = builder.get_toolchain();
+ for(Config::InputOptions::iterator i=cmdline_options.begin(); i!=cmdline_options.end(); )
+ {
+ if(toolchain.has_tool(i->first))
+ {
+ toolchain.get_tool(i->first).set_command(i->second);
+ cmdline_options.erase(i++);
+ }
+ else
+ ++i;
+ }
}
BuilderCLI::~BuilderCLI()
int BuilderCLI::main()
{
FS::Path main_file = cwd/build_file;
- if(!FS::exists(main_file))
+ if(FS::exists(main_file))
+ builder.load_build_file(main_file, &cmdline_options, conf_all);
+ else if(!help)
{
- if(help)
- {
- usage(0, "builder", false);
- return 0;
- }
- else
- {
- IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
- return 1;
- }
+ IO::print(IO::cerr, "The file %s does not exist.\n", main_file);
+ return 1;
}
- builder.load_build_file(main_file, &cmdline_options, conf_all);
-
if(help)
{
- usage(0, "builder", false);
- IO::print("\n");
+ IO::print("Builder 1.0\n"
+ "Copyright © 2006-2013 Mikkosoft Productions, Mikko Rasa\n"
+ "Licensed under the GPL\n\n"
+ "%s", helpmsg);
package_help();
return 0;
}
if(analyzer)
analyzer->analyze();
- const Builder::ProblemList &problems = builder.get_problems();
- if(!problems.empty())
+ if(build_graph.get_goals().is_broken())
{
+ list<string> problems = builder.collect_problems();
IO::print(IO::cerr, "The following problems were detected:\n");
- for(Builder::ProblemList::const_iterator i=problems.begin(); i!=problems.end(); ++i)
- IO::print(IO::cerr, " %s: %s\n", i->package, i->descr);
+ for(list<string>::const_iterator i=problems.begin(); i!=problems.end(); ++i)
+ IO::print(IO::cerr, " %s\n", *i);
if(!analyzer)
IO::print(IO::cerr, "Please fix them and try again.\n");
return 1;
during preparation. */
BuildGraph &build_graph = builder.get_build_graph();
PackageManager &package_manager = builder.get_package_manager();
- VirtualFileSystem &vfs = builder.get_vfs();
package_manager.get_main_package().prepare();
// Add targets from command line as goals
for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
{
- Target *tgt = build_graph.get_target(*i);
- if(!tgt)
- tgt = vfs.get_target(*i);
- if(!tgt)
- tgt = vfs.get_target(cwd/ *i);
+ Target *tgt = resolve_target(*i);
if(!tgt)
{
IO::print("I don't know anything about %s\n", *i);
// Apply what-ifs
for(NameList::iterator i=what_if.begin(); i!=what_if.end(); ++i)
{
- FileTarget *tgt = vfs.get_target(cwd/ *i);
+ FileTarget *tgt = dynamic_cast<FileTarget *>(resolve_target(*i));
if(!tgt)
{
IO::print(IO::cerr, "Unknown what-if target %s\n", *i);
build_graph.force_full_rebuild();
if(!dry_run)
- {
- const PackageManager::PackageMap &packages = package_manager.get_packages();
- for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
- i->second->save_caches();
- }
+ package_manager.save_all_caches();
return true;
}
-void BuilderCLI::usage(const char *reason, const char *argv0, bool brief)
+Target *BuilderCLI::resolve_target(const string &name)
{
- if(reason)
- IO::print(IO::cerr, "%s\n", reason);
-
- if(brief)
- IO::print(IO::cerr, "Usage: %s\n", usagemsg);
- else
- {
- IO::print(IO::cerr, "Builder 1.0\n\n");
- IO::print(IO::cerr, "Usage: %s [options] [<target> ...]\n\n", argv0);
- IO::print(IO::cerr, "Options:\n");
- IO::print(IO::cerr, helpmsg);
- }
+ Target *tgt = builder.get_build_graph().get_target(name);
+ if(!tgt)
+ tgt = builder.get_vfs().get_target(cwd/name);
+ return tgt;
}
void BuilderCLI::package_help()
{
PackageManager &package_manager = builder.get_package_manager();
+ if(package_manager.get_packages().empty())
+ return;
+
SourcePackage &main_pkg = dynamic_cast<SourcePackage &>(package_manager.get_main_package());
const Config &config = main_pkg.get_config();
const Config::OptionMap &options = config.get_options();
- IO::print("Required packages:\n ");
+ IO::print("\nRequired packages:\n ");
const Package::Requirements &requires = main_pkg.get_required_packages();
for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
{
IO::print(", ");
IO::print((*i)->get_name());
}
- IO::print("\n\nPackage configuration:\n");
- for(Config::OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
+ IO::print("\n");
+
+ if(!options.empty())
{
- const Config::Option &opt = i->second;
- IO::print(" %s: %s (%s)", opt.name, opt.description, opt.value);
- if(opt.value!=opt.default_value)
- IO::print(" [%s]", opt.default_value);
- IO::print("\n");
+ IO::print("\nPackage configuration:\n");
+ for(Config::OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
+ {
+ const Config::Option &opt = i->second;
+ string line = format(" %s: %s (%s)", opt.name, opt.description, opt.value);
+ if(!opt.choices.empty())
+ {
+ line += " {";
+ for(list<string>::const_iterator j=opt.choices.begin(); j!=opt.choices.end(); ++j)
+ {
+ if(j!=opt.choices.begin())
+ line += ' ';
+ line += *j;
+ }
+ line += '}';
+ }
+ else if(opt.value!=opt.default_value)
+ line += format(" [%s]", opt.default_value);
+ IO::print("%s\n", line);
+ }
}
}
-
-string BuilderCLI::usagemsg;
-string BuilderCLI::helpmsg;