// Copyright 2014 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.packages; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.StoredEventHandler; import com.google.devtools.build.lib.packages.PackageFactory.Globber; import com.google.devtools.build.lib.syntax.BuildFileAST; import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.ParserInputSource; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import java.io.IOException; import java.util.List; import java.util.Set; import javax.annotation.Nullable; /** A Preprocessor is an interface to implement generic text-based preprocessing of BUILD files. */ public interface Preprocessor { /** Factory for {@link Preprocessor} instances. */ interface Factory { /** Supplier for {@link Factory} instances. */ interface Supplier { /** * Returns a Preprocessor factory to use for getting Preprocessor instances. * *
The CachingPackageLocator is provided so the constructed preprocessors can look up
* other BUILD files.
*/
Factory getFactory(CachingPackageLocator loc);
/** Supplier that always returns {@code NullFactory.INSTANCE}. */
static class NullSupplier implements Supplier {
public static final NullSupplier INSTANCE = new NullSupplier();
private NullSupplier() {
}
@Override
public Factory getFactory(CachingPackageLocator loc) {
return NullFactory.INSTANCE;
}
}
}
/**
* Returns whether this {@link Factory} is still suitable for providing {@link Preprocessor}s.
* If not, all previous preprocessing results should be assumed to be invalid and a new
* {@link Factory} should be created via {@link Supplier#getFactory}.
*/
boolean isStillValid();
/**
* Returns a Preprocessor instance capable of preprocessing a BUILD file independently (e.g. it
* ought to be fine to call {@link #getPreprocessor} for each BUILD file).
*/
@Nullable
Preprocessor getPreprocessor();
/** Factory that always returns {@code null} {@link Preprocessor}s. */
static class NullFactory implements Factory {
public static final NullFactory INSTANCE = new NullFactory();
private NullFactory() {
}
@Override
public boolean isStillValid() {
return true;
}
@Override
public Preprocessor getPreprocessor() {
return null;
}
}
}
/**
* A (result, success) tuple indicating the outcome of preprocessing.
*/
static class Result {
private static final char[] EMPTY_CHARS = new char[0];
public final ParserInputSource result;
public final boolean preprocessed;
public final boolean containsErrors;
public final List