main.go 3.2 KB

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