Skip to content

Commit c8046cb

Browse files
committed
use toolchain
1 parent 4f8e59e commit c8046cb

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

java_stub_template/file/file.txt

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,64 @@ ARGS=(
289289
"${ARGS[@]}")
290290

291291

292+
function run_test_with_large_classpath() {
293+
if [ "%use_argument_file_in_runner_for_improved_performance%" == "True" ]; then
294+
run_test_with_argument_file "$1"
295+
else
296+
create_and_run_classpath_jar "$2"
297+
fi
298+
}
299+
292300
function create_and_run_classpath_jar() {
301+
# Build class path as one single string separated by spaces
302+
MANIFEST_CLASSPATH=""
303+
if is_windows; then
304+
CLASSPATH_SEPARATOR=";"
305+
URI_PREFIX="file:/" # e.g. "file:/C:/temp/foo.jar"
306+
else
307+
CLASSPATH_SEPARATOR=":"
308+
URI_PREFIX="file:$(pwd)/" # e.g. "file:/usr/local/foo.jar"
309+
fi
310+
311+
URI_PREFIX=${URI_PREFIX//\//\\/}
312+
MANIFEST_CLASSPATH="${CLASSPATH_SEPARATOR}${CLASSPATH}"
313+
314+
MANIFEST_CLASSPATH=$(sed "s/ /%20/g" <<< "${MANIFEST_CLASSPATH}")
315+
MANIFEST_CLASSPATH=$(sed "s/$CLASSPATH_SEPARATOR/ $URI_PREFIX/g" <<< "${MANIFEST_CLASSPATH}")
316+
317+
# Create manifest file
318+
MANIFEST_FILE="$(mktemp -t XXXXXXXX.jar_manifest)"
319+
320+
(
321+
echo "Manifest-Version: 1.0"
322+
323+
CLASSPATH_LINE="Class-Path:$MANIFEST_CLASSPATH"
324+
# No line in the MANIFEST.MF file may be longer than 72 bytes.
325+
# A space prefix indicates the line is still the content of the last attribute.
326+
CLASSPATH_MANIFEST_LINES=$(sed -E $'s/(.{71})/\\1\\\n /g' <<< "${CLASSPATH_LINE}")
327+
echo "$CLASSPATH_MANIFEST_LINES"
328+
echo "Created-By: Bazel"
329+
) >$MANIFEST_FILE
330+
331+
# Create classpath JAR file
332+
MANIFEST_JAR_FILE="$(mktemp -t XXXXXXXX-classpath.jar)"
333+
if is_windows; then
334+
MANIFEST_JAR_FILE="$(cygpath --windows "$MANIFEST_JAR_FILE")"
335+
MANIFEST_FILE="$(cygpath --windows "$MANIFEST_FILE")"
336+
fi
337+
JARBIN="${JARBIN:-$(rlocation "$1")}"
338+
$JARBIN cvfm "$MANIFEST_JAR_FILE" "$MANIFEST_FILE" >/dev/null || \
339+
die "ERROR: $self failed because $JARBIN failed"
340+
341+
# Execute JAVA command
342+
$JAVABIN -classpath "$MANIFEST_JAR_FILE" "${ARGS[@]}"
343+
exit_code=$?
344+
rm -f "$MANIFEST_FILE"
345+
rm -f "$MANIFEST_JAR_FILE"
346+
exit $exit_code
347+
}
348+
349+
function run_test_with_argument_file() {
293350
ARGS_FILE="$(mktemp -t XXXXXXXX.args)"
294351
echo "-cp "$CLASSPATH >> $ARGS_FILE
295352
$JAVABIN @$ARGS_FILE "${ARGS[@]}"
@@ -317,9 +374,9 @@ fi
317374
#Difference ends
318375

319376
if is_windows && (("${#CLASSPATH}" > ${CLASSPATH_LIMIT} )); then
320-
create_and_run_classpath_jar "${JARBIN}.exe"
377+
run_test_with_large_classpath "${JARBIN}.exe"
321378
elif (("${#CLASSPATH}" > ${CLASSPATH_LIMIT})); then
322-
create_and_run_classpath_jar "$JARBIN"
379+
run_test_with_large_classpath "$JARBIN"
323380
else
324381
exec $JAVABIN -classpath $CLASSPATH "${ARGS[@]}"
325382
fi

scala/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ toolchain_type(
1111
scala_toolchain(
1212
name = "default_toolchain_impl",
1313
scalacopts = [],
14+
use_argument_file_in_runner_for_improved_performance = True,
1415
visibility = ["//visibility:public"],
1516
)
1617

scala/private/phases/phase_write_executable.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ def _write_executable_non_windows(ctx, executable, rjars, main_class, jvm_flags,
108108
wrapper.short_path,
109109
)
110110

111+
scala_toolchain = ctx.toolchains["//scala:toolchain_type"]
112+
111113
if use_jacoco and ctx.configuration.coverage_enabled:
112-
jacocorunner = ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].jacocorunner
114+
jacocorunner = scala_toolchain.jacocorunner
113115
classpath = ctx.configuration.host_path_separator.join(
114116
["${RUNPATH}%s" % (j.short_path) for j in rjars.to_list() + jacocorunner.files.to_list()],
115117
)
@@ -137,6 +139,7 @@ def _write_executable_non_windows(ctx, executable, rjars, main_class, jvm_flags,
137139
"%set_jacoco_main_class%": """export JACOCO_MAIN_CLASS={}""".format(main_class),
138140
"%set_jacoco_java_runfiles_root%": """export JACOCO_JAVA_RUNFILES_ROOT=$JAVA_RUNFILES/{}/""".format(ctx.workspace_name),
139141
"%set_java_coverage_new_implementation%": """export JAVA_COVERAGE_NEW_IMPLEMENTATION=YES""",
142+
"%use_argument_file_in_runner_for_improved_performance%": str(scala_toolchain.use_argument_file_in_runner_for_improved_performance),
140143
},
141144
is_executable = True,
142145
)
@@ -163,6 +166,7 @@ def _write_executable_non_windows(ctx, executable, rjars, main_class, jvm_flags,
163166
"%set_jacoco_java_runfiles_root%": "",
164167
"%workspace_prefix%": ctx.workspace_name + "/",
165168
"%set_java_coverage_new_implementation%": """export JAVA_COVERAGE_NEW_IMPLEMENTATION=NO""",
169+
"%use_argument_file_in_runner_for_improved_performance%": str(scala_toolchain.use_argument_file_in_runner_for_improved_performance),
166170
},
167171
is_executable = True,
168172
)

