Skip to content

Commit 4e210ff

Browse files
authored
Add dt_patches/compiler_sources repo (#1654)
This is part of the Bzlmod prep work from #1482, but also reduces duplication between the `dt_patches/test_dt_patches*/WORKSPACE` files. The structure of the `dt_patches/compiler_sources/extensions.bzl` declarations accommodates the fact that, unlike `WORKSPACE, `MODULE.bazel` files cannot import constants or contain conditional statements. That is to say, there's no way to port the `if SCALA_VERSION.startswith("2.")` expressions from the previous `WORKSPACE` files to Bzlmod. The new `import_compiler_source_repos` macro, however, works for both `WORKSPACE` and Bzlmod builds, as it's trivial to wrap `import_compiler_source_repos` in a module extension.
1 parent d35448f commit 4e210ff

File tree

8 files changed

+103
-140
lines changed

8 files changed

+103
-140
lines changed

dt_patches/compiler_sources/.bazelrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import ../../.bazelrc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.5.0

dt_patches/compiler_sources/BUILD

Whitespace-only changes.

dt_patches/compiler_sources/WORKSPACE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "compiler_sources")
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
load(
2+
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
3+
"default_maven_server_urls",
4+
)
5+
load(
6+
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl",
7+
"scala_maven_import_external",
8+
)
9+
load(
10+
"@io_bazel_rules_scala//third_party/repositories:scala_2_13.bzl",
11+
_scala_2_version = "scala_version",
12+
)
13+
load(
14+
"@io_bazel_rules_scala//third_party/repositories:scala_3_5.bzl",
15+
_scala_3_version = "scala_version",
16+
)
17+
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
18+
19+
_IS_SCALA_2 = SCALA_VERSION.startswith("2.")
20+
_IS_SCALA_3 = SCALA_VERSION.startswith("3.")
21+
22+
_SCALA_2_VERSION = SCALA_VERSION if _IS_SCALA_2 else _scala_2_version
23+
_SCALA_3_VERSION = SCALA_VERSION if _IS_SCALA_3 else _scala_3_version
24+
25+
_SCALA_VERSION_ARTIFACTS = {
26+
"scala_compiler": "org.scala-lang:scala3-compiler_3:",
27+
"scala_library": "org.scala-lang:scala3-library_3:",
28+
} if _IS_SCALA_3 else {
29+
"scala_compiler": "org.scala-lang:scala-compiler:",
30+
"scala_library": "org.scala-lang:scala-library:",
31+
}
32+
33+
_SCALA_2_ARTIFACTS = {
34+
"scala_reflect": "org.scala-lang:scala-reflect:",
35+
"scala2_library": "org.scala-lang:scala-library:",
36+
}
37+
38+
_SCALA_3_ARTIFACTS = {
39+
"scala3_interfaces": "org.scala-lang:scala3-interfaces:",
40+
"tasty_core": "org.scala-lang:tasty-core_3:",
41+
}
42+
43+
def _versioned_artifacts(scala_version, artifacts):
44+
return {k: v + scala_version for k, v in artifacts.items()}
45+
46+
COMPILER_SOURCES_ARTIFACTS = (
47+
_versioned_artifacts(SCALA_VERSION, _SCALA_VERSION_ARTIFACTS) |
48+
_versioned_artifacts(_SCALA_2_VERSION, _SCALA_2_ARTIFACTS) |
49+
_versioned_artifacts(_SCALA_3_VERSION, _SCALA_3_ARTIFACTS) |
50+
{
51+
"sbt_compiler_interface": "org.scala-sbt:compiler-interface:1.9.6",
52+
"scala_asm": "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
53+
}
54+
)
55+
56+
def import_compiler_source_repos():
57+
for name, artifact in COMPILER_SOURCES_ARTIFACTS.items():
58+
scala_maven_import_external(
59+
name = name,
60+
artifact = artifact,
61+
licenses = ["notice"],
62+
server_urls = default_maven_server_urls(),
63+
)

dt_patches/test_dt_patches/WORKSPACE

+6-64
Original file line numberDiff line numberDiff line change
@@ -31,74 +31,16 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")
3131

3232
scala_config(enable_compiler_dependency_tracking = True)
3333

