Skip to content

Commit

Permalink
Add dt_patches/compiler_sources repo (#1654)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbland authored Dec 8, 2024
1 parent d35448f commit 4e210ff
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 140 deletions.
1 change: 1 addition & 0 deletions dt_patches/compiler_sources/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ../../.bazelrc
1 change: 1 addition & 0 deletions dt_patches/compiler_sources/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.5.0
Empty file.
1 change: 1 addition & 0 deletions dt_patches/compiler_sources/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "compiler_sources")
63 changes: 63 additions & 0 deletions dt_patches/compiler_sources/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
load(
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
"default_maven_server_urls",
)
load(
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl",
"scala_maven_import_external",
)
load(
"@io_bazel_rules_scala//third_party/repositories:scala_2_13.bzl",
_scala_2_version = "scala_version",
)
load(
"@io_bazel_rules_scala//third_party/repositories:scala_3_5.bzl",
_scala_3_version = "scala_version",
)
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")

_IS_SCALA_2 = SCALA_VERSION.startswith("2.")
_IS_SCALA_3 = SCALA_VERSION.startswith("3.")

_SCALA_2_VERSION = SCALA_VERSION if _IS_SCALA_2 else _scala_2_version
_SCALA_3_VERSION = SCALA_VERSION if _IS_SCALA_3 else _scala_3_version

_SCALA_VERSION_ARTIFACTS = {
"scala_compiler": "org.scala-lang:scala3-compiler_3:",
"scala_library": "org.scala-lang:scala3-library_3:",
} if _IS_SCALA_3 else {
"scala_compiler": "org.scala-lang:scala-compiler:",
"scala_library": "org.scala-lang:scala-library:",
}

_SCALA_2_ARTIFACTS = {
"scala_reflect": "org.scala-lang:scala-reflect:",
"scala2_library": "org.scala-lang:scala-library:",
}

_SCALA_3_ARTIFACTS = {
"scala3_interfaces": "org.scala-lang:scala3-interfaces:",
"tasty_core": "org.scala-lang:tasty-core_3:",
}

def _versioned_artifacts(scala_version, artifacts):
return {k: v + scala_version for k, v in artifacts.items()}

COMPILER_SOURCES_ARTIFACTS = (
_versioned_artifacts(SCALA_VERSION, _SCALA_VERSION_ARTIFACTS) |
_versioned_artifacts(_SCALA_2_VERSION, _SCALA_2_ARTIFACTS) |
_versioned_artifacts(_SCALA_3_VERSION, _SCALA_3_ARTIFACTS) |
{
"sbt_compiler_interface": "org.scala-sbt:compiler-interface:1.9.6",
"scala_asm": "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
}
)

