main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. _ "github.com/sagernet/gomobile/asset"
  8. "github.com/sagernet/sing-box/cmd/internal/build_shared"
  9. "github.com/sagernet/sing-box/log"
  10. "github.com/sagernet/sing/common/rw"
  11. )
  12. var (
  13. debugEnabled bool
  14. target string
  15. )
  16. func init() {
  17. flag.BoolVar(&debugEnabled, "debug", false, "enable debug")
  18. flag.StringVar(&target, "target", "android", "target platform")
  19. }
  20. func main() {
  21. flag.Parse()
  22. build_shared.FindMobile()
  23. switch target {
  24. case "android":
  25. buildAndroid()
  26. case "ios":
  27. buildiOS()
  28. }
  29. }
  30. func buildAndroid() {
  31. build_shared.FindSDK()
  32. args := []string{
  33. "bind",
  34. "-v",
  35. "-androidapi", "21",
  36. "-javapkg=io.nekohasekai",
  37. "-libname=box",
  38. }
  39. if !debugEnabled {
  40. args = append(args,
  41. "-trimpath", "-ldflags=-s -w -buildid=",
  42. "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api",
  43. )
  44. } else {
  45. args = append(args, "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug")
  46. }
  47. args = append(args, "./experimental/libbox")
  48. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  49. command.Stdout = os.Stdout
  50. command.Stderr = os.Stderr
  51. err := command.Run()
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. const name = "libbox.aar"
  56. copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs")
  57. if rw.FileExists(copyPath) {
  58. copyPath, _ = filepath.Abs(copyPath)
  59. err = rw.CopyFile(name, filepath.Join(copyPath, name))
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. log.Info("copied to ", copyPath)
  64. }
  65. }
  66. func buildiOS() {
  67. args := []string{
  68. "bind",
  69. "-v",
  70. "-target", "ios,iossimulator,macos",
  71. "-libname=box",
  72. }
  73. if !debugEnabled {
  74. args = append(
  75. args, "-trimpath", "-ldflags=-s -w -buildid=",
  76. "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api",
  77. )
  78. } else {
  79. args = append(args, "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug")
  80. }
  81. args = append(args, "./experimental/libbox")
  82. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  83. command.Stdout = os.Stdout
  84. command.Stderr = os.Stderr
  85. err := command.Run()
  86. if err != nil {
  87. log.Fatal(err)
  88. }
  89. copyPath := filepath.Join("..", "sfi")
  90. if rw.FileExists(copyPath) {
  91. targetDir := filepath.Join(copyPath, "Libbox.xcframework")
  92. targetDir, _ = filepath.Abs(targetDir)
  93. os.RemoveAll(targetDir)
  94. os.Rename("Libbox.xcframework", targetDir)
  95. log.Info("copied to ", targetDir)
  96. }
  97. }