Search in sources :

Example 16 with InvalidConfigurationException

use of com.google.devtools.build.lib.analysis.config.InvalidConfigurationException in project bazel by bazelbuild.

the class CrosstoolConfigurationLoader method findCrosstoolConfiguration.

private static CrosstoolFile findCrosstoolConfiguration(ConfigurationEnvironment env, Label crosstoolTop) throws IOException, InvalidConfigurationException, InterruptedException {
    CrosstoolProto crosstoolProto = getCrosstoolProtofromBuildFile(env, crosstoolTop);
    if (crosstoolProto == null) {
        crosstoolProto = getCrosstoolProtoFromCrosstoolFile(env, crosstoolTop);
    }
    if (crosstoolProto == null) {
        throw new InvalidConfigurationException("The crosstool_top you specified was resolved to '" + crosstoolTop + "', which does not contain a CROSSTOOL file. " + "You can use a crosstool from the depot by specifying its label.");
    } else {
        // Do this before we read the data, so if it changes, we get a different MD5 the next time.
        // Alternatively, we could calculate the MD5 of the contents, which we also read, but this
        // is faster if the file comes from a file system with md5 support.
        final CrosstoolProto finalProto = crosstoolProto;
        String md5 = BaseEncoding.base16().lowerCase().encode(finalProto.getMd5());
        CrosstoolConfig.CrosstoolRelease release;
        try {
            release = crosstoolReleaseCache.get(md5, new Callable<CrosstoolRelease>() {

                @Override
                public CrosstoolRelease call() throws Exception {
                    return toReleaseConfiguration(finalProto.getName(), finalProto.getContents());
                }
            });
        } catch (ExecutionException e) {
            throw new InvalidConfigurationException(e);
        }
        return new CrosstoolFile(finalProto.getName(), release, md5);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) CrosstoolRelease(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease) CrosstoolConfig(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig)

Example 17 with InvalidConfigurationException

use of com.google.devtools.build.lib.analysis.config.InvalidConfigurationException in project bazel by bazelbuild.

the class CrosstoolConfigurationLoader method selectToolchain.

/**
   * Selects a crosstool toolchain corresponding to the given crosstool
   * configuration options. If all of these options are null, it returns the default
   * toolchain specified in the crosstool release. If only cpu is non-null, it
   * returns the default toolchain for that cpu, as specified in the crosstool
   * release. Otherwise, all values must be non-null, and this method
   * returns the toolchain which matches all of the values.
   *
   * @throws NullPointerException if {@code release} is null
   * @throws InvalidConfigurationException if no matching toolchain can be found, or
   *     if the input parameters do not obey the constraints described above
   */
public static CrosstoolConfig.CToolchain selectToolchain(CrosstoolConfig.CrosstoolRelease release, BuildOptions options, Function<String, String> cpuTransformer) throws InvalidConfigurationException {
    CrosstoolConfigurationIdentifier config = CrosstoolConfigurationIdentifier.fromOptions(options);
    if ((config.getCompiler() != null) || (config.getLibc() != null)) {
        ArrayList<CrosstoolConfig.CToolchain> candidateToolchains = new ArrayList<>();
        for (CrosstoolConfig.CToolchain toolchain : release.getToolchainList()) {
            if (config.isCandidateToolchain(toolchain)) {
                candidateToolchains.add(toolchain);
            }
        }
        switch(candidateToolchains.size()) {
            case 0:
                {
                    StringBuilder message = new StringBuilder();
                    message.append("No toolchain found for");
                    message.append(config.describeFlags());
                    message.append(". Valid toolchains are: ");
                    describeToolchainList(message, release.getToolchainList());
                    throw new InvalidConfigurationException(message.toString());
                }
            case 1:
                return candidateToolchains.get(0);
            default:
                {
                    StringBuilder message = new StringBuilder();
                    message.append("Multiple toolchains found for");
                    message.append(config.describeFlags());
                    message.append(": ");
                    describeToolchainList(message, candidateToolchains);
                    throw new InvalidConfigurationException(message.toString());
                }
        }
    }
    String selectedIdentifier = null;
    // We use fake CPU values to allow cross-platform builds for other languages that use the
    // C++ toolchain. Translate to the actual target architecture.
    String desiredCpu = cpuTransformer.apply(config.getCpu());
    for (CrosstoolConfig.DefaultCpuToolchain selector : release.getDefaultToolchainList()) {
        if (selector.getCpu().equals(desiredCpu)) {
            selectedIdentifier = selector.getToolchainIdentifier();
            break;
        }
    }
    if (selectedIdentifier == null) {
        StringBuilder cpuBuilder = new StringBuilder();
        for (CrosstoolConfig.DefaultCpuToolchain selector : release.getDefaultToolchainList()) {
            cpuBuilder.append("  ").append(selector.getCpu()).append(",\n");
        }
        throw new InvalidConfigurationException("No toolchain found for cpu '" + desiredCpu + "'. Valid cpus are: [\n" + cpuBuilder + "]");
    }
    checkToolChain(selectedIdentifier, desiredCpu);
    for (CrosstoolConfig.CToolchain toolchain : release.getToolchainList()) {
        if (toolchain.getToolchainIdentifier().equals(selectedIdentifier)) {
            return toolchain;
        }
    }
    throw new InvalidConfigurationException("Inconsistent crosstool configuration; no toolchain " + "corresponding to '" + selectedIdentifier + "' found for cpu '" + config.getCpu() + "'");
}
Also used : ArrayList(java.util.ArrayList) CrosstoolConfig(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)

Example 18 with InvalidConfigurationException

use of com.google.devtools.build.lib.analysis.config.InvalidConfigurationException in project bazel by bazelbuild.

the class JvmConfigurationLoader method createDefault.

@Nullable
private static Jvm createDefault(ConfigurationEnvironment lookup, String javaHome, String cpu) throws InvalidConfigurationException, LabelSyntaxException, InterruptedException {
    try {
        Label label = Label.parseAbsolute(javaHome);
        label = RedirectChaser.followRedirects(lookup, label, "jdk");
        if (label == null) {
            return null;
        }
        Target javaHomeTarget = lookup.getTarget(label);
        if (javaHomeTarget instanceof Rule) {
            if (!((Rule) javaHomeTarget).getRuleClass().equals("java_runtime_suite")) {
                throw new InvalidConfigurationException("Unexpected javabase rule kind '" + ((Rule) javaHomeTarget).getRuleClass() + "'");
            }
            return createFromRuntimeSuite(lookup, (Rule) javaHomeTarget, cpu);
        }
        throw new InvalidConfigurationException("No JVM target found under " + javaHome + " that would work for " + cpu);
    } catch (NoSuchThingException e) {
        lookup.getEventHandler().handle(Event.error(e.getMessage()));
        throw new InvalidConfigurationException(e.getMessage(), e);
    }
}
Also used : Target(com.google.devtools.build.lib.packages.Target) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) Nullable(javax.annotation.Nullable)

