Search in sources :

Example 1 with OptionsBase

use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.

the class BlazeRuntime method parseOptions.

/**
   * Parses the command line arguments into a {@link OptionsParser} object.
   *
   *  <p>This function needs to parse the --option_sources option manually so that the real option
   * parser can set the source for every option correctly. If that cannot be parsed or is missing,
   * we just report an unknown source for every startup option.
   */
private static OptionsProvider parseOptions(Iterable<BlazeModule> modules, List<String> args) throws OptionsParsingException {
    ImmutableList<Class<? extends OptionsBase>> optionClasses = BlazeCommandUtils.getStartupOptions(modules);
    // First parse the command line so that we get the option_sources argument
    OptionsParser parser = OptionsParser.newOptionsParser(optionClasses);
    parser.setAllowResidue(false);
    parser.parse(OptionPriority.COMMAND_LINE, null, args);
    Function<? super String, String> sourceFunction = sourceFunctionForMap(parser.getOptions(BlazeServerStartupOptions.class).optionSources);
    // Then parse the command line again, this time with the correct option sources
    parser = OptionsParser.newOptionsParser(optionClasses);
    parser.setAllowResidue(false);
    parser.parseWithSourceFunction(OptionPriority.COMMAND_LINE, sourceFunction, args);
    return parser;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) OptionsParser(com.google.devtools.common.options.OptionsParser)

Example 2 with OptionsBase

use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.

