blob: 6f2fe228095265649a05d4f2edd92b635f5aa717 [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
Lukacs Berki6e91eb92015-09-21 09:12:37 +000017import com.google.devtools.build.lib.cmdline.Label;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
19import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
20import com.google.devtools.build.lib.events.Location;
Mark Schaller6df81792015-12-10 18:47:47 +000021import com.google.devtools.build.lib.util.Preconditions;
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() {
80 return license != null && license != License.NO_LICENSE;
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 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() {
112 return "source file";
113 }
114
115 @Override
116 public Rule getAssociatedRule() {
117 return null;
118 }
119
120 @Override
121 public Location getLocation() {
122 return location;
123 }
124}