]> git.tdb.fi Git - builder.git/blobdiff - source/buildercli.cpp
Save caches before starting the build
[builder.git] / source / buildercli.cpp
index bcd439e854c6fc9afb05df7832c0e192c3f87845..0a84364c988e69bee2925f405573fcff63e66d74 100644 (file)
@@ -8,11 +8,14 @@
 #include "buildercli.h"
 #include "filetarget.h"
 #include "sourcepackage.h"
+#include "tool.h"
+#include "toolchain.h"
 
 using namespace std;
 using namespace Msp;
 
 BuilderCLI::BuilderCLI(int argc, char **argv):
+       RegisteredApplication<BuilderCLI>("builder"),
        analyzer(0),
        build(false),
        clean(0),
@@ -63,10 +66,17 @@ BuilderCLI::BuilderCLI(int argc, char **argv):
        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)
@@ -114,14 +124,16 @@ BuilderCLI::BuilderCLI(int argc, char **argv):
        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())
@@ -133,15 +145,15 @@ BuilderCLI::BuilderCLI(int argc, char **argv):
 
        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()/"packages");
 
        package_manager.set_no_externals(no_externals);
 
-       builder.set_architecture(arch);
+       builder.set_architecture(tolower(arch));
 
        list<FS::Path> start_files;
-       start_files.push_back(FS::get_sys_data_dir(argv[0], "builder")/"builderrc");
-       start_files.push_back(FS::get_user_data_dir("builder")/"rc");
+       start_files.push_back(FS::get_sys_data_dir()/"builderrc");
+       start_files.push_back(FS::get_user_data_dir()/"rc");
        for(list<FS::Path>::const_iterator i=start_files.begin(); i!=start_files.end(); ++i)
                if(FS::exists(*i))
                        builder.load_build_file(*i);
@@ -156,6 +168,18 @@ BuilderCLI::BuilderCLI(int argc, char **argv):
                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()
@@ -166,26 +190,24 @@ BuilderCLI::~BuilderCLI()
 int BuilderCLI::main()
 {
        FS::Path main_file = cwd/build_file;
-       if(!FS::exists(main_file))
+       if(FS::exists(main_file))
        {
-               if(help)
-               {
-                       usage(0, "builder", false);
-                       return 0;
-               }
-               else
-               {
-                       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(!dry_run && !cmdline_options.empty())
+                       builder.save_caches();
+       }
+       else if(!help)
+       {
+               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;
        }
@@ -252,12 +274,12 @@ int BuilderCLI::main()
        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;
@@ -268,6 +290,9 @@ int BuilderCLI::main()
        if(build)
                exit_code = builder.build(jobs, dry_run, show_progress);
 
+       if(!dry_run)
+               builder.save_caches();
+
        return exit_code;
 }
 
@@ -278,18 +303,13 @@ bool BuilderCLI::prepare_build()
        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);
@@ -304,7 +324,7 @@ bool BuilderCLI::prepare_build()
        // 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);
@@ -317,39 +337,30 @@ bool BuilderCLI::prepare_build()
                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)
        {
@@ -357,16 +368,29 @@ void BuilderCLI::package_help()
                        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;