Skip to content

Commit

Permalink
Reland "[bazel] Fix golang cross-compilation on RBE"
Browse files Browse the repository at this point in the history
This is a reland of commit b2109e6

See cr/488438081

Original change's description:
> [bazel] Fix golang cross-compilation on RBE
>
> Without this change, one could not have a Mac Bazel host
> and use our Linux executor to build for a Mac target.
> What I observed was the host uploading the Mac go toolchain
> to the Linux RBE instance and that not working.
>
> To fix this, we explicitly download a Linux golang toolchain
> as well as one for the host machine. This is straightforward
> after the fact (See WORKSPACE.bazel) but took a bit of
> experimentation and doc reading to get right.
>
> To *use* the right toolchain, one must tell the Linux
> RBE instance the target platform should be the same as
> the host platform. This is a smidge tricky to do correctly
> in a script (e.g. a Makefile), so I also added a short
> Bazel (read: Python) script to call `go version` and parse
> the output into a Bazel-ready os_arch string.
>
> Change-Id: If009316209c46efa912f39f0f55bd7d6734d27c5
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/604061
> Reviewed-by: Leandro Lovisolo <[email protected]>

Change-Id: I8bbeee8bbf6109810915125d581548ebc36acff0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/605077
Reviewed-by: Leandro Lovisolo <[email protected]>
  • Loading branch information
kjlubick committed Nov 16, 2022
1 parent 35ee411 commit a4cb7f2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def _CheckBazelBUILDFiles(input_api, output_api):
excluded_paths = ["infra/", "bazel/rbe/", "bazel/external/", "bazel/common_config_settings/",
"modules/canvaskit/go/", "experimental/", "bazel/platform", "third_party/",
"tests/", "resources/", "bazel/deps_parser/", "bazel/exporter_tool/",
"tools/gpu/gl/interface/"]
"tools/gpu/gl/interface/", "bazel/utils/"]
is_excluded = any(affected_file_path.startswith(n) for n in excluded_paths)
if is_bazel and not is_excluded:
with open(affected_file_path, 'r') as file:
Expand Down
31 changes: 25 additions & 6 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ pip_install(
#######################################################################################
http_archive(
name = "io_bazel_rules_go",
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
],
)

Expand All @@ -108,7 +108,7 @@ http_archive(
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("//:go_repositories.bzl", "go_repositories")

Expand All @@ -117,9 +117,28 @@ go_repositories()

go_rules_dependencies()

go_register_toolchains(version = "1.18")
# For our Linux RBE pool
go_download_sdk(
name = "go_sdk_linux_amd64",
goarch = "amd64",
goos = "linux",
version = "1.18",
)

# For the host machine
go_download_sdk(
name = "go_sdk",
version = "1.18",
)

gazelle_dependencies(go_repository_default_config = "//:WORKSPACE.bazel")
# Do not specify a version here or it will be an error (because we
# specified the version above when downloading SDKs)
go_register_toolchains()

gazelle_dependencies(
go_repository_default_config = "//:WORKSPACE.bazel",
go_sdk = "go_sdk",
)

# Because the skia infra repo has a dependency on google.golang.org/grpc (aka
# @org_golang_google_grpc), we need to have this library available to build protos.
Expand Down
3 changes: 2 additions & 1 deletion bazel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ generate_gni:
.PHONY: generate_gni_rbe
generate_gni_rbe:
cd .. && bazelisk build //bazel/exporter_tool --config=linux_rbe --jobs=100 \
--remote_download_toplevel && \
--platforms=@io_bazel_rules_go//go/toolchain:$(shell bazelisk run //bazel/utils:go_platform) \
--remote_download_toplevel && \
${EXPORTER_TOOL} -output_format=gni -proj_name=Skia ${EXPORTER_RULES} \
&& bazelisk run //bazel/external/gn -- format gn/*.gni modules/*/*.gni

Expand Down
38 changes: 38 additions & 0 deletions bazel/utils/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# A helper function to get the go platform string of the Bazel host.
# This can be used to do cross-platform go compilations.
# Example:
# $ bazelisk run //bazel:go_platform
# darwin_arm64
py_binary(
name = "go_platform",
srcs = ["go_platform.py"],
data = ["@go_sdk//:bin/go"],
tags = ["no-remote"], # Need the platform of the host
)

_GO_PLATFORM = """
import os
import subprocess
# https://bazel.build/reference/be/make-variables#predefined_label_variables
go_exe = os.path.abspath("$(execpath @go_sdk//:bin/go)")
result = subprocess.run([
go_exe,
"version",
], capture_output=True, encoding="utf-8")
# e.g. go version go1.18 darwin/arm64
os_arch = result.stdout.strip().split(" ")[3]
# e.g. darwin/arm64
print(os_arch.replace("/", "_"))
"""

genrule(
name = "create_go_platform_script",
outs = ["go_platform.py"],
cmd = "echo '%s' > $@" % _GO_PLATFORM,
exec_tools = [
"@go_sdk//:bin/go",
],
)

0 comments on commit a4cb7f2

Please sign in to comment.