main.go 3.3 KB

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