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);
}
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);
}
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);
}
}
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);
}
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());
}
Aggregations