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