| // 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 static com.google.devtools.build.lib.packages.PackageFactory.getContext; | |
| import com.google.common.base.Joiner; | |
| import com.google.common.collect.ImmutableList; | |
| import com.google.common.collect.ImmutableMap; | |
| import com.google.common.flogger.GoogleLogger; | |
| import com.google.devtools.build.lib.cmdline.Label; | |
| import com.google.devtools.build.lib.cmdline.LabelSyntaxException; | |
| import com.google.devtools.build.lib.cmdline.LabelValidator; | |
| import com.google.devtools.build.lib.cmdline.PackageIdentifier; | |
| import com.google.devtools.build.lib.events.Event; | |
| import com.google.devtools.build.lib.io.FileSymlinkException; | |
| import com.google.devtools.build.lib.packages.Globber.BadGlobException; | |
| import com.google.devtools.build.lib.packages.PackageFactory.PackageContext; | |
| import com.google.devtools.build.lib.packages.RuleClass.Builder.ThirdPartyLicenseExistencePolicy; | |
| import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; | |
| import com.google.devtools.build.lib.server.FailureDetails.PackageLoading.Code; | |
| import com.google.devtools.build.lib.starlarkbuildapi.StarlarkNativeModuleApi; | |
| import com.google.devtools.build.lib.util.DetailedExitCode; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.Collection; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| import java.util.Map; | |
| import javax.annotation.Nullable; | |
| import net.starlark.java.eval.Dict; | |
| import net.starlark.java.eval.EvalException; | |
| import net.starlark.java.eval.Mutability; | |
| import net.starlark.java.eval.NoneType; | |
| import net.starlark.java.eval.Sequence; | |
| import net.starlark.java.eval.Starlark; | |
| import net.starlark.java.eval.StarlarkInt; | |
| import net.starlark.java.eval.StarlarkList; | |
| import net.starlark.java.eval.StarlarkThread; | |
| import net.starlark.java.eval.StarlarkValue; | |
| import net.starlark.java.eval.Tuple; | |
| import net.starlark.java.syntax.Location; | |
| /** The Starlark native module. */ | |
| // TODO(cparsons): Move the definition of native.package() to this class. | |
| public class StarlarkNativeModule implements StarlarkNativeModuleApi { | |
| private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); | |
| /** | |
| * This map contains all the (non-rule) functions of the native module (keyed by their symbol | |
| * name). These native module bindings should be added (without the 'native' module namespace) to | |
| * the global Starlark environment for BUILD files. | |
| * | |
| * <p>For example, the function "glob" is available under both a global symbol name {@code glob()} | |
| * as well as under the native module namepsace {@code native.glob()}. An entry of this map is | |
| * thus ("glob" : glob function). | |
| */ | |
| public static final ImmutableMap<String, Object> BINDINGS_FOR_BUILD_FILES = initializeBindings(); | |
| private static ImmutableMap<String, Object> initializeBindings() { | |
| ImmutableMap.Builder<String, Object> bindings = ImmutableMap.builder(); | |
| Starlark.addMethods(bindings, new StarlarkNativeModule()); | |
| return bindings.build(); | |
| } | |
| @Override | |
| public Sequence<?> glob( | |
| Sequence<?> include, | |
| Sequence<?> exclude, | |
| StarlarkInt excludeDirs, | |
| Object allowEmptyArgument, | |
| StarlarkThread thread) | |
| throws EvalException, InterruptedException { | |
| BazelStarlarkContext.from(thread).checkLoadingPhase("native.glob"); | |
| PackageContext context = getContext(thread); | |
| List<String> includes = Type.STRING_LIST.convert(include, "'glob' argument"); | |
| List<String> excludes = Type.STRING_LIST.convert(exclude, "'glob' argument"); | |
| List<String> matches; | |
| boolean allowEmpty; | |
| if (allowEmptyArgument == Starlark.UNBOUND) { | |
| allowEmpty = | |
| !thread.getSemantics().getBool(BuildLanguageOptions.INCOMPATIBLE_DISALLOW_EMPTY_GLOB); | |
| } else if (allowEmptyArgument instanceof Boolean) { | |
| allowEmpty = (Boolean) allowEmptyArgument; | |
| } else { | |
| throw Starlark.errorf( | |
| "expected boolean for argument `allow_empty`, got `%s`", allowEmptyArgument); | |
| } | |
| try { | |
| Globber.Token globToken = | |
| context.globber.runAsync(includes, excludes, excludeDirs.signum() != 0, allowEmpty); | |
| matches = context.globber.fetchUnsorted(globToken); | |
| } catch (IOException e) { | |
| logger.atWarning().withCause(e).log( | |
| "Exception processing includes=%s, excludes=%s)", includes, excludes); | |
| String errorMessage = | |
| String.format( | |
| "error globbing [%s]%s: %s", | |
| Joiner.on(", ").join(includes), | |
| excludes.isEmpty() ? "" : " - [" + Joiner.on(", ").join(excludes) + "]", | |
| e.getMessage()); | |
| Location loc = thread.getCallerLocation(); | |
| Event error = | |
| Package.error( | |
| loc, | |
| errorMessage, | |
| // If there are other IOExceptions that can result from user error, they should be | |
| // tested for here. Currently FileNotFoundException is not one of those, because globs | |
| // only encounter that error in the presence of an inconsistent filesystem. | |
| e instanceof FileSymlinkException | |
| ? Code.EVAL_GLOBS_SYMLINK_ERROR | |
| : Code.GLOB_IO_EXCEPTION); | |
| context.eventHandler.handle(error); | |
| context.pkgBuilder.setIOException(e, errorMessage, error.getProperty(DetailedExitCode.class)); | |
| matches = ImmutableList.of(); | |
| } catch (BadGlobException e) { | |
| throw new EvalException(e); | |
| } | |
| ArrayList<String> result = new ArrayList<>(matches.size()); | |
| for (String match : matches) { | |
| if (match.charAt(0) == '@') { | |
| // Add explicit colon to disambiguate from external repository. | |
| match = ":" + match; | |
| } | |
| result.add(match); | |
| } | |
| result.sort(Comparator.naturalOrder()); | |
| return StarlarkList.copyOf(thread.mutability(), result); | |
| } | |
| @Override | |
| public Object existingRule(String name, StarlarkThread thread) throws EvalException { | |
| BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("native.existing_rule"); | |
| PackageContext context = getContext(thread); | |
| Target target = context.pkgBuilder.getTarget(name); | |
| return target instanceof Rule ? getRuleDict((Rule) target, thread.mutability()) : Starlark.NONE; | |
| } | |
| /* | |
| If necessary, we could allow filtering by tag (anytag, alltags), name (regexp?), kind ? | |
| For now, we ignore this, since users can implement it in Starlark. | |
| */ | |
| @Override | |
| public Dict<String, Dict<String, Object>> existingRules(StarlarkThread thread) | |
| throws EvalException { | |
| BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("native.existing_rules"); | |
| PackageContext context = getContext(thread); | |
| Collection<Target> targets = context.pkgBuilder.getTargets(); | |
| Mutability mu = thread.mutability(); | |
| Dict.Builder<String, Dict<String, Object>> rules = Dict.builder(); | |
| for (Target t : targets) { | |
| if (t instanceof Rule) { | |
| rules.put(t.getName(), getRuleDict((Rule) t, mu)); | |
| } | |
| } | |
| return rules.build(mu); | |
| } | |
| @Override | |
| public NoneType packageGroup( | |
| String name, | |
| Sequence<?> packagesO, | |
| Sequence<?> includesO, | |
| StarlarkThread thread) | |
| throws EvalException { | |
| BazelStarlarkContext.from(thread).checkLoadingPhase("native.package_group"); | |
| PackageContext context = getContext(thread); | |
| List<String> packages = | |
| Type.STRING_LIST.convert(packagesO, "'package_group.packages argument'"); | |
| List<Label> includes = | |
| BuildType.LABEL_LIST.convert( | |
| includesO, "'package_group.includes argument'", context.pkgBuilder.getBuildFileLabel()); | |
| Location loc = thread.getCallerLocation(); | |
| try { | |
| context.pkgBuilder.addPackageGroup(name, packages, includes, context.eventHandler, loc); | |
| return Starlark.NONE; | |
| } catch (LabelSyntaxException e) { | |
| throw Starlark.errorf("package group has invalid name: %s: %s", name, e.getMessage()); | |
| } catch (Package.NameConflictException e) { | |
| throw new EvalException(e); | |
| } | |
| } | |
| @Override | |
| public NoneType exportsFiles( | |
| Sequence<?> srcs, Object visibilityO, Object licensesO, StarlarkThread thread) | |
| throws EvalException { | |
| BazelStarlarkContext.from(thread).checkLoadingPhase("native.exports_files"); | |
| Package.Builder pkgBuilder = getContext(thread).pkgBuilder; | |
| List<String> files = Type.STRING_LIST.convert(srcs, "'exports_files' operand"); | |
| RuleVisibility visibility = | |
| Starlark.isNullOrNone(visibilityO) | |
| ? ConstantRuleVisibility.PUBLIC | |
| : PackageUtils.getVisibility( | |
| pkgBuilder.getBuildFileLabel(), | |
| BuildType.LABEL_LIST.convert( | |
| visibilityO, "'exports_files' operand", pkgBuilder.getBuildFileLabel())); | |
| // TODO(bazel-team): is licenses plural or singular? | |
| License license = BuildType.LICENSE.convertOptional(licensesO, "'exports_files' operand"); | |
| Location loc = thread.getCallerLocation(); | |
| for (String file : files) { | |
| String errorMessage = LabelValidator.validateTargetName(file); | |
| if (errorMessage != null) { | |
| throw Starlark.errorf("%s", errorMessage); | |
| } | |
| try { | |
| InputFile inputFile = pkgBuilder.createInputFile(file, loc); | |
| if (inputFile.isVisibilitySpecified() && inputFile.getVisibility() != visibility) { | |
| throw Starlark.errorf( | |
| "visibility for exported file '%s' declared twice", inputFile.getName()); | |
| } | |
| if (license != null && inputFile.isLicenseSpecified()) { | |
| throw Starlark.errorf( | |
| "licenses for exported file '%s' declared twice", inputFile.getName()); | |
| } | |
| // See if we should check third-party licenses: first checking for any hard-coded policy, | |
| // then falling back to user-settable flags. | |
| boolean checkLicenses; | |
| if (pkgBuilder.getThirdPartyLicenseExistencePolicy() | |
| == ThirdPartyLicenseExistencePolicy.ALWAYS_CHECK) { | |
| checkLicenses = true; | |
| } else if (pkgBuilder.getThirdPartyLicenseExistencePolicy() | |
| == ThirdPartyLicenseExistencePolicy.NEVER_CHECK) { | |
| checkLicenses = false; | |
| } else { | |
| checkLicenses = | |
| !thread | |
| .getSemantics() | |
| .getBool(BuildLanguageOptions.INCOMPATIBLE_DISABLE_THIRD_PARTY_LICENSE_CHECKING); | |
| } | |
| if (checkLicenses | |
| && license == null | |
| && !pkgBuilder.getDefaultLicense().isSpecified() | |
| && RuleClass.isThirdPartyPackage(pkgBuilder.getPackageIdentifier())) { | |
| throw Starlark.errorf( | |
| "third-party file '%s' lacks a license declaration with one of the following types:" | |
| + " notice, reciprocal, permissive, restricted, unencumbered, by_exception_only", | |
| inputFile.getName()); | |
| } | |
| pkgBuilder.setVisibilityAndLicense(inputFile, visibility, license); | |
| } catch (Package.Builder.GeneratedLabelConflict e) { | |
| throw Starlark.errorf("%s", e.getMessage()); | |
| } | |
| } | |
| return Starlark.NONE; | |
| } | |
| @Override | |
| public String packageName(StarlarkThread thread) throws EvalException { | |
| BazelStarlarkContext.from(thread).checkLoadingPhase("native.package_name"); | |
| PackageIdentifier packageId = | |
| PackageFactory.getContext(thread).getBuilder().getPackageIdentifier(); | |
| return packageId.getPackageFragment().getPathString(); | |
| } | |
| @Override | |
| public String repositoryName(StarlarkThread thread) throws EvalException { | |
| BazelStarlarkContext.from(thread).checkLoadingPhase("native.repository_name"); | |
| PackageIdentifier packageId = | |
| PackageFactory.getContext(thread).getBuilder().getPackageIdentifier(); | |
| return packageId.getRepository().toString(); | |
| } | |
| private static Dict<String, Object> getRuleDict(Rule rule, Mutability mu) throws EvalException { | |
| Dict.Builder<String, Object> values = Dict.builder(); | |
| for (Attribute attr : rule.getAttributes()) { | |
| if (!Character.isAlphabetic(attr.getName().charAt(0))) { | |
| continue; | |
| } | |
| if (attr.getName().equals("distribs")) { | |
| // attribute distribs: cannot represent type class java.util.Collections$SingletonSet | |
| // in Starlark: [INTERNAL]. | |
| continue; | |
| } | |
| try { | |
| Object val = starlarkifyValue(mu, rule.getAttr(attr.getName()), rule.getPackage()); | |
| if (val == null) { | |
| continue; | |
| } | |
| values.put(attr.getName(), val); | |
| } catch (NotRepresentableException e) { | |
| throw new NotRepresentableException( | |
| String.format( | |
| "target %s, attribute %s: %s", rule.getName(), attr.getName(), e.getMessage())); | |
| } | |
| } | |
| values.put("name", rule.getName()); | |
| values.put("kind", rule.getRuleClass()); | |
| return values.build(mu); | |
| } | |
| /** | |
| * Converts a target attribute value to a Starlark value for return in {@code | |
| * native.existing_rule()} or {@code native.existing_rules()}. | |
| * | |
| * <p>Any dict values in the result have mutability {@code mu}. | |
| * | |
| * @return the value, or null if we don't want to export it to the user. | |
| * @throws NotRepresentableException if an unknown type is encountered. | |
| */ | |
| @Nullable | |
| private static Object starlarkifyValue(Mutability mu, Object val, Package pkg) | |
| throws NotRepresentableException { | |
| // easy cases | |
| if (val == null | |
| || val instanceof Boolean | |
| || val instanceof String | |
| || val instanceof StarlarkInt) { | |
| return val; | |
| } | |
| if (val instanceof TriState) { | |
| switch ((TriState) val) { | |
| case AUTO: | |
| return StarlarkInt.of(-1); | |
| case YES: | |
| return StarlarkInt.of(1); | |
| case NO: | |
| return StarlarkInt.of(0); | |
| } | |
| } | |
| if (val instanceof Label) { | |
| Label l = (Label) val; | |
| if (l.getPackageName().equals(pkg.getName())) { | |
| return ":" + l.getName(); | |
| } | |
| return l.getCanonicalForm(); | |
| } | |
| if (val instanceof List) { | |
| List<Object> l = new ArrayList<>(); | |
| for (Object o : (List<?>) val) { | |
| Object elt = starlarkifyValue(mu, o, pkg); | |
| if (elt == null) { | |
| continue; | |
| } | |
| l.add(elt); | |
| } | |
| return Tuple.copyOf(l); | |
| } | |
| if (val instanceof Map) { | |
| Dict.Builder<Object, Object> m = Dict.builder(); | |
| for (Map.Entry<?, ?> e : ((Map<?, ?>) val).entrySet()) { | |
| Object key = starlarkifyValue(mu, e.getKey(), pkg); | |
| Object mapVal = starlarkifyValue(mu, e.getValue(), pkg); | |
| if (key == null || mapVal == null) { | |
| continue; | |
| } | |
| m.put(key, mapVal); | |
| } | |
| return m.build(mu); | |
| } | |
| if (val.getClass().isAnonymousClass()) { | |
| // Computed defaults. They will be represented as | |
| // "deprecation": com.google.devtools.build.lib.analysis.BaseRuleClasses$2@6960884a, | |
| // Filter them until we invent something more clever. | |
| return null; | |
| } | |
| if (val instanceof License) { | |
| // License is deprecated as a Starlark type, so omit this type from Starlark values | |
| // to avoid exposing these objects, even though they are technically StarlarkValue. | |
| return null; | |
| } | |
| if (val instanceof BuildType.SelectorList) { | |
| List<Object> selectors = new ArrayList<>(); | |
| for (BuildType.Selector<?> selector : ((BuildType.SelectorList<?>) val).getSelectors()) { | |
| selectors.add( | |
| new SelectorValue( | |
| ((Map<?, ?>) starlarkifyValue(mu, selector.getEntries(), pkg)), | |
| selector.getNoMatchError())); | |
| } | |
| try { | |
| return SelectorList.of(selectors); | |
| } catch (EvalException e) { | |
| throw new NotRepresentableException(e.getMessage()); | |
| } | |
| } | |
| if (val instanceof StarlarkValue) { | |
| return val; | |
| } | |
| // We are explicit about types we don't understand so we minimize changes to existing callers | |
| // if we add more types that we can represent. | |
| throw new NotRepresentableException( | |
| String.format("cannot represent %s (%s) in Starlark", val, val.getClass())); | |
| } | |
| private static class NotRepresentableException extends EvalException { | |
| NotRepresentableException(String msg) { | |
| super(msg); | |
| } | |
| } | |
| } |