Skip to content

Factor scalac provider #981

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

Closed
wants to merge 16 commits into from
Closed
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
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_depen

buildifier_dependencies()

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

scala_register_toolchains()

load("//scala:scala.bzl", "scala_repositories")

scala_repositories()
Expand Down
5 changes: 5 additions & 0 deletions private/example/App.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object App extends scala.App {
def version = scala.util.Properties.versionString

println(s"hello, world from $version!")
}
9 changes: 9 additions & 0 deletions private/example/AppTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.scalatest._

class AppTest extends FlatSpec with Matchers {
it should "have a successful test" in {
System.err.println(s"hello, world from ${scala.util.Properties.versionString}!")

App.version should be (scala.util.Properties.versionString)
}
}
26 changes: 26 additions & 0 deletions private/example/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load(
"@io_bazel_rules_scala//scala:scala.bzl",
"scala_binary",
"scala_library",
"scala_test",
)

scala_library(
name = "library",
srcs = glob(
["*.scala"],
exclude = ["*Test.scala"],
),
)

scala_binary(
name = "app",
main_class = "App",
runtime_deps = [":library"],
)

scala_test(
name = "test",
srcs = ["AppTest.scala"],
deps = [":library"],
)
27 changes: 27 additions & 0 deletions private/example/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("//repositories:bazel_skylib.bzl", "load_bazel_skylib")

load_bazel_skylib()

load("//repositories:rules_jvm_external.bzl", "load_rules_jvm_external")

load_rules_jvm_external()

load("//repositories:rules_scala.bzl", "load_rules_scala")

load_rules_scala()

load("//repositories:rules_proto.bzl", "load_rules_proto")

load_rules_proto()

load("//repositories:rules_python.bzl", "load_rules_python")

load_rules_python()

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

scala_register_toolchains()

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

scala_repositories()
Empty file.
11 changes: 11 additions & 0 deletions private/example/repositories/bazel_skylib.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""load skylib"""

load(":tools.bzl", _github_release = "github_release")

def load_bazel_skylib():
_github_release(
name = "bazel_skylib",
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
repository = "bazelbuild/bazel-skylib",
release = "1.0.2",
)
11 changes: 11 additions & 0 deletions private/example/repositories/rules_jvm_external.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""load rules_jvm_external"""

load(":tools.bzl", _github_archive = "github_archive")

def load_rules_jvm_external():
_github_archive(
name = "rules_jvm_external",
repository = "bazelbuild/rules_jvm_external",
sha256 = "62133c125bf4109dfd9d2af64830208356ce4ef8b165a6ef15bbff7460b35c3a",
tag = "3.0",
)
11 changes: 11 additions & 0 deletions private/example/repositories/rules_proto.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""load rules_proto: needed by protobuf repo"""

load(":tools.bzl", _github_archive = "github_archive")

def load_rules_proto():
_github_archive(
name = "rules_proto",
repository = "bazelbuild/rules_proto",
sha256 = "62847ac7740865d73a2c8199be292bba913d62e79084442f3e829c3058a25e64",
tag = "d7666ec475c1f8d4a6803cbc0a0b6b4374360868",
)
11 changes: 11 additions & 0 deletions private/example/repositories/rules_python.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""load rules_python: needed by protobuf repo"""

load(":tools.bzl", _github_archive = "github_archive")

def load_rules_python():
_github_archive(
name = "rules_python",
repository = "bazelbuild/rules_python",
sha256 = "7d64815f4b22400bed0f1b9da663037e1578573446b7bc78f20f24b2b5459bb9",
tag = "38f86fb55b698c51e8510c807489c9f4e047480e",
)
7 changes: 7 additions & 0 deletions private/example/repositories/rules_scala.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""link back to parent rules_scala repo"""

def load_rules_scala():
native.local_repository(
name = "io_bazel_rules_scala",
path = "../..",
)
28 changes: 28 additions & 0 deletions private/example/repositories/tools.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""helpers for to remove http_archive boilerplate

N.B.: doesn't currently include the bazel mirros
"""

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

