blob: a2339649bf16ab0f15875fb557ca7811cc135bcc [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.pkgcache;
16
dbabkinc885e282018-08-29 02:12:12 -070017
Lukacs Berki50a705c2015-09-16 10:08:55 +000018import com.google.common.base.Splitter;
19import com.google.common.base.Strings;
20import com.google.common.collect.ImmutableList;
Brian Silvermand7d6d622016-03-17 09:53:39 +000021import com.google.common.collect.ImmutableSet;
jmmvfd6f8332020-03-30 13:35:18 -070022import com.google.devtools.build.lib.actions.LocalHostCapacity;
Lukacs Berki50a705c2015-09-16 10:08:55 +000023import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
Kristina Chodorow73fa2032015-08-28 17:57:46 +000024import com.google.devtools.build.lib.cmdline.PackageIdentifier;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import com.google.devtools.build.lib.packages.ConstantRuleVisibility;
26import com.google.devtools.build.lib.packages.RuleVisibility;
jmmvfd6f8332020-03-30 13:35:18 -070027import com.google.devtools.build.lib.util.ResourceConverter;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028import com.google.devtools.common.options.Converter;
29import com.google.devtools.common.options.Converters;
30import com.google.devtools.common.options.Option;
ccalvarin3bc15472017-06-27 17:58:35 +020031import com.google.devtools.common.options.OptionDocumentationCategory;
ccalvarinc82a1972017-07-17 21:13:39 +020032import com.google.devtools.common.options.OptionEffectTag;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033import com.google.devtools.common.options.OptionsBase;
34import com.google.devtools.common.options.OptionsParsingException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035import java.util.List;
36
ajurkowskid74b0ec2020-04-13 10:58:21 -070037/** Options for configuring Packages -- loading and default behaviors. */
38public class PackageOptions extends OptionsBase {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039
40 /**
41 * Converter for the {@code --default_visibility} option.
42 */
43 public static class DefaultVisibilityConverter implements Converter<RuleVisibility> {
44 @Override
45 public RuleVisibility convert(String input) throws OptionsParsingException {
46 if (input.equals("public")) {
47 return ConstantRuleVisibility.PUBLIC;
48 } else if (input.equals("private")) {
49 return ConstantRuleVisibility.PRIVATE;
50 } else {
51 throw new OptionsParsingException("Not a valid default visibility: '" + input
52 + "' (should be 'public' or 'private'");
53 }
54 }
55
56 @Override
57 public String getTypeDescription() {
58 return "default visibility";
59 }
60 }
61
jmmvfd6f8332020-03-30 13:35:18 -070062 /** Converter for globbing threads. */
63 public static class ParallelismConverter extends ResourceConverter {
64 public ParallelismConverter() throws OptionsParsingException {
65 super(
66 /* autoSupplier= */ () ->
67 (int) Math.ceil(LocalHostCapacity.getLocalHostCapacity().getCpuUsage()),
68 /* minValue= */ 1,
69 /* maxValue= */ Integer.MAX_VALUE);
70 }
71 }
72
ccalvarin2eaa02e2017-04-17 23:37:46 +020073 @Option(
Dan Fabulich1d35ca02018-07-05 16:08:06 -070074 name = "package_path",
75 defaultValue = "%workspace%",
76 converter = Converters.ColonSeparatedOptionListConverter.class,
77 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
78 effectTags = {OptionEffectTag.UNKNOWN},
79 help =
80 "A colon-separated list of where to look for packages. "
81 + "Elements beginning with '%workspace%' are relative to the enclosing "
82 + "workspace. If omitted or empty, the default is the output of "
83 + "'bazel info default-package-path'.")
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084 public List<String> packagePath;
85
ccalvarin2eaa02e2017-04-17 23:37:46 +020086 @Option(
Dan Fabulich1d35ca02018-07-05 16:08:06 -070087 name = "show_loading_progress",
88 defaultValue = "true",
89 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
90 effectTags = {OptionEffectTag.UNKNOWN},
91 help = "If enabled, causes Bazel to print \"Loading package:\" messages.")
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010092 public boolean showLoadingProgress;
93
ccalvarin2eaa02e2017-04-17 23:37:46 +020094 @Option(
95 name = "deleted_packages",
96 defaultValue = "",
ccalvarin2eaa02e2017-04-17 23:37:46 +020097 converter = CommaSeparatedPackageNameListConverter.class,
ccalvarin3bc15472017-06-27 17:58:35 +020098 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
99 effectTags = {OptionEffectTag.UNKNOWN},
ccalvarin2eaa02e2017-04-17 23:37:46 +0200100 help =
101 "A comma-separated list of names of packages which the "
102 + "build system will consider non-existent, even if they are "
103 + "visible somewhere on the package path.\n"
104 + "Use this option when deleting a subpackage 'x/y' of an "
105 + "existing package 'x'. For example, after deleting x/y/BUILD "
106 + "in your client, the build system may complain if it "
107 + "encounters a label '//x:y/z' if that is still provided by another "
108 + "package_path entry. Specifying --deleted_packages x/y avoids this "
109 + "problem."
110 )
Lukacs Berki33aa1e12015-07-08 08:11:30 +0000111 public List<PackageIdentifier> deletedPackages;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100112
ccalvarin2eaa02e2017-04-17 23:37:46 +0200113 @Option(
114 name = "default_visibility",
115 defaultValue = "private",
ccalvarin2eaa02e2017-04-17 23:37:46 +0200116 converter = DefaultVisibilityConverter.class,
ccalvarin456adb22017-07-11 14:23:46 +0200117 documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
ccalvarin3bc15472017-06-27 17:58:35 +0200118 effectTags = {OptionEffectTag.UNKNOWN},
ccalvarin2eaa02e2017-04-17 23:37:46 +0200119 help =
120 "Default visibility for packages that don't set it explicitly ('public' or " + "'private')."
121 )
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100122 public RuleVisibility defaultVisibility;
123
ccalvarin2eaa02e2017-04-17 23:37:46 +0200124 @Option(
jmmvfd6f8332020-03-30 13:35:18 -0700125 name = "legacy_globbing_threads",
126 defaultValue = "100",
127 converter = ParallelismConverter.class,
128 documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
129 effectTags = {OptionEffectTag.UNKNOWN},
130 help =
131 "Number of threads to use for glob evaluation. Takes "
132 + ResourceConverter.FLAG_SYNTAX
133 + ". \"auto\" means to use a reasonable value derived from the machine's hardware"
134 + " profile (e.g. the number of processors).")
Eric Fellheimere6566da2015-05-21 23:42:17 +0000135 public int globbingThreads;
136
Janak Ramakrishnan930e89c2016-10-04 20:51:42 +0000137 @Option(
138 name = "experimental_max_directories_to_eagerly_visit_in_globbing",
139 defaultValue = "-1",
ccalvarin456adb22017-07-11 14:23:46 +0200140 documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
ccalvarin3bc15472017-06-27 17:58:35 +0200141 effectTags = {OptionEffectTag.UNKNOWN},
Janak Ramakrishnan930e89c2016-10-04 20:51:42 +0000142 help =
143 "If non-negative, the first time a glob is evaluated in a package, the subdirectories of "
144 + "the package will be traversed in order to warm filesystem caches and compensate for "
145 + "lack of parallelism in globbing. At most this many directories will be visited."
146 )
147 public int maxDirectoriesToEagerlyVisitInGlobbing;
148
ccalvarin2eaa02e2017-04-17 23:37:46 +0200149 @Option(
150 name = "fetch",
151 defaultValue = "true",
ccalvarin456adb22017-07-11 14:23:46 +0200152 documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
ccalvarin3bc15472017-06-27 17:58:35 +0200153 effectTags = {OptionEffectTag.UNKNOWN},
ccalvarin2eaa02e2017-04-17 23:37:46 +0200154 help = "Allows the command to fetch external dependencies"
155 )
Kristina Chodorow6cad14e2015-05-14 18:18:35 +0000156 public boolean fetch;
Lukacs Berki50a705c2015-09-16 10:08:55 +0000157
ccalvarin2eaa02e2017-04-17 23:37:46 +0200158 @Option(
159 name = "experimental_check_output_files",
160 defaultValue = "true",
ccalvarin456adb22017-07-11 14:23:46 +0200161 documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
ccalvarin3bc15472017-06-27 17:58:35 +0200162 effectTags = {OptionEffectTag.UNKNOWN},
ccalvarin2eaa02e2017-04-17 23:37:46 +0200163 help =
164 "Check for modifications made to the output files of a build. Consider setting "
165 + "this flag to false to see the effect on incremental build times."
166 )
Nathan Harmata60108832016-03-25 08:02:42 +0000167 public boolean checkOutputFiles;
168
Lukacs Berki50a705c2015-09-16 10:08:55 +0000169 /**
170 * A converter from strings containing comma-separated names of packages to lists of strings.
171 */
172 public static class CommaSeparatedPackageNameListConverter
173 implements Converter<List<PackageIdentifier>> {
174
175 private static final Splitter COMMA_SPLITTER = Splitter.on(',');
176
177 @Override
178 public List<PackageIdentifier> convert(String input) throws OptionsParsingException {
179 if (Strings.isNullOrEmpty(input)) {
180 return ImmutableList.of();
181 }
182 ImmutableList.Builder<PackageIdentifier> list = ImmutableList.builder();
183 for (String s : COMMA_SPLITTER.split(input)) {
184 try {
185 list.add(PackageIdentifier.parse(s));
186 } catch (LabelSyntaxException e) {
187 throw new OptionsParsingException(e.getMessage());
188 }
189 }
190 return list.build();
191 }
192
193 @Override
194 public String getTypeDescription() {
195 return "comma-separated list of package names";
196 }
197
198 }
Brian Silvermand7d6d622016-03-17 09:53:39 +0000199
200 public ImmutableSet<PackageIdentifier> getDeletedPackages() {
Janak Ramakrishnan464f1182016-11-14 23:12:12 +0000201 if (deletedPackages == null || deletedPackages.isEmpty()) {
Brian Silvermand7d6d622016-03-17 09:53:39 +0000202 return ImmutableSet.of();
203 }
204 ImmutableSet.Builder<PackageIdentifier> newDeletedPackages = ImmutableSet.builder();
205 for (PackageIdentifier pkg : deletedPackages) {
206 newDeletedPackages.add(pkg.makeAbsolute());
207 }
208 return newDeletedPackages.build();
209 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100210}