bazel: transition oci_image ourselves instead of via with_cfg.bzl (#60896)

with_cfg.bzl is a lot of complicated starlark which we can avoid having to try understand when debugging needs arise by doing what it does by-hand and specific to what we need (aka super cut down and simplified).

Extracted from https://github.com/sourcegraph/sourcegraph/compare/jcjh/msp-bazel-delivery#diff-1a8a445b4ce2a72080eca8ae2a3ae24bc904175e9d9aa2dd1938a29746ae86a3 while we were debugging why AW delivery was being problematic (this wasnt the reason, but I did this change in case it _was_ causing issues and itd be more understandable to read)

## Test plan

`bazel run //cmd/batcheshelper:image_tarball && docker run batcheshelper:candidate --help`
This commit is contained in:
Noah S-C 2024-03-06 16:05:38 +00:00 committed by GitHub
parent e9ef9d8740
commit 6ee3e17796
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 25 deletions

View File

@ -159,13 +159,6 @@ http_archive(
url = "https://github.com/keith/rules_multirun/archive/refs/tags/0.6.1.tar.gz",
)
http_archive(
name = "with_cfg.bzl",
sha256 = "c6b80cad298afa8a46bc01cd96df4f4d8660651101f6bf5af58f2724e349017d",
strip_prefix = "with_cfg.bzl-0.2.1",
url = "https://github.com/fmeum/with_cfg.bzl/releases/download/v0.2.1/with_cfg.bzl-v0.2.1.tar.gz",
)
# hermetic_cc_toolchain setup ================================
HERMETIC_CC_TOOLCHAIN_VERSION = "v2.2.1"

View File

@ -1,11 +1,13 @@
"""OCI bazel defs"""
load("@rules_oci//oci:defs.bzl", _oci_image = "oci_image", _oci_push = "oci_push", _oci_tarball = "oci_tarball")
load("@with_cfg.bzl", "with_cfg")
REGISTRY_REPOSITORY_PREFIX = "europe-west1-docker.pkg.dev/sourcegraph-security-logging/rules-oci-test/{}"
# REGISTRY_REPOSITORY_PREFIX = "us.gcr.io/sourcegraph-dev/{}"
# Passthrough the @rules_oci oci_push, so users only have to import this file and not @rules_oci//oci:defs.bzl
oci_push = _oci_push
def image_repository(image):
return REGISTRY_REPOSITORY_PREFIX.format(image)
@ -21,21 +23,37 @@ def oci_tarball(name, **kwargs):
# Apply a transition on oci_image targets and their deps to apply a transition on platforms
# to build binaries for Linux when building on MacOS.
#
# Note: internally, this does some magic with wrapper rules and aliases that will be visible
# in bazel (c)query outputs, and will make {,a,c}query output be non-obvious. For a given
# oci_image target e.g. //cmd/server:image, the following targets will be created:
# - //cmd/server:image, which is an alias to //cmd/server:image_with_cfg
# - //cmd/server:image_with_cfg, uses an internal rule to apply the transition
# - //cmd/server:image_/image, the actual oci_image target
#
# When querying oci_image rules, you should query the final one noted above. The others will
# not surface the information you're actually looking for for things like aquery etc.
_oci_image_builder = with_cfg(_oci_image)
_oci_image_builder.set("platforms", select({
"@platforms//os:macos": [Label("@zig_sdk//platform:linux_amd64")],
"//conditions:default": [],
}))
def oci_image(name, **kwargs):
_oci_image(
name = name + "_underlying",
**kwargs
)
oci_image, _oci_image_internal = _oci_image_builder.build()
oci_push = _oci_push
oci_image_cross(
name = name,
image = ":" + name + "_underlying",
platforms = select({
"@platforms//os:macos": [Label("@zig_sdk//platform:linux_amd64")],
"//conditions:default": [],
}),
visibility = kwargs.pop("visibility", ["//visibility:public"]),
)
# rule that allows transitioning in order to transition an oci_image target and its deps
oci_image_cross = rule(
implementation = lambda ctx: DefaultInfo(files = depset(ctx.files.image)),
attrs = {
"image": attr.label(cfg = transition(
implementation = lambda settings, attr: [
{"//command_line_option:platforms": str(platform)}
for platform in attr.platforms
],
inputs = [],
outputs = ["//command_line_option:platforms"],
)),
"platforms": attr.label_list(),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)