34-
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
35-
load(
36-
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
37-
"default_maven_server_urls",
38-
)
39-
load(
40-
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl",
41-
"scala_maven_import_external",
42-
)
43-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
44-
45-
scala_maven_import_external(
46-
name = "scala_library",
47-
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-library_3:%s" % SCALA_VERSION,
48-
licenses = ["notice"],
49-
server_urls = default_maven_server_urls(),
50-
)
51-
52-
scala_maven_import_external(
53-
name = "scala_compiler",
54-
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-compiler_3:%s" % SCALA_VERSION,
55-
licenses = ["notice"],
56-
server_urls = default_maven_server_urls(),
57-
)
58-
59-
# Scala 2 only
60-
scala_maven_import_external(
61-
name = "scala_reflect",
62-
artifact = "org.scala-lang:scala-reflect:%s" % SCALA_VERSION,
63-
licenses = ["notice"],
64-
server_urls = default_maven_server_urls(),
65-
)
66-
67-
# Scala 3 only
68-
scala_maven_import_external(
69-
name = "scala3_interfaces",
70-
artifact = "org.scala-lang:scala3-interfaces:%s" % SCALA_VERSION,
71-
licenses = ["notice"],
72-
server_urls = default_maven_server_urls(),
73-
)
74-
75-
scala_maven_import_external(
76-
name = "scala2_library",
77-
artifact = "org.scala-lang:scala-library:2.13.15",
78-
licenses = ["notice"],
79-
server_urls = default_maven_server_urls(),
34+
local_repository(
35+
name = "compiler_sources",
36+
path = "../compiler_sources",
8037
)
8138

82-
scala_maven_import_external(
83-
name = "tasty_core",
84-
artifact = "org.scala-lang:tasty-core_3:%s" % SCALA_VERSION,
85-
licenses = ["notice"],
86-
server_urls = default_maven_server_urls(),
87-
)
39+
load("@compiler_sources//:extensions.bzl", "import_compiler_source_repos")
8840

89-
scala_maven_import_external(
90-
name = "scala_asm",
91-
artifact = "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
92-
licenses = ["notice"],
93-
server_urls = default_maven_server_urls(),
94-
)
41+
import_compiler_source_repos()
9542

96-
scala_maven_import_external(
97-
name = "sbt_compiler_interface",
98-
artifact = "org.scala-sbt:compiler-interface:1.9.6",
99-
licenses = ["notice"],
100-
server_urls = default_maven_server_urls(),
101-
)
43+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
10244

10345
scala_toolchains(
10446
fetch_sources = True,

dt_patches/test_dt_patches_user_srcjar/WORKSPACE

+10-76
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspace(name = "test_dt_patches")
22

3-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar")
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

55
http_archive(
66
name = "bazel_skylib",
@@ -31,86 +31,18 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")
3131

3232
scala_config(enable_compiler_dependency_tracking = True)
3333

34-
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
35-
load(
36-
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
37-
"default_maven_server_urls",
38-
)
39-
load(
40-
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl",
41-
"scala_maven_import_external",
42-
)
43-
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")
44-
45-
http_jar(
46-
name = "scala_compiler_srcjar",
47-
sha256 = "95c217cc87ee846b39990e0a9c273824a384dffbac57df84d466f866df4a91ea",
48-
url = "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.16/scala-compiler-2.12.16-sources.jar",
49-
)
50-
51-
http_jar(
52-
name = "scala3_compiler_srcjar",
53-
sha256 = "3c413efa9a2921ef59da7f065c445ae1b6b97057cbbc6b16957ad052a575a3ce",
54-
url = "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.4.3/scala3-compiler_3-3.4.3-sources.jar",
55-
)
56-
57-
scala_maven_import_external(
58-
name = "scala_library",
59-
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-library_3:%s" % SCALA_VERSION,
60-
licenses = ["notice"],
61-
server_urls = default_maven_server_urls(),
62-
)
63-
64-
scala_maven_import_external(
65-
name = "scala_compiler",
66-
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-compiler_3:%s" % SCALA_VERSION,
67-
licenses = ["notice"],
68-
server_urls = default_maven_server_urls(),
69-
)
70-
71-
# Scala 2 only
72-
scala_maven_import_external(
73-
name = "scala_reflect",
74-
artifact = "org.scala-lang:scala-reflect:%s" % SCALA_VERSION,
75-
licenses = ["notice"],
76-
server_urls = default_maven_server_urls(),
77-
)
78-
79-
# Scala 3 only
80-
scala_maven_import_external(
81-
name = "scala3_interfaces",
82-
artifact = "org.scala-lang:scala3-interfaces:%s" % SCALA_VERSION,
83-
licenses = ["notice"],
84-
server_urls = default_maven_server_urls(),
34+
local_repository(
35+
name = "compiler_sources",
36+
path = "../compiler_sources",
8537
)
8638

87-
scala_maven_import_external(
88-
name = "scala2_library",
89-
artifact = "org.scala-lang:scala-library:2.13.15",
90-
licenses = ["notice"],
91-
server_urls = default_maven_server_urls(),
92-
)
39+
load("@compiler_sources//:extensions.bzl", "import_compiler_source_repos")
9340

94-
scala_maven_import_external(
95-
name = "tasty_core",
96-
artifact = "org.scala-lang:tasty-core_3:%s" % SCALA_VERSION,
97-
licenses = ["notice"],
98-
server_urls = default_maven_server_urls(),
99-
)
41+
import_compiler_source_repos()
10042

101-
scala_maven_import_external(
102-
name = "scala_asm",
103-
artifact = "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
104-
licenses = ["notice"],
105-
server_urls = default_maven_server_urls(),
106-
)
43+
load("//:extensions.bzl", "import_compiler_user_srcjar_repos")
10744

108-
scala_maven_import_external(
109-
name = "sbt_compiler_interface",
110-
artifact = "org.scala-sbt:compiler-interface:1.9.6",
111-
licenses = ["notice"],
112-
server_urls = default_maven_server_urls(),
113-
)
45+
import_compiler_user_srcjar_repos()
11446

11547
srcjars_by_version = {
11648
# Invalid
@@ -178,6 +110,8 @@ srcjars_by_version = {
178110
},
179111
}
180112

113+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
114+
181115
scala_toolchains(
182116
fetch_sources = True,
183117
scala_compiler_srcjars = srcjars_by_version,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
2+
3+
def import_compiler_user_srcjar_repos():
4+
http_jar(
5+
name = "scala_compiler_srcjar",
6+
sha256 = "95c217cc87ee846b39990e0a9c273824a384dffbac57df84d466f866df4a91ea",
7+
url = "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.16/scala-compiler-2.12.16-sources.jar",
8+
)
9+
10+
http_jar(
11+
name = "scala3_compiler_srcjar",
12+
sha256 = "3c413efa9a2921ef59da7f065c445ae1b6b97057cbbc6b16957ad052a575a3ce",
13+
url = "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.4.3/scala3-compiler_3-3.4.3-sources.jar",
14+
)
15+
16+
def _compiler_user_srcjar_repos_impl(_ctx):
17+
import_compiler_user_srcjar_repos()
18+
19+
compiler_user_srcjar_repos = module_extension(
20+
implementation = _compiler_user_srcjar_repos_impl,
21+
)

0 commit comments

Comments
 (0)