Skip to content

Commit

Permalink
oci_push to support Windows builds (#565)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartosz Popiela <[email protected]>
Co-authored-by: Alex Eagle <[email protected]>
  • Loading branch information
3 people committed May 11, 2024
1 parent c720572 commit 7cdba81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
5 changes: 4 additions & 1 deletion oci/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,8 @@ bzl_library(
name = "util",
srcs = ["util.bzl"],
visibility = ["//oci:__subpackages__"],
deps = ["@bazel_skylib//lib:versions"],
deps = [
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:versions",
],
)
6 changes: 4 additions & 2 deletions oci/private/push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ _attrs = {
default = "push.sh.tpl",
allow_single_file = True,
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
}

def _quote_args(args):
Expand Down Expand Up @@ -178,14 +179,15 @@ def _impl(ctx):
runfiles = runfiles.merge(jq.default.default_runfiles)
runfiles = runfiles.merge(crane.default.default_runfiles)

return DefaultInfo(executable = executable, runfiles = runfiles)
return DefaultInfo(executable = util.maybe_wrap_launcher_for_windows(ctx, executable), runfiles = runfiles)

oci_push_lib = struct(
implementation = _impl,
attrs = _attrs,
toolchains = [
"@rules_oci//oci:crane_toolchain_type",
"@aspect_bazel_lib//lib:jq_toolchain_type",
"@aspect_bazel_lib//lib:yq_toolchain_type",
"@bazel_tools//tools/sh:toolchain_type",
],
)

Expand Down
30 changes: 16 additions & 14 deletions oci/private/util.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Utilities"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:versions.bzl", "versions")

_IMAGE_PLATFORM_VARIANT_DEFAULTS = {
'linux/arm64': 'v8',
"linux/arm64": "v8",
}

def _parse_image(image):
Expand Down Expand Up @@ -113,12 +115,12 @@ def _validate_image_platform(rctx, image_config):
attr_variant_or_default = attr_variant or _IMAGE_PLATFORM_VARIANT_DEFAULTS.get(rctx.attr.platform, None)
image_variant_or_default = image_variant or _IMAGE_PLATFORM_VARIANT_DEFAULTS.get(image_os + "/" + image_architecture, None)
if image_variant_or_default != attr_variant_or_default:
fail("Image {}/{} has platform variant '{}', but 'platforms' attribute specifies variant '{}'".format(
rctx.attr.registry,
rctx.attr.repository,
image_variant,
attr_variant,
))
fail("Image {}/{} has platform variant '{}', but 'platforms' attribute specifies variant '{}'".format(
rctx.attr.registry,
rctx.attr.repository,
image_variant,
attr_variant,
))

def _warning(rctx, message):
rctx.execute([
Expand Down Expand Up @@ -150,16 +152,18 @@ SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in ("{bash_bin}") do set "bash_bin_dir=%%~dpa"
set PATH=%bash_bin_dir%;%PATH%
set "parent_dir=%~dp0"
set "parent_dir=!parent_dir:\=/!"
set args=%*
rem Escape \ and * in args before passing it with double quote
if defined args (
set args=!args:\=\\\\!
set args=!args:"=\"!
)
"{bash_bin}" -c "{launcher} !args!"
"{bash_bin}" -c "%parent_dir%{launcher} !args!"
""".format(
bash_bin = ctx.toolchains["@bazel_tools//tools/sh:toolchain_type"].path,
launcher = bash_launcher.path,
launcher = paths.relativize(bash_launcher.path, win_launcher.dirname),
),
is_executable = True,
)
Expand All @@ -170,7 +174,7 @@ def _file_exists(rctx, path):
result = rctx.execute(["stat", path])
return result.return_code == 0

_INDEX_JSON_TMPL="""\
_INDEX_JSON_TMPL = """\
{{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
Expand All @@ -184,7 +188,6 @@ _INDEX_JSON_TMPL="""\
}}"""

def _build_manifest_json(media_type, size, digest, platform):

optional_platform = ""

if platform:
Expand All @@ -205,7 +208,7 @@ def _build_manifest_json(media_type, size, digest, platform):
media_type,
size,
digest,
optional_platform = optional_platform
optional_platform = optional_platform,
)

def _assert_crane_version_at_least(ctx, at_least, rule):
Expand All @@ -221,7 +224,6 @@ def _platform_triplet(platform_str):
architecture, _, variant = architecture.partition("/")
return os, architecture, variant


util = struct(
parse_image = _parse_image,
sha256 = _sha256,
Expand All @@ -231,5 +233,5 @@ util = struct(
file_exists = _file_exists,
build_manifest_json = _build_manifest_json,
assert_crane_version_at_least = _assert_crane_version_at_least,
platform_triplet = _platform_triplet
platform_triplet = _platform_triplet,
)

0 comments on commit 7cdba81

Please sign in to comment.