blob: 2d2eb632ceaa40b439264cbe2c8f06d40ba5d2df [file] [log] [blame]
# Copyright 2017 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.
# A template for an action that builds a Go binary. Users should instead use the
# go_binary or go_test rules.
#
# Every go_build target with any go_dependencies declared has a new GOPATH
# constructed. The scopes contained within go_dependencies define symlinks that
# will be added to the GOPATH.
#
# Example:
# go_build("mypackage") {
# gopackage = "fuchsia.googlesource.com/mypackage"
# go_dependencies = [
# {
# package = "fuchsia.googlesource.com/mypackage"
# },
# {
# source = "//third_party/golang/crypto"
# package = "golang.org/x/crypto"
# }
# ]
# }
#
# The above go_dependencies declaration is most typical, it creates a GOPATH
# entry for "fuchsia.googlesource.com/mypackage" that points to the directory
# containing the caller BUILD.gn. If source is ever omitted, it is assumed to be
# the directory of the containing action.
#
# At miniumum there should be a go_dependencies declaration for the package to
# be built.
import("//build/toolchain/clang_toolchain.gni")
template("go_build") {
assert(defined(invoker.gopackage),
"gopackage must be defined for $target_name")
action(target_name) {
forward_variables_from(invoker, [ "deps" ])
output_name = target_name
if (defined(invoker.output_name)) {
output_name = invoker.output_name
}
use_strip = is_fuchsia
output_path = "${root_out_dir}/${output_name}"
outputs = [
output_path,
]
if (use_strip) {
unstripped_output_path = "${root_out_dir}/exe.unstripped/${output_name}"
outputs += [ unstripped_output_path ]
}
script = "//build/go/build.py"
depfile = "${output_path}.d"
go_dependencies = []
if (defined(invoker.go_dependencies)) {
# builds on this code path can be concurrent
go_dependencies = invoker.go_dependencies
} else {
# only run one gobuild.py in parallel
pool = "//build/go:go_pool"
}
args = [
"--fuchsia-root",
rebase_path("//."),
"--root-out-dir",
rebase_path(root_out_dir, root_build_dir),
"--zircon-sysroot",
rebase_path(zircon_sysroot),
"--depfile",
rebase_path(depfile),
"--current-cpu",
current_cpu,
"--current-os",
current_os,
"--go-tool",
rebase_path("//buildtools/go"),
"--binname",
output_name,
"--toolchain-prefix",
rebase_path(clang_prefix, "", root_build_dir),
]
if (use_strip) {
args += [
"--unstripped-binname",
"exe.unstripped/${output_name}",
]
}
if (defined(invoker.test) && invoker.test) {
args += [ "--is-test=true" ]
}
foreach(dep, go_dependencies) {
if (!defined(dep.source)) {
dep.source = "."
}
source = dep.source
args += [
"--go-dependency",
dep.package + "=" + rebase_path(source),
]
}
args += [ invoker.gopackage ]
if (!defined(deps)) {
deps = []
}
deps += [ "//third_party/go:go_runtime" ]
}
}