def github_release(name, repository, release, sha256):
(org, repo) = repository.split("/")
http_archive(
name = name,
sha256 = sha256,
urls = [
"https://github.com/{repository}/releases/download/{release}/{repo}-{release}.tar.gz".format(repository = repository, repo = repo, release = release),
],
)

def github_archive(name, repository, sha256, tag):
(org, repo) = repository.split("/")
without_v = tag[1:] if tag.startswith("v") else tag
http_archive(
name = name,
sha256 = sha256,
strip_prefix = "{repo}-{without_v}".format(repo = repo, without_v = without_v),
urls = [
"https://github.com/{repository}/archive/{tag}.zip".format(repository = repository, tag = tag),
],
)
89 changes: 64 additions & 25 deletions scala/BUILD
Original file line number Diff line number Diff line change
@@ -1,29 +1,86 @@
load(
"@io_bazel_rules_scala//scala:providers.bzl",
_declare_scalac_provider = "declare_scalac_provider",
)
load("//scala:bootstrap_toolchain.bzl", "bootstrap_toolchain")
load("//scala:scala_toolchain.bzl", "scala_toolchain")
load("//scala:test_toolchain.bzl", "test_toolchain")

# default toolchains registered by `scala_register_toolchains`

# bootstrap toolchain

toolchain_type(
name = "bootstrap_toolchain_type",
visibility = ["//visibility:public"],
)

