blob: 3667643cc6324c993dcfd428ee156a6fdb9094d3 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.packages;
16
tomlua155b532017-11-08 20:12:47 +010017import com.google.common.base.Preconditions;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000018import com.google.devtools.build.lib.cmdline.Label;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
20import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
21import com.google.devtools.build.lib.events.Location;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import com.google.devtools.build.lib.vfs.Path;
23import 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
35public 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 Estrena6c88962015-09-28 19:35:18 +000074 @Override
75 public boolean isConfigurable() {
76 return false;
77 }
78
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010079 public boolean isLicenseSpecified() {
lberki0d8d4cf2017-09-05 16:01:44 +020080 return license != null && license.isSpecified();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010081 }
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 Chodorowa1a31ff2016-07-27 16:34:27 +0000107 return label.getPackageIdentifier().getSourceRoot().getRelative(label.getName());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100108 }
109
110 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100111 public String getTargetKind() {
Googlerb448eef2017-05-02 21:50:24 +0200112 return targetKind();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100113 }
114
115 @Override
116 public Rule getAssociatedRule() {
117 return null;
118 }
119
120 @Override
121 public Location getLocation() {
122 return location;
123 }
Googlerb448eef2017-05-02 21:50:24 +0200124
125 /** Returns the target kind for all input files. */
126 public static String targetKind() {
127 return "source file";
128 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129}