Java Code Examples for com.google.devtools.common.options.OptionsParser#parse()

The following examples show how to use com.google.devtools.common.options.OptionsParser#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigurationTestCase.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Variation of {@link #createCollection(String...)} that also supports Starlark-defined options.
 *
 * @param starlarkOptions map of Starlark-defined options where the keys are option names (in the
 *     form of label-like strings) and the values are option values
 * @param args native option name/pair descriptions in command line form (e.g. "--cpu=k8")
 */
protected BuildConfigurationCollection createCollection(
    ImmutableMap<String, Object> starlarkOptions, String... args) throws Exception {
  OptionsParser parser =
      OptionsParser.builder()
          .optionsClasses(
              ImmutableList.<Class<? extends OptionsBase>>builder()
                  .addAll(buildOptionClasses)
                  .add(TestOptions.class)
                  .build())
          .build();
  parser.setStarlarkOptions(starlarkOptions);
  parser.parse(TestConstants.PRODUCT_SPECIFIC_FLAGS);
  parser.parse(args);

  ImmutableSortedSet<String> multiCpu = ImmutableSortedSet.copyOf(
      parser.getOptions(TestOptions.class).multiCpus);

  skyframeExecutor.handleDiffsForTesting(reporter);
  BuildConfigurationCollection collection = skyframeExecutor.createConfigurations(
      reporter, BuildOptions.of(buildOptionClasses, parser), multiCpu, false);
  return collection;
}
 
Example 2
Source File: AarGeneratorActionTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test public void testCheckFlags_MissingClasses() throws IOException, OptionsParsingException {
  Path manifest = tempDir.resolve("AndroidManifest.xml");
  Files.createFile(manifest);
  Path rtxt = tempDir.resolve("R.txt");
  Files.createFile(rtxt);

  String[] args = new String[] {"--manifest", manifest.toString(), "--rtxt", rtxt.toString()};
  OptionsParser optionsParser =
      OptionsParser.builder().optionsClasses(AarGeneratorOptions.class).build();
  optionsParser.parse(args);
  AarGeneratorOptions options = optionsParser.getOptions(AarGeneratorOptions.class);
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("classes must be specified. Building an .aar without"
        + " classes is unsupported.");
  AarGeneratorAction.checkFlags(options);
}
 
Example 3
Source File: BuildOptionsTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void parsingResultMatchEmptyNativeMatchWithStarlark() throws Exception {
  BuildOptions original =
      BuildOptions.builder()
          .addStarlarkOption(Label.parseAbsoluteUnchecked("//custom:flag"), "hello")
          .build();

  ImmutableList<Class<? extends FragmentOptions>> fragmentClasses =
      ImmutableList.<Class<? extends FragmentOptions>>builder()
          .add(CoreOptions.class)
          .add(CppOptions.class)
          .build();

  OptionsParser parser = OptionsParser.builder().optionsClasses(fragmentClasses).build();
  parser.parse("--cxxopt=bar");
  parser.setStarlarkOptions(ImmutableMap.of("//custom:flag", "hello"));

  assertThat(original.matches(parser)).isTrue();
}
 
Example 4
Source File: ExampleWorkerMultiplexer.java    From bazel with Apache License 2.0 6 votes vote down vote up
private static OptionsParser parserHelper(List<String> args) throws Exception {
  ImmutableList.Builder<String> expandedArgs = ImmutableList.builder();
  for (String arg : args) {
    Matcher flagFileMatcher = FLAG_FILE_PATTERN.matcher(arg);
    if (flagFileMatcher.matches()) {
      expandedArgs.addAll(Files.readAllLines(Paths.get(flagFileMatcher.group(1)), UTF_8));
    } else {
      expandedArgs.add(arg);
    }
  }

  OptionsParser parser =
      OptionsParser.builder()
          .optionsClasses(ExampleWorkMultiplexerOptions.class)
          .allowResidue(true)
          .build();
  parser.parse(expandedArgs.build());

  return parser;
}
 
