blob: a931ab954dfa9f200da22f723f1e34b6b8214d11 [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.
Julio Merino28774852016-09-14 16:59:46 +000014#ifndef BAZEL_SRC_MAIN_CPP_STARTUP_OPTIONS_H_
15#define BAZEL_SRC_MAIN_CPP_STARTUP_OPTIONS_H_
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016
17#include <map>
Kristina Chodorow9ce9b0f2015-10-06 03:54:45 +000018#include <memory>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019#include <string>
20#include <vector>
21
Thiago Farina7f9357f2015-04-23 13:57:43 +000022#include "src/main/cpp/util/exit_code.h"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023
24namespace blaze {
25
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000026class WorkspaceLayout;
27
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028// This class holds the parsed startup options for Blaze.
Thiago Farina9bc5c342016-03-21 08:39:48 +000029// These options and their defaults must be kept in sync with those in
30// src/main/java/com/google/devtools/build/lib/runtime/BlazeServerStartupOptions.java.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031// The latter are purely decorative (they affect the help message,
32// which displays the defaults). The actual defaults are defined
33// in the constructor.
34//
35// TODO(bazel-team): The encapsulation is not quite right -- there are some
36// places in blaze.cc where some of these fields are explicitly modified. Their
37// names also don't conform to the style guide.
Julio Merino28774852016-09-14 16:59:46 +000038class StartupOptions {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039 public:
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000040 explicit StartupOptions(const WorkspaceLayout* workspace_layout);
Julio Merino28774852016-09-14 16:59:46 +000041 virtual ~StartupOptions();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043 // Parses a single argument, either from the command line or from the .blazerc
44 // "startup" options.
45 //
46 // rcfile should be an empty string if the option being parsed does not come
47 // from a blazerc.
48 //
Thiago Farina124c0b62015-05-08 12:00:12 +000049 // Sets "is_space_separated" true if arg is unary and uses the "--foo bar"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050 // style, so its value is in next_arg.
51 //
Thiago Farina124c0b62015-05-08 12:00:12 +000052 // Sets "is_space_separated" false if arg is either nullary
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 // (e.g. "--[no]batch") or is unary but uses the "--foo=bar" style.
54 //
55 // Returns the exit code after processing the argument. "error" will contain
56 // a descriptive string for any return value other than
57 // blaze_exit_code::SUCCESS.
Thiago Farina80bb0f22016-10-17 15:57:13 +000058 blaze_exit_code::ExitCode ProcessArg(const std::string &arg,
59 const std::string &next_arg,
60 const std::string &rcfile,
61 bool *is_space_separated,
62 std::string *error);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010063
64 // Adds any other options needed to result.
Julio Merino28774852016-09-14 16:59:46 +000065 //
66 // TODO(jmmv): Now that we support site-specific options via subclasses of
67 // StartupOptions, the "ExtraOptions" concept makes no sense; remove it.
Thiago Farina80bb0f22016-10-17 15:57:13 +000068 virtual void AddExtraOptions(std::vector<std::string> *result) const;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010070 // Checks extra fields when processing arg.
71 //
72 // Returns the exit code after processing the argument. "error" will contain
73 // a descriptive string for any return value other than
74 // blaze_exit_code::SUCCESS.
Julio Merino28774852016-09-14 16:59:46 +000075 //
76 // TODO(jmmv): Now that we support site-specific options via subclasses of
77 // StartupOptions, the "ExtraOptions" concept makes no sense; remove it.
78 virtual blaze_exit_code::ExitCode ProcessArgExtra(
Thiago Farina80bb0f22016-10-17 15:57:13 +000079 const char *arg, const char *next_arg, const std::string &rcfile,
80 const char **value, bool *is_processed, std::string *error);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010081
82 // Return the default path to the JDK used to run Blaze itself
83 // (must be an absolute directory).
Thiago Farina80bb0f22016-10-17 15:57:13 +000084 virtual std::string GetDefaultHostJavabase() const;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010085
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010086 // Returns the path to the JVM. This should be called after parsing
87 // the startup options.
Thiago Farina80bb0f22016-10-17 15:57:13 +000088 virtual std::string GetJvm();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010089
Eric Fellheimer3a695f32016-05-11 17:26:30 +000090 // Returns the executable used to start the Blaze server, typically the given
91 // JVM.
Thiago Farina80bb0f22016-10-17 15:57:13 +000092 virtual std::string GetExe(const std::string &jvm,
93 const std::string &jar_path);
Eric Fellheimer3a695f32016-05-11 17:26:30 +000094
95 // Adds JVM prefix flags to be set. These will be added before all other
96 // JVM flags.
Thiago Farina80bb0f22016-10-17 15:57:13 +000097 virtual void AddJVMArgumentPrefix(const std::string &javabase,
98 std::vector<std::string> *result) const;
Eric Fellheimer3a695f32016-05-11 17:26:30 +000099
100 // Adds JVM suffix flags. These will be added after all other JVM flags, and
101 // just before the Blaze server startup flags.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000102 virtual void AddJVMArgumentSuffix(const std::string &real_install_dir,
103 const std::string &jar_path,
104 std::vector<std::string> *result) const;
Eric Fellheimer3a695f32016-05-11 17:26:30 +0000105
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100106 // Adds JVM tuning flags for Blaze.
107 //
108 // Returns the exit code after this operation. "error" will be set to a
109 // descriptive string for any value other than blaze_exit_code::SUCCESS.
Julio Merino28774852016-09-14 16:59:46 +0000110 virtual blaze_exit_code::ExitCode AddJVMArguments(
Thiago Farina80bb0f22016-10-17 15:57:13 +0000111 const std::string &host_javabase, std::vector<std::string> *result,
112 const std::vector<std::string> &user_options, std::string *error) const;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100113
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +0000114 // Checks whether the argument is a valid nullary option.
115 // E.g. --master_bazelrc, --nomaster_bazelrc.
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +0000116 bool IsNullary(const std::string& arg) const;
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +0000117
118 // Checks whether the argument is a valid unary option.
119 // E.g. --blazerc=foo, --blazerc foo.
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +0000120 bool IsUnary(const std::string& arg) const;
121
122 std::string GetLowercaseProductName() const;
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +0000123
Luis Fernando Pino Duque928a49c2016-06-24 09:43:20 +0000124 // The capitalized name of this binary.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000125 const std::string product_name;
Luis Fernando Pino Duque928a49c2016-06-24 09:43:20 +0000126
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100127 // Blaze's output base. Everything is relative to this. See
128 // the BlazeDirectories Java class for details.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000129 std::string output_base;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100130
131 // Installation base for a specific release installation.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000132 std::string install_base;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100133
134 // The toplevel directory containing Blaze's output. When Blaze is
135 // run by a test, we use TEST_TMPDIR, simplifying the correct
136 // hermetic invocation of Blaze from tests.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000137 std::string output_root;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100138
139 // Blaze's output_user_root. Used only for computing install_base and
140 // output_base.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000141 std::string output_user_root;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100142
Lukacs Berki5fb98d12015-12-09 15:29:46 +0000143 // Whether to put the execroot at $OUTPUT_BASE/$WORKSPACE_NAME (if false) or
144 // $OUTPUT_BASE/execroot/$WORKSPACE_NAME (if true).
145 bool deep_execroot;
146
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100147 // Block for the Blaze server lock. Otherwise,
148 // quit with non-0 exit code if lock can't
149 // be acquired immediately.
150 bool block_for_lock;
151
152 bool host_jvm_debug;
153
Thiago Farina80bb0f22016-10-17 15:57:13 +0000154 std::string host_jvm_profile;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100155
Thiago Farina80bb0f22016-10-17 15:57:13 +0000156 std::vector<std::string> host_jvm_args;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100157
158 bool batch;
159
160 // From the man page: "This policy is useful for workloads that are
161 // non-interactive, but do not want to lower their nice value, and for
162 // workloads that want a deterministic scheduling policy without
163 // interactivity causing extra preemptions (between the workload's tasks)."
164 bool batch_cpu_scheduling;
165
166 // If negative, don't mess with ionice. Otherwise, set a level from 0-7
167 // for best-effort scheduling. 0 is highest priority, 7 is lowest.
168 int io_nice_level;
169
170 int max_idle_secs;
171
Janak Ramakrishnanadc706f2016-03-07 19:12:48 +0000172 bool oom_more_eagerly;
173
Janak Ramakrishnan8cc772e2016-03-23 17:26:12 +0000174 int oom_more_eagerly_threshold;
175
Michajlo Matijkiwaf79a322016-09-16 15:44:35 +0000176 bool write_command_log;
177
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100178 // If true, Blaze will listen to OS-level file change notifications.
179 bool watchfs;
180
181 // Temporary experimental flag that permits configurable attribute syntax
182 // in BUILD files. This will be removed when configurable attributes is
183 // a more stable feature.
184 bool allow_configurable_attributes;
185
186 // Temporary flag for enabling EventBus exceptions to be fatal.
187 bool fatal_event_bus_exceptions;
188
189 // A string to string map specifying where each option comes from. If the
190 // value is empty, it was on the command line, if it is a string, it comes
191 // from a blazerc file, if a key is not present, it is the default.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000192 std::map<std::string, std::string> option_sources;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100193
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100194 // Returns the GetHostJavabase. This should be called after parsing
195 // the --host_javabase option.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000196 std::string GetHostJavabase();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100197
Lukacs Berkibfd9aa32017-03-06 11:21:35 +0000198 // Returns the explicit value of the --host_javabase startup option or the
199 // empty string if it was not specified on the command line.
200 std::string GetExplicitHostJavabase() const;
201
Lukacs Berki8b3b9182016-04-14 08:29:05 +0000202 // Port for gRPC command server. 0 means let the kernel choose, -1 means no
203 // gRPC command server.
Lukacs Berki7e0249e2016-04-21 08:14:08 +0000204 int command_port;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100205
Lukacs Berki71675a52016-11-08 09:48:27 +0000206 // Connection timeout for each gRPC connection attempt.
207 int connect_timeout_secs;
208
Alex Humesky2f3f4cf2015-09-29 01:42:00 +0000209 // Invocation policy proto. May be NULL.
Thiago Farina80bb0f22016-10-17 15:57:13 +0000210 const char *invocation_policy;
Alex Humesky2f3f4cf2015-09-29 01:42:00 +0000211
Lukacs Berki71675a52016-11-08 09:48:27 +0000212 // Whether to output addition debugging information in the client.
213 bool client_debug;
214
Chloe Calvarineaa3be72016-12-13 19:48:34 +0000215 // Whether to check custom file for exit code when the Blaze Server exits
216 // abruptly without proper communication over gRPC.
217 bool use_custom_exit_code_on_abrupt_exit;
218
Julio Merino5a098042017-02-06 16:43:10 +0000219 // Value of the java.util.logging.FileHandler.formatter Java property.
220 std::string java_logging_formatter;
221
Julio Merino41b56882016-09-15 13:48:10 +0000222 protected:
223 // Constructor for subclasses only so that site-specific extensions of this
224 // class can override the product name. The product_name must be the
225 // capitalized version of the name, as in "Bazel".
Julio Merinoe3e3bfa2016-12-08 22:22:12 +0000226 explicit StartupOptions(const std::string &product_name,
227 const WorkspaceLayout* workspace_layout);
Julio Merino41b56882016-09-15 13:48:10 +0000228
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +0000229 // Holds the valid nullary startup options.
230 std::vector<std::string> nullary_options;
231
232 // Holds the valid unary startup options.
233 std::vector<std::string> unary_options;
234
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100235 private:
Thiago Farina80bb0f22016-10-17 15:57:13 +0000236 std::string host_javabase;
Lukacs Berkibfd9aa32017-03-06 11:21:35 +0000237 std::string default_host_javabase;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100238};
239
240} // namespace blaze
Thiago Farinac380dc42016-09-30 08:36:20 +0000241
Julio Merino28774852016-09-14 16:59:46 +0000242#endif // BAZEL_SRC_MAIN_CPP_STARTUP_OPTIONS_H_