Search in sources :

Example 1 with Statement

use of com.google.devtools.build.lib.syntax.Statement in project bazel by bazelbuild.

the class PackageFactory method validateAssignmentStatements.

/**
   * Tests a build AST to ensure that it contains no assignment statements that
   * redefine built-in build rules.
   *
   * @param pkgEnv a package environment initialized with all of the built-in
   *        build rules
   * @param ast the build file AST to be tested
   * @param eventHandler a eventHandler where any errors should be logged
   * @return true if the build file contains no redefinitions of built-in
   *         functions
   */
// TODO(bazel-team): Remove this check. It should be moved to LValue.assign
private static boolean validateAssignmentStatements(Environment pkgEnv, BuildFileAST ast, EventHandler eventHandler) {
    for (Statement stmt : ast.getStatements()) {
        if (stmt instanceof AssignmentStatement) {
            Expression lvalue = ((AssignmentStatement) stmt).getLValue().getExpression();
            if (!(lvalue instanceof Identifier)) {
                continue;
            }
            String target = ((Identifier) lvalue).getName();
            if (pkgEnv.hasVariable(target)) {
                eventHandler.handle(Event.error(stmt.getLocation(), "Reassignment of builtin build " + "function '" + target + "' not permitted"));
                return false;
            }
        }
    }
    return true;
}
Also used : Identifier(com.google.devtools.build.lib.syntax.Identifier) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) AssignmentStatement(com.google.devtools.build.lib.syntax.AssignmentStatement) Expression(com.google.devtools.build.lib.syntax.Expression) FuncallExpression(com.google.devtools.build.lib.syntax.FuncallExpression) AssignmentStatement(com.google.devtools.build.lib.syntax.AssignmentStatement) Statement(com.google.devtools.build.lib.syntax.Statement)

Example 2 with Statement

use of com.google.devtools.build.lib.syntax.Statement in project bazel by bazelbuild.

the class WorkspaceASTFunction method splitAST.

/**
   * Cut {@code ast} into a list of AST separated by load statements. We cut right before each load
   * statement series.
   */
private static ImmutableList<BuildFileAST> splitAST(BuildFileAST ast) {
    ImmutableList.Builder<BuildFileAST> asts = ImmutableList.builder();
    int prevIdx = 0;
    // don't cut if the first statement is a load.
    boolean lastIsLoad = true;
    List<Statement> statements = ast.getStatements();
    for (int idx = 0; idx < statements.size(); idx++) {
        Statement st = statements.get(idx);
        if (st instanceof LoadStatement) {
            if (!lastIsLoad) {
                asts.add(ast.subTree(prevIdx, idx));
                prevIdx = idx;
            }
            lastIsLoad = true;
        } else {
            lastIsLoad = false;
        }
    }
    if (!statements.isEmpty()) {
        asts.add(ast.subTree(prevIdx, statements.size()));
    }
    return asts.build();
}
Also used : LoadStatement(com.google.devtools.build.lib.syntax.LoadStatement) ImmutableList(com.google.common.collect.ImmutableList) Statement(com.google.devtools.build.lib.syntax.Statement) LoadStatement(com.google.devtools.build.lib.syntax.LoadStatement) BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST)

Example 3 with Statement

use of com.google.devtools.build.lib.syntax.Statement in project bazel by bazelbuild.

the class SkylarkImportLookupFunction method execAndExport.

public static void execAndExport(BuildFileAST ast, Label extensionLabel, EventHandler eventHandler, com.google.devtools.build.lib.syntax.Environment extensionEnv) throws InterruptedException {
    ImmutableList<Statement> statements = ast.getStatements();
    for (Statement statement : statements) {
        ast.execTopLevelStatement(statement, extensionEnv, eventHandler);
        possiblyExport(statement, extensionLabel, eventHandler, extensionEnv);
    }
}
Also used : AssignmentStatement(com.google.devtools.build.lib.syntax.AssignmentStatement) Statement(com.google.devtools.build.lib.syntax.Statement) LoadStatement(com.google.devtools.build.lib.syntax.LoadStatement)

