Search in sources :

Example 1 with BuildFileAST

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

the class PackageFactory method createPackageFromPreprocessingResult.

/****************************************************************************
   * Package creation.
   */
/**
   * Loads, scans parses and evaluates the build file at "buildFile", and
   * creates and returns a Package builder instance capable of building a package identified by
   * "packageId".
   *
   * <p>This method returns a builder to allow the caller to do additional work, if necessary.
   *
   * <p>This method assumes "packageId" is a valid package name according to the
   * {@link LabelValidator#validatePackageName} heuristic.
   *
   * <p>See {@link #evaluateBuildFile} for information on AST retention.
   *
   * <p>Executes {@code globber.onCompletion()} on completion and executes
   * {@code globber.onInterrupt()} on an {@link InterruptedException}.
   */
// Used outside of bazel!
public Package.Builder createPackageFromPreprocessingResult(String workspaceName, PackageIdentifier packageId, Path buildFile, Preprocessor.Result preprocessingResult, List<Statement> preludeStatements, Map<String, Extension> imports, ImmutableList<Label> skylarkFileDependencies, RuleVisibility defaultVisibility, Globber globber) throws InterruptedException {
    StoredEventHandler localReporterForParsing = new StoredEventHandler();
    // Run the lexer and parser with a local reporter, so that errors from other threads do not
    // show up below.
    BuildFileAST buildFileAST = parseBuildFile(packageId, preprocessingResult.result, preludeStatements, localReporterForParsing);
    AstAfterPreprocessing astAfterPreprocessing = new AstAfterPreprocessing(preprocessingResult, buildFileAST, localReporterForParsing);
    return createPackageFromPreprocessingAst(workspaceName, packageId, buildFile, astAfterPreprocessing, imports, skylarkFileDependencies, defaultVisibility, globber);
}
Also used : StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) AstAfterPreprocessing(com.google.devtools.build.lib.packages.Preprocessor.AstAfterPreprocessing) BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST)

Example 2 with BuildFileAST

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

the class WorkspaceFactory method parse.

@VisibleForTesting
public void parse(ParserInputSource source, @Nullable StoredEventHandler localReporter) throws BuildFileContainsErrorsException, InterruptedException {
    // because most people are just using it to resolve Maven dependencies.
    if (localReporter == null) {
        localReporter = new StoredEventHandler();
    }
    BuildFileAST buildFileAST = BuildFileAST.parseBuildFile(source, localReporter);
    if (buildFileAST.containsErrors()) {
        throw new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Failed to parse " + source.getPath());
    }
    execute(buildFileAST, null, localReporter);
}
Also used : StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with BuildFileAST

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

the class WorkspaceASTFunction method compute.

@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException, WorkspaceASTFunctionException {
    RootedPath workspaceRoot = (RootedPath) skyKey.argument();
    FileValue workspaceFileValue = (FileValue) env.getValue(FileValue.key(workspaceRoot));
    if (workspaceFileValue == null) {
        return null;
    }
    Path repoWorkspace = workspaceRoot.getRoot().getRelative(workspaceRoot.getRelativePath());
    try {
        BuildFileAST ast = BuildFileAST.parseBuildFile(ParserInputSource.create(ruleClassProvider.getDefaultWorkspacePrefix(), new PathFragment("/DEFAULT.WORKSPACE")), env.getListener());
        if (ast.containsErrors()) {
            throw new WorkspaceASTFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Failed to parse default WORKSPACE file"), Transience.PERSISTENT);
        }
        if (workspaceFileValue.exists()) {
            ast = BuildFileAST.parseBuildFile(ParserInputSource.create(repoWorkspace), ast.getStatements(), env.getListener());
            if (ast.containsErrors()) {
                throw new WorkspaceASTFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Failed to parse WORKSPACE file"), Transience.PERSISTENT);
            }
        }
        ast = BuildFileAST.parseBuildFile(ParserInputSource.create(ruleClassProvider.getDefaultWorkspaceSuffix(), new PathFragment("/DEFAULT.WORKSPACE.SUFFIX")), ast.getStatements(), env.getListener());
        if (ast.containsErrors()) {
            throw new WorkspaceASTFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Failed to parse default WORKSPACE file suffix"), Transience.PERSISTENT);
        }
        return new WorkspaceASTValue(splitAST(ast));
    } catch (IOException ex) {
        throw new WorkspaceASTFunctionException(ex, Transience.TRANSIENT);
    }
}
Also used : RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST)

Example 4 with BuildFileAST

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

the class ASTFileLookupFunction method compute.

