blob: a09a4e69c46a697a2b625881a877ca7cd8316390 [file] [log] [blame]
vladmos360fb4d2017-04-11 11:14:22 +00001// Copyright 2017 The Bazel Authors. All rights reserved.
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.
14package com.google.devtools.build.lib.analysis;
15
vladmos360fb4d2017-04-11 11:14:22 +000016import com.google.devtools.build.lib.actions.Artifact;
adonovanbc74d1d2020-04-30 12:08:31 -070017import com.google.devtools.build.lib.collect.nestedset.Depset;
vladmos360fb4d2017-04-11 11:14:22 +000018import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
cparsonsdcd0abc2018-06-21 10:13:53 -070019import com.google.devtools.build.lib.packages.BuiltinProvider;
dslomovf1296572017-08-22 16:29:06 +020020import com.google.devtools.build.lib.packages.NativeInfo;
gregce59f5cba2020-07-22 12:18:43 -070021import com.google.devtools.build.lib.starlarkbuildapi.DefaultInfoApi;
cparsonsdcd0abc2018-06-21 10:13:53 -070022import javax.annotation.Nullable;
adonovan450c7ad2020-09-14 13:00:21 -070023import net.starlark.java.eval.EvalException;
24import net.starlark.java.eval.Starlark;
25import net.starlark.java.eval.StarlarkThread;
26import net.starlark.java.syntax.Location;
vladmos360fb4d2017-04-11 11:14:22 +000027
dslomov375f95b2017-08-21 12:52:41 +020028/** DefaultInfo is provided by all targets implicitly and contains all standard fields. */
vladmos360fb4d2017-04-11 11:14:22 +000029@Immutable
cparsonsdcd0abc2018-06-21 10:13:53 -070030public final class DefaultInfo extends NativeInfo implements DefaultInfoApi {
vladmos360fb4d2017-04-11 11:14:22 +000031
Googlerd21a0d12019-11-21 13:52:30 -080032 private final Depset files;
cparsonsdcd0abc2018-06-21 10:13:53 -070033 private final Runfiles runfiles;
34 private final Runfiles dataRunfiles;
35 private final Runfiles defaultRunfiles;
36 private final Artifact executable;
vladmosaa5e0602017-04-12 17:43:53 +000037 private final FilesToRunProvider filesToRunProvider;
vladmos360fb4d2017-04-11 11:14:22 +000038
cparsonsdcd0abc2018-06-21 10:13:53 -070039 /**
40 * Singleton instance of the provider type for {@link DefaultInfo}.
41 */
42 public static final DefaultInfoProvider PROVIDER = new DefaultInfoProvider();
dslomov3aa7d2f2017-04-11 17:55:55 +000043
dslomov375f95b2017-08-21 12:52:41 +020044 private DefaultInfo(
cparsonsdcd0abc2018-06-21 10:13:53 -070045 @Nullable RunfilesProvider runfilesProvider,
vladmosaa5e0602017-04-12 17:43:53 +000046 FileProvider fileProvider,
47 FilesToRunProvider filesToRunProvider) {
cparsonsdcd0abc2018-06-21 10:13:53 -070048 this(
49 Location.BUILTIN,
Googler1a6a5a12019-11-22 11:54:01 -080050 Depset.of(Artifact.TYPE, fileProvider.getFilesToBuild()),
cparsonsdcd0abc2018-06-21 10:13:53 -070051 Runfiles.EMPTY,
52 (runfilesProvider == null) ? Runfiles.EMPTY : runfilesProvider.getDataRunfiles(),
53 (runfilesProvider == null) ? Runfiles.EMPTY : runfilesProvider.getDefaultRunfiles(),
54 filesToRunProvider.getExecutable(),
Googlerd21a0d12019-11-21 13:52:30 -080055 filesToRunProvider);
cparsonsdcd0abc2018-06-21 10:13:53 -070056 }
57
58 private DefaultInfo(
59 Location loc,
Googlerd21a0d12019-11-21 13:52:30 -080060 Depset files,
61 Runfiles runfiles,
62 Runfiles dataRunfiles,
63 Runfiles defaultRunfiles,
64 Artifact executable,
cparsonsdcd0abc2018-06-21 10:13:53 -070065 @Nullable FilesToRunProvider filesToRunProvider) {
66 super(PROVIDER, loc);
67 this.files = files;
68 this.runfiles = runfiles;
69 this.dataRunfiles = dataRunfiles;
70 this.defaultRunfiles = defaultRunfiles;
71 this.executable = executable;
vladmosaa5e0602017-04-12 17:43:53 +000072 this.filesToRunProvider = filesToRunProvider;
vladmos360fb4d2017-04-11 11:14:22 +000073 }
74
dslomov375f95b2017-08-21 12:52:41 +020075 public static DefaultInfo build(
cparsonsdcd0abc2018-06-21 10:13:53 -070076 @Nullable RunfilesProvider runfilesProvider,
vladmos360fb4d2017-04-11 11:14:22 +000077 FileProvider fileProvider,
78 FilesToRunProvider filesToRunProvider) {
dslomovf1296572017-08-22 16:29:06 +020079 return new DefaultInfo(runfilesProvider, fileProvider, filesToRunProvider);
vladmosaa5e0602017-04-12 17:43:53 +000080 }
81
82 @Override
Googlerd21a0d12019-11-21 13:52:30 -080083 public Depset getFiles() {
cparsonsdcd0abc2018-06-21 10:13:53 -070084 return files;
vladmosaa5e0602017-04-12 17:43:53 +000085 }
vladmos360fb4d2017-04-11 11:14:22 +000086
vladmosaa5e0602017-04-12 17:43:53 +000087 @Override
Googler3e1bf5c2019-11-04 12:41:49 -080088 public FilesToRunProvider getFilesToRun() {
cparsonsdcd0abc2018-06-21 10:13:53 -070089 return filesToRunProvider;
90 }
91
92 /**
93 * Returns a set of runfiles acting as both the data runfiles and the default runfiles.
94 *
95 * This is kept for legacy reasons.
96 */
97 public Runfiles getStatelessRunfiles() {
98 return runfiles;
99 }
100
101 @Override
102 public Runfiles getDataRunfiles() {
103 return dataRunfiles;
104 }
105
106 @Override
107 public Runfiles getDefaultRunfiles() {
cparsonsfba7cfb2018-06-26 14:15:18 -0700108 if (dataRunfiles == null && defaultRunfiles == null) {
gregceca48e9a2020-04-14 08:54:38 -0700109 // This supports the legacy Starlark runfiles constructor -- if the 'runfiles' attribute
cparsonsfba7cfb2018-06-26 14:15:18 -0700110 // is used, then default_runfiles will return all runfiles.
111 return runfiles;
112 } else {
113 return defaultRunfiles;
114 }
cparsonsdcd0abc2018-06-21 10:13:53 -0700115 }
116
117 /**
118 * If the rule producing this info object is marked 'executable' or 'test', this is an artifact
119 * representing the file that should be executed to run the target. This is null otherwise.
120 */
121 public Artifact getExecutable() {
122 return executable;
123 }
124
125 /**
126 * Provider implementation for {@link DefaultInfoApi}.
127 */
128 public static class DefaultInfoProvider extends BuiltinProvider<DefaultInfo>
129 implements DefaultInfoApi.DefaultInfoApiProvider<Runfiles, Artifact> {
130 private DefaultInfoProvider() {
131 super("DefaultInfo", DefaultInfo.class);
132 }
133
134 @Override
adonovan7891d3b2020-01-22 12:40:50 -0800135 public DefaultInfo constructor(
136 Object files,
137 Object runfilesObj,
138 Object dataRunfilesObj,
139 Object defaultRunfilesObj,
140 Object executable,
141 StarlarkThread thread)
cparsonsdcd0abc2018-06-21 10:13:53 -0700142 throws EvalException {
143
144 Runfiles statelessRunfiles = castNoneToNull(Runfiles.class, runfilesObj);
145 Runfiles dataRunfiles = castNoneToNull(Runfiles.class, dataRunfilesObj);
146 Runfiles defaultRunfiles = castNoneToNull(Runfiles.class, defaultRunfilesObj);
147
148 if ((statelessRunfiles != null) && (dataRunfiles != null || defaultRunfiles != null)) {
adonovan7891d3b2020-01-22 12:40:50 -0800149 throw Starlark.errorf(
150 "Cannot specify the provider 'runfiles' together with 'data_runfiles' or"
151 + " 'default_runfiles'");
cparsonsdcd0abc2018-06-21 10:13:53 -0700152 }
153
154 return new DefaultInfo(
adonovan7891d3b2020-01-22 12:40:50 -0800155 thread.getCallerLocation(),
Googlerd21a0d12019-11-21 13:52:30 -0800156 castNoneToNull(Depset.class, files),
cparsonsdcd0abc2018-06-21 10:13:53 -0700157 statelessRunfiles,
158 dataRunfiles,
159 defaultRunfiles,
Googlerd21a0d12019-11-21 13:52:30 -0800160 castNoneToNull(Artifact.class, executable),
161 null);
cparsonsdcd0abc2018-06-21 10:13:53 -0700162 }
163 }
164
165 private static <T> T castNoneToNull(Class<T> clazz, Object value) {
Googler641bdf72019-11-12 10:32:26 -0800166 if (value == Starlark.NONE) {
cparsonsdcd0abc2018-06-21 10:13:53 -0700167 return null;
168 } else {
169 return clazz.cast(value);
170 }
vladmos360fb4d2017-04-11 11:14:22 +0000171 }
172}