Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Googler | d1c8061 | 2015-03-23 13:08:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.packages; |
| 16 | |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.packages.PackageFactory.getContext; |
| 18 | |
| 19 | import com.google.common.base.Joiner; |
| 20 | import com.google.common.base.Preconditions; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import com.google.devtools.build.lib.cmdline.Label; |
| 24 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
| 25 | import com.google.devtools.build.lib.cmdline.LabelValidator; |
laurentlb | d6443d2 | 2018-02-26 05:55:21 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.events.Event; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.packages.Globber.BadGlobException; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.PackageFactory.PackageContext; |
| 30 | import com.google.devtools.build.lib.packages.RuleClass.Builder.ThirdPartyLicenseExistencePolicy; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.Type.ConversionException; |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.skylarkbuildapi.StarlarkNativeModuleApi; |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 33 | import com.google.devtools.build.lib.syntax.Dict; |
Googler | d1c8061 | 2015-03-23 13:08:09 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.syntax.EvalException; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.syntax.EvalUtils; |
adonovan | f5262c5 | 2020-04-02 09:25:14 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.syntax.Location; |
Googler | 48b446e | 2019-11-07 11:31:22 -0800 | [diff] [blame] | 37 | import com.google.devtools.build.lib.syntax.Mutability; |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 38 | import com.google.devtools.build.lib.syntax.NoneType; |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 39 | import com.google.devtools.build.lib.syntax.Sequence; |
Googler | 2e63b2a | 2019-11-11 08:59:12 -0800 | [diff] [blame] | 40 | import com.google.devtools.build.lib.syntax.Starlark; |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 41 | import com.google.devtools.build.lib.syntax.StarlarkList; |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.syntax.StarlarkThread; |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 43 | import com.google.devtools.build.lib.syntax.StarlarkValue; |
Googler | cfd681f | 2019-11-11 07:24:02 -0800 | [diff] [blame] | 44 | import com.google.devtools.build.lib.syntax.Tuple; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 45 | import java.io.IOException; |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.Collection; |
Benjamin Peterson | 3b39e1e | 2020-01-31 10:21:38 -0800 | [diff] [blame] | 48 | import java.util.Comparator; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 49 | import java.util.List; |
| 50 | import java.util.Map; |
| 51 | import java.util.TreeMap; |
| 52 | import javax.annotation.Nullable; |
Googler | d1c8061 | 2015-03-23 13:08:09 +0000 | [diff] [blame] | 53 | |
gregce | ad752cc | 2020-04-10 10:32:59 -0700 | [diff] [blame] | 54 | /** The Starlark native module. */ |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 55 | // TODO(cparsons): Move the definition of native.package() to this class. |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 56 | public class StarlarkNativeModule implements StarlarkNativeModuleApi { |
Googler | d1c8061 | 2015-03-23 13:08:09 +0000 | [diff] [blame] | 57 | |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 58 | /** |
| 59 | * This map contains all the (non-rule) functions of the native module (keyed by their symbol |
| 60 | * name). These native module bindings should be added (without the 'native' module namespace) to |
| 61 | * the global Starlark environment for BUILD files. |
| 62 | * |
| 63 | * <p>For example, the function "glob" is available under both a global symbol name {@code glob()} |
| 64 | * as well as under the native module namepsace {@code native.glob()}. An entry of this map is |
| 65 | * thus ("glob" : glob function). |
| 66 | */ |
| 67 | public static final ImmutableMap<String, Object> BINDINGS_FOR_BUILD_FILES = initializeBindings(); |
| 68 | |
| 69 | private static ImmutableMap<String, Object> initializeBindings() { |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 70 | ImmutableMap.Builder<String, Object> bindings = ImmutableMap.builder(); |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 71 | Starlark.addMethods(bindings, new StarlarkNativeModule()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 72 | return bindings.build(); |
| 73 | } |
| 74 | |
cparsons | 30d877a | 2018-05-07 05:58:01 -0700 | [diff] [blame] | 75 | @Override |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 76 | public Sequence<?> glob( |
| 77 | Sequence<?> include, |
| 78 | Sequence<?> exclude, |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 79 | Integer excludeDirs, |
| 80 | Object allowEmptyArgument, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 81 | StarlarkThread thread) |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 82 | throws EvalException, ConversionException, InterruptedException { |
adonovan | b23b8c7 | 2019-12-12 11:42:14 -0800 | [diff] [blame] | 83 | BazelStarlarkContext.from(thread).checkLoadingPhase("native.glob"); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 84 | PackageContext context = getContext(thread); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 85 | |
| 86 | List<String> includes = Type.STRING_LIST.convert(include, "'glob' argument"); |
| 87 | List<String> excludes = Type.STRING_LIST.convert(exclude, "'glob' argument"); |
| 88 | |
| 89 | List<String> matches; |
| 90 | boolean allowEmpty; |
Googler | f7e471b | 2019-11-11 10:10:07 -0800 | [diff] [blame] | 91 | if (allowEmptyArgument == Starlark.UNBOUND) { |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 92 | allowEmpty = !thread.getSemantics().incompatibleDisallowEmptyGlob(); |
| 93 | } else if (allowEmptyArgument instanceof Boolean) { |
| 94 | allowEmpty = (Boolean) allowEmptyArgument; |
| 95 | } else { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 96 | throw Starlark.errorf( |
| 97 | "expected boolean for argument `allow_empty`, got `%s`", allowEmptyArgument); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 98 | } |
| 99 | |
nharmata | 5e025a1 | 2019-06-28 09:58:13 -0700 | [diff] [blame] | 100 | try { |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 101 | Globber.Token globToken = |
| 102 | context.globber.runAsync(includes, excludes, excludeDirs != 0, allowEmpty); |
Benjamin Peterson | 3b39e1e | 2020-01-31 10:21:38 -0800 | [diff] [blame] | 103 | matches = context.globber.fetchUnsorted(globToken); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 104 | } catch (IOException e) { |
| 105 | String errorMessage = |
| 106 | String.format( |
| 107 | "error globbing [%s]%s: %s", |
| 108 | Joiner.on(", ").join(includes), |
| 109 | excludes.isEmpty() ? "" : " - [" + Joiner.on(", ").join(excludes) + "]", |
| 110 | e.getMessage()); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 111 | Location loc = thread.getCallerLocation(); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 112 | context.eventHandler.handle(Event.error(loc, errorMessage)); |
| 113 | context.pkgBuilder.setIOExceptionAndMessage(e, errorMessage); |
| 114 | matches = ImmutableList.of(); |
| 115 | } catch (BadGlobException e) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 116 | throw new EvalException(null, e.getMessage()); |
nharmata | 5e025a1 | 2019-06-28 09:58:13 -0700 | [diff] [blame] | 117 | } |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 118 | |
Benjamin Peterson | 3b39e1e | 2020-01-31 10:21:38 -0800 | [diff] [blame] | 119 | ArrayList<String> result = new ArrayList<>(matches.size()); |
| 120 | for (String match : matches) { |
| 121 | if (match.charAt(0) == '@') { |
| 122 | // Add explicit colon to disambiguate from external repository. |
| 123 | match = ":" + match; |
| 124 | } |
| 125 | result.add(match); |
| 126 | } |
| 127 | result.sort(Comparator.naturalOrder()); |
| 128 | |
| 129 | return StarlarkList.copyOf(thread.mutability(), result); |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 130 | } |
Han-Wen Nienhuys | 67e6f98 | 2016-01-15 15:54:12 +0000 | [diff] [blame] | 131 | |
cparsons | 30d877a | 2018-05-07 05:58:01 -0700 | [diff] [blame] | 132 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 133 | public Object existingRule(String name, StarlarkThread thread) |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 134 | throws EvalException, InterruptedException { |
adonovan | b23b8c7 | 2019-12-12 11:42:14 -0800 | [diff] [blame] | 135 | BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("native.existing_rule"); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 136 | PackageContext context = getContext(thread); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 137 | Target target = context.pkgBuilder.getTarget(name); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 138 | Dict<String, Object> rule = targetDict(target, thread.mutability()); |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 139 | return rule != null ? rule : Starlark.NONE; |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 140 | } |
Han-Wen Nienhuys | de435f4 | 2016-01-28 12:35:13 +0000 | [diff] [blame] | 141 | |
Han-Wen Nienhuys | 67e6f98 | 2016-01-15 15:54:12 +0000 | [diff] [blame] | 142 | /* |
| 143 | If necessary, we could allow filtering by tag (anytag, alltags), name (regexp?), kind ? |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 144 | For now, we ignore this, since users can implement it in Starlark. |
Han-Wen Nienhuys | 67e6f98 | 2016-01-15 15:54:12 +0000 | [diff] [blame] | 145 | */ |
cparsons | 30d877a | 2018-05-07 05:58:01 -0700 | [diff] [blame] | 146 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 147 | public Dict<String, Dict<String, Object>> existingRules(StarlarkThread thread) |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 148 | throws EvalException, InterruptedException { |
adonovan | b23b8c7 | 2019-12-12 11:42:14 -0800 | [diff] [blame] | 149 | BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("native.existing_rules"); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 150 | PackageContext context = getContext(thread); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 151 | Collection<Target> targets = context.pkgBuilder.getTargets(); |
Googler | bd5bc96 | 2019-11-08 14:21:14 -0800 | [diff] [blame] | 152 | Mutability mu = thread.mutability(); |
Googler | 6456fcb | 2019-11-22 13:05:42 -0800 | [diff] [blame] | 153 | Dict<String, Dict<String, Object>> rules = Dict.of(mu); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 154 | for (Target t : targets) { |
| 155 | if (t instanceof Rule) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 156 | Dict<String, Object> rule = targetDict(t, mu); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 157 | Preconditions.checkNotNull(rule); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 158 | rules.put(t.getName(), rule, (Location) null); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | return rules; |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 163 | } |
Googler | d1c8061 | 2015-03-23 13:08:09 +0000 | [diff] [blame] | 164 | |
cparsons | 30d877a | 2018-05-07 05:58:01 -0700 | [diff] [blame] | 165 | @Override |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 166 | public NoneType packageGroup( |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 167 | String name, |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 168 | Sequence<?> packagesO, |
| 169 | Sequence<?> includesO, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 170 | StarlarkThread thread) |
| 171 | throws EvalException { |
adonovan | b23b8c7 | 2019-12-12 11:42:14 -0800 | [diff] [blame] | 172 | BazelStarlarkContext.from(thread).checkLoadingPhase("native.package_group"); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 173 | PackageContext context = getContext(thread); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 174 | |
| 175 | List<String> packages = |
| 176 | Type.STRING_LIST.convert(packagesO, "'package_group.packages argument'"); |
| 177 | List<Label> includes = |
| 178 | BuildType.LABEL_LIST.convert( |
| 179 | includesO, "'package_group.includes argument'", context.pkgBuilder.getBuildFileLabel()); |
| 180 | |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 181 | Location loc = thread.getCallerLocation(); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 182 | try { |
| 183 | context.pkgBuilder.addPackageGroup(name, packages, includes, context.eventHandler, loc); |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 184 | return Starlark.NONE; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 185 | } catch (LabelSyntaxException e) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 186 | throw Starlark.errorf("package group has invalid name: %s: %s", name, e.getMessage()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 187 | } catch (Package.NameConflictException e) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 188 | throw new EvalException(null, e.getMessage()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 189 | } |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 190 | } |
Googler | 1048057 | 2015-03-24 11:08:13 +0000 | [diff] [blame] | 191 | |
cparsons | 30d877a | 2018-05-07 05:58:01 -0700 | [diff] [blame] | 192 | @Override |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 193 | public NoneType exportsFiles( |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 194 | Sequence<?> srcs, Object visibilityO, Object licensesO, StarlarkThread thread) |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 195 | throws EvalException { |
adonovan | b23b8c7 | 2019-12-12 11:42:14 -0800 | [diff] [blame] | 196 | BazelStarlarkContext.from(thread).checkLoadingPhase("native.exports_files"); |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 197 | Package.Builder pkgBuilder = getContext(thread).pkgBuilder; |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 198 | List<String> files = Type.STRING_LIST.convert(srcs, "'exports_files' operand"); |
| 199 | |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 200 | RuleVisibility visibility = |
| 201 | EvalUtils.isNullOrNone(visibilityO) |
| 202 | ? ConstantRuleVisibility.PUBLIC |
michajlo | cab889d | 2020-02-20 17:48:36 -0800 | [diff] [blame] | 203 | : PackageUtils.getVisibility( |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 204 | pkgBuilder.getBuildFileLabel(), |
| 205 | BuildType.LABEL_LIST.convert( |
| 206 | visibilityO, "'exports_files' operand", pkgBuilder.getBuildFileLabel())); |
| 207 | |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 208 | // TODO(bazel-team): is licenses plural or singular? |
| 209 | License license = BuildType.LICENSE.convertOptional(licensesO, "'exports_files' operand"); |
| 210 | |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 211 | Location loc = thread.getCallerLocation(); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 212 | for (String file : files) { |
| 213 | String errorMessage = LabelValidator.validateTargetName(file); |
| 214 | if (errorMessage != null) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 215 | throw Starlark.errorf("%s", errorMessage); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 216 | } |
| 217 | try { |
| 218 | InputFile inputFile = pkgBuilder.createInputFile(file, loc); |
| 219 | if (inputFile.isVisibilitySpecified() && inputFile.getVisibility() != visibility) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 220 | throw Starlark.errorf( |
| 221 | "visibility for exported file '%s' declared twice", inputFile.getName()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 222 | } |
| 223 | if (license != null && inputFile.isLicenseSpecified()) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 224 | throw Starlark.errorf( |
| 225 | "licenses for exported file '%s' declared twice", inputFile.getName()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // See if we should check third-party licenses: first checking for any hard-coded policy, |
| 229 | // then falling back to user-settable flags. |
| 230 | boolean checkLicenses; |
| 231 | if (pkgBuilder.getThirdPartyLicenseExistencePolicy() |
| 232 | == ThirdPartyLicenseExistencePolicy.ALWAYS_CHECK) { |
| 233 | checkLicenses = true; |
| 234 | } else if (pkgBuilder.getThirdPartyLicenseExistencePolicy() |
| 235 | == ThirdPartyLicenseExistencePolicy.NEVER_CHECK) { |
| 236 | checkLicenses = false; |
| 237 | } else { |
| 238 | checkLicenses = !thread.getSemantics().incompatibleDisableThirdPartyLicenseChecking(); |
| 239 | } |
| 240 | |
| 241 | if (checkLicenses |
| 242 | && license == null |
| 243 | && !pkgBuilder.getDefaultLicense().isSpecified() |
| 244 | && RuleClass.isThirdPartyPackage(pkgBuilder.getPackageIdentifier())) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 245 | throw Starlark.errorf( |
| 246 | "third-party file '%s' lacks a license declaration with one of the following types:" |
| 247 | + " notice, reciprocal, permissive, restricted, unencumbered, by_exception_only", |
| 248 | inputFile.getName()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | pkgBuilder.setVisibilityAndLicense(inputFile, visibility, license); |
| 252 | } catch (Package.Builder.GeneratedLabelConflict e) { |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 253 | throw Starlark.errorf("%s", e.getMessage()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 256 | return Starlark.NONE; |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 257 | } |
Googler | 1048057 | 2015-03-24 11:08:13 +0000 | [diff] [blame] | 258 | |
cparsons | 30d877a | 2018-05-07 05:58:01 -0700 | [diff] [blame] | 259 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 260 | public String packageName(StarlarkThread thread) throws EvalException { |
adonovan | b23b8c7 | 2019-12-12 11:42:14 -0800 | [diff] [blame] | 261 | BazelStarlarkContext.from(thread).checkLoadingPhase("native.package_name"); |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 262 | PackageIdentifier packageId = |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 263 | PackageFactory.getContext(thread).getBuilder().getPackageIdentifier(); |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 264 | return packageId.getPackageFragment().getPathString(); |
| 265 | } |
laurentlb | ba51cda | 2017-05-04 20:19:16 +0200 | [diff] [blame] | 266 | |
cparsons | 30d877a | 2018-05-07 05:58:01 -0700 | [diff] [blame] | 267 | @Override |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 268 | public String repositoryName(StarlarkThread thread) throws EvalException { |
adonovan | b23b8c7 | 2019-12-12 11:42:14 -0800 | [diff] [blame] | 269 | BazelStarlarkContext.from(thread).checkLoadingPhase("native.repository_name"); |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 270 | PackageIdentifier packageId = |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 271 | PackageFactory.getContext(thread).getBuilder().getPackageIdentifier(); |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 272 | return packageId.getRepository().toString(); |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 273 | } |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 274 | |
| 275 | @Nullable |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 276 | private static Dict<String, Object> targetDict(Target target, Mutability mu) |
Googler | bd5bc96 | 2019-11-08 14:21:14 -0800 | [diff] [blame] | 277 | throws EvalException { |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 278 | if (!(target instanceof Rule)) { |
| 279 | return null; |
| 280 | } |
Googler | 6456fcb | 2019-11-22 13:05:42 -0800 | [diff] [blame] | 281 | Dict<String, Object> values = Dict.of(mu); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 282 | |
| 283 | Rule rule = (Rule) target; |
| 284 | AttributeContainer cont = rule.getAttributeContainer(); |
| 285 | for (Attribute attr : rule.getAttributes()) { |
| 286 | if (!Character.isAlphabetic(attr.getName().charAt(0))) { |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (attr.getName().equals("distribs")) { |
| 291 | // attribute distribs: cannot represent type class java.util.Collections$SingletonSet |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 292 | // in Starlark: [INTERNAL]. |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 293 | continue; |
| 294 | } |
| 295 | |
| 296 | try { |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 297 | Object val = starlarkifyValue(mu, cont.getAttr(attr.getName()), target.getPackage()); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 298 | if (val == null) { |
| 299 | continue; |
| 300 | } |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 301 | values.put(attr.getName(), val, (Location) null); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 302 | } catch (NotRepresentableException e) { |
| 303 | throw new NotRepresentableException( |
| 304 | String.format( |
| 305 | "target %s, attribute %s: %s", target.getName(), attr.getName(), e.getMessage())); |
| 306 | } |
| 307 | } |
| 308 | |
adonovan | 7891d3b | 2020-01-22 12:40:50 -0800 | [diff] [blame] | 309 | values.put("name", rule.getName(), (Location) null); |
| 310 | values.put("kind", rule.getRuleClass(), (Location) null); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 311 | return values; |
| 312 | } |
| 313 | |
| 314 | /** |
Googler | 48b446e | 2019-11-07 11:31:22 -0800 | [diff] [blame] | 315 | * Converts a target attribute value to a Starlark value for return in {@code |
| 316 | * native.existing_rule()} or {@code native.existing_rules()}. |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 317 | * |
Googler | bd5bc96 | 2019-11-08 14:21:14 -0800 | [diff] [blame] | 318 | * <p>Any dict values in the result have mutability {@code mu}. |
Googler | 48b446e | 2019-11-07 11:31:22 -0800 | [diff] [blame] | 319 | * |
| 320 | * @return the value, or null if we don't want to export it to the user. |
| 321 | * @throws NotRepresentableException if an unknown type is encountered. |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 322 | */ |
| 323 | @Nullable |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 324 | private static Object starlarkifyValue(Mutability mu, Object val, Package pkg) |
Googler | bd5bc96 | 2019-11-08 14:21:14 -0800 | [diff] [blame] | 325 | throws NotRepresentableException { |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 326 | if (val == null) { |
| 327 | return null; |
| 328 | } |
| 329 | if (val instanceof Boolean) { |
| 330 | return val; |
| 331 | } |
| 332 | if (val instanceof Integer) { |
| 333 | return val; |
| 334 | } |
| 335 | if (val instanceof String) { |
| 336 | return val; |
| 337 | } |
| 338 | |
| 339 | if (val instanceof TriState) { |
| 340 | switch ((TriState) val) { |
| 341 | case AUTO: |
| 342 | return -1; |
| 343 | case YES: |
| 344 | return 1; |
| 345 | case NO: |
| 346 | return 0; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (val instanceof Label) { |
| 351 | Label l = (Label) val; |
| 352 | if (l.getPackageName().equals(pkg.getName())) { |
| 353 | return ":" + l.getName(); |
| 354 | } |
| 355 | return l.getCanonicalForm(); |
| 356 | } |
| 357 | |
| 358 | if (val instanceof List) { |
| 359 | List<Object> l = new ArrayList<>(); |
| 360 | for (Object o : (List) val) { |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 361 | Object elt = starlarkifyValue(mu, o, pkg); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 362 | if (elt == null) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | l.add(elt); |
| 367 | } |
| 368 | |
Googler | cfd681f | 2019-11-11 07:24:02 -0800 | [diff] [blame] | 369 | return Tuple.copyOf(l); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 370 | } |
| 371 | if (val instanceof Map) { |
| 372 | Map<Object, Object> m = new TreeMap<>(); |
| 373 | for (Map.Entry<?, ?> e : ((Map<?, ?>) val).entrySet()) { |
gregce | d281df7 | 2020-05-11 12:27:06 -0700 | [diff] [blame] | 374 | Object key = starlarkifyValue(mu, e.getKey(), pkg); |
| 375 | Object mapVal = starlarkifyValue(mu, e.getValue(), pkg); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 376 | |
| 377 | if (key == null || mapVal == null) { |
| 378 | continue; |
| 379 | } |
| 380 | |
| 381 | m.put(key, mapVal); |
| 382 | } |
Googler | 084b64b | 2019-11-19 14:41:30 -0800 | [diff] [blame] | 383 | return Starlark.fromJava(m, mu); |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 384 | } |
| 385 | if (val.getClass().isAnonymousClass()) { |
| 386 | // Computed defaults. They will be represented as |
| 387 | // "deprecation": com.google.devtools.build.lib.analysis.BaseRuleClasses$2@6960884a, |
| 388 | // Filter them until we invent something more clever. |
| 389 | return null; |
| 390 | } |
| 391 | |
| 392 | if (val instanceof License) { |
| 393 | // License is deprecated as a Starlark type, so omit this type from Starlark values |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 394 | // to avoid exposing these objects, even though they are technically StarlarkValue. |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 395 | return null; |
| 396 | } |
| 397 | |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 398 | if (val instanceof StarlarkValue) { |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 399 | return val; |
| 400 | } |
| 401 | |
| 402 | if (val instanceof BuildType.SelectorList) { |
| 403 | // This is terrible: |
| 404 | // 1) this value is opaque, and not a BUILD value, so it cannot be used in rule arguments |
| 405 | // 2) its representation has a pointer address, so it breaks hermeticity. |
| 406 | // |
| 407 | // Even though this is clearly imperfect, we return this value because otherwise |
| 408 | // native.rules() fails if there is any rule using a select() in the BUILD file. |
| 409 | // |
adonovan | d8ee69a | 2020-01-28 11:47:54 -0800 | [diff] [blame] | 410 | // To remedy this, we should return a SelectorList. To do so, we have to |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 411 | // 1) recurse into the Selector contents of SelectorList, so those values are Starlarkified |
| 412 | // too |
cparsons | d6cc76d | 2019-10-31 15:04:48 -0700 | [diff] [blame] | 413 | // 2) get the right Class<?> value. We could probably get at that by looking at |
| 414 | // ((SelectorList)val).getSelectors().first().getEntries().first().getClass(). |
| 415 | |
| 416 | return val; |
| 417 | } |
| 418 | |
| 419 | // We are explicit about types we don't understand so we minimize changes to existing callers |
| 420 | // if we add more types that we can represent. |
| 421 | throw new NotRepresentableException( |
| 422 | String.format("cannot represent %s (%s) in Starlark", val, val.getClass())); |
| 423 | } |
michajlo | e2fce44 | 2020-02-20 16:59:00 -0800 | [diff] [blame] | 424 | |
| 425 | private static class NotRepresentableException extends EvalException { |
| 426 | NotRepresentableException(String msg) { |
| 427 | super(null, msg); |
| 428 | } |
| 429 | } |
Googler | d1c8061 | 2015-03-23 13:08:09 +0000 | [diff] [blame] | 430 | } |