Skip to content

Commit fb6e0f0

Browse files
authored
Use ::: as scalacopts delimiter (#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 8f9f878 commit fb6e0f0

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
@@ -139,7 +139,9 @@ StatsfileOutput: {statsfile_output}
139139
""".format(
140140
out = output.path,
141141
manifest = manifest.path,
142-
scala_opts = ",".join(scalacopts),
142+
# Using ':::' as delimiter because ',' can collide with actual scalac options
143+
# https://github.com/bazelbuild/rules_scala/issues/1049
144+
scala_opts = ":::".join(scalacopts),
143145
print_compile_time = print_compile_time,
144146
expect_java_output = expect_java_output,
145147
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
@@ -36,7 +36,7 @@ public CompileOptions(List<String> args) {
3636
outputName = getOrError(argMap, "JarOutput", "Missing required arg JarOutput");
3737
manifestPath = getOrError(argMap, "Manifest", "Missing required arg Manifest");
3838

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

102+
protected static String[] getTripleColonList(Map<String, String> m, String k) {
103+
if (m.containsKey(k)) {
104+
String v = m.get(k);
105+
if ("".equals(v)) {
106+
return new String[] {};
107+
} else {
108+
return v.split(":::");
109+
}
110+
} else {
111+
return new String[] {};
112+
}
113+
}
114+
102115
private static String[] getCommaList(Map<String, String> m, String k) {
103116
if (m.containsKey(k)) {
104117
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)