Search in sources :

Example 36 with OptionsParser

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

the class InvocationPolicyEnforcerTest method createOptionsPolicyEnforcer.

private static InvocationPolicyEnforcer createOptionsPolicyEnforcer(InvocationPolicy.Builder invocationPolicyBuilder) throws Exception {
    InvocationPolicy policyProto = invocationPolicyBuilder.build();
    // An OptionsPolicyEnforcer could be constructed in the test directly from the InvocationPolicy
    // proto, however Blaze will actually take the policy as another flag with a Base64 encoded
    // binary proto and parse that, so exercise that code path in the test.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    policyProto.writeTo(out);
    String policyBase64 = BaseEncoding.base64().encode(out.toByteArray());
    OptionsParser startupOptionsParser = OptionsParser.newOptionsParser(BlazeServerStartupOptions.class);
    String policyOption = "--invocation_policy=" + policyBase64;
    startupOptionsParser.parse(policyOption);
    return InvocationPolicyEnforcer.create(startupOptionsParser.getOptions(BlazeServerStartupOptions.class).invocationPolicy);
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OptionsParser(com.google.devtools.common.options.OptionsParser)

Example 37 with OptionsParser

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

the class ConfigurationTestCase method createCollection.

protected BuildConfigurationCollection createCollection(String... args) throws Exception {
    OptionsParser parser = OptionsParser.newOptionsParser(ImmutableList.<Class<? extends OptionsBase>>builder().addAll(buildOptionClasses).add(TestOptions.class).build());
    parser.parse(args);
    InvocationPolicyEnforcer optionsPolicyEnforcer = analysisMock.getInvocationPolicyEnforcer();
    optionsPolicyEnforcer.enforce(parser);
    ImmutableSortedSet<String> multiCpu = ImmutableSortedSet.copyOf(parser.getOptions(TestOptions.class).multiCpus);
    BuildOptions buildOptions = BuildOptions.applyStaticConfigOverride(BuildOptions.of(buildOptionClasses, parser));
    skyframeExecutor.handleDiffs(reporter);
    BuildConfigurationCollection collection = skyframeExecutor.createConfigurations(reporter, configurationFactory, buildOptions, multiCpu, false);
    return collection;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParser(com.google.devtools.common.options.OptionsParser) BuildConfigurationCollection(com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection)

Example 38 with OptionsParser

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

the class PackageCacheTest method parsePackageCacheOptions.

private PackageCacheOptions parsePackageCacheOptions(String... options) throws Exception {
    OptionsParser parser = OptionsParser.newOptionsParser(PackageCacheOptions.class);
    parser.parse(new String[] { "--default_visibility=public" });
    parser.parse(options);
    InvocationPolicyEnforcer optionsPolicyEnforcer = analysisMock.getInvocationPolicyEnforcer();
    try {
        optionsPolicyEnforcer.enforce(parser);
    } catch (OptionsParsingException e) {
        throw new IllegalStateException(e);
    }
    return parser.getOptions(PackageCacheOptions.class);
}
Also used : InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser)

Example 39 with OptionsParser

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

the class ExampleWorker method processRequest.

private static void processRequest(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.newOptionsParser(ExampleWorkOptions.class);
    parser.setAllowResidue(true);
    parser.parse(expandedArgs.build());
    ExampleWorkOptions options = parser.getOptions(ExampleWorkOptions.class);
    List<String> outputs = new ArrayList<>();
    if (options.writeUUID) {
        outputs.add("UUID " + workerUuid.toString());
    }
    if (options.writeCounter) {
        outputs.add("COUNTER " + workUnitCounter++);
    }
    String residueStr = Joiner.on(' ').join(parser.getResidue());
    if (options.uppercase) {
        residueStr = residueStr.toUpperCase();
    }
    outputs.add(residueStr);
    if (options.printInputs) {
        for (Entry<String, String> input : inputs.entrySet()) {
            outputs.add("INPUT " + input.getKey() + " " + input.getValue());
        }
    }
    if (options.printEnv) {
        for (Entry<String, String> entry : System.getenv().entrySet()) {
            outputs.add(entry.getKey() + "=" + entry.getValue());
        }
    }
    String outputStr = Joiner.on('\n').join(outputs);
    if (options.outputFile.isEmpty()) {
        System.out.println(outputStr);
    } else {
        try (PrintStream outputFile = new PrintStream(options.outputFile)) {
            outputFile.println(outputStr);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) OptionsParser(com.google.devtools.common.options.OptionsParser) ExampleWorkOptions(com.google.devtools.build.lib.worker.ExampleWorkerOptions.ExampleWorkOptions)

Example 40 with OptionsParser

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

the class AndroidResourceMergingAction method main.

public static void main(String[] args) throws Exception {
    final Stopwatch timer = Stopwatch.createStarted();
    OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class, AaptConfigOptions.class);
    optionsParser.enableParamsFileSupport(FileSystems.getDefault());
    optionsParser.parseAndExitUponError(args);
    AaptConfigOptions aaptConfigOptions = optionsParser.getOptions(AaptConfigOptions.class);
    Options options = optionsParser.getOptions(Options.class);
    Preconditions.checkNotNull(options.primaryData);
    Preconditions.checkNotNull(options.primaryManifest);
    Preconditions.checkNotNull(options.classJarOutput);
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("android_resource_merge_tmp")) {
        Path tmp = scopedTmp.getPath();
        Path mergedAssets = tmp.resolve("merged_assets");
        Path mergedResources = tmp.resolve("merged_resources");
        Path generatedSources = tmp.resolve("generated_resources");
        Path processedManifest = tmp.resolve("manifest-processed/AndroidManifest.xml");
        logger.fine(String.format("Setup finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        VariantType packageType = VariantType.LIBRARY;
        AndroidResourceClassWriter resourceClassWriter = AndroidResourceClassWriter.createWith(aaptConfigOptions.androidJar, generatedSources, Strings.nullToEmpty(options.packageForR));
        resourceClassWriter.setIncludeClassFile(true);
        resourceClassWriter.setIncludeJavaFile(false);
        final MergedAndroidData mergedData = AndroidResourceMerger.mergeData(options.primaryData, options.primaryManifest, options.directData, options.transitiveData, mergedResources, mergedAssets, new StubPngCruncher(), packageType, options.symbolsBinOut, resourceClassWriter);
        logger.fine(String.format("Merging finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        // the manifests compatible with the old manifest merger.
        if (options.manifestOutput != null) {
            MergedAndroidData processedData = AndroidManifestProcessor.with(stdLogger).processManifest(packageType, options.packageForR, null, /* applicationId */
            -1, /* versionCode */
            null, /* versionName */
            mergedData, processedManifest);
            AndroidResourceOutputs.copyManifestToOutput(processedData, options.manifestOutput);
        }
        AndroidResourceOutputs.createClassJar(generatedSources, options.classJarOutput);
        logger.fine(String.format("Create classJar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        if (options.resourcesOutput != null) {
            Path resourcesDir = AndroidResourceProcessor.processDataBindings(mergedData.getResourceDir(), options.dataBindingInfoOut, packageType, options.packageForR, options.primaryManifest);
            // For now, try compressing the library resources that we pass to the validator. This takes
            // extra CPU resources to pack and unpack (~2x), but can reduce the zip size (~4x).
            AndroidResourceOutputs.createResourcesZip(resourcesDir, mergedData.getAssetDir(), options.resourcesOutput, true);
            logger.fine(String.format("Create resources.zip finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        }
    } catch (MergingException e) {
        logger.log(Level.SEVERE, "Error during merging resources", e);
        throw e;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Unexpected", e);
        throw e;
    }
    logger.fine(String.format("Resources merged in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
}
Also used : Path(java.nio.file.Path) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions) MergingException(com.android.ide.common.res2.MergingException) VariantType(com.android.builder.core.VariantType) Stopwatch(com.google.common.base.Stopwatch) OptionsParser(com.google.devtools.common.options.OptionsParser) MergingException(com.android.ide.common.res2.MergingException) IOException(java.io.IOException) PngException(com.android.ide.common.internal.PngException) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)

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