From e86e51a666bc9b2c7a36f8947704cd646edf665d Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Wed, 13 Nov 2024 16:05:48 -0500 Subject: [PATCH] Add dt_patches/compiler_sources repo 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. --- dt_patches/compiler_sources/.bazelrc | 1 + dt_patches/compiler_sources/.bazelversion | 1 + dt_patches/compiler_sources/BUILD | 0 dt_patches/compiler_sources/WORKSPACE | 1 + dt_patches/compiler_sources/extensions.bzl | 63 ++++++++++++++ dt_patches/test_dt_patches/WORKSPACE | 70 ++------------- .../test_dt_patches_user_srcjar/WORKSPACE | 86 +++---------------- .../extensions.bzl | 21 +++++ 8 files changed, 103 insertions(+), 140 deletions(-) create mode 100644 dt_patches/compiler_sources/.bazelrc create mode 100644 dt_patches/compiler_sources/.bazelversion create mode 100644 dt_patches/compiler_sources/BUILD create mode 100644 dt_patches/compiler_sources/WORKSPACE create mode 100644 dt_patches/compiler_sources/extensions.bzl create mode 100644 dt_patches/test_dt_patches_user_srcjar/extensions.bzl diff --git a/dt_patches/compiler_sources/.bazelrc b/dt_patches/compiler_sources/.bazelrc new file mode 100644 index 000000000..005efba2f --- /dev/null +++ b/dt_patches/compiler_sources/.bazelrc @@ -0,0 +1 @@ +import ../../.bazelrc diff --git a/dt_patches/compiler_sources/.bazelversion b/dt_patches/compiler_sources/.bazelversion new file mode 100644 index 000000000..f22d756da --- /dev/null +++ b/dt_patches/compiler_sources/.bazelversion @@ -0,0 +1 @@ +6.5.0 diff --git a/dt_patches/compiler_sources/BUILD b/dt_patches/compiler_sources/BUILD new file mode 100644 index 000000000..e69de29bb diff --git a/dt_patches/compiler_sources/WORKSPACE b/dt_patches/compiler_sources/WORKSPACE new file mode 100644 index 000000000..0dc1a4060 --- /dev/null +++ b/dt_patches/compiler_sources/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "compiler_sources") diff --git a/dt_patches/compiler_sources/extensions.bzl b/dt_patches/compiler_sources/extensions.bzl new file mode 100644 index 000000000..0b3b6f796 --- /dev/null +++ b/dt_patches/compiler_sources/extensions.bzl @@ -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(), + ) diff --git a/dt_patches/test_dt_patches/WORKSPACE b/dt_patches/test_dt_patches/WORKSPACE index a3b7df676..a6e17702f 100644 --- a/dt_patches/test_dt_patches/WORKSPACE +++ b/dt_patches/test_dt_patches/WORKSPACE @@ -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:toolchains.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:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/dt_patches/test_dt_patches_user_srcjar/WORKSPACE b/dt_patches/test_dt_patches_user_srcjar/WORKSPACE index f587be3f2..91596ca70 100644 --- a/dt_patches/test_dt_patches_user_srcjar/WORKSPACE +++ b/dt_patches/test_dt_patches_user_srcjar/WORKSPACE @@ -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", @@ -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:toolchains.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 @@ -178,6 +110,8 @@ srcjars_by_version = { }, } +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") + scala_toolchains( fetch_sources = True, scala_compiler_srcjars = srcjars_by_version, diff --git a/dt_patches/test_dt_patches_user_srcjar/extensions.bzl b/dt_patches/test_dt_patches_user_srcjar/extensions.bzl new file mode 100644 index 000000000..19ce3c1e8 --- /dev/null +++ b/dt_patches/test_dt_patches_user_srcjar/extensions.bzl @@ -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, +)