1 #!/bin/bash 2 set -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 14 target="$1" 15 shift 16 17 binary="$1" 18 shift 19 20 # Get path to temporary directory on device and copy the binary over. 21 tmpdir="$(target_tmpdir)" 22 target_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. 26 targetdir="${tmpdir}" 27 goroot="$(go_${target} env GOROOT)" 28 if [[ "${PWD}" == ${goroot}/src/* ]] 29 then 30 targetdir="${tmpdir}/go/src/${PWD#${goroot}/src/}" 31 fi 32 33 # Set GOROOT, and forward some environment variables to the remote shell. 34 vars="GOROOT=${tmpdir}/go" 35 vars+="${GOOS:+ GOOS=${GOOS}}" 36 vars+="${GOARCH:+ GOARCH=${GOARCH}}" 37 vars+="${GOMAXPROCS:+ GOMAXPROCS=${GOMAXPROCS}}" 38 vars+="${GOTRACEBACK:+ GOTRACEBACK=${GOTRACEBACK}}" 39 40 # Remotely execute the binary using ssh (for ChromeOS) or adb (for Android). 41 target_sh ${target} "cd ${targetdir} && ${vars} ${tmpdir}/a.out $*" 42