From 313e9756b8e5dbee16050d585b5a997a8abaaeab Mon Sep 17 00:00:00 2001 From: Andy Scott Date: Fri, 27 Dec 2019 12:58:20 -0500 Subject: [PATCH] expand locations in scalacptops, take 2 (#907) * expand locations in scalacopts (#890) * expand locations in scalac options * allow plugins in expansion * add a happy path test * make the target names more obvious * comment * access ctx.attr.plugins with fallback * reformat --- scala/private/rule_impls.bzl | 3 +- test/plugins/BUILD | 40 +++++++++++++++++++ .../check_expand_location_plugin.scala | 25 ++++++++++++ test/plugins/trivial.scala | 5 +++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 test/plugins/BUILD create mode 100644 test/plugins/check_expand_location_plugin.scala create mode 100644 test/plugins/trivial.scala diff --git a/scala/private/rule_impls.bzl b/scala/private/rule_impls.bzl index 925179430..34469e913 100644 --- a/scala/private/rule_impls.bzl +++ b/scala/private/rule_impls.bzl @@ -238,7 +238,8 @@ CurrentTarget: {current_target} compiler_classpath = _join_path(compiler_classpath_jars.to_list(), separator) toolchain = ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"] - scalacopts = toolchain.scalacopts + in_scalacopts + scalacopts_expansion_targets = getattr(ctx.attr, "plugins", []) + scalacopts = [ctx.expand_location(v, scalacopts_expansion_targets) for v in toolchain.scalacopts + in_scalacopts] scalac_args = """ Classpath: {cp} diff --git a/test/plugins/BUILD b/test/plugins/BUILD new file mode 100644 index 000000000..1a5cf607e --- /dev/null +++ b/test/plugins/BUILD @@ -0,0 +1,40 @@ +load("//scala:scala.bzl", "scala_library") + +scala_library( + name = "check_expand_location", + srcs = ["trivial.scala"], + plugins = [ + ":check_expand_location_plugin_deploy.jar", + ], + scalacopts = [ + "-P:diablerie:location=$(location :check_expand_location_plugin_deploy.jar)", + ], +) + +scala_library( + name = "check_expand_location_plugin", + srcs = [ + "check_expand_location_plugin.scala", + ], + resource_strip_prefix = package_name(), + resources = [ + ":gen-scalac-plugin.xml", + ], + deps = [ + "@io_bazel_rules_scala_scala_compiler", + ], +) + +_gen_plugin_xml_cmd = """ +cat > $@ << EOF + + plugin + plugin.Plugin + +""" + +genrule( + name = "gen-scalac-plugin.xml", + outs = ["scalac-plugin.xml"], + cmd = _gen_plugin_xml_cmd, +) diff --git a/test/plugins/check_expand_location_plugin.scala b/test/plugins/check_expand_location_plugin.scala new file mode 100644 index 000000000..fc5f61289 --- /dev/null +++ b/test/plugins/check_expand_location_plugin.scala @@ -0,0 +1,25 @@ +package plugin + +import scala.tools.nsc.Global +import scala.tools.nsc.Phase +import scala.tools.nsc.plugins.{ Plugin => NscPlugin} +import scala.tools.nsc.plugins.PluginComponent + +import java.io.File + +final class Plugin(override val global: Global) extends NscPlugin { + override val name: String = "diablerie" + override val description: String = "just another plugin" + override val components: List[PluginComponent] = Nil + + override def processOptions(options: List[String], error: String => Unit): Unit = { + options + .find(_.startsWith("location=")) + .map(_.stripPrefix("location=")) + .map(v => new File(v).exists) match { + case Some(true) => () + case Some(false) => error("expanded location doesn't exist") + case None => error("missing location argument") + } + } +} diff --git a/test/plugins/trivial.scala b/test/plugins/trivial.scala new file mode 100644 index 000000000..ed5e226bc --- /dev/null +++ b/test/plugins/trivial.scala @@ -0,0 +1,5 @@ +package trivial + +object Trivial { + // feel free to reuse this file for other plugin tests +}