blob: 245e7384ba45f3ced686d0bdeef308265ecff6ff [file] [log] [blame]
# Copyright 2016 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import functools
from recipe_engine import recipe_api
GO_CIPD_PACKAGE = "infra/3pp/tools/go/${platform}"
GO_CIPD_VERSION = "version:2@1.20"
# Clang is needed for cgo.
CLANG_CIPD_PACKAGE = "fuchsia/third_party/clang/${platform}"
CLANG_CIPD_VERSION = "git_revision:6d667d4b261e81f325756fdfd5bb43b3b3d2451d"
class GoApi(recipe_api.RecipeApi):
"""GoApi provides support for Go."""
def build(self, args, step_name="go build"):
cmd = [
"build",
*args,
]
return self._run(step_name, cmd)
def test(self, args, step_name="go test", **kwargs):
cmd = [
"test",
*args,
]
return self._run(step_name, cmd, **kwargs)
def list(self, args, step_name="go list", **kwargs):
cmd = [
"list",
*args,
]
return self._run(step_name, cmd, **kwargs)
def install(self, args, step_name="go install"):
cmd = [
"install",
*args,
]
return self._run(step_name, cmd)
def _run(self, step_name, args, **kwargs):
env = {
"GOROOT": self.go_root,
"CC": self.go_root.join("clang", "bin", "clang"),
}
with self.m.linux_sdk():
if self.m.context.env.get("GOOS", self.m.platform.name) == "linux":
# The context API interprets bare percent signs as formatting
# directives so they must be escaped.
sysroot_dir = str(self.m.linux_sdk.sysroot).replace("%", "%%")
env["CGO_CFLAGS"] = env["CGO_LDFLAGS"] = f"--sysroot={sysroot_dir}"
with self.m.context(env=env):
return self.m.step(
step_name,
[self.go_bin.join("go")] + args,
**kwargs,
)
@property
def go_bin(self):
return self.go_root.join("bin")
@functools.cached_property
def go_root(self):
"""Ensure that Go is installed and return the path to the Go dir."""
with self.m.step.nest("ensure go"), self.m.context(infra_steps=True):
# Force-install the linux sysroot so the installation step doesn't
# pollute the top level.
with self.m.linux_sdk():
pass
pkgs = self.m.cipd.EnsureFile()
pkgs.add_package(GO_CIPD_PACKAGE, GO_CIPD_VERSION)
pkgs.add_package(CLANG_CIPD_PACKAGE, CLANG_CIPD_VERSION, "clang")
dest = self.m.path["start_dir"].join("cipd", "go")
self.m.cipd.ensure(dest, pkgs)
return dest