Skip to content

Commit

Permalink
go_download_sdk: allow patching to stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
hunshcn committed Sep 10, 2023
1 parent f03a723 commit cc45410
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 12 deletions.
73 changes: 65 additions & 8 deletions go/private/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,61 @@ load(
"//go/private/skylib/lib:versions.bzl",
"versions",
)
load(
"@bazel_tools//tools/build_defs/repo:utils.bzl",
"patch",
)

patch_attrs = {
"patches": attr.label_list(
default = [],
doc =
"A list of files that are to be applied as patches after " +
"extracting the archive. By default, it uses the Bazel-native patch implementation " +
"which doesn't support fuzz match and binary patch, but Bazel will fall back to use " +
"patch command line tool if `patch_tool` attribute is specified or there are " +
"arguments other than `-p` in `patch_args` attribute.",
),
"remote_patches": attr.string_dict(
default = {},
doc =
"A map of patch file URL to its integrity value, they are applied after extracting " +
"the archive and before applying patch files from the `patches` attribute. " +
"It uses the Bazel-native patch implementation, you can specify the patch strip " +
"number with `remote_patch_strip`",
),
"remote_patch_strip": attr.int(
default = 0,
doc =
"The number of leading slashes to be stripped from the file name in the remote patches.",
),
"patch_tool": attr.string(
default = "",
doc = "The patch(1) utility to use. If this is specified, Bazel will use the specified " +
"patch tool instead of the Bazel-native patch implementation.",
),
"patch_args": attr.string_list(
default = ["-p0"],
doc =
"The arguments given to the patch tool. Defaults to -p0, " +
"however -p1 will usually be needed for patches generated by " +
"git. If multiple -p arguments are specified, the last one will take effect." +
"If arguments other than -p are specified, Bazel will fall back to use patch " +
"command line tool instead of the Bazel-native patch implementation. When falling " +
"back to patch command line tool and patch_tool attribute is not specified, " +
"`patch` will be used. This only affects patch files in the `patches` attribute.",
),
"patch_cmds": attr.string_list(
default = [],
doc = "Sequence of Bash commands to be applied on Linux/Macos after patches are applied.",
),
"patch_cmds_win": attr.string_list(
default = [],
doc = "Sequence of Powershell commands to be applied on Windows after patches are " +
"applied. If this attribute is not set, patch_cmds will be executed on Windows, " +
"which requires Bash binary to exist.",
),
}

MIN_SUPPORTED_VERSION = (1, 14, 0)

Expand All @@ -37,15 +92,15 @@ def _go_host_sdk_impl(ctx):
go_host_sdk_rule = repository_rule(
implementation = _go_host_sdk_impl,
environ = ["GOROOT"],
attrs = {
attrs = dict({
"version": attr.string(),
"experiments": attr.string_list(
doc = "Go experiments to enable via GOEXPERIMENT",
),
"_sdk_build_file": attr.label(
default = Label("//go/private:BUILD.sdk.bazel"),
),
},
}, **patch_attrs),
)

def go_host_sdk(name, register_toolchains = True, **kwargs):
Expand Down Expand Up @@ -136,7 +191,7 @@ def _go_download_sdk_impl(ctx):

go_download_sdk_rule = repository_rule(
implementation = _go_download_sdk_impl,
attrs = {
attrs = dict({
"goos": attr.string(),
"goarch": attr.string(),
"sdks": attr.string_list_dict(),
Expand All @@ -149,7 +204,7 @@ go_download_sdk_rule = repository_rule(
"_sdk_build_file": attr.label(
default = Label("//go/private:BUILD.sdk.bazel"),
),
},
}, **patch_attrs),
)

def _define_version_constants(version, prefix = ""):
Expand Down Expand Up @@ -325,7 +380,7 @@ def _go_local_sdk_impl(ctx):

_go_local_sdk = repository_rule(
implementation = _go_local_sdk_impl,
attrs = {
attrs = dict({
"path": attr.string(),
"version": attr.string(),
"experiments": attr.string_list(
Expand All @@ -334,7 +389,7 @@ _go_local_sdk = repository_rule(
"_sdk_build_file": attr.label(
default = Label("//go/private:BUILD.sdk.bazel"),
),
},
}, **patch_attrs),
)

def go_local_sdk(name, register_toolchains = True, **kwargs):
Expand Down Expand Up @@ -371,7 +426,7 @@ def _go_wrap_sdk_impl(ctx):

_go_wrap_sdk = repository_rule(
implementation = _go_wrap_sdk_impl,
attrs = {
attrs = dict({
"root_file": attr.label(
mandatory = False,
doc = "A file in the SDK root direcotry. Used to determine GOROOT.",
Expand All @@ -387,7 +442,7 @@ _go_wrap_sdk = repository_rule(
"_sdk_build_file": attr.label(
default = Label("//go/private:BUILD.sdk.bazel"),
),
},
}, **patch_attrs),
)

def go_wrap_sdk(name, register_toolchains = True, **kwargs):
Expand Down Expand Up @@ -478,6 +533,8 @@ def _sdk_build_file(ctx, platform, version, experiments):
if not "nocoverageredesign" in experiments and not "coverageredesign" in experiments:
experiments = experiments + ["nocoverageredesign"]

patch(ctx)

ctx.template(
"BUILD.bazel",
ctx.path(ctx.attr._sdk_build_file),
Expand Down
Loading

0 comments on commit cc45410

Please sign in to comment.