blob: 55cc85d81251321a56cfa8c9f745f7357da14ad3 [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#include "src/main/cpp/startup_options.h"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010015
Julio Merino28774852016-09-14 16:59:46 +000016#include <assert.h>
Julio Merino28774852016-09-14 16:59:46 +000017
Googler2f2f9112015-07-22 10:01:27 +000018#include <cstdio>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019#include <cstdlib>
20
Julio Merino28774852016-09-14 16:59:46 +000021#include "src/main/cpp/blaze_util.h"
László Csomor6f1e31a2017-01-27 11:01:41 +000022#include "src/main/cpp/blaze_util_platform.h"
23#include "src/main/cpp/util/errors.h"
Thiago Farina7f9357f2015-04-23 13:57:43 +000024#include "src/main/cpp/util/exit_code.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000025#include "src/main/cpp/util/file.h"
Laszlo Csomor9c951962016-11-10 13:31:27 +000026#include "src/main/cpp/util/file_platform.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000027#include "src/main/cpp/util/numbers.h"
28#include "src/main/cpp/util/strings.h"
Julio Merino69a8d722016-09-13 22:29:39 +000029#include "src/main/cpp/workspace_layout.h"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030
31namespace blaze {
32
Thiago Farina80bb0f22016-10-17 15:57:13 +000033using std::string;
Julio Merino28774852016-09-14 16:59:46 +000034using std::vector;
35
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000036StartupOptions::StartupOptions(const WorkspaceLayout* workspace_layout)
37 : StartupOptions("Bazel", workspace_layout) {}
Julio Merino41b56882016-09-15 13:48:10 +000038
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000039StartupOptions::StartupOptions(const string &product_name,
Googler226724a2017-02-14 17:57:07 +000040 const WorkspaceLayout *workspace_layout)
Thiago Farinac380dc42016-09-30 08:36:20 +000041 : product_name(product_name),
42 deep_execroot(true),
43 block_for_lock(true),
44 host_jvm_debug(false),
45 batch(false),
46 batch_cpu_scheduling(false),
47 io_nice_level(-1),
48 oom_more_eagerly(false),
49 oom_more_eagerly_threshold(100),
50 write_command_log(true),
51 watchfs(false),
52 allow_configurable_attributes(false),
53 fatal_event_bus_exceptions(false),
54 command_port(0),
Lukacs Berki71675a52016-11-08 09:48:27 +000055 connect_timeout_secs(10),
56 invocation_policy(NULL),
Chloe Calvarineaa3be72016-12-13 19:48:34 +000057 client_debug(false),
Julio Merino5a098042017-02-06 16:43:10 +000058 use_custom_exit_code_on_abrupt_exit(true),
Googler2f111922017-02-28 20:58:45 +000059 java_logging_formatter("java.util.logging.SimpleFormatter") {
Laszlo Csomorcefa9a22016-11-22 10:50:07 +000060 bool testing = !blaze::GetEnv("TEST_TMPDIR").empty();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010061 if (testing) {
Laszlo Csomorcefa9a22016-11-22 10:50:07 +000062 output_root = MakeAbsolute(blaze::GetEnv("TEST_TMPDIR"));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010063 } else {
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000064 output_root = workspace_layout->GetOutputRoot();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 }
66
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +000067 const string product_name_lower = GetLowercaseProductName();
Kristina Chodorow427dd252015-03-26 14:40:55 +000068 output_user_root = blaze_util::JoinPath(
Luis Fernando Pino Duque928a49c2016-06-24 09:43:20 +000069 output_root, "_" + product_name_lower + "_" + GetUserName());
Lukacs Berki28764a32016-07-15 14:29:57 +000070 // 3 hours (but only 15 seconds if used within a test)
71 max_idle_secs = testing ? 15 : (3 * 3600);
Chloe Calvarineaa3be72016-12-13 19:48:34 +000072 nullary_options = {"deep_execroot",
73 "block_for_lock",
74 "host_jvm_debug",
75 "master_blazerc",
76 "master_bazelrc",
77 "batch",
78 "batch_cpu_scheduling",
79 "allow_configurable_attributes",
80 "fatal_event_bus_exceptions",
81 "experimental_oom_more_eagerly",
82 "write_command_log",
83 "watchfs",
84 "client_debug",
Googler2f111922017-02-28 20:58:45 +000085 "use_custom_exit_code_on_abrupt_exit"};
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +000086 unary_options = {"output_base", "install_base",
87 "output_user_root", "host_jvm_profile", "host_javabase",
88 "host_jvm_args", "bazelrc", "blazerc", "io_nice_level",
89 "max_idle_secs", "experimental_oom_more_eagerly_threshold",
Lukacs Berki71675a52016-11-08 09:48:27 +000090 "command_port", "invocation_policy", "connect_timeout_secs"};
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010091}
92
Thiago Farinac380dc42016-09-30 08:36:20 +000093StartupOptions::~StartupOptions() {}
94
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +000095string StartupOptions::GetLowercaseProductName() const {
96 string lowercase_product_name = product_name;
97 blaze_util::ToLower(&lowercase_product_name);
98 return lowercase_product_name;
99}
100
101bool StartupOptions::IsNullary(const string& arg) const {
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +0000102 for (string option : nullary_options) {
103 if (GetNullaryOption(arg.c_str(), ("--" + option).c_str()) ||
104 GetNullaryOption(arg.c_str(), ("--no" + option).c_str())) {
105 return true;
106 }
107 }
108 return false;
109}
110
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +0000111bool StartupOptions::IsUnary(const string& arg) const {
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +0000112 for (string option : unary_options) {
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +0000113 // The second argument of GetUnaryOption is not relevant to determine
114 // whether the option is unary or not, hence we set it to the empty string
115 // by default.
116 //
117 // TODO(lpino): Improve GetUnaryOption to only require the arg and the
118 // option we are looking for.
119 if (GetUnaryOption(arg.c_str(), "", ("--" + option).c_str()) != NULL) {
Luis Fernando Pino Duque8949c072016-10-28 15:17:22 +0000120 return true;
121 }
122 }
123 return false;
124}
125
Julio Merino28774852016-09-14 16:59:46 +0000126void StartupOptions::AddExtraOptions(vector<string> *result) const {}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100127
Julio Merino28774852016-09-14 16:59:46 +0000128blaze_exit_code::ExitCode StartupOptions::ProcessArg(
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129 const string &argstr, const string &next_argstr, const string &rcfile,
Thiago Farinaa0592a12015-05-07 13:59:59 +0000130 bool *is_space_separated, string *error) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100131 // We have to parse a specific option syntax, so GNU getopts won't do. All
132 // options begin with "--" or "-". Values are given together with the option
133 // delimited by '=' or in the next option.
134 const char* arg = argstr.c_str();
135 const char* next_arg = next_argstr.empty() ? NULL : next_argstr.c_str();
136 const char* value = NULL;
137
138 if ((value = GetUnaryOption(arg, next_arg, "--output_base")) != NULL) {
139 output_base = MakeAbsolute(value);
140 option_sources["output_base"] = rcfile;
141 } else if ((value = GetUnaryOption(arg, next_arg,
142 "--install_base")) != NULL) {
143 install_base = MakeAbsolute(value);
144 option_sources["install_base"] = rcfile;
145 } else if ((value = GetUnaryOption(arg, next_arg,
146 "--output_user_root")) != NULL) {
147 output_user_root = MakeAbsolute(value);
148 option_sources["output_user_root"] = rcfile;
Lukacs Berki5fb98d12015-12-09 15:29:46 +0000149 } else if (GetNullaryOption(arg, "--deep_execroot")) {
150 deep_execroot = true;
151 option_sources["deep_execroot"] = rcfile;
152 } else if (GetNullaryOption(arg, "--nodeep_execroot")) {
153 deep_execroot = false;
154 option_sources["deep_execroot"] = rcfile;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100155 } else if (GetNullaryOption(arg, "--noblock_for_lock")) {
156 block_for_lock = false;
157 option_sources["block_for_lock"] = rcfile;
158 } else if (GetNullaryOption(arg, "--host_jvm_debug")) {
159 host_jvm_debug = true;
160 option_sources["host_jvm_debug"] = rcfile;
161 } else if ((value = GetUnaryOption(arg, next_arg,
162 "--host_jvm_profile")) != NULL) {
163 host_jvm_profile = value;
164 option_sources["host_jvm_profile"] = rcfile;
165 } else if ((value = GetUnaryOption(arg, next_arg,
166 "--host_javabase")) != NULL) {
Janak Ramakrishnan8dd9ce02016-02-01 20:34:10 +0000167 // TODO(bazel-team): Consider examining the javabase and re-execing in case
168 // of architecture mismatch.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100169 host_javabase = MakeAbsolute(value);
170 option_sources["host_javabase"] = rcfile;
Janak Ramakrishnanc1063442015-12-23 05:07:02 +0000171 } else if ((value = GetUnaryOption(arg, next_arg, "--host_jvm_args")) !=
172 NULL) {
Janak Ramakrishnan533657e2015-11-13 23:34:14 +0000173 host_jvm_args.push_back(value);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100174 option_sources["host_jvm_args"] = rcfile; // NB: This is incorrect
Damien Martin-Guillerez8cde69c2015-06-05 11:33:44 +0000175 } else if ((value = GetUnaryOption(arg, next_arg, "--bazelrc")) != NULL) {
176 if (rcfile != "") {
177 *error = "Can't specify --bazelrc in the .bazelrc file.";
178 return blaze_exit_code::BAD_ARGV;
179 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100180 } else if ((value = GetUnaryOption(arg, next_arg, "--blazerc")) != NULL) {
181 if (rcfile != "") {
182 *error = "Can't specify --blazerc in the .blazerc file.";
183 return blaze_exit_code::BAD_ARGV;
184 }
185 } else if (GetNullaryOption(arg, "--nomaster_blazerc") ||
186 GetNullaryOption(arg, "--master_blazerc")) {
187 if (rcfile != "") {
188 *error = "Can't specify --[no]master_blazerc in .blazerc file.";
189 return blaze_exit_code::BAD_ARGV;
190 }
191 option_sources["blazerc"] = rcfile;
Damien Martin-Guillerez50d124b2015-06-24 15:20:09 +0000192 } else if (GetNullaryOption(arg, "--nomaster_bazelrc") ||
193 GetNullaryOption(arg, "--master_bazelrc")) {
194 if (rcfile != "") {
195 *error = "Can't specify --[no]master_bazelrc in .bazelrc file.";
196 return blaze_exit_code::BAD_ARGV;
197 }
198 option_sources["blazerc"] = rcfile;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100199 } else if (GetNullaryOption(arg, "--batch")) {
200 batch = true;
201 option_sources["batch"] = rcfile;
202 } else if (GetNullaryOption(arg, "--nobatch")) {
203 batch = false;
204 option_sources["batch"] = rcfile;
205 } else if (GetNullaryOption(arg, "--batch_cpu_scheduling")) {
206 batch_cpu_scheduling = true;
207 option_sources["batch_cpu_scheduling"] = rcfile;
208 } else if (GetNullaryOption(arg, "--nobatch_cpu_scheduling")) {
209 batch_cpu_scheduling = false;
210 option_sources["batch_cpu_scheduling"] = rcfile;
211 } else if (GetNullaryOption(arg, "--allow_configurable_attributes")) {
212 allow_configurable_attributes = true;
213 option_sources["allow_configurable_attributes"] = rcfile;
214 } else if (GetNullaryOption(arg, "--noallow_configurable_attributes")) {
215 allow_configurable_attributes = false;
216 option_sources["allow_configurable_attributes"] = rcfile;
217 } else if (GetNullaryOption(arg, "--fatal_event_bus_exceptions")) {
218 fatal_event_bus_exceptions = true;
219 option_sources["fatal_event_bus_exceptions"] = rcfile;
220 } else if (GetNullaryOption(arg, "--nofatal_event_bus_exceptions")) {
221 fatal_event_bus_exceptions = false;
222 option_sources["fatal_event_bus_exceptions"] = rcfile;
223 } else if ((value = GetUnaryOption(arg, next_arg,
224 "--io_nice_level")) != NULL) {
225 if (!blaze_util::safe_strto32(value, &io_nice_level) ||
226 io_nice_level > 7) {
227 blaze_util::StringPrintf(error,
228 "Invalid argument to --io_nice_level: '%s'. Must not exceed 7.",
229 value);
230 return blaze_exit_code::BAD_ARGV;
231 }
232 option_sources["io_nice_level"] = rcfile;
233 } else if ((value = GetUnaryOption(arg, next_arg,
234 "--max_idle_secs")) != NULL) {
235 if (!blaze_util::safe_strto32(value, &max_idle_secs) ||
236 max_idle_secs < 0) {
237 blaze_util::StringPrintf(error,
238 "Invalid argument to --max_idle_secs: '%s'.", value);
239 return blaze_exit_code::BAD_ARGV;
240 }
241 option_sources["max_idle_secs"] = rcfile;
Janak Ramakrishnanadc706f2016-03-07 19:12:48 +0000242 } else if (GetNullaryOption(arg, "--experimental_oom_more_eagerly")) {
243 oom_more_eagerly = true;
244 option_sources["experimental_oom_more_eagerly"] = rcfile;
245 } else if (GetNullaryOption(arg, "--noexperimental_oom_more_eagerly")) {
246 oom_more_eagerly = false;
247 option_sources["experimental_oom_more_eagerly"] = rcfile;
Janak Ramakrishnan2c4289e2016-04-25 14:26:12 +0000248 } else if ((value = GetUnaryOption(
249 arg, next_arg,
250 "--experimental_oom_more_eagerly_threshold")) != NULL) {
Janak Ramakrishnan8cc772e2016-03-23 17:26:12 +0000251 if (!blaze_util::safe_strto32(value, &oom_more_eagerly_threshold) ||
252 oom_more_eagerly_threshold < 0) {
253 blaze_util::StringPrintf(error,
254 "Invalid argument to "
255 "--experimental_oom_more_eagerly_threshold: "
256 "'%s'.",
257 value);
258 return blaze_exit_code::BAD_ARGV;
259 }
260 option_sources["experimental_oom_more_eagerly_threshold"] = rcfile;
Michajlo Matijkiwaf79a322016-09-16 15:44:35 +0000261 } else if (GetNullaryOption(arg, "--write_command_log")) {
262 write_command_log = true;
263 option_sources["write_command_log"] = rcfile;
264 } else if (GetNullaryOption(arg, "--nowrite_command_log")) {
265 write_command_log = false;
266 option_sources["write_command_log"] = rcfile;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100267 } else if (GetNullaryOption(arg, "--watchfs")) {
268 watchfs = true;
269 option_sources["watchfs"] = rcfile;
Han-Wen Nienhuys1367b702015-06-26 14:04:44 +0000270 } else if (GetNullaryOption(arg, "--nowatchfs")) {
271 watchfs = false;
272 option_sources["watchfs"] = rcfile;
Lukacs Berki71675a52016-11-08 09:48:27 +0000273 } else if (GetNullaryOption(arg, "--client_debug")) {
274 client_debug = true;
275 option_sources["client_debug"] = rcfile;
276 } else if (GetNullaryOption(arg, "--noclient_debug")) {
277 client_debug = false;
278 option_sources["client_debug"] = rcfile;
Chloe Calvarineaa3be72016-12-13 19:48:34 +0000279 } else if (GetNullaryOption(arg, "--use_custom_exit_code_on_abrupt_exit")) {
280 use_custom_exit_code_on_abrupt_exit = true;
281 option_sources["use_custom_exit_code_on_abrupt_exit"] = rcfile;
282 } else if (GetNullaryOption(arg, "--nouse_custom_exit_code_on_abrupt_exit")) {
283 use_custom_exit_code_on_abrupt_exit = false;
284 option_sources["use_custom_exit_code_on_abrupt_exit"] = rcfile;
Lukacs Berki71675a52016-11-08 09:48:27 +0000285 } else if ((value = GetUnaryOption(
286 arg, next_arg, "--connect_timeout_secs")) != NULL) {
287 if (!blaze_util::safe_strto32(value, &connect_timeout_secs) ||
288 connect_timeout_secs < 1 || connect_timeout_secs > 120) {
289 blaze_util::StringPrintf(error,
290 "Invalid argument to --connect_timeout_secs: '%s'.\n"
291 "Must be an integer between 1 and 120.\n",
292 value);
293 return blaze_exit_code::BAD_ARGV;
294 }
295 option_sources["connect_timeout_secs"] = rcfile;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100296 } else if ((value = GetUnaryOption(
Lukacs Berki7e0249e2016-04-21 08:14:08 +0000297 arg, next_arg, "--command_port")) != NULL) {
298 if (!blaze_util::safe_strto32(value, &command_port) ||
Lukacs Berki3a3c4832016-10-06 14:20:04 +0000299 command_port < 0 || command_port > 65535) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100300 blaze_util::StringPrintf(error,
Lukacs Berki3a3c4832016-10-06 14:20:04 +0000301 "Invalid argument to --command_port: '%s'.\n"
302 "Must be a valid port number or 0.\n",
Lukacs Berki8b3b9182016-04-14 08:29:05 +0000303 value);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100304 return blaze_exit_code::BAD_ARGV;
305 }
Lukacs Berki71675a52016-11-08 09:48:27 +0000306 option_sources["command_port"] = rcfile;
Alex Humesky2f3f4cf2015-09-29 01:42:00 +0000307 } else if ((value = GetUnaryOption(arg, next_arg, "--invocation_policy"))
308 != NULL) {
309 if (invocation_policy == NULL) {
310 invocation_policy = value;
311 option_sources["invocation_policy"] = rcfile;
312 } else {
313 *error = "The startup flag --invocation_policy cannot be specified "
314 "multiple times.";
315 return blaze_exit_code::BAD_ARGV;
316 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100317 } else {
318 bool extra_argument_processed;
319 blaze_exit_code::ExitCode process_extra_arg_exit_code = ProcessArgExtra(
320 arg, next_arg, rcfile, &value, &extra_argument_processed, error);
321 if (process_extra_arg_exit_code != blaze_exit_code::SUCCESS) {
322 return process_extra_arg_exit_code;
323 }
324 if (!extra_argument_processed) {
Kristina Chodorow11d40d22015-03-17 18:26:59 +0000325 blaze_util::StringPrintf(
Alex Gaynora4d7bc72015-12-29 16:13:23 +0000326 error,
Luis Fernando Pino Duqued5e008c2016-12-08 16:59:16 +0000327 "Unknown startup option: '%s'.\n"
Alex Gaynora4d7bc72015-12-29 16:13:23 +0000328 " For more info, run '%s help startup_options'.",
Luis Fernando Pino Duqued5e008c2016-12-08 16:59:16 +0000329 arg, GetLowercaseProductName().c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100330 return blaze_exit_code::BAD_ARGV;
331 }
332 }
333
Thiago Farinaa0592a12015-05-07 13:59:59 +0000334 *is_space_separated = ((value == next_arg) && (value != NULL));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100335 return blaze_exit_code::SUCCESS;
336}
337
Julio Merino28774852016-09-14 16:59:46 +0000338blaze_exit_code::ExitCode StartupOptions::ProcessArgExtra(
339 const char *arg, const char *next_arg, const string &rcfile,
340 const char **value, bool *is_processed, string *error) {
341 *is_processed = false;
342 return blaze_exit_code::SUCCESS;
343}
344
Julio Merino28774852016-09-14 16:59:46 +0000345string StartupOptions::GetDefaultHostJavabase() const {
346 return blaze::GetDefaultHostJavabase();
347}
348
349string StartupOptions::GetHostJavabase() {
Philipp Wollermann95048272017-03-17 15:11:58 +0000350 // 1) Allow overriding the host_javabase via --host_javabase.
Julio Merino28774852016-09-14 16:59:46 +0000351 if (host_javabase.empty()) {
Lukacs Berkibfd9aa32017-03-06 11:21:35 +0000352 if (default_host_javabase.empty()) {
Philipp Wollermann95048272017-03-17 15:11:58 +0000353 string bundled_jre_path = blaze_util::JoinPath(
354 install_base, "_embedded_binaries/embedded_tools/jdk");
355 if (blaze_util::CanExecuteFile(blaze_util::JoinPath(
356 bundled_jre_path, GetJavaBinaryUnderJavabase()))) {
357 // 2) Use a bundled JVM if we have one.
358 default_host_javabase = bundled_jre_path;
359 } else {
360 // 3) Otherwise fall back to using the default system JVM.
361 default_host_javabase = GetDefaultHostJavabase();
362 }
Lukacs Berkibfd9aa32017-03-06 11:21:35 +0000363 }
364
365 return default_host_javabase;
366 } else {
367 return host_javabase;
Julio Merino28774852016-09-14 16:59:46 +0000368 }
Lukacs Berkibfd9aa32017-03-06 11:21:35 +0000369}
370
371string StartupOptions::GetExplicitHostJavabase() const {
Julio Merino28774852016-09-14 16:59:46 +0000372 return host_javabase;
373}
374
375string StartupOptions::GetJvm() {
Laszlo Csomor00549b42017-01-11 09:12:10 +0000376 string java_program =
377 blaze_util::JoinPath(GetHostJavabase(), GetJavaBinaryUnderJavabase());
378 if (!blaze_util::CanExecuteFile(java_program)) {
Laszlo Csomor9c951962016-11-10 13:31:27 +0000379 if (!blaze_util::PathExists(java_program)) {
Julio Merino28774852016-09-14 16:59:46 +0000380 fprintf(stderr, "Couldn't find java at '%s'.\n", java_program.c_str());
381 } else {
László Csomor6f1e31a2017-01-27 11:01:41 +0000382 fprintf(stderr, "Java at '%s' exists but is not executable: %s\n",
383 java_program.c_str(), blaze_util::GetLastErrorString().c_str());
Julio Merino28774852016-09-14 16:59:46 +0000384 }
385 exit(1);
386 }
387 // If the full JDK is installed
Laszlo Csomor760f7862016-12-19 15:46:47 +0000388 string jdk_rt_jar = blaze_util::JoinPath(GetHostJavabase(), "jre/lib/rt.jar");
Julio Merino28774852016-09-14 16:59:46 +0000389 // If just the JRE is installed
Laszlo Csomor760f7862016-12-19 15:46:47 +0000390 string jre_rt_jar = blaze_util::JoinPath(GetHostJavabase(), "lib/rt.jar");
Laszlo Csomor00549b42017-01-11 09:12:10 +0000391 if (blaze_util::CanReadFile(jdk_rt_jar) ||
392 blaze_util::CanReadFile(jre_rt_jar)) {
Julio Merino28774852016-09-14 16:59:46 +0000393 return java_program;
394 }
395 fprintf(stderr, "Problem with java installation: "
396 "couldn't find/access rt.jar in %s\n", GetHostJavabase().c_str());
397 exit(1);
398}
399
400string StartupOptions::GetExe(const string &jvm, const string &jar_path) {
401 return jvm;
402}
403
404void StartupOptions::AddJVMArgumentPrefix(const string &javabase,
405 std::vector<string> *result) const {
406}
407
408void StartupOptions::AddJVMArgumentSuffix(const string &real_install_dir,
409 const string &jar_path,
410 std::vector<string> *result) const {
411 result->push_back("-jar");
Laszlo Csomor4875b252017-03-08 17:40:24 +0000412 result->push_back(
413 blaze::PathAsJvmFlag(blaze_util::JoinPath(real_install_dir, jar_path)));
Julio Merino28774852016-09-14 16:59:46 +0000414}
415
416blaze_exit_code::ExitCode StartupOptions::AddJVMArguments(
417 const string &host_javabase, vector<string> *result,
418 const vector<string> &user_options, string *error) const {
419 // Configure logging
Laszlo Csomor760f7862016-12-19 15:46:47 +0000420 const string propFile =
421 blaze_util::JoinPath(output_base, "javalog.properties");
László Csomore64ed192017-02-23 15:43:54 +0000422 string java_log(
423 blaze::PathAsJvmFlag(blaze_util::JoinPath(output_base, "java.log")));
Laszlo Csomor49970e02016-11-28 08:55:47 +0000424 if (!blaze_util::WriteFile("handlers=java.util.logging.FileHandler\n"
425 ".level=INFO\n"
426 "java.util.logging.FileHandler.level=INFO\n"
427 "java.util.logging.FileHandler.pattern=" +
László Csomore64ed192017-02-23 15:43:54 +0000428 java_log +
Laszlo Csomor760f7862016-12-19 15:46:47 +0000429 "\n"
Julio Merino0bc863d2017-03-09 17:36:32 +0000430 "java.util.logging.FileHandler.limit=1024000\n"
Laszlo Csomor49970e02016-11-28 08:55:47 +0000431 "java.util.logging.FileHandler.count=1\n"
László Csomore64ed192017-02-23 15:43:54 +0000432 "java.util.logging.FileHandler.formatter=" +
433 java_logging_formatter + "\n",
Laszlo Csomor49970e02016-11-28 08:55:47 +0000434 propFile)) {
Julio Merino28774852016-09-14 16:59:46 +0000435 perror(("Couldn't write logging file " + propFile).c_str());
436 } else {
437 result->push_back("-Djava.util.logging.config.file=" + propFile);
438 }
439 return blaze_exit_code::SUCCESS;
440}
441
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100442} // namespace blaze