1
0
mirror of https://github.com/b4tman/armhf-alpine-qemu.git synced 2024-12-05 01:26:54 +00:00
armhf-alpine-qemu/resin-xbuild.go
2021-10-11 00:34:34 +03:00

67 lines
1.1 KiB
Go

package main
import (
"log"
"os"
"os/exec"
filepath "path/filepath"
"syscall"
)
func crossBuildStart() {
err := os.Remove("/bin/sh")
if err != nil {
log.Fatal(err)
}
err = os.Link("/usr/bin/resin-xbuild", "/bin/sh")
if err != nil {
log.Fatal(err)
}
}
func crossBuildEnd() {
err := os.Remove("/bin/sh")
if err != nil {
log.Fatal(err)
}
err = os.Link("/bin/sh.real", "/bin/sh")
if err != nil {
log.Fatal(err)
}
}
func runShell() error {
cmd := exec.Command("/usr/bin/qemu-arm-static", append([]string{"-execve", "-0", os.Args[0], "/bin/sh"}, os.Args[1:]...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func main() {
command := filepath.Base(os.Args[0])
switch command {
case "cross-build-start":
crossBuildStart()
case "cross-build-end":
crossBuildEnd()
default:
code := 0
crossBuildEnd()
if err := runShell(); err != nil {
code = 1
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
code = status.ExitStatus()
}
}
}
crossBuildStart()
os.Exit(code)
}
}