Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few cross-build examples #1587

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/crossbuild/1_single/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library", "scala_test")

# Here we demonstrate the simplest case,
# single binary, test or library for which we set a specific version or use the default one:

# This one will be compiled by 2.11 compiler:
scala_library(
name = "lib211",
srcs = ["lib.scala"],
scala_version = "2.11.12",
)

# This one will be compiled by 2.13 compiler:
scala_test(
name = "test213",
srcs = ["test.scala"],
scala_version = "2.13.12",
)

# This one will be compiled by 3.3 compiler (the default one):
scala_binary(
name = "bin33",
srcs = ["bin.scala"],
main_class = "X",
)
3 changes: 3 additions & 0 deletions examples/crossbuild/1_single/bin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object C extends App {
println("Hello")
}
1 change: 1 addition & 0 deletions examples/crossbuild/1_single/lib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A
7 changes: 7 additions & 0 deletions examples/crossbuild/1_single/test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import org.scalatest.flatspec.AnyFlatSpec

class Zero extends AnyFlatSpec {
"Equality" should "be tested" in {
assert (0 == -0)
}
}
37 changes: 37 additions & 0 deletions examples/crossbuild/2_deps/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library")

# Here we demonstrate how scala_version is propagated through deps.

# This one will always be compiled by 2.11 compiler:
scala_library(
name = "lib211",
srcs = ["lib.scala"],
scala_version = "2.11.12",
)

# This one will be compiled by 3.3 compiler (unless requested otherwise)
scala_library(
name = "lib",
srcs = ["lib_default.scala"],
)

scala_binary(
name = "bin213",
srcs = ["bin.scala"], # compiled with 2.13 (as per `scala_version`)
main_class = "C",
scala_version = "2.13.12",
deps = [
":lib", # compiled 2.13 (as per `scala_version`)
":lib211", # compiled with 2.11 (that target overrides version)
],
)

scala_binary(
name = "bin33",
srcs = ["bin.scala"], # compiled with 3.3 (the default)
main_class = "C",
deps = [
":lib", # compiled with 3.3 (default)
":lib211", # compiled with 2.11 (that target overrides version)
],
)
3 changes: 3 additions & 0 deletions examples/crossbuild/2_deps/bin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object C extends App {
println("Hello, world")
}
1 change: 1 addition & 0 deletions examples/crossbuild/2_deps/lib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A
1 change: 1 addition & 0 deletions examples/crossbuild/2_deps/lib_default.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class B
35 changes: 35 additions & 0 deletions examples/crossbuild/3_select/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library")
load("@io_bazel_rules_scala//scala:scala_cross_version_select.bzl", "select_for_scala_version")

# Here we demonstrate how to provide distinct source files depending on the version requested

# Trying to provide library that works with all Scala versions:
scala_library(
name = "lib",
srcs = select_for_scala_version(
before_3 = [
# for Scala version < 3
"lib2.scala",
],
since_3 = [
# for 3 ≤ Scala version
"lib3.scala",
],
),
)

scala_binary(
name = "bin2",
srcs = ["bin.scala"],
main_class = "B",
scala_version = "2.13.12",
deps = [":lib"],
)

scala_binary(
name = "bin3",
srcs = ["bin.scala"],
main_class = "B",
scala_version = "3.3.1",
deps = [":lib"],
)
3 changes: 3 additions & 0 deletions examples/crossbuild/3_select/bin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object B extends App {
(new A).say
}
3 changes: 3 additions & 0 deletions examples/crossbuild/3_select/lib2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A {
def say = println("Hello 2")
}
3 changes: 3 additions & 0 deletions examples/crossbuild/3_select/lib3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A:
def say =
println("Hello 3")
54 changes: 54 additions & 0 deletions examples/crossbuild/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
workspace(name = "cross_build")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "bazel_skylib",
sha256 = "d00f1389ee20b60018e92644e0948e16e350a7707219e7a390fb0a99b6ec9262",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.0/bazel-skylib-1.7.0.tar.gz",
"/~https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.0/bazel-skylib-1.7.0.tar.gz",
],
)

local_repository(
name = "io_bazel_rules_scala",
path = "../..",
)

load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(
scala_version = "3.3.1",
scala_versions = [
"2.11.12",
"2.13.12",
"3.3.1",
],
)

load(
"@io_bazel_rules_scala//scala:scala.bzl",
"rules_scala_setup",
"rules_scala_toolchain_deps_repositories",
)

rules_scala_setup()

rules_scala_toolchain_deps_repositories()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()

load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")

scalatest_repositories()

scalatest_toolchain()
7 changes: 6 additions & 1 deletion test/shell/test_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ function semanticdb_example() {
test_example examples/semanticdb build_semanticdb_example
}

function cross_build_example() {
test_example examples/crossbuild "bazel build //..."
}

$runner scalatest_repositories_example
$runner specs2_junit_repositories_example
$runner multi_framework_toolchain_example
$runner semanticdb_example
$runner scala3_1_example
$runner scala3_2_example
$runner scala3_3_example
$runner scala3_3_example
$runner cross_build_example