Skip to content

Commit 35a7dc2

Browse files
Add a few cross-build examples (#1587)
* Set up example * A few examples --------- Co-authored-by: mkuta <[email protected]>
1 parent d4d8316 commit 35a7dc2

File tree

14 files changed

+182
-1
lines changed

14 files changed

+182
-1
lines changed

examples/crossbuild/1_single/BUILD

+25
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+
)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object C extends App {
2+
println("Hello")
3+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A
+7
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

+37
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

+3
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

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class B

examples/crossbuild/3_select/BUILD

+35
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+
)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object B extends App {
2+
(new A).say
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class A {
2+
def say = println("Hello 2")
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class A:
2+
def say =
3+
println("Hello 3")

examples/crossbuild/WORKSPACE

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
workspace(name = "cross_build")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "bazel_skylib",
7+
sha256 = "d00f1389ee20b60018e92644e0948e16e350a7707219e7a390fb0a99b6ec9262",
8+
urls = [
9+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.0/bazel-skylib-1.7.0.tar.gz",
10+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.0/bazel-skylib-1.7.0.tar.gz",
11+
],
12+
)
13+
14+
local_repository(
15+
name = "io_bazel_rules_scala",
16+
path = "../..",
17+
)
18+
19+
load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")
20+
21+
scala_config(
22+
scala_version = "3.3.1",
23+
scala_versions = [
24+
"2.11.12",
25+
"2.13.12",
26+
"3.3.1",
27+
],
28+
)
29+
30+
load(
31+
"@io_bazel_rules_scala//scala:scala.bzl",
32+
"rules_scala_setup",
33+
"rules_scala_toolchain_deps_repositories",
34+
)
35+
36+
rules_scala_setup()
37+
38+
rules_scala_toolchain_deps_repositories()
39+
40+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
41+
42+
rules_proto_dependencies()
43+
44+
rules_proto_toolchains()
45+
46+
load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")
47+
48+
scala_register_toolchains()
49+
50+
load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")
51+
52+
scalatest_repositories()
53+
54+
scalatest_toolchain()

test/shell/test_examples.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ function semanticdb_example() {
5151
test_example examples/semanticdb build_semanticdb_example
5252
}
5353

54+
function cross_build_example() {
55+
test_example examples/crossbuild "bazel build //..."
56+
}
57+
5458
$runner scalatest_repositories_example
5559
$runner specs2_junit_repositories_example
5660
$runner multi_framework_toolchain_example
5761
$runner semanticdb_example
5862
$runner scala3_1_example
5963
$runner scala3_2_example
60-
$runner scala3_3_example
64+
$runner scala3_3_example
65+
$runner cross_build_example

0 commit comments

Comments
 (0)