From 7cdba8177986f823ba98336875363413a9e790a4 Mon Sep 17 00:00:00 2001 From: Bartosz Popiela Date: Sat, 11 May 2024 17:14:01 +0200 Subject: [PATCH] oci_push to support Windows builds (#565) Co-authored-by: Bartosz Popiela Co-authored-by: Alex Eagle --- oci/private/BUILD.bazel | 5 ++++- oci/private/push.bzl | 6 ++++-- oci/private/util.bzl | 30 ++++++++++++++++-------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/oci/private/BUILD.bazel b/oci/private/BUILD.bazel index 91ccb4af..5af904cb 100644 --- a/oci/private/BUILD.bazel +++ b/oci/private/BUILD.bazel @@ -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", + ], ) diff --git a/oci/private/push.bzl b/oci/private/push.bzl index ae8b1d84..d305180e 100644 --- a/oci/private/push.bzl +++ b/oci/private/push.bzl @@ -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): @@ -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", ], ) diff --git a/oci/private/util.bzl b/oci/private/util.bzl index 44a4deb2..7c2a2c25 100644 --- a/oci/private/util.bzl +++ b/oci/private/util.bzl @@ -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): @@ -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([ @@ -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, ) @@ -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", @@ -184,7 +188,6 @@ _INDEX_JSON_TMPL="""\ }}""" def _build_manifest_json(media_type, size, digest, platform): - optional_platform = "" if platform: @@ -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): @@ -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, @@ -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, )