Example 5
Source File: BuildOptionsTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void parsingResultMatchNullOption() throws Exception {
  BuildOptions original = BuildOptions.of(BUILD_CONFIG_OPTIONS);

  OptionsParser parser = OptionsParser.builder().optionsClasses(BUILD_CONFIG_OPTIONS).build();
  parser.parse("--platform_suffix=foo"); // Note: platform_suffix is null by default.

  assertThat(original.matches(parser)).isFalse();
}
 
Example 6
Source File: CommandLineEventTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsingOfCompiledToolCommandLine() throws OptionsParsingException {
  OptionsParser parser =
      OptionsParser.builder().optionsClasses(CommonCommandOptions.class).build();
  CommandLine original =
      CommandLine.newBuilder()
          .setCommandLineLabel("something meaningful")
          .addSections(
              CommandLineSection.newBuilder()
                  .setSectionLabel("command")
                  .setChunkList(ChunkList.newBuilder().addChunk("aCommand")))
          .addSections(
              CommandLineSection.newBuilder()
                  .setSectionLabel("someArguments")
                  .setChunkList(ChunkList.newBuilder().addChunk("arg1").addChunk("arg2")))
          .addSections(
              CommandLineSection.newBuilder()
                  .setSectionLabel("someOptions")
                  .setOptionList(OptionList.getDefaultInstance()))
          .build();
  parser.parse(
      "--experimental_tool_command_line=" + BaseEncoding.base64().encode(original.toByteArray()));

  ToolCommandLineEvent event = parser.getOptions(CommonCommandOptions.class).toolCommandLine;
  StructuredCommandLineId id = event.getEventId().getStructuredCommandLine();
  CommandLine line = event.asStreamProto(null).getStructuredCommandLine();

  assertThat(id.getCommandLineLabel()).isEqualTo("tool");
  assertThat(line.getCommandLineLabel()).isEqualTo("something meaningful");
  assertThat(line.getSectionsCount()).isEqualTo(3);
  assertThat(line.getSections(0).getSectionTypeCase()).isEqualTo(SectionTypeCase.CHUNK_LIST);
  assertThat(line.getSections(0).getChunkList().getChunkCount()).isEqualTo(1);
  assertThat(line.getSections(0).getChunkList().getChunk(0)).isEqualTo("aCommand");
  assertThat(line.getSections(1).getSectionTypeCase()).isEqualTo(SectionTypeCase.CHUNK_LIST);
  assertThat(line.getSections(1).getChunkList().getChunkCount()).isEqualTo(2);
  assertThat(line.getSections(1).getChunkList().getChunk(0)).isEqualTo("arg1");
  assertThat(line.getSections(1).getChunkList().getChunk(1)).isEqualTo("arg2");
  assertThat(line.getSections(2).getSectionTypeCase()).isEqualTo(SectionTypeCase.OPTION_LIST);
  assertThat(line.getSections(2).getOptionList().getOptionCount()).isEqualTo(0);
}
 
Example 7
Source File: BuildOptionsTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void parsingResultMatchEmptyNativeMatch() throws Exception {
  BuildOptions original = BuildOptions.of(BUILD_CONFIG_OPTIONS, "--cpu=foo");

  ImmutableList<Class<? extends FragmentOptions>> fragmentClasses =
      ImmutableList.<Class<? extends FragmentOptions>>builder()
          .add(CoreOptions.class)
          .add(CppOptions.class)
          .build();

  OptionsParser parser = OptionsParser.builder().optionsClasses(fragmentClasses).build();
  parser.parse("--cxxopt=bar");

  assertThat(original.matches(parser)).isFalse();
}
 
Example 8
Source File: PackageLoadingTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
private OptionsParser parse(String... options) throws Exception {
  OptionsParser parser =
      OptionsParser.builder()
          .optionsClasses(PackageOptions.class, StarlarkSemanticsOptions.class)
          .build();
  parser.parse("--default_visibility=public");
  parser.parse(options);

  return parser;
}
 
Example 9
Source File: PackageFactoryApparatus.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Parses and evaluates {@code buildFile} with custom {@code eventHandler} and returns the
 * resulting {@link Package} instance.
 */
