| #!/bin/bash | |
| # Copyright 2018 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. | |
| ### run the go tool in fuchsia target configuration | |
| ## Usage: | |
| ## fx go <go tool args> | |
| ## fx go --package=packagename <go tool args> | |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh | |
| fx-config-read | |
| function usage { | |
| fx-command-help | |
| exit 1 | |
| } | |
| case "${FUCHSIA_ARCH}" in | |
| arm64) | |
| GOARCH=arm64 | |
| ;; | |
| x64) | |
| GOARCH=amd64 | |
| ;; | |
| esac | |
| GOROOT="${FUCHSIA_BUILD_DIR}/tools/goroot" | |
| if [[ ! -d "${GOROOT}" ]]; then | |
| echo >&2 "ERROR: You must build the goroot before running this command" | |
| exit 1 | |
| fi | |
| while getopts ":-:" opt; do | |
| case $opt in | |
| \?) | |
| echo >&2 "ERROR: Unrecognized short option: -$OPTARG" | |
| usage | |
| ;; | |
| -) | |
| case $OPTARG in | |
| package=*) PACKAGE=${OPTARG#*=};; | |
| *) | |
| echo >&2 "ERROR: Unrecognized long option: $OPTARG" | |
| usage | |
| ;; | |
| esac | |
| ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| ## If --package is provided and has a gopath in $FUCHSIA_BUILD_DIR/gen/gopaths, | |
| ## prepend the given gopath to $GOPATH before invoking $GOROOT/bin/go. | |
| function package_gopath { | |
| local package="$1" | |
| if [[ -z "$package" ]]; then | |
| return 1 | |
| fi | |
| gopathdir="$FUCHSIA_BUILD_DIR/gen/gopaths" | |
| d="$gopathdir/$package" | |
| if [[ ! -d "$d" ]]; then | |
| echo >&2 "ERROR: Package not found in $gopathdir: $package" | |
| return 1 | |
| fi | |
| echo $d | |
| } | |
| if [[ -n $PACKAGE ]]; then | |
| maybe_gopath="$(package_gopath $PACKAGE)" | |
| GOPATH="${maybe_gopath}:${GOPATH}" | |
| fi | |
| source "${FUCHSIA_DIR}/buildtools/vars.sh" | |
| CC="${GOROOT}/misc/fuchsia/clangwrap.sh" \ | |
| FDIO_INCLUDE="${FUCHSIA_DIR}/zircon/system/ulib/fdio/include" \ | |
| FUCHSIA_SHARED_LIBS="${FUCHSIA_BUILD_DIR}/${FUCHSIA_ARCH}-shared" \ | |
| ZIRCON_SYSROOT="${FUCHSIA_BUILD_DIR}/sdk/exported/zircon_sysroot/arch/${FUCHSIA_ARCH}/sysroot" \ | |
| CLANG_PREFIX="${BUILDTOOLS_CLANG_DIR}/bin" \ | |
| GOOS=fuchsia GOARCH=${GOARCH} CGO_ENABLED=1 GOPATH="${GOPATH}" GOROOT="${GOROOT}" \ | |
| "${GOROOT}/bin/go" "$@" |