def import_compiler_source_repos():
for name, artifact in COMPILER_SOURCES_ARTIFACTS.items():
scala_maven_import_external(
name = name,
artifact = artifact,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
70 changes: 6 additions & 64 deletions dt_patches/test_dt_patches/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,74 +31,16 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(enable_compiler_dependency_tracking = True)

load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
load(
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
"default_maven_server_urls",
)
load(
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl",
"scala_maven_import_external",
)
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")

scala_maven_import_external(
name = "scala_library",
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-library_3:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

scala_maven_import_external(
name = "scala_compiler",
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-compiler_3:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

# Scala 2 only
scala_maven_import_external(
name = "scala_reflect",
artifact = "org.scala-lang:scala-reflect:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

# Scala 3 only
scala_maven_import_external(
name = "scala3_interfaces",
artifact = "org.scala-lang:scala3-interfaces:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

scala_maven_import_external(
name = "scala2_library",
artifact = "org.scala-lang:scala-library:2.13.15",
licenses = ["notice"],
server_urls = default_maven_server_urls(),
local_repository(
name = "compiler_sources",
path = "../compiler_sources",
)

scala_maven_import_external(
name = "tasty_core",
artifact = "org.scala-lang:tasty-core_3:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
load("@compiler_sources//:extensions.bzl", "import_compiler_source_repos")

scala_maven_import_external(
name = "scala_asm",
artifact = "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
import_compiler_source_repos()

scala_maven_import_external(
name = "sbt_compiler_interface",
artifact = "org.scala-sbt:compiler-interface:1.9.6",
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")

scala_toolchains(
fetch_sources = True,
Expand Down
86 changes: 10 additions & 76 deletions dt_patches/test_dt_patches_user_srcjar/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
workspace(name = "test_dt_patches")

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

http_archive(
name = "bazel_skylib",
Expand Down Expand Up @@ -31,86 +31,18 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")

scala_config(enable_compiler_dependency_tracking = True)

load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains")
load(
"@io_bazel_rules_scala//scala:scala_cross_version.bzl",
"default_maven_server_urls",
)
load(
"@io_bazel_rules_scala//scala:scala_maven_import_external.bzl",
"scala_maven_import_external",
)
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")

http_jar(
name = "scala_compiler_srcjar",
sha256 = "95c217cc87ee846b39990e0a9c273824a384dffbac57df84d466f866df4a91ea",
url = "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.16/scala-compiler-2.12.16-sources.jar",
)

http_jar(
name = "scala3_compiler_srcjar",
sha256 = "3c413efa9a2921ef59da7f065c445ae1b6b97057cbbc6b16957ad052a575a3ce",
url = "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.4.3/scala3-compiler_3-3.4.3-sources.jar",
)

scala_maven_import_external(
name = "scala_library",
artifact = "org.scala-lang:scala-library:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-library_3:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

scala_maven_import_external(
name = "scala_compiler",
artifact = "org.scala-lang:scala-compiler:%s" % SCALA_VERSION if SCALA_VERSION.startswith("2.") else "org.scala-lang:scala3-compiler_3:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

# Scala 2 only
scala_maven_import_external(
name = "scala_reflect",
artifact = "org.scala-lang:scala-reflect:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

# Scala 3 only
scala_maven_import_external(
name = "scala3_interfaces",
artifact = "org.scala-lang:scala3-interfaces:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
local_repository(
name = "compiler_sources",
path = "../compiler_sources",
)

scala_maven_import_external(
name = "scala2_library",
artifact = "org.scala-lang:scala-library:2.13.15",
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
load("@compiler_sources//:extensions.bzl", "import_compiler_source_repos")

scala_maven_import_external(
name = "tasty_core",
artifact = "org.scala-lang:tasty-core_3:%s" % SCALA_VERSION,
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
import_compiler_source_repos()

scala_maven_import_external(
name = "scala_asm",
artifact = "org.scala-lang.modules:scala-asm:9.7.0-scala-2",
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
load("//:extensions.bzl", "import_compiler_user_srcjar_repos")

scala_maven_import_external(
name = "sbt_compiler_interface",
artifact = "org.scala-sbt:compiler-interface:1.9.6",
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)
import_compiler_user_srcjar_repos()

srcjars_by_version = {
# Invalid
Expand Down Expand Up @@ -178,6 +110,8 @@ srcjars_by_version = {
},
}

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

scala_toolchains(
fetch_sources = True,
scala_compiler_srcjars = srcjars_by_version,
Expand Down
21 changes: 21 additions & 0 deletions dt_patches/test_dt_patches_user_srcjar/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")

def import_compiler_user_srcjar_repos():
http_jar(
name = "scala_compiler_srcjar",
sha256 = "95c217cc87ee846b39990e0a9c273824a384dffbac57df84d466f866df4a91ea",
url = "https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.16/scala-compiler-2.12.16-sources.jar",
)

http_jar(
name = "scala3_compiler_srcjar",
sha256 = "3c413efa9a2921ef59da7f065c445ae1b6b97057cbbc6b16957ad052a575a3ce",
url = "https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.4.3/scala3-compiler_3-3.4.3-sources.jar",
)

def _compiler_user_srcjar_repos_impl(_ctx):
import_compiler_user_srcjar_repos()

compiler_user_srcjar_repos = module_extension(
implementation = _compiler_user_srcjar_repos_impl,
)

0 comments on commit 4e210ff

Please sign in to comment.