Search in sources :

Example 46 with Package

use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.

the class CrosstoolConfigurationLoader method getCrosstoolProtoFromCrosstoolFile.

private static CrosstoolProto getCrosstoolProtoFromCrosstoolFile(ConfigurationEnvironment env, Label crosstoolTop) throws IOException, InvalidConfigurationException, InterruptedException {
    final Path path;
    try {
        Package containingPackage = env.getTarget(crosstoolTop.getLocalTargetLabel("BUILD")).getPackage();
        if (containingPackage == null) {
            return null;
        }
        path = env.getPath(containingPackage, CROSSTOOL_CONFIGURATION_FILENAME);
    } catch (LabelSyntaxException e) {
        throw new InvalidConfigurationException(e);
    } catch (NoSuchThingException e) {
        // Handled later
        return null;
    }
    if (path == null || !path.exists()) {
        return null;
    }
    return new CrosstoolProto(path.getDigest(), "CROSSTOOL file " + path.getPathString()) {

        @Override
        public String getContents() throws IOException {
            try (InputStream inputStream = path.getInputStream()) {
                return new String(FileSystemUtils.readContentAsLatin1(inputStream));
            }
        }
    };
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) InputStream(java.io.InputStream) Package(com.google.devtools.build.lib.packages.Package) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)

Example 47 with Package

use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.

the class RepositoryFunction method getRule.

/**
   * Uses a remote repository name to fetch the corresponding Rule describing how to get it.
   *
   * <p>This should be the unique entry point for resolving a remote repository function.
   */
@Nullable
public static Rule getRule(String repository, Environment env) throws RepositoryFunctionException, InterruptedException {
    SkyKey packageLookupKey = PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    PackageLookupValue packageLookupValue = (PackageLookupValue) env.getValue(packageLookupKey);
    if (packageLookupValue == null) {
        return null;
    }
    RootedPath workspacePath = packageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
    do {
        WorkspaceFileValue value = (WorkspaceFileValue) env.getValue(workspaceKey);
        if (value == null) {
            return null;
        }
        Package externalPackage = value.getPackage();
        if (externalPackage.containsErrors()) {
            Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
            throw new RepositoryFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Could not load //external package"), Transience.PERSISTENT);
        }
        Rule rule = externalPackage.getRule(repository);
        if (rule != null) {
            return rule;
        }
        workspaceKey = value.next();
    } while (workspaceKey != null);
    throw new RepositoryNotFoundException(repository);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) PackageLookupValue(com.google.devtools.build.lib.skyframe.PackageLookupValue) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) WorkspaceFileValue(com.google.devtools.build.lib.skyframe.WorkspaceFileValue) Package(com.google.devtools.build.lib.packages.Package) Rule(com.google.devtools.build.lib.packages.Rule) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Nullable(javax.annotation.Nullable)

Example 48 with Package

use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.

the class AspectFunction method createAliasAspect.

private static SkyValue createAliasAspect(Environment env, Target originalTarget, Aspect aspect, AspectKey originalKey, ConfiguredTarget configuredTarget) throws InterruptedException {
    ImmutableList<Label> aliasChain = configuredTarget.getProvider(AliasProvider.class).getAliasChain();
    // Find the next alias in the chain: either the next alias (if there are two) or the name of
    // the real configured target.
    Label aliasLabel = aliasChain.size() > 1 ? aliasChain.get(1) : configuredTarget.getLabel();
    SkyKey depKey = ActionLookupValue.key(originalKey.withLabel(aliasLabel));
    // Compute the AspectValue of the target the alias refers to (which can itself be either an
    // alias or a real target)
    AspectValue real = (AspectValue) env.getValue(depKey);
    if (env.valuesMissing()) {
        return null;
    }
    NestedSet<Package> transitivePackages = NestedSetBuilder.<Package>stableOrder().addTransitive(real.getTransitivePackages()).add(originalTarget.getPackage()).build();
    return new AspectValue(originalKey, aspect, originalTarget.getLabel(), originalTarget.getLocation(), ConfiguredAspect.forAlias(real.getConfiguredAspect()), ImmutableList.<ActionAnalysisMetadata>of(), transitivePackages);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) AliasProvider(com.google.devtools.build.lib.rules.AliasProvider) Label(com.google.devtools.build.lib.cmdline.Label) Package(com.google.devtools.build.lib.packages.Package)

Example 49 with Package

use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.

the class LocalRepositoryLookupFunction method maybeCheckWorkspaceForRepository.

/**
   * Checks whether the directory exists and is a workspace root. Returns {@link Optional#absent()}
   * if Skyframe needs to re-run, {@link Optional#of(LocalRepositoryLookupValue)} otherwise.
   */