Example 19 with InvalidConfigurationException

use of com.google.devtools.build.lib.analysis.config.InvalidConfigurationException in project bazel by bazelbuild.

the class JvmConfigurationLoader method selectRuntime.

private static Label selectRuntime(Rule javaRuntimeSuite, String cpu) throws InvalidConfigurationException {
    RawAttributeMapper suiteAttributes = RawAttributeMapper.of(javaRuntimeSuite);
    Map<String, Label> runtimes = suiteAttributes.get("runtimes", BuildType.LABEL_DICT_UNARY);
    if (runtimes.containsKey(cpu)) {
        return runtimes.get(cpu);
    }
    if (suiteAttributes.isAttributeValueExplicitlySpecified("default")) {
        return suiteAttributes.get("default", BuildType.LABEL);
    }
    throw new InvalidConfigurationException("No JVM target found under " + javaRuntimeSuite + " that would work for " + cpu);
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) Label(com.google.devtools.build.lib.cmdline.Label) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)

Example 20 with InvalidConfigurationException

use of com.google.devtools.build.lib.analysis.config.InvalidConfigurationException in project bazel by bazelbuild.

the class InfoCommand method exec.

@Override
public ExitCode exec(final CommandEnvironment env, final OptionsProvider optionsProvider) {
    final BlazeRuntime runtime = env.getRuntime();
    env.getReporter().switchToAnsiAllowingHandler();
    Options infoOptions = optionsProvider.getOptions(Options.class);
    OutErr outErr = env.getReporter().getOutErr();
    // Creating a BuildConfiguration is expensive and often unnecessary. Delay the creation until
    // it is needed.
    Supplier<BuildConfiguration> configurationSupplier = new Supplier<BuildConfiguration>() {

        private BuildConfiguration configuration;

        @Override
        public BuildConfiguration get() {
            if (configuration != null) {
                return configuration;
            }
            try {
                // In order to be able to answer configuration-specific queries, we need to setup the
                // package path. Since info inherits all the build options, all the necessary information
                // is available here.
                env.setupPackageCache(optionsProvider, runtime.getDefaultsPackageContent(optionsProvider));
                // TODO(bazel-team): What if there are multiple configurations? [multi-config]
                configuration = env.getConfigurations(optionsProvider).getTargetConfigurations().get(0);
                return configuration;
            } catch (InvalidConfigurationException e) {
                env.getReporter().handle(Event.error(e.getMessage()));
                throw new ExitCausingRuntimeException(ExitCode.COMMAND_LINE_ERROR);
            } catch (AbruptExitException e) {
                throw new ExitCausingRuntimeException("unknown error: " + e.getMessage(), e.getExitCode());
            } catch (InterruptedException e) {
                env.getReporter().handle(Event.error("interrupted"));
                throw new ExitCausingRuntimeException(ExitCode.INTERRUPTED);
            }
        }
    };
    Map<String, InfoItem> items = getInfoItemMap(env, optionsProvider);
    try {
        if (infoOptions.showMakeEnvironment) {
            Map<String, String> makeEnv = configurationSupplier.get().getMakeEnvironment();
            for (Map.Entry<String, String> entry : makeEnv.entrySet()) {
                InfoItem item = new InfoItem.MakeInfoItem(entry.getKey(), entry.getValue());
                items.put(item.getName(), item);
            }
        }
        List<String> residue = optionsProvider.getResidue();
        if (residue.size() > 1) {
            env.getReporter().handle(Event.error("at most one key may be specified"));
            return ExitCode.COMMAND_LINE_ERROR;
        }
        String key = residue.size() == 1 ? residue.get(0) : null;
        env.getEventBus().post(new NoBuildEvent());
        if (key != null) {
            // print just the value for the specified key:
            byte[] value;
            if (items.containsKey(key)) {
                value = items.get(key).get(configurationSupplier, env);
            } else {
                env.getReporter().handle(Event.error("unknown key: '" + key + "'"));
                return ExitCode.COMMAND_LINE_ERROR;
            }
            try {
                outErr.getOutputStream().write(value);
                outErr.getOutputStream().flush();
            } catch (IOException e) {
                env.getReporter().handle(Event.error("Cannot write info block: " + e.getMessage()));
                return ExitCode.ANALYSIS_FAILURE;
            }
        } else {
            // print them all
            // We'll need this later anyway
            configurationSupplier.get();
            for (InfoItem infoItem : items.values()) {
                if (infoItem.isHidden()) {
                    continue;
                }
                outErr.getOutputStream().write((infoItem.getName() + ": ").getBytes(StandardCharsets.UTF_8));
                outErr.getOutputStream().write(infoItem.get(configurationSupplier, env));
            }
        }
    } catch (AbruptExitException e) {
        return e.getExitCode();
    } catch (ExitCausingRuntimeException e) {
        return e.getExitCode();
    } catch (IOException e) {
        return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
    } catch (InterruptedException e) {
        return ExitCode.INTERRUPTED;
    }
    return ExitCode.SUCCESS;
}
Also used : OutErr(com.google.devtools.build.lib.util.io.OutErr) NoBuildEvent(com.google.devtools.build.lib.analysis.NoBuildEvent) IOException(java.io.IOException) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) Supplier(com.google.common.base.Supplier) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)24 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)8 Label (com.google.devtools.build.lib.cmdline.Label)8 Nullable (javax.annotation.Nullable)7 Target (com.google.devtools.build.lib.packages.Target)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)5 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)4 Attribute (com.google.devtools.build.lib.packages.Attribute)4 Rule (com.google.devtools.build.lib.packages.Rule)4 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)3 InconsistentAspectOrderException (com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException)3 TargetAndConfiguration (com.google.devtools.build.lib.analysis.TargetAndConfiguration)3 Fragment (com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment)3 ConfigMatchingProvider (com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider)3 Package (com.google.devtools.build.lib.packages.Package)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)2 Dependency (com.google.devtools.build.lib.analysis.Dependency)2