forked from balena-io-library/armv7hf-debian-qemu
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cross-build.go
executable file
·79 lines (69 loc) · 1.6 KB
/
cross-build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"log"
"os"
"os/exec"
"syscall"
)
func crossBuildStart() {
if _, err := os.Stat("/bin/sh.real"); os.IsNotExist(err) {
err = os.Link("/bin/sh", "/bin/sh.real")
if err != nil {
log.Fatal(err)
}
}
err := os.Remove("/bin/sh")
if err != nil {
log.Fatal(err)
}
err = os.Link("/usr/bin/cross-build", "/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 {
if _, err := os.Stat("/usr/bin/qemu-arm-static"); err == nil {
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()
} else {
cmd := exec.Command("/usr/bin/qemu-aarch64-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() {
switch os.Args[0] {
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)
}
}