Skip to content

Commit

Permalink
Allow to select host/remote Go SDK (#3303)
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k authored Sep 28, 2022
1 parent b6f10d7 commit 3e0fcc4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
8 changes: 8 additions & 0 deletions go/private/BUILD.sdk.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ config_setting(
},
)

config_setting(
name = "match_sdk_type",
flag_values = {
sdk_version_label: "{sdk_type}",
},
)

selects.config_setting_group(
name = "sdk_version_setting",
match_any = [
Expand All @@ -105,6 +112,7 @@ selects.config_setting_group(
":match_major_minor_version",
":match_patch_version",
":match_prerelease_version",
":match_sdk_type",
],
)

Expand Down
5 changes: 3 additions & 2 deletions go/private/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _go_host_sdk_impl(ctx):
goroot = _detect_host_sdk(ctx)
platform = _detect_sdk_platform(ctx, goroot)
version = _detect_sdk_version(ctx, goroot)
_sdk_build_file(ctx, platform, version)
_sdk_build_file(ctx, platform, version, "host")
_local_sdk(ctx, goroot)

_go_host_sdk = repository_rule(
Expand Down Expand Up @@ -231,7 +231,7 @@ def _local_sdk(ctx, path):
for entry in ["src", "pkg", "bin", "lib"]:
ctx.symlink(path + "/" + entry, entry)

def _sdk_build_file(ctx, platform, version):
def _sdk_build_file(ctx, platform, version, sdk_type = "remote"):
ctx.file("ROOT")
goos, _, goarch = platform.partition("_")

Expand All @@ -254,6 +254,7 @@ def _sdk_build_file(ctx, platform, version):
"{minor_version}": str(minor),
"{patch_version}": str(patch),
"{prerelease_suffix}": prerelease,
"{sdk_type}": sdk_type,
},
)

Expand Down
2 changes: 2 additions & 0 deletions go/toolchains.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ the flag ``--@io_bazel_rules_go//go/toolchain:sdk_version="version"`` where
``"version"`` is the SDK version you would like to build with, eg. ``"1.18.3"``.
The SDK version can omit the patch, or include a prerelease part, eg. ``"1"``,
``"1.18"``, ``"1.18.0"``, and ``"1.19.0beta1"`` are all valid values for ``sdk_version``.
When ``go_host_sdk`` is used, ``"version"`` can be set to ``host`` to refer to the host Go SDK.
It can also be set ``remote`` to match any non-host version.

The toolchain
~~~~~~~~~~~~~
Expand Down
31 changes: 17 additions & 14 deletions tests/core/go_download_sdk/go_download_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ go_download_sdk(
{
desc: "multiple_sdks",
rule: `
load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk")
load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_host_sdk")
go_download_sdk(
name = "go_sdk",
Expand All @@ -107,7 +107,8 @@ go_download_sdk(
`,
optToWantVersion: map[string]string{
"": "go1.16",
"--@io_bazel_rules_go//go/toolchain:sdk_version=1": "go1.16",
"--@io_bazel_rules_go//go/toolchain:sdk_version=remote": "go1.16",
"--@io_bazel_rules_go//go/toolchain:sdk_version=1": "go1.16",
"--@io_bazel_rules_go//go/toolchain:sdk_version=1.17": "go1.17",
"--@io_bazel_rules_go//go/toolchain:sdk_version=1.17.0": "go1.17",
"--@io_bazel_rules_go//go/toolchain:sdk_version=1.17.1": "go1.17.1",
Expand All @@ -122,7 +123,7 @@ go_download_sdk(

i := bytes.Index(origWorkspaceData, []byte("go_rules_dependencies()"))
if i < 0 {
t.Fatalf("%s: could not find call to go_rules_dependencies()")
t.Fatal("could not find call to go_rules_dependencies()")
}

buf := &bytes.Buffer{}
Expand All @@ -143,17 +144,19 @@ go_register_toolchains()
}()

for opt, wantVersion := range test.optToWantVersion {
args := []string{
"test",
"//:version_test",
"--test_arg=-version=" + wantVersion,
}
if opt != "" {
args = append(args, opt)
}
if err := bazel_testing.RunBazel(args...); err != nil {
t.Fatal(err)
}
t.Run(wantVersion, func(t *testing.T) {
args := []string{
"test",
"//:version_test",
"--test_arg=-version=" + wantVersion,
}
if opt != "" {
args = append(args, opt)
}
if err := bazel_testing.RunBazel(args...); err != nil {
t.Fatal(err)
}
})
}
})
}
Expand Down

0 comments on commit 3e0fcc4

Please sign in to comment.