bootstrap_toolchain(
name = "bootstrap_toolchain_impl",
classpath = [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
macro_classpath = [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
repl_classpath = [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@io_bazel_rules_scala_scala_compiler",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "bootstrap_toolchain",
toolchain = ":bootstrap_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala:bootstrap_toolchain_type",
visibility = ["//visibility:public"],
)

# standard toolchain

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

scala_toolchain(
name = "default_toolchain_impl",
name = "toolchain_impl",
scalac = "//src/java/io/bazel/rulesscala/scalac",
scalacopts = [],
visibility = ["//visibility:public"],
)

toolchain(
name = "default_toolchain",
toolchain = ":default_toolchain_impl",
name = "toolchain",
toolchain = ":toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type",
visibility = ["//visibility:public"],
)

# test toolchain

toolchain_type(
name = "test_toolchain_type",
visibility = ["//visibility:public"],
)

test_toolchain(
name = "test_toolchain_impl",
visibility = ["//visibility:public"],
)

toolchain(
name = "test_toolchain",
toolchain = ":test_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala:test_toolchain_type",
visibility = ["//visibility:public"],
)

# unused dependency checker toolchain

scala_toolchain(
name = "unused_dependency_checker_error_toolchain_impl",
scalac = "//src/java/io/bazel/rulesscala/scalac",
unused_dependency_checker_mode = "error",
visibility = ["//visibility:public"],
)
Expand All @@ -41,24 +98,6 @@ java_import(
visibility = ["//visibility:public"],
)

_declare_scalac_provider(
name = "scalac_default",
default_classpath = [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
default_macro_classpath = [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
],
default_repl_classpath = [
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_reflect",
"@io_bazel_rules_scala_scala_compiler",
],
visibility = ["//visibility:public"],
)

java_library(
name = "PlaceHolderClassToCreateEmptyJarForScalaImport",
srcs = ["PlaceHolderClassToCreateEmptyJarForScalaImport.java"],
Expand Down
27 changes: 27 additions & 0 deletions scala/bootstrap_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
BootstrapInfo = provider(
doc = "BootstrapInfo",
fields = [
"classpath",
"macro_classpath",
"repl_classpath",
],
)

def _impl(ctx):
toolchain = platform_common.ToolchainInfo(
bootstrapinfo = BootstrapInfo(
classpath = ctx.attr.classpath,
macro_classpath = ctx.attr.macro_classpath,
repl_classpath = ctx.attr.repl_classpath,
),
)
return [toolchain]

bootstrap_toolchain = rule(
_impl,
attrs = {
"classpath": attr.label_list(mandatory = True, allow_files = True),
"repl_classpath": attr.label_list(mandatory = True, allow_files = True),
"macro_classpath": attr.label_list(mandatory = True, allow_files = True),
},
)
2 changes: 1 addition & 1 deletion scala/plusone.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PlusOneDeps = provider(
)

def _collect_plus_one_deps_aspect_impl(target, ctx):
if (ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].plus_one_deps_mode == "off"):
if (ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scalainfo.plus_one_deps_mode == "off"):
return []
export_plus_one_deps = []
for exported_dep in getattr(ctx.rule.attr, "exports", []):
Expand Down
5 changes: 0 additions & 5 deletions scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ implicit_deps = {
"_java_runtime": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
),
"_scalac": attr.label(
default = Label(
"@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac",
),
),
"_exe": attr.label(
executable = True,
cfg = "host",
Expand Down
2 changes: 1 addition & 1 deletion scala/private/coverage_replacements_provider.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _is_enabled(ctx):
if "@io_bazel_rules_scala//scala:toolchain_type" not in ctx.toolchains:
return False
else:
return ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].enable_code_coverage_aspect == "on"
return ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scalainfo.enable_code_coverage_aspect == "on"

coverage_replacements_provider = struct(
aspect = _aspect,
Expand Down
15 changes: 15 additions & 0 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,18 @@ def scala_repositories(
name = "io_bazel_rules_scala/dependency/scala/guava",
actual = "@io_bazel_rules_scala_guava",
)

native.bind(
name = "io_bazel_rules_scala/dependency/scalac_rules_commons_io",
actual = "@scalac_rules_commons_io",
)

native.bind(
name = "io_bazel_rules_scala/dependency/scala/scalatest/scalatest",
actual = "@io_bazel_rules_scala_scalatest",
)

native.bind(
name = "io_bazel_rules_scala/dependency/scala/scalactic/scalactic",
actual = "@io_bazel_rules_scala_scalactic",
)
8 changes: 4 additions & 4 deletions scala/private/phases/phase_collect_jars.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ load(

def phase_collect_jars_scalatest(ctx, p):
args = struct(
base_classpath = p.scalac_provider.default_classpath + [ctx.attr._scalatest],
base_classpath = ctx.toolchains["@io_bazel_rules_scala//scala:bootstrap_toolchain_type"].bootstrapinfo.classpath + [ctx.attr._scalatest],
extra_runtime_deps = [
ctx.attr._scalatest_reporter,
ctx.attr._scalatest_runner,
Expand All @@ -25,13 +25,13 @@ def phase_collect_jars_scalatest(ctx, p):

def phase_collect_jars_repl(ctx, p):
args = struct(
base_classpath = p.scalac_provider.default_repl_classpath,
base_classpath = ctx.toolchains["@io_bazel_rules_scala//scala:bootstrap_toolchain_type"].bootstrapinfo.repl_classpath,
)
return _phase_collect_jars_default(ctx, p, args)

def phase_collect_jars_macro_library(ctx, p):
args = struct(
base_classpath = p.scalac_provider.default_macro_classpath,
base_classpath = ctx.toolchains["@io_bazel_rules_scala//scala:bootstrap_toolchain_type"].bootstrapinfo.macro_classpath,
)
return _phase_collect_jars_default(ctx, p, args)

Expand All @@ -58,7 +58,7 @@ def phase_collect_jars_common(ctx, p):
def _phase_collect_jars_default(ctx, p, _args = struct()):
return _phase_collect_jars(
ctx,
_args.base_classpath if hasattr(_args, "base_classpath") else p.scalac_provider.default_classpath,
_args.base_classpath if hasattr(_args, "base_classpath") else ctx.toolchains["@io_bazel_rules_scala//scala:bootstrap_toolchain_type"].bootstrapinfo.classpath,
_args.extra_deps if hasattr(_args, "extra_deps") else [],
_args.extra_runtime_deps if hasattr(_args, "extra_runtime_deps") else [],
_args.unused_dependency_checker_mode if hasattr(_args, "unused_dependency_checker_mode") else p.unused_deps_checker,
Expand Down
Loading