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 Aug 28, 2023
1 parent 2e821f6 commit 507e24a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 5 additions & 2 deletions go/private/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _go_download_sdk_impl(ctx):
if platform not in sdks:
fail("unsupported platform {}".format(platform))
filename, sha256 = sdks[platform]
_remote_sdk(ctx, [url.format(filename) for url in ctx.attr.urls], ctx.attr.strip_prefix, sha256)
_remote_sdk(ctx, [url.format(filename) for url in ctx.attr.urls], ctx.attr.strip_prefix, sha256, ctx.attr.patches)

detected_version = _detect_sdk_version(ctx, ".")
_sdk_build_file(ctx, platform, detected_version, experiments = ctx.attr.experiments)
Expand Down Expand Up @@ -149,6 +149,7 @@ go_download_sdk_rule = repository_rule(
"_sdk_build_file": attr.label(
default = Label("//go/private:BUILD.sdk.bazel"),
),
"patches": attr.label_list(),
},
)

Expand Down Expand Up @@ -406,7 +407,7 @@ def go_wrap_sdk(name, register_toolchains = True, **kwargs):
def _register_toolchains(repo):
native.register_toolchains("@{}_toolchains//:all".format(repo))

def _remote_sdk(ctx, urls, strip_prefix, sha256):
def _remote_sdk(ctx, urls, strip_prefix, sha256, patches):
if len(urls) == 0:
fail("no urls specified")
host_goos, _ = detect_host_platform(ctx)
Expand Down Expand Up @@ -461,6 +462,8 @@ def _remote_sdk(ctx, urls, strip_prefix, sha256):
stripPrefix = strip_prefix,
sha256 = sha256,
)
for p in patches:
ctx.patch(p)

def _local_sdk(ctx, path):
for entry in ["src", "pkg", "bin", "lib", "misc"]:
Expand Down
4 changes: 4 additions & 0 deletions go/toolchains.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ This downloads a Go SDK for use in toolchains.
| Go distribution (with a different SHA-256 sum) or a version of Go |
| not supported by rules_go (for example, a beta or release candidate). |
+--------------------------------+-----------------------------+---------------------------------------------+
| :param:`patches` | :type:`label_list` | :value:`None` |
+--------------------------------+-----------------------------+---------------------------------------------+
| List of patches to apply, should be a standard unified diff format file. |
+--------------------------------+-----------------------------+---------------------------------------------+

**Example**:

Expand Down
36 changes: 32 additions & 4 deletions tests/core/go_download_sdk/go_download_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package go_download_sdk_test

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
Expand All @@ -25,12 +25,26 @@ import (
func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{
Main: `
-- go.patch --
--- src/runtime/extern.go 1969-12-31 16:00:00
+++ src/runtime/extern.go 2000-01-01 00:00:00.000000000 -0000
@@ -244,7 +244,7 @@
// It is either the commit hash and date at the time of the build or,
// when possible, a release tag like "go1.3".
func Version() string {
- return sys.TheVersion
+ return "go100.0"
}
// GOOS is the running program's operating system target:
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_test")
go_test(
name = "version_test",
srcs = ["version_test.go"],
pure = "on",
)
-- version_test.go --
Expand Down Expand Up @@ -59,6 +73,20 @@ func Test(t *testing.T) {
optToWantVersion map[string]string
fetchOnly string
}{
{
desc: "patch",
rule: `
load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk")
go_download_sdk(
name = "go_sdk_patch",
version = "1.16",
patches = ["//:go.patch"],
)
`,
optToWantVersion: map[string]string{"": "go100.0"},
},
{
desc: "version",
rule: `
Expand Down Expand Up @@ -133,7 +161,7 @@ go_download_sdk(
},
} {
t.Run(test.desc, func(t *testing.T) {
origWorkspaceData, err := ioutil.ReadFile("WORKSPACE")
origWorkspaceData, err := os.ReadFile("WORKSPACE")
if err != nil {
t.Fatal(err)
}
Expand All @@ -151,11 +179,11 @@ go_rules_dependencies()
go_register_toolchains()
`)
if err := ioutil.WriteFile("WORKSPACE", buf.Bytes(), 0666); err != nil {
if err := os.WriteFile("WORKSPACE", buf.Bytes(), 0666); err != nil {
t.Fatal(err)
}
defer func() {
if err := ioutil.WriteFile("WORKSPACE", origWorkspaceData, 0666); err != nil {
if err := os.WriteFile("WORKSPACE", origWorkspaceData, 0666); err != nil {
t.Errorf("error restoring WORKSPACE: %v", err)
}
}()
Expand Down

0 comments on commit 507e24a

Please sign in to comment.