Merge pull request #16167 from github/redsun82/kotlin-patched-registry

Bazel: move patching of `rules_kotlin` to a registry override
This commit is contained in:
Paolo Tranquilli
2024-04-10 15:30:40 +02:00
committed by GitHub
12 changed files with 137 additions and 13 deletions

View File

@@ -14,5 +14,7 @@ build:linux --cxxopt=-std=c++20
build:macos --cxxopt=-std=c++20 --cpu=darwin_x86_64
build:windows --cxxopt=/std:c++20 --cxxopt=/Zc:preprocessor
import %workspace%/.bazelrc.exported
common --registry=file://%workspace%/misc/bazel/registry
common --registry=https://bcr.bazel.build
try-import %workspace%/local.bazelrc

View File

@@ -1,4 +0,0 @@
# these settings are shared between `codeql` and `semmle-code`
# emitting jdeps does not work when building the 2.0.0+ kotlin extractor
build --@rules_kotlin//kotlin/settings:jvm_emit_jdeps=false

View File

@@ -21,14 +21,7 @@ bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "abseil-cpp", version = "20240116.0", repo_name = "absl")
bazel_dep(name = "nlohmann_json", version = "3.11.3", repo_name = "json")
bazel_dep(name = "fmt", version = "10.0.0")
bazel_dep(name = "rules_kotlin", version = "1.9.4")
# we patch `rules_kotlin` to allow passing `-language-version` at a jvm_kt_library level
single_version_override(
module_name = "rules_kotlin",
patch_strip = 1,
patches = ["//java/kotlin-extractor:rules_kotlin.patch"],
)
bazel_dep(name = "rules_kotlin", version = "1.9.4-codeql.1")
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(

View File

@@ -0,0 +1,3 @@
Versions to be patched can be taken from https://github.com/bazelbuild/bazel-central-repository. After adding patches
inside `<repo>/<version>/patches`, and eventually renaming `<version>`, run [`fix.py`](./fix.py) to align all metadata
to the renamed version and added patches.

View File

@@ -0,0 +1,3 @@
{
"mirrors": []
}

53
misc/bazel/registry/fix.py Executable file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env python3
"""
Fix metadata in overridden registry, updating `metadata.json` to list correct versions and `source.json`
to list correct patches with sha256 hashes.
"""
import pathlib
import json
import base64
import hashlib
import re
this_dir = pathlib.Path(__file__).resolve().parent
def sha256(file):
with open(file, 'rb') as input:
hash = hashlib.sha256(input.read()).digest()
hash = base64.b64encode(hash).decode()
return f"sha256-{hash}"
def patch_file(file, f):
try:
data = file.read_text()
except FileNotFoundError:
data = None
file.write_text(f(data))
def patch_json(file, **kwargs):
def update(data):
data = json.loads(data) if data else {}
data.update(kwargs)
return json.dumps(data, indent=4) + "\n"
patch_file(file, update)
for entry in this_dir.joinpath("modules").iterdir():
if not entry.is_dir():
continue
versions = [e for e in entry.iterdir() if e.is_dir()]
patch_json(entry / "metadata.json", versions=[v.name for v in versions])
for version in versions:
patch_json(version / "source.json", patches={
p.name: sha256(p) for p in version.joinpath("patches").iterdir()
})
patch_file(version / "MODULE.bazel",
lambda s: re.sub(r'''version\s*=\s*['"].*['"]''', f'version = "{version.name}"', s, 1))

View File

@@ -0,0 +1,31 @@
module(
name = "rules_kotlin",
version = "1.9.4-codeql.1",
repo_name = "rules_kotlin",
)
bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "rules_java", version = "7.2.0")
bazel_dep(name = "rules_python", version = "0.23.1")
bazel_dep(name = "rules_cc", version = "0.0.8")
rules_kotlin_extensions = use_extension(
"//src/main/starlark/core/repositories:bzlmod_setup.bzl",
"rules_kotlin_extensions",
)
use_repo(
rules_kotlin_extensions,
"com_github_google_ksp",
"com_github_jetbrains_kotlin",
"com_github_pinterest_ktlint",
"rules_android",
)
register_toolchains("//kotlin/internal:default_toolchain")
# TODO(bencodes) We should be able to remove this once rules_android has rolled out official Bzlmod support
remote_android_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "remote_android_tools_extensions")
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")

View File

@@ -1,3 +1,5 @@
We need to build different extractor variants with different -language-version options, which is not allowed
in current kotlin_rules
diff --git a/src/main/starlark/core/options/opts.kotlinc.bzl b/src/main/starlark/core/options/opts.kotlinc.bzl
index 9b15fb8..c0ac2cd 100644
--- a/src/main/starlark/core/options/opts.kotlinc.bzl

View File

@@ -0,0 +1,14 @@
Emitting jdeps is broken for the 2.0.0 kotlin extractor, and we don't need those files.
Patching it here rather than passing `--@rules_kotlin//kotlin/settings:jvm_emit_jdeps=false`
allows us to not have to specify that option (and therefore pull in `rules_kotlin`) in `semmle-code`.
--- a/kotlin/settings/BUILD.bazel 2000-01-01 01:00:00.000000000 +0100
+++ b/kotlin/settings/BUILD.bazel 2024-04-10 14:51:16.060085986 +0200
@@ -16,7 +16,7 @@
# Flag that controls the emission of jdeps files during kotlin jvm compilation.
bool_flag(
name = "jvm_emit_jdeps",
- build_setting_default = True, # Upstream default behavior
+ build_setting_default = False,
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,12 @@
===================================================================
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,7 +1,7 @@
module(
name = "rules_kotlin",
- version = "1.9.0",
+ version = "1.9.4-patched",
repo_name = "rules_kotlin",
)
bazel_dep(name = "platforms", version = "0.0.6")

View File

@@ -0,0 +1,10 @@
{
"integrity": "sha256-dsD8wsI+33NjIK3tGs2d3guuQY5XMd8Skz2IbLqGt5U=",
"url": "https://github.com/bazelbuild/rules_kotlin/releases/download/v1.9.4/rules_kotlin-v1.9.4.tar.gz",
"patches": {
"module_dot_bazel_version.patch": "sha256-0GnFHOv9wuuv3jFcHBSXrdo7JMFP7y66O5C4rccy5wg=",
"codeql_do_not_emit_jdeps.patch": "sha256-x/HsujFlR1FGrgmbAbRZag9V4vKZZinBcs73tgRS478=",
"codeql_add_language_version_option.patch": "sha256-qFpP/hIvqGzjJi0h8LAQK0UuWqwlj/oCecZYGqlMVP8="
},
"patch_strip": 1
}

View File

@@ -0,0 +1,5 @@
{
"versions": [
"1.9.4-codeql.1"
]
}