public Package createPackage(
    PackageIdentifier packageIdentifier,
    RootedPath buildFile,
    ExtendedEventHandler reporter,
    String starlarkOption)
    throws Exception {

  OptionsParser parser =
      OptionsParser.builder().optionsClasses(StarlarkSemanticsOptions.class).build();
  parser.parse(
      starlarkOption == null
          ? ImmutableList.<String>of()
          : ImmutableList.<String>of(starlarkOption));
  StarlarkSemantics semantics =
      parser.getOptions(StarlarkSemanticsOptions.class).toStarlarkSemantics();

  try {
    Package externalPkg =
        factory
            .newExternalPackageBuilder(
                RootedPath.toRootedPath(
                    buildFile.getRoot(),
                    buildFile
                        .getRootRelativePath()
                        .getRelative(LabelConstants.WORKSPACE_FILE_NAME)),
                "TESTING",
                semantics)
            .build();
    Package pkg =
        factory.createPackageForTesting(
            packageIdentifier, externalPkg, buildFile, getPackageLocator(), reporter, semantics);
    return pkg;
  } catch (InterruptedException e) {
    throw new IllegalStateException(e);
  }
}
 
Example 10
Source File: AllIncompatibleChangesExpansionTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void allChangesSelected() throws OptionsParsingException {
  OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleOptions.class).build();
  parser.parse("--all");
  ExampleOptions opts = parser.getOptions(ExampleOptions.class);
  assertThat(opts.x).isFalse();
  assertThat(opts.y).isTrue();
  assertThat(opts.incompatibleA).isTrue();
  assertThat(opts.incompatibleB).isTrue();
  assertThat(opts.incompatibleC).isTrue();
}
 
Example 11
Source File: AllIncompatibleChangesExpansionTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void noChangesSelected() throws OptionsParsingException {
  OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleOptions.class).build();
  parser.parse("");
  ExampleOptions opts = parser.getOptions(ExampleOptions.class);
  assertThat(opts.x).isFalse();
  assertThat(opts.y).isTrue();
  assertThat(opts.incompatibleA).isFalse();
  assertThat(opts.incompatibleB).isFalse();
  assertThat(opts.incompatibleC).isTrue();
}
 
Example 12
Source File: CommandLineEventTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void testActiveBazelrcs_OriginalCommandLine() throws OptionsParsingException {
  OptionsParser fakeStartupOptions =
      OptionsParser.builder()
          .optionsClasses(BlazeServerStartupOptions.class, Options.class)
          .build();
  fakeStartupOptions.parse(
      "--bazelrc=/some/path", "--master_bazelrc", "--bazelrc", "/some/other/path");
  OptionsParser fakeCommandOptions =
      OptionsParser.builder().optionsClasses(TestOptions.class).build();

  CommandLine line =
      new OriginalCommandLineEvent(
              "testblaze",
              fakeStartupOptions,
              "someCommandName",
              fakeCommandOptions,
              Optional.empty())
          .asStreamProto(null)
          .getStructuredCommandLine();

  assertThat(line).isNotNull();
  assertThat(line.getCommandLineLabel()).isEqualTo("original");
  checkCommandLineSectionLabels(line);

  // Expect the provided rc-related startup options are correctly listed
  assertThat(line.getSections(0).getChunkList().getChunk(0)).isEqualTo("testblaze");
  assertThat(line.getSections(1).getOptionList().getOptionCount()).isEqualTo(3);
  assertThat(line.getSections(1).getOptionList().getOption(0).getCombinedForm())
      .isEqualTo("--bazelrc=/some/path");
  assertThat(line.getSections(1).getOptionList().getOption(1).getCombinedForm())
      .isEqualTo("--master_bazelrc");
  assertThat(line.getSections(1).getOptionList().getOption(2).getCombinedForm())
      .isEqualTo("--bazelrc /some/other/path");
  assertThat(line.getSections(2).getChunkList().getChunk(0)).isEqualTo("someCommandName");
  assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(0);
  assertThat(line.getSections(4).getChunkList().getChunkCount()).isEqualTo(0);
}
 
Example 13
Source File: BuildViewTestCase.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static StarlarkSemanticsOptions parseStarlarkSemanticsOptions(String... options)
    throws Exception {
  OptionsParser parser =
      OptionsParser.builder().optionsClasses(StarlarkSemanticsOptions.class).build();
  parser.parse(options);
  return parser.getOptions(StarlarkSemanticsOptions.class);
}
 
