Skip to content

Commit

Permalink
Toolchain deps infra (#1072)
Browse files Browse the repository at this point in the history
* Add scala toolchain deps infra

* Add scalac_provider_attr doc

* Remove guava and commons_io deps

* Extract minimal rules_scala_setup()

* Fix aspect tests

* Migrate ScalacProvider attribute to DepsInfo mmapping

* Remove ScalacProvider deps rule

* Update docs according ScalacProvider migration to DepsInfo

* Refactor dep_provider map to DepInfo id field

* Update stale docs

* Rename depset_id to deps_id in DepInfo provider

* Lint
  • Loading branch information
liucijus authored Aug 3, 2020
1 parent 8f1c83b commit eabb1d2
Show file tree
Hide file tree
Showing 42 changed files with 595 additions and 224 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ java_import_external(
jar_urls = ["https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar"],
licenses = ["notice"], # Apache 2.0
neverlink = True,
testonly_ = True,
)

## Linting
Expand Down
63 changes: 62 additions & 1 deletion docs/scala_toolchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ scala_register_toolchains()
```python
# //toolchains/BUILD
load("@io_bazel_rules_scala//scala:scala_toolchain.bzl", "scala_toolchain")
load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider")

scala_toolchain(
name = "my_toolchain_impl",
dep_providers = [
":my_scala_compile_classpath_provider",
":my_scala_library_classpath_provider",
":my_scala_macro_classpath_provider",
":my_scala_xml_provider",
":my_parser_combinators_provider",
],
scalacopts = ["-Ywarn-unused"],
unused_dependency_checker_mode = "off",
visibility = ["//visibility:public"]
Expand All @@ -37,6 +45,47 @@ scala_register_toolchains()
toolchain = "my_toolchain_impl",
visibility = ["//visibility:public"]
)

declare_deps_provider(
name = "my_scala_compile_classpath_provider",
deps_id = "scala_compile_classpath",
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scala_compiler",
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"//external:io_bazel_rules_scala/dependency/scala/scala_reflect",
],
)

declare_deps_provider(
name = "my_scala_library_classpath_provider",
deps_id = "scala_library_classpath",
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"//external:io_bazel_rules_scala/dependency/scala/scala_reflect",
],
)

declare_deps_provider(
name = "my_scala_macro_classpath_provider",
deps_id = "scala_macro_classpath",
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"//external:io_bazel_rules_scala/dependency/scala/scala_reflect",
],
)

declare_deps_provider(
name = "my_scala_xml_provider",
deps_id = "scala_xml",
deps = ["@scala_xml_dep"],
)

declare_deps_provider(
name = "my_parser_combinators_provider",
deps_id = "parser_combinators",
deps = ["@parser_combinators_dep"],
)
```

2. Register your custom toolchain from `WORKSPACE`:
Expand All @@ -56,6 +105,18 @@ scala_register_toolchains()
</tr>
</thead>
<tbody>
<tr>
<td><code>dep_providers</code></td>
<td>
<p><code>List of labels; optional</code></p>
<p>
Allows to configure dependencies lists by configuring <code>DepInfo</code> provider targets.
Currently supported depset ids: <code>scala_compile_classpath</code>,
<code>scala_library_classpath</code>, <code>scala_macro_classpath</code>, <code>scala_xml</code>,
<code>parser_combinators</code>.
</p>
</td>
</tr>
<tr>
<td><code>scalacopts</code></td>
<td>
Expand Down Expand Up @@ -109,4 +170,4 @@ scala_register_toolchains()
</td>
</tr>
</tbody>
</table>
</table>
51 changes: 38 additions & 13 deletions scala/BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
load("@rules_java//java:defs.bzl", "java_import", "java_library")
load(
"@io_bazel_rules_scala//scala:providers.bzl",
_declare_scalac_provider = "declare_scalac_provider",
)
load("//scala:scala_toolchain.bzl", "scala_toolchain")
load("//scala/toolchains:toolchains.bzl", "declare_deps_toolchain")
load("//scala:providers.bzl", "declare_deps_provider")

toolchain_type(
name = "toolchain_type",
Expand Down Expand Up @@ -71,26 +69,53 @@ java_import(
visibility = ["//visibility:public"],
)

_declare_scalac_provider(
name = "scalac_default",
default_classpath = [
java_library(
name = "PlaceHolderClassToCreateEmptyJarForScalaImport",
srcs = ["PlaceHolderClassToCreateEmptyJarForScalaImport.java"],
visibility = ["//visibility:public"],
)

declare_deps_provider(
name = "scala_compile_classpath_provider",
deps_id = "scala_compile_classpath",
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scala_compiler",
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"//external:io_bazel_rules_scala/dependency/scala/scala_reflect",
],
default_macro_classpath = [
)

declare_deps_provider(
name = "scala_library_classpath_provider",
deps_id = "scala_library_classpath",
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"//external:io_bazel_rules_scala/dependency/scala/scala_reflect",
],
default_repl_classpath = [
)

declare_deps_provider(
name = "scala_macro_classpath_provider",
deps_id = "scala_macro_classpath",
visibility = ["//visibility:public"],
deps = [
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"//external:io_bazel_rules_scala/dependency/scala/scala_reflect",
"//external:io_bazel_rules_scala/dependency/scala/scala_compiler",
],
)

declare_deps_provider(
name = "scala_xml_provider",
deps_id = "scala_xml",
visibility = ["//visibility:public"],
deps = ["//external:io_bazel_rules_scala/dependency/scala/scala_xml"],
)

java_library(
name = "PlaceHolderClassToCreateEmptyJarForScalaImport",
srcs = ["PlaceHolderClassToCreateEmptyJarForScalaImport.java"],
declare_deps_provider(
name = "parser_combinators_provider",
deps_id = "parser_combinators",
visibility = ["//visibility:public"],
deps = ["//external:io_bazel_rules_scala/dependency/scala/parser_combinators"],
)
2 changes: 1 addition & 1 deletion scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ resolve_deps = {
"_scala_toolchain": attr.label_list(
default = [
Label(
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"@io_bazel_rules_scala//scala/private/toolchain_deps:scala_library_classpath",
),
],
allow_files = False,
Expand Down
156 changes: 65 additions & 91 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,69 @@ def _default_scala_extra_jars():
},
}

def rules_scala_setup():
if not native.existing_rule("com_google_protobuf"):
http_archive(
name = "com_google_protobuf",
sha256 = "cf754718b0aa945b00550ed7962ddc167167bd922b842199eeb6505e6f344852",
strip_prefix = "protobuf-3.11.3",
urls = [
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.11.3.tar.gz",
"https://github.com/protocolbuffers/protobuf/archive/v3.11.3.tar.gz",
],
)

native.bind(
name = "io_bazel_rules_scala/dependency/com_google_protobuf/protobuf_java",
actual = "@com_google_protobuf//:protobuf_java",
)

if not native.existing_rule("rules_cc"):
http_archive(
name = "rules_cc",
sha256 = "29daf0159f0cf552fcff60b49d8bcd4f08f08506d2da6e41b07058ec50cfeaec",
strip_prefix = "rules_cc-b7fe9697c0c76ab2fd431a891dbb9a6a32ed7c3e",
urls = ["https://github.com/bazelbuild/rules_cc/archive/b7fe9697c0c76ab2fd431a891dbb9a6a32ed7c3e.tar.gz"],
)

if not native.existing_rule("rules_java"):
http_archive(
name = "rules_java",
sha256 = "220b87d8cfabd22d1c6d8e3cdb4249abd4c93dcc152e0667db061fb1b957ee68",
urls = ["https://github.com/bazelbuild/rules_java/releases/download/0.1.1/rules_java-0.1.1.tar.gz"],
)

if not native.existing_rule("rules_proto"):
http_archive(
name = "rules_proto",
sha256 = "4d421d51f9ecfe9bf96ab23b55c6f2b809cbaf0eea24952683e397decfbd0dd0",
strip_prefix = "rules_proto-f6b8d89b90a7956f6782a4a3609b2f0eee3ce965",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz",
"https://github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz",
],
)

if not native.existing_rule("rules_python"):
http_archive(
name = "rules_python",
sha256 = "e5470e92a18aa51830db99a4d9c492cc613761d5bdb7131c04bd92b9834380f6",
strip_prefix = "rules_python-4b84ad270387a7c439ebdccfd530e2339601ef27",
urls = ["https://github.com/bazelbuild/rules_python/archive/4b84ad270387a7c439ebdccfd530e2339601ef27.tar.gz"],
)

if not native.existing_rule("zlib"): # needed by com_google_protobuf
http_archive(
name = "zlib",
build_file = "@com_google_protobuf//third_party:zlib.BUILD",
sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
strip_prefix = "zlib-1.2.11",
urls = [
"https://mirror.bazel.build/zlib.net/zlib-1.2.11.tar.gz",
"https://zlib.net/zlib-1.2.11.tar.gz",
],
)

def scala_repositories(
scala_version_shas = (
_default_scala_version(),
Expand All @@ -60,6 +123,8 @@ def scala_repositories(
maven_servers = _default_maven_server_urls(),
scala_extra_jars = _default_scala_extra_jars(),
fetch_sources = False):
rules_scala_setup()

(scala_version, scala_version_jar_shas) = scala_version_shas
major_version = _extract_major_version(scala_version)

Expand Down Expand Up @@ -120,92 +185,6 @@ def scala_repositories(
fetch_sources = fetch_sources,
)

# used by ScalacProcessor
_scala_maven_import_external(
name = "scalac_rules_commons_io",
artifact = "commons-io:commons-io:2.6",
artifact_sha256 = "f877d304660ac2a142f3865badfc971dec7ed73c747c7f8d5d2f5139ca736513",
licenses = ["notice"],
server_urls = maven_servers,
fetch_sources = fetch_sources,
)

_scala_maven_import_external(
name = "io_bazel_rules_scala_guava",
artifact = "com.google.guava:guava:21.0",
artifact_sha256 = "972139718abc8a4893fa78cba8cf7b2c903f35c97aaf44fa3031b0669948b480",
licenses = ["notice"],
server_urls = maven_servers,
fetch_sources = fetch_sources,
)

if not native.existing_rule("com_google_protobuf"):
http_archive(
name = "com_google_protobuf",
sha256 = "cf754718b0aa945b00550ed7962ddc167167bd922b842199eeb6505e6f344852",
strip_prefix = "protobuf-3.11.3",
urls = [
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.11.3.tar.gz",
"https://github.com/protocolbuffers/protobuf/archive/v3.11.3.tar.gz",
],
)

if not native.existing_rule("rules_cc"):
http_archive(
name = "rules_cc",
sha256 = "29daf0159f0cf552fcff60b49d8bcd4f08f08506d2da6e41b07058ec50cfeaec",
strip_prefix = "rules_cc-b7fe9697c0c76ab2fd431a891dbb9a6a32ed7c3e",
urls = ["https://github.com/bazelbuild/rules_cc/archive/b7fe9697c0c76ab2fd431a891dbb9a6a32ed7c3e.tar.gz"],
)

if not native.existing_rule("rules_java"):
http_archive(
name = "rules_java",
sha256 = "220b87d8cfabd22d1c6d8e3cdb4249abd4c93dcc152e0667db061fb1b957ee68",
urls = ["https://github.com/bazelbuild/rules_java/releases/download/0.1.1/rules_java-0.1.1.tar.gz"],
)

if not native.existing_rule("rules_proto"):
http_archive(
name = "rules_proto",
sha256 = "4d421d51f9ecfe9bf96ab23b55c6f2b809cbaf0eea24952683e397decfbd0dd0",
strip_prefix = "rules_proto-f6b8d89b90a7956f6782a4a3609b2f0eee3ce965",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz",
"https://github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz",
],
)

if not native.existing_rule("rules_python"):
http_archive(
name = "rules_python",
sha256 = "e5470e92a18aa51830db99a4d9c492cc613761d5bdb7131c04bd92b9834380f6",
strip_prefix = "rules_python-4b84ad270387a7c439ebdccfd530e2339601ef27",
urls = ["https://github.com/bazelbuild/rules_python/archive/4b84ad270387a7c439ebdccfd530e2339601ef27.tar.gz"],
)

if not native.existing_rule("zlib"): # needed by com_google_protobuf
http_archive(
name = "zlib",
build_file = "@com_google_protobuf//third_party:zlib.BUILD",
sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
strip_prefix = "zlib-1.2.11",
urls = [
"https://mirror.bazel.build/zlib.net/zlib-1.2.11.tar.gz",
"https://zlib.net/zlib-1.2.11.tar.gz",
],
)

native.bind(
name = "io_bazel_rules_scala/dependency/com_google_protobuf/protobuf_java",
actual = "@com_google_protobuf//:protobuf_java",
)

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

native.bind(
name = "io_bazel_rules_scala/dependency/scalatest/scalatest",
actual = "@io_bazel_rules_scala//scala/scalatest:scalatest",
Expand Down Expand Up @@ -236,11 +215,6 @@ def scala_repositories(
actual = "@io_bazel_rules_scala_scala_parser_combinators",
)

native.bind(
name = "io_bazel_rules_scala/dependency/scala/guava",
actual = "@io_bazel_rules_scala_guava",
)

native.bind(
name = "io_bazel_rules_scala/dependency/scala/scalatest/scalatest",
actual = "@io_bazel_rules_scala_scalatest",
Expand Down
16 changes: 15 additions & 1 deletion scala/private/phases/phase_scalac_provider.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ load(
"@io_bazel_rules_scala//scala:providers.bzl",
_ScalacProvider = "ScalacProvider",
)
load(
"@io_bazel_rules_scala//scala/private/toolchain_deps:toolchain_deps.bzl",
"find_deps_info_on",
)

def phase_scalac_provider(ctx, p):
return ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"].scalac_provider_attr[_ScalacProvider]
toolchain_type_label = "@io_bazel_rules_scala//scala:toolchain_type"

library_classpath = find_deps_info_on(ctx, toolchain_type_label, "scala_library_classpath").deps
compile_classpath = find_deps_info_on(ctx, toolchain_type_label, "scala_compile_classpath").deps
macro_classpath = find_deps_info_on(ctx, toolchain_type_label, "scala_macro_classpath").deps

return _ScalacProvider(
default_classpath = library_classpath,
default_repl_classpath = compile_classpath,
default_macro_classpath = macro_classpath,
)
2 changes: 1 addition & 1 deletion scala/private/rules/scala_junit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ _junit_resolve_deps = {
"_scala_toolchain": attr.label_list(
default = [
Label(
"//external:io_bazel_rules_scala/dependency/scala/scala_library",
"@io_bazel_rules_scala//scala/private/toolchain_deps:scala_library_classpath",
),
Label("//external:io_bazel_rules_scala/dependency/junit/junit"),
Label(
Expand Down
Loading

0 comments on commit eabb1d2

Please sign in to comment.