the class CanonicalizeCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    Options canonicalizeOptions = options.getOptions(Options.class);
    String commandName = canonicalizeOptions.forCommand;
    BlazeCommand command = runtime.getCommandMap().get(commandName);
    if (command == null) {
        env.getReporter().handle(Event.error("Not a valid command: '" + commandName + "' (should be one of " + Joiner.on(", ").join(runtime.getCommandMap().keySet()) + ")"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    Collection<Class<? extends OptionsBase>> optionsClasses = BlazeCommandUtils.getOptions(command.getClass(), runtime.getBlazeModules(), runtime.getRuleClassProvider());
    try {
        OptionsParser parser = OptionsParser.newOptionsParser(optionsClasses);
        parser.setAllowResidue(false);
        parser.parse(options.getResidue());
        InvocationPolicyEnforcer invocationPolicyEnforcer = InvocationPolicyEnforcer.create(canonicalizeOptions.invocationPolicy);
        invocationPolicyEnforcer.enforce(parser, commandName);
        List<String> result = parser.canonicalize();
        for (String piece : result) {
            env.getReporter().getOutErr().printOutLn(piece);
        }
    } catch (OptionsParsingException e) {
        env.getReporter().handle(Event.error(e.getMessage()));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    return ExitCode.SUCCESS;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 3 with OptionsBase

use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.

the class DumpCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    DumpOptions dumpOptions = options.getOptions(DumpOptions.class);
    boolean anyOutput = dumpOptions.dumpPackages || dumpOptions.dumpVfs || dumpOptions.dumpActionCache || dumpOptions.dumpRuleClasses || (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF);
    if (!anyOutput) {
        Map<String, String> categories = new HashMap<>();
        categories.put("verbosity", "Options that control what internal state is dumped");
        Collection<Class<? extends OptionsBase>> optionList = new ArrayList<>();
        optionList.add(DumpOptions.class);
        env.getReporter().getOutErr().printErrLn(BlazeCommandUtils.expandHelpTopic(getClass().getAnnotation(Command.class).name(), getClass().getAnnotation(Command.class).help(), getClass(), optionList, categories, OptionsParser.HelpVerbosity.LONG, runtime.getProductName()));
        return ExitCode.ANALYSIS_FAILURE;
    }
    PrintStream out = new PrintStream(env.getReporter().getOutErr().getOutputStream());
    try {
        out.println("Warning: this information is intended for consumption by developers");
        out.println("only, and may change at any time.  Script against it at your own risk!");
        out.println();
        boolean success = true;
        if (dumpOptions.dumpPackages) {
            env.getPackageManager().dump(out);
            out.println();
        }
        if (dumpOptions.dumpVfs) {
            out.println("Filesystem cache");
            FileSystemUtils.dump(env.getOutputBase().getFileSystem(), out);
            out.println();
        }
        if (dumpOptions.dumpActionCache) {
            success &= dumpActionCache(env, out);
            out.println();
        }
        if (dumpOptions.dumpRuleClasses) {
            dumpRuleClasses(runtime, out);
            out.println();
        }
        if (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF) {
            success &= dumpSkyframe(env.getSkyframeExecutor(), dumpOptions.dumpSkyframe == SkyframeDumpOption.SUMMARY, out);
            out.println();
        }
        return success ? ExitCode.SUCCESS : ExitCode.ANALYSIS_FAILURE;
    } finally {
        out.flush();
    }
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) Command(com.google.devtools.build.lib.runtime.Command) ArrayList(java.util.ArrayList) RuleClass(com.google.devtools.build.lib.packages.RuleClass) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 4 with OptionsBase

use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.

the class ConfigSetting method matchesConfig.

/**
   * Given a list of [flagName, flagValue] pairs, returns true if flagName == flagValue for
   * every item in the list under this configuration, false otherwise.
   */
private boolean matchesConfig(Map<String, String> expectedSettings, BuildConfiguration config) throws OptionsParsingException {
    // Rather than returning fast when we find a mismatch, continue looking at the other flags
    // to check that they're indeed valid flag specifications.
    boolean foundMismatch = false;
    // Since OptionsParser instantiation involves reflection, let's try to minimize that happening.
    Map<Class<? extends OptionsBase>, OptionsParser> parserCache = new HashMap<>();
    for (Map.Entry<String, String> setting : expectedSettings.entrySet()) {
        String optionName = setting.getKey();
        String expectedRawValue = setting.getValue();
        Class<? extends OptionsBase> optionClass = config.getOptionClass(optionName);
        if (optionClass == null) {
            throw new OptionsParsingException("unknown option: '" + optionName + "'");
        }
        OptionsParser parser = parserCache.get(optionClass);
        if (parser == null) {
            parser = OptionsParser.newOptionsParser(optionClass);
            parserCache.put(optionClass, parser);
        }
        parser.parse("--" + optionName + "=" + expectedRawValue);
        Object expectedParsedValue = parser.getOptions(optionClass).asMap().get(optionName);
        if (!optionMatches(config, optionName, expectedParsedValue)) {
            foundMismatch = true;
        }
    }
    return !foundMismatch;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) HashMap(java.util.HashMap) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with OptionsBase

use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.

the class HelpCommand method emitCompletionHelp.

private void emitCompletionHelp(BlazeRuntime runtime, OutErr outErr) {
    // First startup_options
    Iterable<BlazeModule> blazeModules = runtime.getBlazeModules();
    ConfiguredRuleClassProvider ruleClassProvider = runtime.getRuleClassProvider();
    Map<String, BlazeCommand> commandsByName = getSortedCommands(runtime);
    outErr.printOutLn("BAZEL_COMMAND_LIST=\"" + SPACE_JOINER.join(commandsByName.keySet()) + "\"");
    outErr.printOutLn("BAZEL_INFO_KEYS=\"");
    for (String name : InfoCommand.getHardwiredInfoItemNames(runtime.getProductName())) {
        outErr.printOutLn(name);
    }
    outErr.printOutLn("\"");
    outErr.printOutLn("BAZEL_STARTUP_OPTIONS=\"");
    Iterable<Class<? extends OptionsBase>> options = BlazeCommandUtils.getStartupOptions(blazeModules);
    outErr.printOut(OptionsParser.newOptionsParser(options).getOptionsCompletion());
    outErr.printOutLn("\"");
    for (Map.Entry<String, BlazeCommand> e : commandsByName.entrySet()) {
        BlazeCommand command = e.getValue();
        String varName = e.getKey().toUpperCase(Locale.US).replace('-', '_');
        Command annotation = command.getClass().getAnnotation(Command.class);
        if (!annotation.completion().isEmpty()) {
            outErr.printOutLn("BAZEL_COMMAND_" + varName + "_ARGUMENT=\"" + annotation.completion() + "\"");
        }
        options = BlazeCommandUtils.getOptions(command.getClass(), blazeModules, ruleClassProvider);
        outErr.printOutLn("BAZEL_COMMAND_" + varName + "_FLAGS=\"");
        outErr.printOut(OptionsParser.newOptionsParser(options).getOptionsCompletion());
        outErr.printOutLn("\"");
    }
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) ConfiguredRuleClassProvider(com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) Command(com.google.devtools.build.lib.runtime.Command) RuleClass(com.google.devtools.build.lib.packages.RuleClass) BlazeModule(com.google.devtools.build.lib.runtime.BlazeModule) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

OptionsBase (com.google.devtools.common.options.OptionsBase)7 OptionsParser (com.google.devtools.common.options.OptionsParser)5 BlazeCommand (com.google.devtools.build.lib.runtime.BlazeCommand)3 InvocationPolicyEnforcer (com.google.devtools.build.lib.flags.InvocationPolicyEnforcer)2 RuleClass (com.google.devtools.build.lib.packages.RuleClass)2 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)2 Command (com.google.devtools.build.lib.runtime.Command)2 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 ConfiguredRuleClassProvider (com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider)1 BuildConfigurationCollection (com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection)1 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)1 BuildEventStreamOptions (com.google.devtools.build.lib.buildeventstream.transports.BuildEventStreamOptions)1 BlazeModule (com.google.devtools.build.lib.runtime.BlazeModule)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1