diff --git a/docs/scala_toolchain.md b/docs/scala_toolchain.md index bb9796d43..d0e2d75b3 100644 --- a/docs/scala_toolchain.md +++ b/docs/scala_toolchain.md @@ -2,8 +2,6 @@ `scala_toolchain` allows you to define global configuration to all Scala targets. -Currently, the only option that can be set is `scalacopts` but the plan is to expand it to other options as well. - **Some scala_toolchain must be registered!** ### Several options to configure `scala_toolchain`: @@ -46,3 +44,57 @@ scala_register_toolchains() # WORKSPACE register_toolchains("//toolchains:my_scala_toolchain") ``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Attributes
scalacopts +

List of strings; optional

+

+ Extra compiler options for this binary to be passed to scalac. +

+

+ This is overridden by the `scalac_jvm_flags` attribute on individual targets. +

+
scalac_jvm_flags +

List of strings; optional

+

+ List of JVM flags to be passed to scalac. For example ["-Xmx5G"] could be passed to control memory usage of Scalac. +

+
unused_dependency_checker_mode +

String; optional

+

+ Enable unused dependency checking (see Unused dependency checking). + Possible values are: off, warn and error. +

+
enable_code_coverage_aspect +

"on" or "off"; optional; defaults to "off"

+

+ This enables instrumenting tests with jacoco code coverage. +

+
\ No newline at end of file diff --git a/scala/private/rule_impls.bzl b/scala/private/rule_impls.bzl index 8a40f55fc..96a96bae4 100644 --- a/scala/private/rule_impls.bzl +++ b/scala/private/rule_impls.bzl @@ -294,6 +294,13 @@ StatsfileOutput: {statsfile_output} resource_jars + [manifest, argfile] + scalac_inputs ) + # scalac_jvm_flags passed in on the target override scalac_jvm_flags passed in on the + # toolchain + if scalac_jvm_flags: + final_scalac_jvm_flags = _expand_location(ctx, scalac_jvm_flags) + else: + final_scalac_jvm_flags = ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scalac_jvm_flags + ctx.actions.run( inputs = ins, outputs = outs, @@ -312,7 +319,7 @@ StatsfileOutput: {statsfile_output} # consume the flags on startup. arguments = [ "--jvm_flag=%s" % f - for f in _expand_location(ctx, scalac_jvm_flags) + for f in final_scalac_jvm_flags ] + ["@" + argfile.path], ) diff --git a/scala/scala_toolchain.bzl b/scala/scala_toolchain.bzl index 157eae715..0faab9151 100644 --- a/scala/scala_toolchain.bzl +++ b/scala/scala_toolchain.bzl @@ -10,6 +10,7 @@ def _scala_toolchain_impl(ctx): unused_dependency_checker_mode = ctx.attr.unused_dependency_checker_mode, plus_one_deps_mode = ctx.attr.plus_one_deps_mode, enable_code_coverage_aspect = ctx.attr.enable_code_coverage_aspect, + scalac_jvm_flags = ctx.attr.scalac_jvm_flags, ) return [toolchain] @@ -32,6 +33,7 @@ scala_toolchain = rule( "enable_code_coverage_aspect": attr.string( default = "off", values = ["off", "on"], - ) + ), + "scalac_jvm_flags": attr.string_list(), }, ) diff --git a/test_expect_failure/scalac_jvm_opts/BUILD b/test_expect_failure/scalac_jvm_opts/BUILD new file mode 100644 index 000000000..1b7772ec0 --- /dev/null +++ b/test_expect_failure/scalac_jvm_opts/BUILD @@ -0,0 +1,43 @@ +load("//scala:scala_toolchain.bzl", "scala_toolchain") +load("//scala:scala.bzl", "scala_library") + +scala_toolchain( + name = "failing_toolchain_impl", + # This will fail because 1M isn't enough + scalac_jvm_flags = ["-Xmx1M"], + visibility = ["//visibility:public"], +) + +scala_toolchain( + name = "passing_toolchain_impl", + # This will pass because 1G is enough + scalac_jvm_flags = ["-Xmx1G"], + visibility = ["//visibility:public"], +) + +toolchain( + name = "failing_scala_toolchain", + toolchain = "failing_toolchain_impl", + toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type", + visibility = ["//visibility:public"], +) + +toolchain( + name = "passing_scala_toolchain", + toolchain = "passing_toolchain_impl", + toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type", + visibility = ["//visibility:public"], +) + +scala_library( + name = "empty_build", + srcs = ["Empty.scala"], +) + +scala_library( + name = "empty_overriding_build", + srcs = ["Empty.scala"], + # This overrides the option passed in on the toolchain, and should BUILD, even if + # the `failing_scala_toolchain` is used. + scalac_jvm_flags = ["-Xmx1G"], +) diff --git a/test_expect_failure/scalac_jvm_opts/Empty.scala b/test_expect_failure/scalac_jvm_opts/Empty.scala new file mode 100644 index 000000000..691dbdd9b --- /dev/null +++ b/test_expect_failure/scalac_jvm_opts/Empty.scala @@ -0,0 +1,3 @@ +package test_expect_failure.scalac_jvm_opts + +class Empty \ No newline at end of file diff --git a/test_rules_scala.sh b/test_rules_scala.sh index ff65051bb..f76542e91 100755 --- a/test_rules_scala.sh +++ b/test_rules_scala.sh @@ -825,6 +825,18 @@ test_scalaopts_from_scala_toolchain() { action_should_fail build --extra_toolchains="//test_expect_failure/scalacopts_from_toolchain:failing_scala_toolchain" //test_expect_failure/scalacopts_from_toolchain:failing_build } +test_scalac_jvm_flags_from_scala_toolchain_fails() { + action_should_fail build --extra_toolchains="//test_expect_failure/scalac_jvm_opts:failing_scala_toolchain" //test_expect_failure/scalac_jvm_opts:empty_build +} + +test_scalac_jvm_flags_from_scala_toolchain_passes() { + bazel build --extra_toolchains="//test_expect_failure/scalac_jvm_opts:passing_scala_toolchain" //test_expect_failure/scalac_jvm_opts:empty_build +} + +test_scalac_jvm_flags_on_target_overrides_toolchain_passes() { + bazel build --extra_toolchains="//test_expect_failure/scalac_jvm_opts:failing_scala_toolchain" //test_expect_failure/scalac_jvm_opts:empty_overriding_build +} + test_unused_dependency_checker_mode_set_in_rule() { action_should_fail build //test_expect_failure/unused_dependency_checker:failing_build } @@ -1104,3 +1116,6 @@ $runner bazel test //test/... --extra_toolchains="//test_expect_failure/plus_one $runner test_unused_dependency_fails_even_if_also_exists_in_plus_one_deps $runner test_coverage_on $runner scala_pb_library_targets_do_not_have_host_deps +$runner test_scalac_jvm_flags_on_target_overrides_toolchain_passes +$runner test_scalac_jvm_flags_from_scala_toolchain_passes +$runner test_scalac_jvm_flags_from_scala_toolchain_fails \ No newline at end of file