blob: a9b54617592a09571e831c9b0c6f435bd28d7de2 [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
vladmosaa5e0602017-04-12 17:43:53 +000016import com.google.common.collect.ImmutableCollection;
17import com.google.common.collect.ImmutableList;
vladmos360fb4d2017-04-11 11:14:22 +000018import com.google.common.collect.ImmutableMap;
19import com.google.devtools.build.lib.actions.Artifact;
20import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
dslomov3aa7d2f2017-04-11 17:55:55 +000021import com.google.devtools.build.lib.events.Location;
dslomovf1296572017-08-22 16:29:06 +020022import com.google.devtools.build.lib.packages.NativeInfo;
dslomovde965ac2017-07-31 21:07:51 +020023import com.google.devtools.build.lib.packages.NativeProvider;
vladmos360fb4d2017-04-11 11:14:22 +000024import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
25import java.util.Map;
vladmosaa5e0602017-04-12 17:43:53 +000026import java.util.concurrent.atomic.AtomicReference;
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
dslomovf1296572017-08-22 16:29:06 +020030public final class DefaultInfo extends NativeInfo {
vladmos360fb4d2017-04-11 11:14:22 +000031
32 // Accessors for Skylark
33 private static final String DATA_RUNFILES_FIELD = "data_runfiles";
34 private static final String DEFAULT_RUNFILES_FIELD = "default_runfiles";
35 private static final String FILES_FIELD = "files";
vladmosaa5e0602017-04-12 17:43:53 +000036 private static final ImmutableList<String> KEYS =
37 ImmutableList.of(
38 DATA_RUNFILES_FIELD,
39 DEFAULT_RUNFILES_FIELD,
40 FILES_FIELD,
41 FilesToRunProvider.SKYLARK_NAME);
42
43 private final RunfilesProvider runfilesProvider;
44 private final FileProvider fileProvider;
45 private final FilesToRunProvider filesToRunProvider;
46 private final AtomicReference<SkylarkNestedSet> files = new AtomicReference<>();
vladmos360fb4d2017-04-11 11:14:22 +000047
dslomov3aa7d2f2017-04-11 17:55:55 +000048 public static final String SKYLARK_NAME = "DefaultInfo";
dslomovf1296572017-08-22 16:29:06 +020049
50 // todo(dslomov,vladmos): make this provider return DefaultInfo.
51 public static final NativeProvider<NativeInfo> PROVIDER =
52 new NativeProvider<NativeInfo>(NativeInfo.class, SKYLARK_NAME) {
dslomov3aa7d2f2017-04-11 17:55:55 +000053 @Override
dslomovf1296572017-08-22 16:29:06 +020054 protected NativeInfo createInstanceFromSkylark(Object[] args, Location loc) {
dslomov3aa7d2f2017-04-11 17:55:55 +000055 @SuppressWarnings("unchecked")
56 Map<String, Object> kwargs = (Map<String, Object>) args[0];
dslomovf1296572017-08-22 16:29:06 +020057 return new NativeInfo(this, kwargs, loc);
dslomov3aa7d2f2017-04-11 17:55:55 +000058 }
59 };
60
dslomov375f95b2017-08-21 12:52:41 +020061 private DefaultInfo(
vladmosaa5e0602017-04-12 17:43:53 +000062 RunfilesProvider runfilesProvider,
63 FileProvider fileProvider,
64 FilesToRunProvider filesToRunProvider) {
65 // Fields map is not used here to prevent memory regression
dslomovf1296572017-08-22 16:29:06 +020066 super(PROVIDER, ImmutableMap.<String, Object>of());
vladmosaa5e0602017-04-12 17:43:53 +000067 this.runfilesProvider = runfilesProvider;
68 this.fileProvider = fileProvider;
69 this.filesToRunProvider = filesToRunProvider;
vladmos360fb4d2017-04-11 11:14:22 +000070 }
71
dslomov375f95b2017-08-21 12:52:41 +020072 public static DefaultInfo build(
vladmos360fb4d2017-04-11 11:14:22 +000073 RunfilesProvider runfilesProvider,
74 FileProvider fileProvider,
75 FilesToRunProvider filesToRunProvider) {
dslomovf1296572017-08-22 16:29:06 +020076 return new DefaultInfo(runfilesProvider, fileProvider, filesToRunProvider);
vladmosaa5e0602017-04-12 17:43:53 +000077 }
78
79 @Override
80 public Object getValue(String name) {
81 switch (name) {
82 case DATA_RUNFILES_FIELD:
83 return (runfilesProvider == null) ? Runfiles.EMPTY : runfilesProvider.getDataRunfiles();
84 case DEFAULT_RUNFILES_FIELD:
85 return (runfilesProvider == null) ? Runfiles.EMPTY : runfilesProvider.getDefaultRunfiles();
86 case FILES_FIELD:
87 if (files.get() == null) {
88 files.compareAndSet(
89 null, SkylarkNestedSet.of(Artifact.class, fileProvider.getFilesToBuild()));
90 }
91 return files.get();
92 case FilesToRunProvider.SKYLARK_NAME:
93 return filesToRunProvider;
vladmos360fb4d2017-04-11 11:14:22 +000094 }
vladmosaa5e0602017-04-12 17:43:53 +000095 return null;
96 }
vladmos360fb4d2017-04-11 11:14:22 +000097
vladmosaa5e0602017-04-12 17:43:53 +000098 @Override
99 public ImmutableCollection<String> getKeys() {
100 return KEYS;
vladmos360fb4d2017-04-11 11:14:22 +0000101 }
102}