Skip to content

Commit 150744c

Browse files
committed
A few examples
1 parent 6067a41 commit 150744c

File tree

12 files changed

+122
-0
lines changed

12 files changed

+122
-0
lines changed

examples/crossbuild/1_single/BUILD

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library", "scala_test")
2+
3+
# Here we demonstrate the simplest case,
4+
# single binary, test or library for which we set a specific version or use the default one:
5+
6+
# This one will be compiled by 2.11 compiler:
7+
scala_library(
8+
name = "lib211",
9+
srcs = ["lib.scala"],
10+
scala_version = "2.11.12",
11+
)
12+
13+
# This one will be compiled by 2.13 compiler:
14+
scala_test(
15+
name = "test213",
16+
srcs = ["test.scala"],
17+
scala_version = "2.13.12",
18+
)
19+
20+
# This one will be compiled by 3.3 compiler (the default one):
21+
scala_binary(
22+
name = "bin33",
23+
srcs = ["bin.scala"],
24+
main_class = "X",
25+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object C extends App {
2+
println("Hello")
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import org.scalatest.flatspec.AnyFlatSpec
2+
3+
class Zero extends AnyFlatSpec {
4+
"Equality" should "be tested" in {
5+
assert (0 == -0)
6+
}
7+
}

examples/crossbuild/2_deps/BUILD

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library")
2+
3+
# Here we demonstrate how scala_version is propagated through deps.
4+
5+
# This one will always be compiled by 2.11 compiler:
6+
scala_library(
7+
name = "lib211",
8+
srcs = ["lib.scala"],
9+
scala_version = "2.11.12",
10+
)
11+
12+
# This one will be compiled by 3.3 compiler (unless requested otherwise)
13+
scala_library(
14+
name = "lib",
15+
srcs = ["lib_default.scala"],
16+
)
17+
18+
scala_binary(
19+
name = "bin213",
20+
srcs = ["bin.scala"], # compiled with 2.13 (as per `scala_version`)
21+
main_class = "C",
22+
scala_version = "2.13.12",
23+
deps = [
24+
":lib", # compiled 2.13 (as per `scala_version`)
25+
":lib211", # compiled with 2.11 (that target overrides version)
26+
],
27+
)
28+
29+
scala_binary(
30+
name = "bin33",
31+
srcs = ["bin.scala"], # compiled with 3.3 (the default)
32+
main_class = "C",
33+
deps = [
34+
":lib", # compiled with 3.3 (default)
35+
":lib211", # compiled with 2.11 (that target overrides version)
36+
],
37+
)

examples/crossbuild/2_deps/bin.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object C extends App {
2+
println("Hello, world")
3+
}

examples/crossbuild/2_deps/lib.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class B

examples/crossbuild/3_select/BUILD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library")
2+
load("@io_bazel_rules_scala//scala:scala_cross_version_select.bzl", "select_for_scala_version")
3+
4+
# Here we demonstrate how to provide distinct source files depending on the version requested
5+
6+
# Trying to provide library that works with all Scala versions:
7+
scala_library(
8+
name = "lib",
9+
srcs = select_for_scala_version(
10+
before_3 = [
11+
# for Scala version < 3
12+
"lib2.scala",
13+
],
14+
since_3 = [
15+
# for 3 ≤ Scala version
16+
"lib3.scala",
17+
],
18+
),
19+
)
20+
21+
scala_binary(
22+
name = "bin2",
23+
srcs = ["bin.scala"],
24+
main_class = "B",
25+
scala_version = "2.13.12",
26+
deps = [":lib"],
27+
)
28+
29+
scala_binary(
30+
name = "bin3",
31+
srcs = ["bin.scala"],
32+
main_class = "B",
33+
scala_version = "3.3.1",
34+
deps = [":lib"],
35+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object B extends App {
2+
(new A).say
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class A {
2+
def say = println("Hello 2")
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class A:
2+
def say =
3+
println("Hello 3")

0 commit comments

Comments
 (0)