Skip to content

Commit

Permalink
Use jar from java runtime and not local_jdk (#720)
Browse files Browse the repository at this point in the history
* Use jar from java runtime and not local_jdk
Work around and somewhat prepare for resolution of
bazelbuild/bazel#6955

* use stub template from an http_archive

* remove unused symbol
  • Loading branch information
ittaiz authored and johnynek committed Mar 27, 2019
1 parent 379c65e commit c904132
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
10 changes: 10 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
workspace(name = "io_bazel_rules_scala")


load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
#used to test what happens when classpath is too large
#until https://github.com/bazelbuild/bazel/issues/6955 is resolved
http_archive(
name = "java_stub_template",
sha256 = "1859a37dccaee8c56b98869bf1f22f6f5b909606aff74ddcfd59e9757a038dd5",
urls = ["https://github.com/bazelbuild/rules_scala/archive/8b8271e3ee5709e1340b19790d0b396a0ff3dd0f.tar.gz"],
strip_prefix = "rules_scala-8b8271e3ee5709e1340b19790d0b396a0ff3dd0f/java_stub_template",
)

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("//scala:scala.bzl", "scala_repositories")

Expand Down
19 changes: 14 additions & 5 deletions scala/private/rule_impls.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,18 @@ def _path_is_absolute(path):
def _runfiles_root(ctx):
return "${TEST_SRCDIR}/%s" % ctx.workspace_name

def _write_java_wrapper(ctx, args = "", wrapper_preamble = ""):
"""This creates a wrapper that sets up the correct path
to stand in for the java command."""

def _java_bin(ctx):
java_path = str(ctx.attr._java_runtime[java_common.JavaRuntimeInfo].java_executable_runfiles_path)
if _path_is_absolute(java_path):
javabin = java_path
else:
runfiles_root = _runfiles_root(ctx)
javabin = "%s/%s" % (runfiles_root, java_path)
return javabin

def _write_java_wrapper(ctx, args = "", wrapper_preamble = ""):
"""This creates a wrapper that sets up the correct path
to stand in for the java command."""

exec_str = ""
if wrapper_preamble == "":
Expand All @@ -576,13 +578,18 @@ JAVA_EXEC_TO_USE=${{REAL_EXTERNAL_JAVA_BIN:-$DEFAULT_JAVABIN}}
""".format(
preamble = wrapper_preamble,
exec_str = exec_str,
javabin = javabin,
javabin = _java_bin(ctx),
args = args,
),
is_executable = True,
)
return wrapper

def _jar_path_based_on_java_bin(ctx):
java_bin = _java_bin(ctx)
jar_path = java_bin.rpartition("/")[0] + "/jar"
return jar_path

def _write_executable(ctx, rjars, main_class, jvm_flags, wrapper, use_jacoco):
template = ctx.attr._java_stub_template.files.to_list()[0]

Expand Down Expand Up @@ -613,6 +620,7 @@ def _write_executable(ctx, rjars, main_class, jvm_flags, wrapper, use_jacoco):
substitutions = {
"%classpath%": classpath,
"%javabin%": javabin,
"%jarbin%": _jar_path_based_on_java_bin(ctx),
"%jvm_flags%": jvm_flags,
"%needs_runfiles%": "",
"%runfiles_manifest_only%": "",
Expand All @@ -638,6 +646,7 @@ def _write_executable(ctx, rjars, main_class, jvm_flags, wrapper, use_jacoco):
"%classpath%": classpath,
"%java_start_class%": main_class,
"%javabin%": javabin,
"%jarbin%": _jar_path_based_on_java_bin(ctx),
"%jvm_flags%": jvm_flags,
"%needs_runfiles%": "",
"%runfiles_manifest_only%": "",
Expand Down
7 changes: 7 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ scala_specs2_junit_test(
"//test:test_scala_proto_server",
]]

sh_test(
name = "test_binary_run_with_large_classpath",
srcs = ["test_binary_run_with_large_classpath.sh"],
args = ["$(location //test/src/main/scala/scalarules/test/large_classpath:largeClasspath)"],
data = ["//test/src/main/scala/scalarules/test/large_classpath:largeClasspath"],
)

# Make sure scala_binary works in genrule environment
genrule(
name = "ScalaBinaryInGenrule",
Expand Down
6 changes: 6 additions & 0 deletions test/test_binary_run_with_large_classpath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

echo "Executing: " $@
#limiting the classpath to simulate a large classpath which is over the OS limit
export CLASSPATH_LIMIT=10
$@

0 comments on commit c904132

Please sign in to comment.