Skip to content

Commit 533657e

Browse files
janakdrdamienmg
authored andcommitted
Optionally allow Bazel to pass JVM options containing spaces directly through to the JVM instead of (almost certainly incorrectly) splitting the options along spaces.
This allows us to pass non-quote-delimited strings to the JVM, which is necessary for things like -XX:OnOutOfMemoryError="kill -3 %p" (normally bash strips those quotes, but they're not stripped when passed via --host_jvm_args). -- MOS_MIGRATED_REVID=107820087
1 parent b503be7 commit 533657e

7 files changed

Lines changed: 60 additions & 18 deletions

File tree

site/docs/bazel-user-manual.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,16 +3282,17 @@ <h4 id='flag--output_user_root'><code class='flag'>--output_user_root=<var>dir</
32823282

32833283
<h4 id='flag--host_jvm_args'><code class='flag'>--host_jvm_args=<var>string</var></code></h4>
32843284
<p>
3285-
Specifies a space-separated list of startup options to be passed to
3286-
the Java virtual machine in which <i>Bazel itself</i> runs. This
3287-
can be used to set the stack size, for example:
3285+
Specifies a startup option to be passed to the Java virtual machine in which <i>Bazel itself</i>
3286+
runs. This can be used to set the stack size, for example:
32883287
</p>
32893288
<pre>
32903289
% bazel --host_jvm_args="-Xss256K" build //foo
32913290
</pre>
32923291
<p>
32933292
This option can be used multiple times with individual arguments. Note that
3294-
setting this flag should rarely be needed.
3293+
setting this flag should rarely be needed. You can also pass a space-separated list of strings,
3294+
each of which will be interpreted as a separate JVM argument, but this feature will soon be
3295+
deprecated.
32953296

32963297
</p>
32973298
<p>

src/main/cpp/blaze.cc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,25 @@ static vector<string> GetArgumentArray() {
261261

262262
vector<string> user_options;
263263

264-
blaze_util::SplitQuotedStringUsing(globals->options.host_jvm_args, ' ',
265-
&user_options);
264+
if (globals->options.preserve_spaces_in_host_jvm_args) {
265+
user_options.insert(user_options.begin(),
266+
globals->options.host_jvm_args.begin(),
267+
globals->options.host_jvm_args.end());
268+
} else {
269+
for (const auto &arg : globals->options.host_jvm_args) {
270+
// int num_segments =
271+
blaze_util::SplitQuotedStringUsing(arg, ' ', &user_options);
272+
// TODO(janakr): Enable this warning when users have been migrated.
273+
// if (num_segments > 1) {
274+
// fprintf(stderr, "WARNING: You are passing multiple jvm options"
275+
// " under a single --host_jvm_args option: %s. This will stop
276+
// working "
277+
// "soon. Instead, pass each option under its own
278+
// --host_jvm_args "
279+
// "option.\n", arg);
280+
//
281+
}
282+
}
266283

267284
// Add JVM arguments particular to building blaze64 and particular JVM
268285
// versions.
@@ -350,8 +367,13 @@ static vector<string> GetArgumentArray() {
350367
if (!globals->options.host_jvm_profile.empty()) {
351368
result.push_back("--host_jvm_profile=" + globals->options.host_jvm_profile);
352369
}
370+
if (globals->options.preserve_spaces_in_host_jvm_args) {
371+
result.push_back("--experimental_preserve_spaces_in_host_jvm_args");
372+
}
353373
if (!globals->options.host_jvm_args.empty()) {
354-
result.push_back("--host_jvm_args=" + globals->options.host_jvm_args);
374+
for (const auto &arg : globals->options.host_jvm_args) {
375+
result.push_back("--host_jvm_args=" + arg);
376+
}
355377
}
356378

357379
if (globals->options.invocation_policy != NULL &&

src/main/cpp/blaze_startup_options.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ class BlazeStartupOptions {
126126

127127
string host_jvm_profile;
128128

129-
string host_jvm_args;
129+
bool preserve_spaces_in_host_jvm_args;
130+
131+
std::vector<string> host_jvm_args;
130132

131133
bool batch;
132134

src/main/cpp/blaze_startup_options_common.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void BlazeStartupOptions::Init() {
4141
block_for_lock = true;
4242
host_jvm_debug = false;
4343
host_javabase = "";
44+
// TODO(janakr): change this to true when ready, then delete it.
45+
preserve_spaces_in_host_jvm_args = false;
4446
batch = false;
4547
batch_cpu_scheduling = false;
4648
blaze_cpu = false;
@@ -73,6 +75,7 @@ void BlazeStartupOptions::Copy(
7375
lhs->host_jvm_debug = rhs.host_jvm_debug;
7476
lhs->host_jvm_profile = rhs.host_jvm_profile;
7577
lhs->host_javabase = rhs.host_javabase;
78+
lhs->preserve_spaces_in_host_jvm_args = rhs.preserve_spaces_in_host_jvm_args;
7679
lhs->host_jvm_args = rhs.host_jvm_args;
7780
lhs->batch = rhs.batch;
7881
lhs->batch_cpu_scheduling = rhs.batch_cpu_scheduling;
@@ -126,13 +129,13 @@ blaze_exit_code::ExitCode BlazeStartupOptions::ProcessArg(
126129
// and re-execing.
127130
host_javabase = MakeAbsolute(value);
128131
option_sources["host_javabase"] = rcfile;
132+
} else if (GetNullaryOption(
133+
arg, "--experimental_preserve_spaces_in_host_jvm_args")) {
134+
preserve_spaces_in_host_jvm_args = true;
135+
option_sources["preserve_spaces_in_host_jvm_args"] = rcfile;
129136
} else if ((value = GetUnaryOption(arg, next_arg,
130137
"--host_jvm_args")) != NULL) {
131-
if (host_jvm_args.empty()) {
132-
host_jvm_args = value;
133-
} else {
134-
host_jvm_args = host_jvm_args + " " + value;
135-
}
138+
host_jvm_args.push_back(value);
136139
option_sources["host_jvm_args"] = rcfile; // NB: This is incorrect
137140
} else if ((value = GetUnaryOption(arg, next_arg, "--blaze_cpu")) != NULL) {
138141
blaze_cpu = true;

src/main/cpp/util/strings.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ void SplitStringUsing(
128128
}
129129
}
130130

131-
void SplitQuotedStringUsing(const string &contents, const char delimeter,
132-
std::vector<string> *output) {
131+
size_t SplitQuotedStringUsing(const string &contents, const char delimeter,
132+
std::vector<string> *output) {
133133
size_t len = contents.length();
134134
size_t start = 0;
135135
size_t quote = string::npos; // quote position
136+
size_t num_segments = 0;
136137

137138
for (size_t pos = 0; pos < len; ++pos) {
138139
if (start == pos && contents[start] == delimeter) {
@@ -147,13 +148,16 @@ void SplitQuotedStringUsing(const string &contents, const char delimeter,
147148
} else if (quote == string::npos && contents[pos] == delimeter) {
148149
output->push_back(string(contents, start, pos - start));
149150
start = pos + 1;
151+
num_segments++;
150152
}
151153
}
152154

153155
// A trailing element
154156
if (start < len) {
155157
output->push_back(string(contents, start));
158+
num_segments++;
156159
}
160+
return num_segments;
157161
}
158162

159163
void Replace(const string &oldsub, const string &newsub, string *str) {

src/main/cpp/util/strings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ std::vector<string> Split(const string &contents, const char delimeter);
8080
void SplitStringUsing(
8181
const string &contents, const char delimeter, std::vector<string> *output);
8282

83-
// Same as above, but adds results to output.
84-
void SplitQuotedStringUsing(const string &contents, const char delimeter,
85-
std::vector<string> *output);
83+
// Same as above, but adds results to output. Returns number of elements added.
84+
size_t SplitQuotedStringUsing(const string &contents, const char delimeter,
85+
std::vector<string> *output);
8686

8787
// Global replace of oldsub with newsub.
8888
void Replace(const string &oldsub, const string &newsub, string *str);

src/main/java/com/google/devtools/build/lib/runtime/BlazeServerStartupOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ public String getTypeDescription() {
213213
help = "Unused.")
214214
public String unusedSkyframe;
215215

216+
@Option(
217+
name = "experimental_preserve_spaces_in_host_jvm_args",
218+
defaultValue = "false",
219+
category = "undocumented",
220+
help =
221+
"If this option is true, each argument to --host_jvm_args is considered a single JVM "
222+
+ "flag, even if it has spaces in it."
223+
)
224+
public boolean unusedPreserveSpacesInHostJvmArgs;
225+
216226
@Option(name = "fatal_event_bus_exceptions",
217227
defaultValue = "false", // NOTE: purely decorative!
218228
category = "undocumented",

0 commit comments

Comments
 (0)