Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 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 Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 14 | #include "src/main/cpp/startup_options.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 15 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 16 | #include <assert.h> |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 17 | |
Googler | 2f2f911 | 2015-07-22 10:01:27 +0000 | [diff] [blame] | 18 | #include <cstdio> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | #include <cstdlib> |
| 20 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 21 | #include "src/main/cpp/blaze_util.h" |
László Csomor | 6f1e31a | 2017-01-27 11:01:41 +0000 | [diff] [blame] | 22 | #include "src/main/cpp/blaze_util_platform.h" |
| 23 | #include "src/main/cpp/util/errors.h" |
Thiago Farina | 7f9357f | 2015-04-23 13:57:43 +0000 | [diff] [blame] | 24 | #include "src/main/cpp/util/exit_code.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 25 | #include "src/main/cpp/util/file.h" |
Laszlo Csomor | 9c95196 | 2016-11-10 13:31:27 +0000 | [diff] [blame] | 26 | #include "src/main/cpp/util/file_platform.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 27 | #include "src/main/cpp/util/numbers.h" |
| 28 | #include "src/main/cpp/util/strings.h" |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 29 | #include "src/main/cpp/workspace_layout.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | |
| 31 | namespace blaze { |
| 32 | |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 33 | using std::string; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 34 | using std::vector; |
| 35 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 36 | StartupOptions::StartupOptions(const WorkspaceLayout* workspace_layout) |
| 37 | : StartupOptions("Bazel", workspace_layout) {} |
Julio Merino | 41b5688 | 2016-09-15 13:48:10 +0000 | [diff] [blame] | 38 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 39 | StartupOptions::StartupOptions(const string &product_name, |
Googler | 226724a | 2017-02-14 17:57:07 +0000 | [diff] [blame] | 40 | const WorkspaceLayout *workspace_layout) |
Thiago Farina | c380dc4 | 2016-09-30 08:36:20 +0000 | [diff] [blame] | 41 | : 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 Berki | 71675a5 | 2016-11-08 09:48:27 +0000 | [diff] [blame] | 55 | connect_timeout_secs(10), |
| 56 | invocation_policy(NULL), |
Chloe Calvarin | eaa3be7 | 2016-12-13 19:48:34 +0000 | [diff] [blame] | 57 | client_debug(false), |
Julio Merino | 5a09804 | 2017-02-06 16:43:10 +0000 | [diff] [blame] | 58 | use_custom_exit_code_on_abrupt_exit(true), |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 59 | java_logging_formatter("java.util.logging.SimpleFormatter") { |
Laszlo Csomor | cefa9a2 | 2016-11-22 10:50:07 +0000 | [diff] [blame] | 60 | bool testing = !blaze::GetEnv("TEST_TMPDIR").empty(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 61 | if (testing) { |
Laszlo Csomor | cefa9a2 | 2016-11-22 10:50:07 +0000 | [diff] [blame] | 62 | output_root = MakeAbsolute(blaze::GetEnv("TEST_TMPDIR")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | } else { |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 64 | output_root = workspace_layout->GetOutputRoot(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 67 | const string product_name_lower = GetLowercaseProductName(); |
Kristina Chodorow | 427dd25 | 2015-03-26 14:40:55 +0000 | [diff] [blame] | 68 | output_user_root = blaze_util::JoinPath( |
Luis Fernando Pino Duque | 928a49c | 2016-06-24 09:43:20 +0000 | [diff] [blame] | 69 | output_root, "_" + product_name_lower + "_" + GetUserName()); |
Lukacs Berki | 28764a3 | 2016-07-15 14:29:57 +0000 | [diff] [blame] | 70 | // 3 hours (but only 15 seconds if used within a test) |
| 71 | max_idle_secs = testing ? 15 : (3 * 3600); |
Chloe Calvarin | eaa3be7 | 2016-12-13 19:48:34 +0000 | [diff] [blame] | 72 | 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", |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 85 | "use_custom_exit_code_on_abrupt_exit"}; |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 86 | 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 Berki | 71675a5 | 2016-11-08 09:48:27 +0000 | [diff] [blame] | 90 | "command_port", "invocation_policy", "connect_timeout_secs"}; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Thiago Farina | c380dc4 | 2016-09-30 08:36:20 +0000 | [diff] [blame] | 93 | StartupOptions::~StartupOptions() {} |
| 94 | |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 95 | string StartupOptions::GetLowercaseProductName() const { |
| 96 | string lowercase_product_name = product_name; |
| 97 | blaze_util::ToLower(&lowercase_product_name); |
| 98 | return lowercase_product_name; |
| 99 | } |
| 100 | |
| 101 | bool StartupOptions::IsNullary(const string& arg) const { |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 102 | 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 Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 111 | bool StartupOptions::IsUnary(const string& arg) const { |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 112 | for (string option : unary_options) { |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 113 | // 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 Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 126 | void StartupOptions::AddExtraOptions(vector<string> *result) const {} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 127 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 128 | blaze_exit_code::ExitCode StartupOptions::ProcessArg( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | const string &argstr, const string &next_argstr, const string &rcfile, |
Thiago Farina | a0592a1 | 2015-05-07 13:59:59 +0000 | [diff] [blame] | 130 | bool *is_space_separated, string *error) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | // 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 Berki | 5fb98d1 | 2015-12-09 15:29:46 +0000 | [diff] [blame] | 149 | } 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | } 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 Ramakrishnan | 8dd9ce0 | 2016-02-01 20:34:10 +0000 | [diff] [blame] | 167 | // TODO(bazel-team): Consider examining the javabase and re-execing in case |
| 168 | // of architecture mismatch. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | host_javabase = MakeAbsolute(value); |
| 170 | option_sources["host_javabase"] = rcfile; |
Janak Ramakrishnan | c106344 | 2015-12-23 05:07:02 +0000 | [diff] [blame] | 171 | } else if ((value = GetUnaryOption(arg, next_arg, "--host_jvm_args")) != |
| 172 | NULL) { |
Janak Ramakrishnan | 533657e | 2015-11-13 23:34:14 +0000 | [diff] [blame] | 173 | host_jvm_args.push_back(value); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 174 | option_sources["host_jvm_args"] = rcfile; // NB: This is incorrect |
Damien Martin-Guillerez | 8cde69c | 2015-06-05 11:33:44 +0000 | [diff] [blame] | 175 | } 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 180 | } 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-Guillerez | 50d124b | 2015-06-24 15:20:09 +0000 | [diff] [blame] | 192 | } 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | } 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 Ramakrishnan | adc706f | 2016-03-07 19:12:48 +0000 | [diff] [blame] | 242 | } 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 Ramakrishnan | 2c4289e | 2016-04-25 14:26:12 +0000 | [diff] [blame] | 248 | } else if ((value = GetUnaryOption( |
| 249 | arg, next_arg, |
| 250 | "--experimental_oom_more_eagerly_threshold")) != NULL) { |
Janak Ramakrishnan | 8cc772e | 2016-03-23 17:26:12 +0000 | [diff] [blame] | 251 | 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 Matijkiw | af79a32 | 2016-09-16 15:44:35 +0000 | [diff] [blame] | 261 | } 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 267 | } else if (GetNullaryOption(arg, "--watchfs")) { |
| 268 | watchfs = true; |
| 269 | option_sources["watchfs"] = rcfile; |
Han-Wen Nienhuys | 1367b70 | 2015-06-26 14:04:44 +0000 | [diff] [blame] | 270 | } else if (GetNullaryOption(arg, "--nowatchfs")) { |
| 271 | watchfs = false; |
| 272 | option_sources["watchfs"] = rcfile; |
Lukacs Berki | 71675a5 | 2016-11-08 09:48:27 +0000 | [diff] [blame] | 273 | } 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 Calvarin | eaa3be7 | 2016-12-13 19:48:34 +0000 | [diff] [blame] | 279 | } 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 Berki | 71675a5 | 2016-11-08 09:48:27 +0000 | [diff] [blame] | 285 | } 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 296 | } else if ((value = GetUnaryOption( |
Lukacs Berki | 7e0249e | 2016-04-21 08:14:08 +0000 | [diff] [blame] | 297 | arg, next_arg, "--command_port")) != NULL) { |
| 298 | if (!blaze_util::safe_strto32(value, &command_port) || |
Lukacs Berki | 3a3c483 | 2016-10-06 14:20:04 +0000 | [diff] [blame] | 299 | command_port < 0 || command_port > 65535) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 300 | blaze_util::StringPrintf(error, |
Lukacs Berki | 3a3c483 | 2016-10-06 14:20:04 +0000 | [diff] [blame] | 301 | "Invalid argument to --command_port: '%s'.\n" |
| 302 | "Must be a valid port number or 0.\n", |
Lukacs Berki | 8b3b918 | 2016-04-14 08:29:05 +0000 | [diff] [blame] | 303 | value); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 304 | return blaze_exit_code::BAD_ARGV; |
| 305 | } |
Lukacs Berki | 71675a5 | 2016-11-08 09:48:27 +0000 | [diff] [blame] | 306 | option_sources["command_port"] = rcfile; |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 307 | } 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 317 | } 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 Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 325 | blaze_util::StringPrintf( |
Alex Gaynor | a4d7bc7 | 2015-12-29 16:13:23 +0000 | [diff] [blame] | 326 | error, |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 327 | "Unknown startup option: '%s'.\n" |
Alex Gaynor | a4d7bc7 | 2015-12-29 16:13:23 +0000 | [diff] [blame] | 328 | " For more info, run '%s help startup_options'.", |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 329 | arg, GetLowercaseProductName().c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 330 | return blaze_exit_code::BAD_ARGV; |
| 331 | } |
| 332 | } |
| 333 | |
Thiago Farina | a0592a1 | 2015-05-07 13:59:59 +0000 | [diff] [blame] | 334 | *is_space_separated = ((value == next_arg) && (value != NULL)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 335 | return blaze_exit_code::SUCCESS; |
| 336 | } |
| 337 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 338 | blaze_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 Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 345 | string StartupOptions::GetDefaultHostJavabase() const { |
| 346 | return blaze::GetDefaultHostJavabase(); |
| 347 | } |
| 348 | |
| 349 | string StartupOptions::GetHostJavabase() { |
Philipp Wollermann | 9504827 | 2017-03-17 15:11:58 +0000 | [diff] [blame] | 350 | // 1) Allow overriding the host_javabase via --host_javabase. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 351 | if (host_javabase.empty()) { |
Lukacs Berki | bfd9aa3 | 2017-03-06 11:21:35 +0000 | [diff] [blame] | 352 | if (default_host_javabase.empty()) { |
Philipp Wollermann | 9504827 | 2017-03-17 15:11:58 +0000 | [diff] [blame] | 353 | 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 Berki | bfd9aa3 | 2017-03-06 11:21:35 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return default_host_javabase; |
| 366 | } else { |
| 367 | return host_javabase; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 368 | } |
Lukacs Berki | bfd9aa3 | 2017-03-06 11:21:35 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | string StartupOptions::GetExplicitHostJavabase() const { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 372 | return host_javabase; |
| 373 | } |
| 374 | |
| 375 | string StartupOptions::GetJvm() { |
Laszlo Csomor | 00549b4 | 2017-01-11 09:12:10 +0000 | [diff] [blame] | 376 | string java_program = |
| 377 | blaze_util::JoinPath(GetHostJavabase(), GetJavaBinaryUnderJavabase()); |
| 378 | if (!blaze_util::CanExecuteFile(java_program)) { |
Laszlo Csomor | 9c95196 | 2016-11-10 13:31:27 +0000 | [diff] [blame] | 379 | if (!blaze_util::PathExists(java_program)) { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 380 | fprintf(stderr, "Couldn't find java at '%s'.\n", java_program.c_str()); |
| 381 | } else { |
László Csomor | 6f1e31a | 2017-01-27 11:01:41 +0000 | [diff] [blame] | 382 | fprintf(stderr, "Java at '%s' exists but is not executable: %s\n", |
| 383 | java_program.c_str(), blaze_util::GetLastErrorString().c_str()); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 384 | } |
| 385 | exit(1); |
| 386 | } |
| 387 | // If the full JDK is installed |
Laszlo Csomor | 760f786 | 2016-12-19 15:46:47 +0000 | [diff] [blame] | 388 | string jdk_rt_jar = blaze_util::JoinPath(GetHostJavabase(), "jre/lib/rt.jar"); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 389 | // If just the JRE is installed |
Laszlo Csomor | 760f786 | 2016-12-19 15:46:47 +0000 | [diff] [blame] | 390 | string jre_rt_jar = blaze_util::JoinPath(GetHostJavabase(), "lib/rt.jar"); |
Laszlo Csomor | 00549b4 | 2017-01-11 09:12:10 +0000 | [diff] [blame] | 391 | if (blaze_util::CanReadFile(jdk_rt_jar) || |
| 392 | blaze_util::CanReadFile(jre_rt_jar)) { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 393 | 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 | |
| 400 | string StartupOptions::GetExe(const string &jvm, const string &jar_path) { |
| 401 | return jvm; |
| 402 | } |
| 403 | |
| 404 | void StartupOptions::AddJVMArgumentPrefix(const string &javabase, |
| 405 | std::vector<string> *result) const { |
| 406 | } |
| 407 | |
| 408 | void StartupOptions::AddJVMArgumentSuffix(const string &real_install_dir, |
| 409 | const string &jar_path, |
| 410 | std::vector<string> *result) const { |
| 411 | result->push_back("-jar"); |
Laszlo Csomor | 4875b25 | 2017-03-08 17:40:24 +0000 | [diff] [blame] | 412 | result->push_back( |
| 413 | blaze::PathAsJvmFlag(blaze_util::JoinPath(real_install_dir, jar_path))); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | blaze_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 Csomor | 760f786 | 2016-12-19 15:46:47 +0000 | [diff] [blame] | 420 | const string propFile = |
| 421 | blaze_util::JoinPath(output_base, "javalog.properties"); |
László Csomor | e64ed19 | 2017-02-23 15:43:54 +0000 | [diff] [blame] | 422 | string java_log( |
| 423 | blaze::PathAsJvmFlag(blaze_util::JoinPath(output_base, "java.log"))); |
Laszlo Csomor | 49970e0 | 2016-11-28 08:55:47 +0000 | [diff] [blame] | 424 | 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ó Csomor | e64ed19 | 2017-02-23 15:43:54 +0000 | [diff] [blame] | 428 | java_log + |
Laszlo Csomor | 760f786 | 2016-12-19 15:46:47 +0000 | [diff] [blame] | 429 | "\n" |
Julio Merino | 0bc863d | 2017-03-09 17:36:32 +0000 | [diff] [blame] | 430 | "java.util.logging.FileHandler.limit=1024000\n" |
Laszlo Csomor | 49970e0 | 2016-11-28 08:55:47 +0000 | [diff] [blame] | 431 | "java.util.logging.FileHandler.count=1\n" |
László Csomor | e64ed19 | 2017-02-23 15:43:54 +0000 | [diff] [blame] | 432 | "java.util.logging.FileHandler.formatter=" + |
| 433 | java_logging_formatter + "\n", |
Laszlo Csomor | 49970e0 | 2016-11-28 08:55:47 +0000 | [diff] [blame] | 434 | propFile)) { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 435 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 442 | } // namespace blaze |