Skip to content

Commit

Permalink
Make SDK rules more flexible (bazel-contrib#2070)
Browse files Browse the repository at this point in the history
go_download_sdk now accepts a "version" attribute. This allows
downloading a specific version of Go without needing to load
SDK_REPOSITORIES.

go_register_toolchains now detects any declared go_*_sdk rule, not
just one named "go_sdk".

At this point, nothing in rules_go should rely on toolchains being in
a repository named "go_sdk".

Fixes bazel-contrib#1507
  • Loading branch information
Jay Conrod authored May 20, 2019
1 parent a3e3b98 commit e9ba5a4
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 434 deletions.
5 changes: 1 addition & 4 deletions go/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ load(
"@io_bazel_rules_go//go/private:repositories.bzl",
_go_rules_dependencies = "go_rules_dependencies",
)
load(
"@io_bazel_rules_go//go/toolchain:toolchains.bzl",
_go_register_toolchains = "go_register_toolchains",
)
load(
"@io_bazel_rules_go//go/private:sdk.bzl",
_go_download_sdk = "go_download_sdk",
_go_host_sdk = "go_host_sdk",
_go_local_sdk = "go_local_sdk",
_go_register_toolchains = "go_register_toolchains",
_go_wrap_sdk = "go_wrap_sdk",
)

Expand Down
66 changes: 63 additions & 3 deletions go/private/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@ load(
"env_execute",
"executable_path",
)
load(
"@io_bazel_rules_go//go/private:nogo.bzl",
"go_register_nogo",
)
load(
"@io_bazel_rules_go//go/private:sdk_list.bzl",
"DEFAULT_VERSION",
"MIN_SUPPORTED_VERSION",
"SDK_REPOSITORIES",
)
load(
"@io_bazel_rules_go//go/platform:list.bzl",
"generate_toolchain_names",
)
load(
"@io_bazel_rules_go//go/private:skylib/lib/versions.bzl",
"versions",
)

def _go_host_sdk_impl(ctx):
goroot = _detect_host_sdk(ctx)
Expand All @@ -38,14 +52,28 @@ def go_host_sdk(name, **kwargs):
_register_toolchains(name)

def _go_download_sdk_impl(ctx):
sdks = ctx.attr.sdks
if ctx.attr.version:
if ctx.attr.sdks:
fail("version and sdks must not both be set")
if ctx.attr.version not in SDK_REPOSITORIES:
fail("unknown Go version: {}".format(ctx.attr.version))
sdks = SDK_REPOSITORIES[ctx.attr.version]
elif ctx.attr.sdks:
sdks = ctx.attr.sdks
else:
sdks = SDK_REPOSITORIES[DEFAULT_VERSION]

if not ctx.attr.goos and not ctx.attr.goarch:
platform = _detect_host_platform(ctx)
else:
if not ctx.attr.goos:
fail("goos set but goarch not set")
if not ctx.attr.goarch:
fail("goarch set but goos not set")
platform = ctx.attr.goos + "_" + ctx.attr.goarch
if platform not in sdks:
fail("Unsupported platform {}".format(platform))
filename, sha256 = ctx.attr.sdks[platform]
fail("unsupported platform {}".format(platform))
filename, sha256 = sdks[platform]
_sdk_build_file(ctx, platform)
_remote_sdk(ctx, [url.format(filename) for url in ctx.attr.urls], ctx.attr.strip_prefix, sha256)

Expand All @@ -56,6 +84,7 @@ _go_download_sdk = repository_rule(
"goarch": attr.string(),
"sdks": attr.string_list_dict(),
"urls": attr.string_list(default = ["https://dl.google.com/go/{}"]),
"version": attr.string(),
"strip_prefix": attr.string(default = "go"),
},
)
Expand Down Expand Up @@ -209,3 +238,34 @@ def _detect_sdk_platform(ctx, goroot):
if f.find("_") >= 0:
return f
fail("Could not detect SDK platform")

def go_register_toolchains(go_version = None, nogo = None):
"""See /go/toolchains.rst#go-register-toolchains for full documentation."""
sdk_kinds = ("_go_download_sdk", "_go_host_sdk", "_go_local_sdk", "_go_wrap_sdk")
existing_rules = native.existing_rules()
sdk_rules = [r for r in existing_rules.values() if r["kind"] in sdk_kinds]
if len(sdk_rules) == 0 and "go_sdk" in existing_rules:
# may be local_repository in bazel_tests.
sdk_rules.append(existing_rules["go_sdk"])

if go_version and len(sdk_rules) > 0:
fail("go_version set after go sdk rule declared ({})".format(", ".join([r["name"] for r in sdk_rules])))
if len(sdk_rules) == 0:
if not go_version:
go_version = DEFAULT_VERSION
if go_version == "host":
go_host_sdk(name = "go_sdk")
else:
if not versions.is_at_least(MIN_SUPPORTED_VERSION, go_version):
print("DEPRECATED: go_register_toolchains: support for Go versions before {} will be removed soon".format(MIN_SUPPORTED_VERSION))
go_download_sdk(
name = "go_sdk",
version = go_version,
)

if nogo:
# Override default definition in go_rules_dependencies().
go_register_nogo(
name = "io_bazel_rules_nogo",
nogo = nogo,
)
Loading

0 comments on commit e9ba5a4

Please sign in to comment.