Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 20 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
| 21 | import com.google.devtools.build.lib.events.Location; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.Path; |
| 23 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 24 | |
| 25 | /** |
| 26 | * A file that is an input to the build system. |
| 27 | * |
| 28 | * <p>In the build system, a file is considered an <i>input</i> file iff it is |
| 29 | * not generated by the build system (e.g. it's maintained under version |
| 30 | * control, or created by the test harness). It has nothing to do with the |
| 31 | * type of the file; a generated file containing <code>Java</code> source code |
| 32 | * is an OutputFile, not an InputFile. |
| 33 | */ |
| 34 | @Immutable @ThreadSafe |
| 35 | public final class InputFile extends FileTarget { |
| 36 | private final Location location; |
| 37 | private final RuleVisibility visibility; |
| 38 | private final License license; |
| 39 | |
| 40 | /** |
| 41 | * Constructs an input file with the given label, which must be a label for |
| 42 | * the given package, and package-default visibility. |
| 43 | */ |
| 44 | InputFile(Package pkg, Label label, Location location) { |
| 45 | this(pkg, label, location, null, License.NO_LICENSE); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Constructs an input file with the given label, which must be a label for the given package |
| 50 | * that was parsed from the specified location, and has the specified visibility. |
| 51 | */ |
| 52 | InputFile(Package pkg, Label label, Location location, RuleVisibility visibility, |
| 53 | License license) { |
| 54 | super(pkg, label); |
| 55 | Preconditions.checkNotNull(location); |
| 56 | this.location = location; |
| 57 | this.visibility = visibility; |
| 58 | this.license = license; |
| 59 | } |
| 60 | |
| 61 | public boolean isVisibilitySpecified() { |
| 62 | return visibility != null; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public RuleVisibility getVisibility() { |
| 67 | if (visibility != null) { |
| 68 | return visibility; |
| 69 | } else { |
| 70 | return pkg.getDefaultVisibility(); |
| 71 | } |
| 72 | } |
| 73 | |
Greg Estren | a6c8896 | 2015-09-28 19:35:18 +0000 | [diff] [blame] | 74 | @Override |
| 75 | public boolean isConfigurable() { |
| 76 | return false; |
| 77 | } |
| 78 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | public boolean isLicenseSpecified() { |
lberki | 0d8d4cf | 2017-09-05 16:01:44 +0200 | [diff] [blame] | 80 | return license != null && license.isSpecified(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public License getLicense() { |
| 85 | if (license != null) { |
| 86 | return license; |
| 87 | } else { |
| 88 | return pkg.getDefaultLicense(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the path to the location of the input file (which is necessarily |
| 94 | * within the source tree, not beneath <code>bin</code> or |
| 95 | * <code>genfiles</code>. |
| 96 | * |
| 97 | * <p>Prefer {@link #getExecPath} if possible. |
| 98 | */ |
| 99 | public Path getPath() { |
| 100 | return pkg.getPackageDirectory().getRelative(label.getName()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the exec path of the file, i.e. the path relative to the package source root. |
| 105 | */ |
| 106 | public PathFragment getExecPath() { |
Kristina Chodorow | a1a31ff | 2016-07-27 16:34:27 +0000 | [diff] [blame] | 107 | return label.getPackageIdentifier().getSourceRoot().getRelative(label.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 111 | public String getTargetKind() { |
Googler | b448eef | 2017-05-02 21:50:24 +0200 | [diff] [blame] | 112 | return targetKind(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Rule getAssociatedRule() { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Location getLocation() { |
| 122 | return location; |
| 123 | } |
Googler | b448eef | 2017-05-02 21:50:24 +0200 | [diff] [blame] | 124 | |
| 125 | /** Returns the target kind for all input files. */ |
| 126 | public static String targetKind() { |
| 127 | return "source file"; |
| 128 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | } |