vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.analysis; |
| 15 | |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableCollection; |
| 17 | import com.google.common.collect.ImmutableList; |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.devtools.build.lib.actions.Artifact; |
| 20 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.events.Location; |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 22 | import com.google.devtools.build.lib.packages.NativeInfo; |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.packages.NativeProvider; |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.syntax.SkylarkNestedSet; |
| 25 | import java.util.Map; |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 26 | import java.util.concurrent.atomic.AtomicReference; |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 27 | |
dslomov | 375f95b | 2017-08-21 12:52:41 +0200 | [diff] [blame] | 28 | /** DefaultInfo is provided by all targets implicitly and contains all standard fields. */ |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 29 | @Immutable |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 30 | public final class DefaultInfo extends NativeInfo { |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 31 | |
| 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"; |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 36 | 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<>(); |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 47 | |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 48 | public static final String SKYLARK_NAME = "DefaultInfo"; |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 49 | |
| 50 | // todo(dslomov,vladmos): make this provider return DefaultInfo. |
| 51 | public static final NativeProvider<NativeInfo> PROVIDER = |
| 52 | new NativeProvider<NativeInfo>(NativeInfo.class, SKYLARK_NAME) { |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 53 | @Override |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 54 | protected NativeInfo createInstanceFromSkylark(Object[] args, Location loc) { |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 55 | @SuppressWarnings("unchecked") |
| 56 | Map<String, Object> kwargs = (Map<String, Object>) args[0]; |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 57 | return new NativeInfo(this, kwargs, loc); |
dslomov | 3aa7d2f | 2017-04-11 17:55:55 +0000 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
dslomov | 375f95b | 2017-08-21 12:52:41 +0200 | [diff] [blame] | 61 | private DefaultInfo( |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 62 | RunfilesProvider runfilesProvider, |
| 63 | FileProvider fileProvider, |
| 64 | FilesToRunProvider filesToRunProvider) { |
| 65 | // Fields map is not used here to prevent memory regression |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 66 | super(PROVIDER, ImmutableMap.<String, Object>of()); |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 67 | this.runfilesProvider = runfilesProvider; |
| 68 | this.fileProvider = fileProvider; |
| 69 | this.filesToRunProvider = filesToRunProvider; |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 70 | } |
| 71 | |
dslomov | 375f95b | 2017-08-21 12:52:41 +0200 | [diff] [blame] | 72 | public static DefaultInfo build( |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 73 | RunfilesProvider runfilesProvider, |
| 74 | FileProvider fileProvider, |
| 75 | FilesToRunProvider filesToRunProvider) { |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 76 | return new DefaultInfo(runfilesProvider, fileProvider, filesToRunProvider); |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 77 | } |
| 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; |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 94 | } |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 95 | return null; |
| 96 | } |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 97 | |
vladmos | aa5e060 | 2017-04-12 17:43:53 +0000 | [diff] [blame] | 98 | @Override |
| 99 | public ImmutableCollection<String> getKeys() { |
| 100 | return KEYS; |
vladmos | 360fb4d | 2017-04-11 11:14:22 +0000 | [diff] [blame] | 101 | } |
| 102 | } |