@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException {
    Label fileLabel = (Label) skyKey.argument();
    PathFragment filePathFragment = fileLabel.toPathFragment();
    //
    // Determine whether the package designated by fileLabel exists.
    //
    SkyKey pkgSkyKey = PackageLookupValue.key(fileLabel.getPackageIdentifier());
    PackageLookupValue pkgLookupValue = null;
    try {
        pkgLookupValue = (PackageLookupValue) env.getValueOrThrow(pkgSkyKey, BuildFileNotFoundException.class, InconsistentFilesystemException.class);
    } catch (BuildFileNotFoundException e) {
        throw new ASTLookupFunctionException(new ErrorReadingSkylarkExtensionException(e), Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        throw new ASTLookupFunctionException(e, Transience.PERSISTENT);
    }
    if (pkgLookupValue == null) {
        return null;
    }
    if (!pkgLookupValue.packageExists()) {
        return ASTFileLookupValue.forBadPackage(fileLabel, pkgLookupValue.getErrorMsg());
    }
    //
    // Determine whether the file designated by fileLabel exists.
    //
    Path packageRoot = pkgLookupValue.getRoot();
    RootedPath rootedPath = RootedPath.toRootedPath(packageRoot, filePathFragment);
    SkyKey fileSkyKey = FileValue.key(rootedPath);
    FileValue fileValue = null;
    try {
        fileValue = (FileValue) env.getValueOrThrow(fileSkyKey, IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class);
    } catch (IOException | FileSymlinkException e) {
        throw new ASTLookupFunctionException(new ErrorReadingSkylarkExtensionException(e), Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        throw new ASTLookupFunctionException(e, Transience.PERSISTENT);
    }
    if (fileValue == null) {
        return null;
    }
    if (!fileValue.isFile()) {
        return ASTFileLookupValue.forBadFile(fileLabel);
    }
    //
    // Both the package and the file exist; load the file and parse it as an AST.
    //
    BuildFileAST ast = null;
    Path path = rootedPath.asPath();
    try {
        long astFileSize = fileValue.getSize();
        try (Mutability mutability = Mutability.create("validate")) {
            ValidationEnvironment validationEnv = new ValidationEnvironment(ruleClassProvider.createSkylarkRuleClassEnvironment(fileLabel, mutability, env.getListener(), /*astFileContentHashCode=*/
            null, /*importMap=*/
            null).setupDynamic(Runtime.PKG_NAME, Runtime.NONE).setupDynamic(Runtime.REPOSITORY_NAME, Runtime.NONE));
            ast = BuildFileAST.parseSkylarkFile(path, astFileSize, env.getListener());
            ast = ast.validate(validationEnv, env.getListener());
        }
    } catch (IOException e) {
        throw new ASTLookupFunctionException(new ErrorReadingSkylarkExtensionException(e), Transience.TRANSIENT);
    }
    return ASTFileLookupValue.withFile(ast);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) ValidationEnvironment(com.google.devtools.build.lib.syntax.ValidationEnvironment) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Mutability(com.google.devtools.build.lib.syntax.Mutability) IOException(java.io.IOException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST)

Example 5 with BuildFileAST

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

the class SkylarkRuleClassFunctionsTest method evalAndExport.

protected void evalAndExport(String... lines) throws Exception {
    BuildFileAST buildFileAST = BuildFileAST.parseAndValidateSkylarkString(ev.getEnvironment(), lines);
    SkylarkImportLookupFunction.execAndExport(buildFileAST, FAKE_LABEL, ev.getEventHandler(), ev.getEnvironment());
}
Also used : BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST)

Aggregations

BuildFileAST (com.google.devtools.build.lib.syntax.BuildFileAST)11 StoredEventHandler (com.google.devtools.build.lib.events.StoredEventHandler)3 Path (com.google.devtools.build.lib.vfs.Path)3 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)3 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)3 SkyKey (com.google.devtools.build.skyframe.SkyKey)3 IOException (java.io.IOException)3 ImmutableList (com.google.common.collect.ImmutableList)2 Label (com.google.devtools.build.lib.cmdline.Label)2 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)2 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)2 Package (com.google.devtools.build.lib.packages.Package)2 AstAfterPreprocessing (com.google.devtools.build.lib.packages.Preprocessor.AstAfterPreprocessing)2 Mutability (com.google.devtools.build.lib.syntax.Mutability)2 ValidationEnvironment (com.google.devtools.build.lib.syntax.ValidationEnvironment)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)1 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)1 LegacyGlobber (com.google.devtools.build.lib.packages.PackageFactory.LegacyGlobber)1 Preprocessor (com.google.devtools.build.lib.packages.Preprocessor)1