resin-xbuild.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "os/exec"
  6. "syscall"
  7. )
  8. func crossBuildStart() {
  9. err := os.Remove("/bin/sh")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. err = os.Link("/usr/bin/resin-xbuild", "/bin/sh")
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. }
  18. func crossBuildEnd() {
  19. err := os.Remove("/bin/sh")
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. err = os.Link("/bin/sh.real", "/bin/sh")
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. }
  28. func runShell() error {
  29. cmd := exec.Command("/usr/bin/qemu-arm-static", append([]string{"-0", "/bin/sh", "/bin/sh"}, os.Args[1:]...)...)
  30. cmd.Stdin = os.Stdin
  31. cmd.Stdout = os.Stdout
  32. cmd.Stderr = os.Stderr
  33. return cmd.Run()
  34. }
  35. func main() {
  36. switch os.Args[0] {
  37. case "cross-build-start":
  38. crossBuildStart()
  39. case "cross-build-end":
  40. crossBuildEnd()
  41. case "/bin/sh":
  42. code := 0
  43. crossBuildEnd()
  44. if err := runShell(); err != nil {
  45. code = 1
  46. if exiterr, ok := err.(*exec.ExitError); ok {
  47. if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
  48. code = status.ExitStatus()
  49. }
  50. }
  51. }
  52. crossBuildStart()
  53. // Hack to bypass apk issues with triggering
  54. code = 0
  55. os.Exit(code)
  56. }
  57. }