Example 4 with Statement

use of com.google.devtools.build.lib.syntax.Statement in project bazel by bazelbuild.

the class PackageFunction method compute.

@Override
public SkyValue compute(SkyKey key, Environment env) throws PackageFunctionException, InterruptedException {
    PackageIdentifier packageId = (PackageIdentifier) key.argument();
    SkyKey packageLookupKey = PackageLookupValue.key(packageId);
    PackageLookupValue packageLookupValue;
    try {
        packageLookupValue = (PackageLookupValue) env.getValueOrThrow(packageLookupKey, BuildFileNotFoundException.class, InconsistentFilesystemException.class);
    } catch (BuildFileNotFoundException e) {
        throw new PackageFunctionException(e, Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        // This error is not transient from the perspective of the PackageFunction.
        throw new PackageFunctionException(new NoSuchPackageException(packageId, e.getMessage(), e), Transience.PERSISTENT);
    }
    if (packageLookupValue == null) {
        return null;
    }
    if (!packageLookupValue.packageExists()) {
        switch(packageLookupValue.getErrorReason()) {
            case NO_BUILD_FILE:
            case DELETED_PACKAGE:
                throw new PackageFunctionException(new BuildFileNotFoundException(packageId, packageLookupValue.getErrorMsg()), Transience.PERSISTENT);
            case INVALID_PACKAGE_NAME:
                throw new PackageFunctionException(new InvalidPackageNameException(packageId, packageLookupValue.getErrorMsg()), Transience.PERSISTENT);
            default:
                // We should never get here.
                throw new IllegalStateException();
        }
    }
    if (packageId.equals(Label.EXTERNAL_PACKAGE_IDENTIFIER)) {
        return getExternalPackage(env, packageLookupValue.getRoot());
    }
    WorkspaceNameValue workspaceNameValue = (WorkspaceNameValue) env.getValue(WorkspaceNameValue.key());
    if (workspaceNameValue == null) {
        return null;
    }
    String workspaceName = workspaceNameValue.maybeGetName();
    if (workspaceName == null) {
        throw new PackageFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER), Transience.PERSISTENT);
    }
    RootedPath buildFileRootedPath = packageLookupValue.getRootedPath(packageId);
    FileValue buildFileValue = null;
    Path buildFilePath = buildFileRootedPath.asPath();
    String replacementContents = null;
    if (!isDefaultsPackage(packageId)) {
        buildFileValue = getBuildFileValue(env, buildFileRootedPath);
        if (buildFileValue == null) {
            return null;
        }
    } else {
        replacementContents = PrecomputedValue.DEFAULTS_PACKAGE_CONTENTS.get(env);
        if (replacementContents == null) {
            return null;
        }
    }
    RuleVisibility defaultVisibility = PrecomputedValue.DEFAULT_VISIBILITY.get(env);
    if (defaultVisibility == null) {
        return null;
    }
    SkyKey astLookupKey = ASTFileLookupValue.key(preludeLabel);
    ASTFileLookupValue astLookupValue = null;
    try {
        astLookupValue = (ASTFileLookupValue) env.getValueOrThrow(astLookupKey, ErrorReadingSkylarkExtensionException.class, InconsistentFilesystemException.class);
    } catch (ErrorReadingSkylarkExtensionException | InconsistentFilesystemException e) {
        throw new PackageFunctionException(new NoSuchPackageException(packageId, "Error encountered while reading the prelude file: " + e.getMessage()), Transience.PERSISTENT);
    }
    if (astLookupValue == null) {
        return null;
    }
    // The prelude file doesn't have to exist. If not, we substitute an empty statement list.
    List<Statement> preludeStatements = astLookupValue.lookupSuccessful() ? astLookupValue.getAST().getStatements() : ImmutableList.<Statement>of();
    CacheEntryWithGlobDeps<Package.Builder> packageBuilderAndGlobDeps = loadPackage(workspaceName, replacementContents, packageId, buildFilePath, buildFileValue, defaultVisibility, preludeStatements, packageLookupValue.getRoot(), env);
    if (packageBuilderAndGlobDeps == null) {
        return null;
    }
    Package.Builder pkgBuilder = packageBuilderAndGlobDeps.value;
    pkgBuilder.buildPartial();
    try {
        // Since the Skyframe dependencies we request below in
        // markDependenciesAndPropagateFilesystemExceptions are requested independently of
        // the ones requested here in
        // handleLabelsCrossingSubpackagesAndPropagateInconsistentFilesystemExceptions, we don't
        // bother checking for missing values and instead piggyback on the env.missingValues() call
        // for the former. This avoids a Skyframe restart.
        handleLabelsCrossingSubpackagesAndPropagateInconsistentFilesystemExceptions(packageLookupValue.getRoot(), packageId, pkgBuilder, env);
    } catch (InternalInconsistentFilesystemException e) {
        packageFunctionCache.invalidate(packageId);
        throw new PackageFunctionException(e.toNoSuchPackageException(), e.isTransient() ? Transience.TRANSIENT : Transience.PERSISTENT);
    }
    Set<SkyKey> globKeys = packageBuilderAndGlobDeps.globDepKeys;
    Map<Label, Path> subincludes = pkgBuilder.getSubincludes();
    boolean packageShouldBeConsideredInError;
    try {
        packageShouldBeConsideredInError = markDependenciesAndPropagateFilesystemExceptions(env, globKeys, subincludes, packageId, pkgBuilder.containsErrors());
    } catch (InternalInconsistentFilesystemException e) {
        packageFunctionCache.invalidate(packageId);
        throw new PackageFunctionException(e.toNoSuchPackageException(), e.isTransient() ? Transience.TRANSIENT : Transience.PERSISTENT);
    }
    if (env.valuesMissing()) {
        return null;
    }
    Event.replayEventsOn(env.getListener(), pkgBuilder.getEvents());
    if (packageShouldBeConsideredInError) {
        pkgBuilder.setContainsErrors();
    }
    Package pkg = pkgBuilder.finishBuild();
    // We know this SkyFunction will not be called again, so we can remove the cache entry.
    packageFunctionCache.invalidate(packageId);
    packageFactory.afterDoneLoadingPackage(pkg);
    return new PackageValue(pkg);
}
Also used : BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) InvalidPackageNameException(com.google.devtools.build.lib.packages.InvalidPackageNameException) Label(com.google.devtools.build.lib.cmdline.Label) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) SkyKey(com.google.devtools.build.skyframe.SkyKey) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) Statement(com.google.devtools.build.lib.syntax.Statement) RuleVisibility(com.google.devtools.build.lib.packages.RuleVisibility) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Package(com.google.devtools.build.lib.packages.Package)

Aggregations

Statement (com.google.devtools.build.lib.syntax.Statement)4 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)2 AssignmentStatement (com.google.devtools.build.lib.syntax.AssignmentStatement)2 LoadStatement (com.google.devtools.build.lib.syntax.LoadStatement)2 ImmutableList (com.google.common.collect.ImmutableList)1 Label (com.google.devtools.build.lib.cmdline.Label)1 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)1 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)1 InvalidPackageNameException (com.google.devtools.build.lib.packages.InvalidPackageNameException)1 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)1 Package (com.google.devtools.build.lib.packages.Package)1 RuleVisibility (com.google.devtools.build.lib.packages.RuleVisibility)1 BuildFileAST (com.google.devtools.build.lib.syntax.BuildFileAST)1 Expression (com.google.devtools.build.lib.syntax.Expression)1 FuncallExpression (com.google.devtools.build.lib.syntax.FuncallExpression)1 Identifier (com.google.devtools.build.lib.syntax.Identifier)1 Path (com.google.devtools.build.lib.vfs.Path)1 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)1 SkyKey (com.google.devtools.build.skyframe.SkyKey)1