scala/scala_toolchain.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def _scala_toolchain_impl(ctx):
8484
enable_diagnostics_report = enable_diagnostics_report,
8585
jacocorunner = ctx.attr.jacocorunner,
8686
enable_stats_file = enable_stats_file,
87+
use_argument_file_in_runner_for_improved_performance = ctx.attr.use_argument_file_in_runner_for_improved_performance,
8788
)
8889
return [toolchain]
8990

@@ -137,6 +138,10 @@ scala_toolchain = rule(
137138
default = True,
138139
doc = "Enable writing of statsfile",
139140
),
141+
"use_argument_file_in_runner_for_improved_performance": attr.bool(
142+
default = False,
143+
doc = "Changes java binaries scripts (including tests) to use argument files and not classpath jars to improve performance, requires java > 8"
144+
)
140145
},
141146
fragments = ["java"],
142147
)

test/shell/test_scala_binary.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ test_scala_binary_expect_failure_on_missing_direct_deps_located_in_dependency_wh
1818
test_scala_library_expect_failure_on_missing_direct_deps ${dependency_target} ${test_target}
1919
}
2020

21+
test_scala_binary_allows_opt_in_to_use_of_argument_file_in_runner_for_improved_performance() {
22+
23+
bazel test --extra_toolchains="//test/toolchains:use_argument_file_in_runner_for_improved_performance" //test/src/main/scala/scalarules/test/large_classpath:largeClasspath
24+
25+
}
26+
2127
$runner test_scala_binary_expect_failure_on_missing_direct_deps
2228
$runner test_scala_binary_expect_failure_on_missing_direct_deps_located_in_dependency_which_is_scala_binary
29+
$runner test_scala_binary_allows_opt_in_to_use_of_argument_file_in_runner_for_improved_performance

test/toolchains/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,16 @@ toolchain(
117117
toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type",
118118
visibility = ["//visibility:public"],
119119
)
120+
121+
scala_toolchain(
122+
name = "use_argument_file_in_runner_for_improved_performance_impl",
123+
use_argument_file_in_runner_for_improved_performance = True,
124+
visibility = ["//visibility:public"],
125+
)
126+
127+
toolchain(
128+
name = "use_argument_file_in_runner_for_improved_performance",
129+
toolchain = "use_argument_file_in_runner_for_improved_performance_impl",
130+
toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type",
131+
visibility = ["//visibility:public"],
132+
)

0 commit comments

Comments
 (0)