Example 14
Source File: CommandLineEventTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void testOptionWithImplicitRequirement_OriginalCommandLine()
    throws OptionsParsingException {
  OptionsParser fakeStartupOptions =
      OptionsParser.builder().optionsClasses(BlazeServerStartupOptions.class).build();
  OptionsParser fakeCommandOptions =
      OptionsParser.builder().optionsClasses(TestOptions.class).build();
  fakeCommandOptions.parse(
      PriorityCategory.COMMAND_LINE,
      "command line",
      ImmutableList.of("--test_implicit_requirement=foo"));

  CommandLine line =
      new OriginalCommandLineEvent(
              "testblaze",
              fakeStartupOptions,
              "someCommandName",
              fakeCommandOptions,
              Optional.of(ImmutableList.of()))
          .asStreamProto(null)
          .getStructuredCommandLine();

  assertThat(line).isNotNull();
  assertThat(line.getCommandLineLabel()).isEqualTo("original");
  checkCommandLineSectionLabels(line);

  assertThat(line.getSections(0).getChunkList().getChunk(0)).isEqualTo("testblaze");
  assertThat(line.getSections(1).getOptionList().getOptionCount()).isEqualTo(0);
  assertThat(line.getSections(2).getChunkList().getChunk(0)).isEqualTo("someCommandName");
  assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(1);
  assertThat(line.getSections(3).getOptionList().getOption(0).getCombinedForm())
      .isEqualTo("--test_implicit_requirement=foo");
  assertThat(line.getSections(4).getChunkList().getChunkCount()).isEqualTo(0);
}
 
Example 15
Source File: CqueryCommand.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void editOptions(OptionsParser optionsParser) {
  CqueryOptions cqueryOptions = optionsParser.getOptions(CqueryOptions.class);
  try {
    if (!cqueryOptions.transitions.equals(CqueryOptions.Transitions.NONE)) {
      optionsParser.parse(
          PriorityCategory.COMPUTED_DEFAULT,
          "Option required by setting the --transitions flag",
          ImmutableList.of("--output=transitions"));
    }
    optionsParser.parse(
        PriorityCategory.COMPUTED_DEFAULT,
        "Options required by cquery",
        ImmutableList.of("--nobuild"));
    optionsParser.parse(
        PriorityCategory.COMPUTED_DEFAULT,
        "cquery should include 'tags = [\"manual\"]' targets by default",
        ImmutableList.of("--build_manual_tests"));
    optionsParser.parse(
        PriorityCategory.COMPUTED_DEFAULT,
        // https://github.com/bazelbuild/bazel/issues/11078
        "cquery should not exclude test_suite rules",
        ImmutableList.of("--noexpand_test_suites"));
    if (cqueryOptions.showRequiredConfigFragments != IncludeConfigFragmentsEnum.OFF) {
      optionsParser.parse(
          PriorityCategory.COMPUTED_DEFAULT,
          "Options required by cquery's --show_config_fragments flag",
          ImmutableList.of(
              "--include_config_fragments_provider="
                  + cqueryOptions.showRequiredConfigFragments));
    }
  } catch (OptionsParsingException e) {
    throw new IllegalStateException("Cquery's known options failed to parse", e);
  }
}
 
Example 16
Source File: TestCommand.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void editOptions(OptionsParser optionsParser) {
  TestOutputFormat testOutput = optionsParser.getOptions(ExecutionOptions.class).testOutput;
  try {
    if (testOutput == ExecutionOptions.TestOutputFormat.STREAMED) {
      optionsParser.parse(
          PriorityCategory.SOFTWARE_REQUIREMENT,
          "streamed output requires locally run tests, without sharding",
          ImmutableList.of("--test_sharding_strategy=disabled", "--test_strategy=exclusive"));
    }
  } catch (OptionsParsingException e) {
    throw new IllegalStateException("Known options failed to parse", e);
  }
}
 
