1#!/bin/bash 2set -e -o pipefail 3 4# This wrapper copies an executable to a target device and executes it there. 5# 6# Usage: go_target_exec <target> <binary> <args>... 7# 8# This script can work with both ChromeOS/Android devices. 9# 10# It uses "target_tmpdir" to get the path to the temporary directory on the device. 11# It uses "target_cp" to copy the binary to the temporary directory on the device. 12# It uses "target_sh" to execute the binary remotely and get the output/exitcode. 13 14target="$1" 15shift 16 17binary="$1" 18shift 19 20# Get path to temporary directory on device and copy the binary over. 21tmpdir="$(target_tmpdir)" 22target_cp ${binary} ${target}:${tmpdir}/a.out 23 24# If current directory is inside GOROOT, then execute the binary in the 25# corresponding directory inside GOROOT on the device. 26targetdir="${tmpdir}" 27goroot="$(go_${target} env GOROOT)" 28if [[ "${PWD}" == ${goroot}/src/* ]] 29then 30 targetdir="${tmpdir}/goroot/src/${PWD#${goroot}/src/}" 31fi 32 33# Set GOROOT, and forward some environment variables to the remote shell. 34vars="GOROOT=${tmpdir}/goroot" 35vars+="${GOOS:+ GOOS=${GOOS}}" 36vars+="${GOARCH:+ GOARCH=${GOARCH}}" 37vars+="${GOMAXPROCS:+ GOMAXPROCS=${GOMAXPROCS}}" 38vars+="${GOTRACEBACK:+ GOTRACEBACK=${GOTRACEBACK}}" 39 40# Remotely execute the binary using ssh (for ChromeOS) or adb (for Android). 41target_sh ${target} "cd ${targetdir} && ${vars} ${GOLOADER} ${tmpdir}/a.out $*" 42