main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. _ "github.com/sagernet/gomobile/event/key"
  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. var (
  31. sharedFlags []string
  32. debugFlags []string
  33. )
  34. func init() {
  35. sharedFlags = append(sharedFlags, "-trimpath")
  36. sharedFlags = append(sharedFlags, "-ldflags")
  37. currentTag, err := build_shared.ReadTag()
  38. if err != nil {
  39. currentTag = "unknown"
  40. }
  41. sharedFlags = append(sharedFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -s -w -buildid=")
  42. debugFlags = append(debugFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag)
  43. }
  44. func buildAndroid() {
  45. build_shared.FindSDK()
  46. args := []string{
  47. "bind",
  48. "-v",
  49. "-androidapi", "21",
  50. "-javapkg=io.nekohasekai",
  51. "-libname=box",
  52. }
  53. if !debugEnabled {
  54. args = append(args, sharedFlags...)
  55. } else {
  56. args = append(args, debugFlags...)
  57. }
  58. args = append(args, "-tags")
  59. if !debugEnabled {
  60. args = append(args, "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api")
  61. } else {
  62. args = append(args, "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug")
  63. }
  64. args = append(args, "./experimental/libbox")
  65. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  66. command.Stdout = os.Stdout
  67. command.Stderr = os.Stderr
  68. err := command.Run()
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. const name = "libbox.aar"
  73. copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs")
  74. if rw.FileExists(copyPath) {
  75. copyPath, _ = filepath.Abs(copyPath)
  76. err = rw.CopyFile(name, filepath.Join(copyPath, name))
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. log.Info("copied to ", copyPath)
  81. }
  82. }
  83. func buildiOS() {
  84. args := []string{
  85. "bind",
  86. "-v",
  87. "-target", "ios,iossimulator,macos",
  88. "-libname=box",
  89. }
  90. if !debugEnabled {
  91. args = append(args, sharedFlags...)
  92. } else {
  93. args = append(args, debugFlags...)
  94. }
  95. args = append(args, "-tags")
  96. if !debugEnabled {
  97. args = append(args, "with_gvisor,with_utls,with_clash_api,with_low_memory,with_conntrack")
  98. } else {
  99. args = append(args, "with_gvisor,with_utls,with_clash_api,with_low_memory,with_conntrack,debug")
  100. }
  101. args = append(args, "./experimental/libbox")
  102. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  103. command.Stdout = os.Stdout
  104. command.Stderr = os.Stderr
  105. err := command.Run()
  106. if err != nil {
  107. log.Fatal(err)
  108. }
  109. copyPath := filepath.Join("..", "sing-box-for-ios")
  110. if rw.FileExists(copyPath) {
  111. targetDir := filepath.Join(copyPath, "Libbox.xcframework")
  112. targetDir, _ = filepath.Abs(targetDir)
  113. os.RemoveAll(targetDir)
  114. os.Rename("Libbox.xcframework", targetDir)
  115. log.Info("copied to ", targetDir)
  116. }
  117. }