private Optional<LocalRepositoryLookupValue> maybeCheckWorkspaceForRepository(Environment env, final RootedPath directory) throws InterruptedException, LocalRepositoryLookupFunctionException {
    // Look up the main WORKSPACE file by the external package, to find all repositories.
    PackageLookupValue externalPackageLookupValue;
    try {
        externalPackageLookupValue = (PackageLookupValue) env.getValueOrThrow(PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
        if (externalPackageLookupValue == null) {
            return Optional.absent();
        }
    } catch (BuildFileNotFoundException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("BuildFileNotFoundException while loading the //external package", e), Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("InconsistentFilesystemException while loading the //external package", e), Transience.PERSISTENT);
    }
    RootedPath workspacePath = externalPackageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
    do {
        WorkspaceFileValue value;
        try {
            value = (WorkspaceFileValue) env.getValueOrThrow(workspaceKey, PackageFunctionException.class, NameConflictException.class);
            if (value == null) {
                return Optional.absent();
            }
        } catch (PackageFunctionException e) {
            // TODO(jcater): When WFF is rewritten to not throw a PFE, update this.
            throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("PackageFunctionException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
        } catch (NameConflictException e) {
            throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("NameConflictException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
        }
        Package externalPackage = value.getPackage();
        if (externalPackage.containsErrors()) {
            Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
        }
        // Find all local_repository rules in the WORKSPACE, and check if any have a "path" attribute
        // the same as the requested directory.
        Iterable<Rule> localRepositories = externalPackage.getRulesMatchingRuleClass(LocalRepositoryRule.NAME);
        Rule rule = Iterables.find(localRepositories, new Predicate<Rule>() {

            @Override
            public boolean apply(@Nullable Rule rule) {
                AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
                PathFragment pathAttr = new PathFragment(mapper.get("path", Type.STRING));
                return directory.getRelativePath().equals(pathAttr);
            }
        }, null);
        if (rule != null) {
            try {
                return Optional.of(LocalRepositoryLookupValue.success(RepositoryName.create("@" + rule.getName())));
            } catch (LabelSyntaxException e) {
                // validated.
                throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("LabelSyntaxException while creating the repository name from the rule " + rule.getName(), e), Transience.PERSISTENT);
            }
        }
        workspaceKey = value.next();
    // TODO(bazel-team): This loop can be quadratic in the number of load() statements, consider
    // rewriting or unrolling.
    } while (workspaceKey != null);
    return Optional.of(LocalRepositoryLookupValue.notFound());
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) ErrorDeterminingRepositoryException(com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) PackageFunctionException(com.google.devtools.build.lib.skyframe.PackageFunction.PackageFunctionException) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Package(com.google.devtools.build.lib.packages.Package) LocalRepositoryRule(com.google.devtools.build.lib.rules.repository.LocalRepositoryRule) Rule(com.google.devtools.build.lib.packages.Rule) AggregatingAttributeMapper(com.google.devtools.build.lib.packages.AggregatingAttributeMapper)

Example 50 with Package

use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.

the class PackageFunction method getExternalPackage.

/**
   * Adds a dependency on the WORKSPACE file, representing it as a special type of package.
   *
   * @throws PackageFunctionException if there is an error computing the workspace file or adding
   *     its rules to the //external package.
   */
private SkyValue getExternalPackage(Environment env, Path packageLookupPath) throws PackageFunctionException, InterruptedException {
    RootedPath workspacePath = RootedPath.toRootedPath(packageLookupPath, Label.EXTERNAL_PACKAGE_FILE_NAME);
    SkyKey workspaceKey = ExternalPackageFunction.key(workspacePath);
    PackageValue workspace = null;
    try {
        // This may throw a NoSuchPackageException if the WORKSPACE file was malformed or had other
        // problems. Since this function can't add much context, we silently bubble it up.
        workspace = (PackageValue) env.getValueOrThrow(workspaceKey, IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class, EvalException.class, SkylarkImportFailedException.class);
    } catch (IOException | FileSymlinkException | InconsistentFilesystemException | EvalException | SkylarkImportFailedException e) {
        throw new PackageFunctionException(new NoSuchPackageException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Error encountered while dealing with the WORKSPACE file: " + e.getMessage()), Transience.PERSISTENT);
    }
    if (workspace == null) {
        return null;
    }
    Package pkg = workspace.getPackage();
    Event.replayEventsOn(env.getListener(), pkg.getEvents());
    packageFactory.afterDoneLoadingPackage(pkg);
    return new PackageValue(pkg);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) Package(com.google.devtools.build.lib.packages.Package) RootedPath(com.google.devtools.build.lib.vfs.RootedPath)

Aggregations

Package (com.google.devtools.build.lib.packages.Package)61 SkyKey (com.google.devtools.build.skyframe.SkyKey)25 Test (org.junit.Test)20 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)14 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)14 Label (com.google.devtools.build.lib.cmdline.Label)13 Path (com.google.devtools.build.lib.vfs.Path)13 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)13 Target (com.google.devtools.build.lib.packages.Target)12 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)11 Nullable (javax.annotation.Nullable)10 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)5 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)5 LinkedHashSet (java.util.LinkedHashSet)5 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)4 SkyValue (com.google.devtools.build.skyframe.SkyValue)4 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)3 Attribute (com.google.devtools.build.lib.packages.Attribute)3