diff --git a/.bazelrc b/.bazelrc index 2e4711826449..8cac362d7c19 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/.bazelrc.exported b/.bazelrc.exported deleted file mode 100644 index 900f660bba0b..000000000000 --- a/.bazelrc.exported +++ /dev/null @@ -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 diff --git a/MODULE.bazel b/MODULE.bazel index 0f6546b4e364..4872f5ae08f0 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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( diff --git a/misc/bazel/registry/README.md b/misc/bazel/registry/README.md new file mode 100644 index 000000000000..5d1723d0eacb --- /dev/null +++ b/misc/bazel/registry/README.md @@ -0,0 +1,3 @@ +Versions to be patched can be taken from https://github.com/bazelbuild/bazel-central-repository. After adding patches +inside `//patches`, and eventually renaming ``, run [`fix.py`](./fix.py) to align all metadata +to the renamed version and added patches. diff --git a/misc/bazel/registry/bazel_registry.json b/misc/bazel/registry/bazel_registry.json new file mode 100644 index 000000000000..ea3f94f7a1e4 --- /dev/null +++ b/misc/bazel/registry/bazel_registry.json @@ -0,0 +1,3 @@ +{ + "mirrors": [] +} diff --git a/misc/bazel/registry/fix.py b/misc/bazel/registry/fix.py new file mode 100755 index 000000000000..c904ebf49517 --- /dev/null +++ b/misc/bazel/registry/fix.py @@ -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)) diff --git a/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/MODULE.bazel b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/MODULE.bazel new file mode 100644 index 000000000000..4982ad4b9a4d --- /dev/null +++ b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/MODULE.bazel @@ -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") diff --git a/java/kotlin-extractor/rules_kotlin.patch b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/codeql_add_language_version_option.patch similarity index 88% rename from java/kotlin-extractor/rules_kotlin.patch rename to misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/codeql_add_language_version_option.patch index 3bc9ff4aac27..bb8fa5e4fcaf 100644 --- a/java/kotlin-extractor/rules_kotlin.patch +++ b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/codeql_add_language_version_option.patch @@ -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 diff --git a/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/codeql_do_not_emit_jdeps.patch b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/codeql_do_not_emit_jdeps.patch new file mode 100644 index 000000000000..1a01bd2c91bc --- /dev/null +++ b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/codeql_do_not_emit_jdeps.patch @@ -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"], + ) + diff --git a/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/module_dot_bazel_version.patch b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/module_dot_bazel_version.patch new file mode 100644 index 000000000000..7a33385b1702 --- /dev/null +++ b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/patches/module_dot_bazel_version.patch @@ -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") diff --git a/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/source.json b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/source.json new file mode 100644 index 000000000000..5941e8379534 --- /dev/null +++ b/misc/bazel/registry/modules/rules_kotlin/1.9.4-codeql.1/source.json @@ -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 +} diff --git a/misc/bazel/registry/modules/rules_kotlin/metadata.json b/misc/bazel/registry/modules/rules_kotlin/metadata.json new file mode 100644 index 000000000000..d1270e0eeb1f --- /dev/null +++ b/misc/bazel/registry/modules/rules_kotlin/metadata.json @@ -0,0 +1,5 @@ +{ + "versions": [ + "1.9.4-codeql.1" + ] +}