Example 17
Source File: AqueryCommand.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public void editOptions(OptionsParser optionsParser) {
  try {
    optionsParser.parse(
        PriorityCategory.COMPUTED_DEFAULT,
        "Option required by aquery",
        ImmutableList.of("--nobuild"));
  } catch (OptionsParsingException e) {
    throw new IllegalStateException("Aquery's known options failed to parse", e);
  }
}
 
Example 18
Source File: PlatformMappingValue.java    From bazel with Apache License 2.0 5 votes vote down vote up
private OptionsParsingResult parse(Iterable<String> args, BuildOptions defaultBuildOptions)
    throws OptionsParsingException {
  OptionsParser parser =
      OptionsParser.builder()
          .optionsClasses(defaultBuildOptions.getFragmentClasses())
          // We need the ability to re-map internal options in the mappings file.
          .ignoreInternalOptions(false)
          .build();
  parser.parse(ImmutableList.copyOf(args));
  // TODO(schmitt): Parse starlark options as well.
  return parser;
}
 
Example 19
Source File: ValidateAndLinkResourcesAction.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  OptionsParser optionsParser =
      OptionsParser.builder()
          .optionsClasses(
              Options.class, Aapt2ConfigOptions.class, ResourceProcessorCommonOptions.class)
          .argsPreProcessor(new ShellQuotedParamsFilePreProcessor(FileSystems.getDefault()))
          .build();
  optionsParser.parse(args);

  Options options = optionsParser.getOptions(Options.class);
  final Aapt2ConfigOptions aapt2Options = optionsParser.getOptions(Aapt2ConfigOptions.class);
  final Profiler profiler = LoggingProfiler.createAndStart("manifest");

  try (ScopedTemporaryDirectory scopedTmp =
          new ScopedTemporaryDirectory("android_resources_tmp");
      ExecutorServiceCloser executorService = ExecutorServiceCloser.createWithFixedPoolOf(15)) {
    CompiledResources resources =
        // TODO(b/64570523): Remove when the flags are standardized.
        Optional.ofNullable(options.resources)
            .orElseGet(
                () ->
                    CompiledResources.from(
                        Preconditions.checkNotNull(options.compiled),
                        Preconditions.checkNotNull(options.manifest)))
            // We need to make the manifest aapt safe (w.r.t., placeholders). For now, just stub
            // it out.
            .processManifest(
                manifest ->
                    AndroidManifest.parseFrom(manifest)
                        .writeDummyManifestForAapt(
                            scopedTmp.getPath().resolve("manifest-aapt-dummy"),
                            options.packageForR));
    List<CompiledResources> deps =
        options.compiledDeps.stream()
            .map(CompiledResources::from)
            .collect(ImmutableList.toImmutableList());
    profiler.recordEndOf("manifest").startTask("validate");

    // TODO(b/146663858): distinguish direct/transitive deps for "strict deps".
    // TODO(b/128711690): validate AndroidManifest.xml
    checkVisibilityOfResourceReferences(
        /*androidManifest=*/ XmlNode.getDefaultInstance(), resources, deps);

    profiler.recordEndOf("validate").startTask("link");
    ResourceLinker.create(aapt2Options.aapt2, executorService, scopedTmp.getPath())
        .profileUsing(profiler)
        // NB: these names are really confusing.
        //   .dependencies is meant for linking in android.jar
        //   .include is meant for regular dependencies
        .dependencies(Optional.ofNullable(options.deprecatedLibraries).orElse(options.libraries))
        .include(deps)
        .buildVersion(aapt2Options.buildToolsVersion)
        .outputAsProto(aapt2Options.resourceTableAsProto)
        .linkStatically(resources)
        .copyLibraryTo(options.staticLibraryOut)
        .copySourceJarTo(options.sourceJarOut)
        .copyRTxtTo(options.rTxtOut);
    profiler.recordEndOf("link");
  }
}
 
Example 20
Source File: BuildViewTestCase.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static PackageOptions parsePackageOptions(String... options) throws Exception {
  OptionsParser parser = OptionsParser.builder().optionsClasses(PackageOptions.class).build();
  parser.parse("--default_visibility=public");
  parser.parse(options);
  return parser.getOptions(PackageOptions.class);
}