Skip to content

Commit 794bce0

Browse files
wisechengyiAndre Rocha
authored and
Andre Rocha
committed
Use ::: as scalacopts delimiter (bazel-contrib#1053)
* initial * polish * lint * one more * remove integ test * add unit test * add tests that uses scalacopts * minor * comment * accumlative macro options * rm tests * more clear options
1 parent a248d1e commit 794bce0

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

scala/private/rule_impls.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ DiagnosticsFile: {diagnostics_output}
141141
""".format(
142142
out = output.path,
143143
manifest = manifest.path,
144-
scala_opts = ",".join(scalacopts),
144+
# Using ':::' as delimiter because ',' can collide with actual scalac options
145+
# https://github.com/bazelbuild/rules_scala/issues/1049
146+
scala_opts = ":::".join(scalacopts),
145147
print_compile_time = print_compile_time,
146148
expect_java_output = expect_java_output,
147149
plugin_arg = plugin_arg,

src/java/io/bazel/rulesscala/scalac/CompileOptions.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public CompileOptions(List<String> args) {
3737
outputName = getOrError(argMap, "JarOutput", "Missing required arg JarOutput");
3838
manifestPath = getOrError(argMap, "Manifest", "Missing required arg Manifest");
3939

40-
scalaOpts = getCommaList(argMap, "ScalacOpts");
40+
scalaOpts = getTripleColonList(argMap, "ScalacOpts");
4141
printCompileTime = booleanGetOrFalse(argMap, "PrintCompileTime");
4242
expectJavaOutput = booleanGetOrTrue(argMap, "ExpectJavaOutput");
4343
pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins"));
@@ -101,6 +101,19 @@ private static HashMap<String, String> buildArgMap(List<String> lines) {
101101
return hm;
102102
}
103103

104+
protected static String[] getTripleColonList(Map<String, String> m, String k) {
105+
if (m.containsKey(k)) {
106+
String v = m.get(k);
107+
if ("".equals(v)) {
108+
return new String[] {};
109+
} else {
110+
return v.split(":::");
111+
}
112+
} else {
113+
return new String[] {};
114+
}
115+
}
116+
104117
private static String[] getCommaList(Map<String, String> m, String k) {
105118
if (m.containsKey(k)) {
106119
String v = m.get(k);

test/scalacopts/A.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package scalacopts
2+
3+
import scala.reflect.macros.blackbox
4+
import scala.language.experimental.macros
5+
6+
object Macros {
7+
def hello: String = macro macroSettings
8+
9+
def macroSettings(c: blackbox.Context): c.Expr[String] = {
10+
import c.universe._
11+
// c.settings are the values from scalac's -Xmacro-settings
12+
val s = c.settings.mkString(",")
13+
c.Expr(q"""${s}""")
14+
}
15+
}

test/scalacopts/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("//scala:scala.bzl", "scala_macro_library", "scala_test")
2+
3+
scala_macro_library(
4+
name = "A",
5+
srcs = ["A.scala"],
6+
)
7+
8+
scala_test(
9+
name = "EchoMacroSettings",
10+
srcs = ["EchoMacroSettings.scala"],
11+
scalacopts = [
12+
# The macro settings are used in the test itself.
13+
"-Xmacro-settings:name=Mike,location=US",
14+
"-Xmacro-settings:hobby=basketball,soccer",
15+
],
16+
deps = [
17+
":A",
18+
],
19+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package scalacopts
16+
17+
import org.scalatest._
18+
19+
class EchoMacroSettings extends FlatSpec {
20+
"macro output" should "match scalac options" in {
21+
assert(Macros.hello.equals("name=Mike,location=US,hobby=basketball,soccer"))
22+
}
23+
}

0 commit comments

Comments
 (0)