Search in sources :

Example 21 with OptionsParser

use of com.google.devtools.common.options.OptionsParser 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 22 with OptionsParser

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

the class BuildOptions method of.

/**
   * Creates a BuildOptions class by taking the option values from command-line arguments and
   * applying the specified original options.
   */
@VisibleForTesting
static BuildOptions of(List<Class<? extends FragmentOptions>> optionsList, BuildOptions originalOptions, String... args) throws OptionsParsingException {
    Builder builder = builder();
    OptionsParser parser = OptionsParser.newOptionsParser(ImmutableList.<Class<? extends OptionsBase>>copyOf(optionsList));
    parser.parse(args);
    for (Class<? extends FragmentOptions> optionsClass : optionsList) {
        builder.add(parser.getOptions(optionsClass));
    }
    builder.setOriginalOptions(originalOptions);
    return builder.build();
}
Also used : OptionsParser(com.google.devtools.common.options.OptionsParser) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 23 with OptionsParser

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

the class XcodeGen method main.

public static void main(String[] args) throws IOException, OptionsParsingException {
    OptionsParser parser = OptionsParser.newOptionsParser(XcodeGenOptions.class);
    parser.parse(args);
    XcodeGenOptions options = parser.getOptions(XcodeGenOptions.class);
    if (options.control == null) {
        throw new IllegalArgumentException("--control must be specified\n" + Options.getUsage(XcodeGenOptions.class));
    }
    FileSystem fileSystem = FileSystems.getDefault();
    Control controlPb;
    try (InputStream in = Files.newInputStream(fileSystem.getPath(options.control))) {
        controlPb = Control.parseFrom(in);
    }
    Path pbxprojPath = fileSystem.getPath(controlPb.getPbxproj());
    Iterator<String> srcList = allSourceFilePaths(controlPb).iterator();
    Path workspaceRoot;
    // TODO(bazel-team): Remove this if-else clause once Bazel passes in the workspace root.
    if (controlPb.hasWorkspaceRoot()) {
        workspaceRoot = fileSystem.getPath(controlPb.getWorkspaceRoot());
    } else if (!srcList.hasNext()) {
        workspaceRoot = XcodeprojGeneration.relativeWorkspaceRoot(pbxprojPath);
    } else {
        // Get the absolute path to the workspace root.
        // TODO(bazel-team): Remove this hack, possibly by converting Xcodegen to be run with
        // "bazel run" and using RUNFILES to get the workspace root. For now, this is needed to work
        // around Xcode's handling of symlinks not playing nicely with how Bazel stores output
        // artifacts in /private/var/tmp. This means a relative path from .xcodeproj in bazel-out to
        // the workspace root in .xcodeproj will not work properly at certain times during
        // Xcode/xcodebuild execution. Walking up the path of a known source file prevents having
        // to reason about a file that might only be accessible through a symlink, like a tools jar.
        Path relSourceFilePath = fileSystem.getPath(srcList.next());
        Path absSourceFilePath = relSourceFilePath.toAbsolutePath();
        workspaceRoot = absSourceFilePath;
        for (int i = 0; i < relSourceFilePath.getNameCount(); i++) {
            workspaceRoot = workspaceRoot.getParent();
        }
    }
    try (OutputStream out = Files.newOutputStream(pbxprojPath)) {
        // This workspace root here is relative to the PWD, so that the .xccurrentversion
        // files can actually be read. The other workspaceRoot is relative to the .xcodeproj
        // root or is absolute.
        Path relativeWorkspaceRoot = fileSystem.getPath(".");
        PBXProject project = XcodeprojGeneration.xcodeproj(workspaceRoot, controlPb, ImmutableList.of(new CurrentVersionSetter(relativeWorkspaceRoot), new PbxReferencesGrouper(fileSystem)));
        XcodeprojGeneration.write(out, project);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OptionsParser(com.google.devtools.common.options.OptionsParser) Control(com.google.devtools.build.xcode.xcodegen.proto.XcodeGenProtos.Control) TargetControl(com.google.devtools.build.xcode.xcodegen.proto.XcodeGenProtos.TargetControl) FileSystem(java.nio.file.FileSystem) PBXProject(com.facebook.buck.apple.xcode.xcodeproj.PBXProject)

Example 24 with OptionsParser

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

the class GenerateWorkspace method main.

public static void main(String[] args) throws InterruptedException {
    OptionsParser parser = OptionsParser.newOptionsParser(GenerateWorkspaceOptions.class);
    parser.parseAndExitUponError(args);
    GenerateWorkspaceOptions options = parser.getOptions(GenerateWorkspaceOptions.class);
    if (options.mavenProjects.isEmpty() && options.bazelProjects.isEmpty() && options.artifacts.isEmpty()) {
        printUsage(parser);
        return;
    }
    GenerateWorkspace workspaceFileGenerator = new GenerateWorkspace(options.outputDir);
    workspaceFileGenerator.generateFromWorkspace(options.bazelProjects);
    workspaceFileGenerator.generateFromPom(options.mavenProjects);
    workspaceFileGenerator.generateFromArtifacts(options.artifacts);
    if (!workspaceFileGenerator.hasErrors()) {
        workspaceFileGenerator.writeResults();
    }
    workspaceFileGenerator.cleanup();
    if (workspaceFileGenerator.hasErrors()) {
        System.exit(1);
    }
}
Also used : OptionsParser(com.google.devtools.common.options.OptionsParser)

Example 25 with OptionsParser

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

the class DexMapper method main.

/**
   * @param args the command line arguments
   */
public static void main(String[] args) {
    OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class);
    optionsParser.parseAndExitUponError(args);
    Options options = optionsParser.getOptions(Options.class);
    List<String> inputs = options.inputJars;
    List<String> outputs = options.outputJars;
    String filterFile = options.mainDexFilter;
    String resourceFile = options.outputResources;
    try {
        Predicate<String> inputFilter = Predicates.alwaysTrue();
        if (options.inclusionFilterJar != null) {
            inputFilter = SplitZipFilters.entriesIn(options.inclusionFilterJar);
        }
        new SplitZip().setVerbose(false).useDefaultEntryDate().setSplitDexedClasses(options.splitDexedClasses).addInputs(inputs).addOutputs(outputs).setInputFilter(inputFilter).setMainClassListFile(filterFile).setResourceFile(resourceFile).run().close();
    } catch (Exception ex) {
        System.err.println("Caught exception" + ex.getMessage());
        ex.printStackTrace(System.out);
        System.exit(1);
    }
}
Also used : OptionsParser(com.google.devtools.common.options.OptionsParser)

Aggregations

OptionsParser (com.google.devtools.common.options.OptionsParser)50 Path (java.nio.file.Path)11 IOException (java.io.IOException)10 Stopwatch (com.google.common.base.Stopwatch)8 Test (org.junit.Test)7 InvocationPolicyEnforcer (com.google.devtools.build.lib.flags.InvocationPolicyEnforcer)6 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)6 AaptConfigOptions (com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)5 OptionsBase (com.google.devtools.common.options.OptionsBase)5 MergingException (com.android.ide.common.res2.MergingException)4 Path (com.google.devtools.build.lib.vfs.Path)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 EventBus (com.google.common.eventbus.EventBus)3 FlagAaptOptions (com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 VariantType (com.android.builder.core.VariantType)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 DexingOptions (com.google.devtools.build.android.dexer.Dexing.DexingOptions)2 SpawnActionContext (com.google.devtools.build.lib.actions.SpawnActionContext)2