2022-10-25 12:55:00 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2023-04-28 11:35:57 +08:00
|
|
|
"strings"
|
2022-10-25 12:55:00 +08:00
|
|
|
|
2023-12-09 01:36:13 +08:00
|
|
|
_ "github.com/sagernet/gomobile"
|
2022-10-25 12:55:00 +08:00
|
|
|
"github.com/sagernet/sing-box/cmd/internal/build_shared"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing/common/rw"
|
|
|
|
)
|
|
|
|
|
2023-02-22 20:52:57 +08:00
|
|
|
var (
|
|
|
|
debugEnabled bool
|
|
|
|
target string
|
|
|
|
)
|
2022-10-25 12:55:00 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&debugEnabled, "debug", false, "enable debug")
|
2023-02-22 20:52:57 +08:00
|
|
|
flag.StringVar(&target, "target", "android", "target platform")
|
2022-10-25 12:55:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-02-22 20:52:57 +08:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-10-25 12:55:00 +08:00
|
|
|
build_shared.FindMobile()
|
|
|
|
|
2023-02-22 20:52:57 +08:00
|
|
|
switch target {
|
|
|
|
case "android":
|
|
|
|
buildAndroid()
|
|
|
|
case "ios":
|
|
|
|
buildiOS()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 16:53:25 +08:00
|
|
|
var (
|
|
|
|
sharedFlags []string
|
|
|
|
debugFlags []string
|
2023-04-28 11:35:57 +08:00
|
|
|
sharedTags []string
|
2023-06-18 10:00:32 +08:00
|
|
|
iosTags []string
|
2023-04-28 11:35:57 +08:00
|
|
|
debugTags []string
|
2023-03-09 16:53:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sharedFlags = append(sharedFlags, "-trimpath")
|
|
|
|
sharedFlags = append(sharedFlags, "-ldflags")
|
|
|
|
currentTag, err := build_shared.ReadTag()
|
|
|
|
if err != nil {
|
|
|
|
currentTag = "unknown"
|
|
|
|
}
|
|
|
|
sharedFlags = append(sharedFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -s -w -buildid=")
|
|
|
|
debugFlags = append(debugFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag)
|
2023-04-28 11:35:57 +08:00
|
|
|
|
2023-08-29 19:43:41 +08:00
|
|
|
sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_ech", "with_utls", "with_clash_api")
|
2023-06-18 10:00:32 +08:00
|
|
|
iosTags = append(iosTags, "with_dhcp", "with_low_memory", "with_conntrack")
|
2023-04-28 11:35:57 +08:00
|
|
|
debugTags = append(debugTags, "debug")
|
2023-03-09 16:53:25 +08:00
|
|
|
}
|
|
|
|
|
2023-02-22 20:52:57 +08:00
|
|
|
func buildAndroid() {
|
|
|
|
build_shared.FindSDK()
|
|
|
|
|
2022-10-25 12:55:00 +08:00
|
|
|
args := []string{
|
|
|
|
"bind",
|
|
|
|
"-v",
|
|
|
|
"-androidapi", "21",
|
|
|
|
"-javapkg=io.nekohasekai",
|
|
|
|
"-libname=box",
|
|
|
|
}
|
|
|
|
if !debugEnabled {
|
2023-03-09 16:53:25 +08:00
|
|
|
args = append(args, sharedFlags...)
|
2022-10-25 12:55:00 +08:00
|
|
|
} else {
|
2023-03-09 16:53:25 +08:00
|
|
|
args = append(args, debugFlags...)
|
2022-10-25 12:55:00 +08:00
|
|
|
}
|
|
|
|
|
2023-03-09 16:53:25 +08:00
|
|
|
args = append(args, "-tags")
|
|
|
|
if !debugEnabled {
|
2023-04-28 11:35:57 +08:00
|
|
|
args = append(args, strings.Join(sharedTags, ","))
|
2023-03-09 16:53:25 +08:00
|
|
|
} else {
|
2023-04-28 11:35:57 +08:00
|
|
|
args = append(args, strings.Join(append(sharedTags, debugTags...), ","))
|
2023-03-09 16:53:25 +08:00
|
|
|
}
|
2022-10-25 12:55:00 +08:00
|
|
|
args = append(args, "./experimental/libbox")
|
|
|
|
|
|
|
|
command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
|
|
|
|
command.Stdout = os.Stdout
|
|
|
|
command.Stderr = os.Stderr
|
|
|
|
err := command.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = "libbox.aar"
|
|
|
|
copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs")
|
|
|
|
if rw.FileExists(copyPath) {
|
|
|
|
copyPath, _ = filepath.Abs(copyPath)
|
|
|
|
err = rw.CopyFile(name, filepath.Join(copyPath, name))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Info("copied to ", copyPath)
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 20:52:57 +08:00
|
|
|
|
|
|
|
func buildiOS() {
|
|
|
|
args := []string{
|
|
|
|
"bind",
|
|
|
|
"-v",
|
2023-07-29 08:37:10 +08:00
|
|
|
"-target", "ios,iossimulator,tvos,tvossimulator,macos",
|
2023-02-22 20:52:57 +08:00
|
|
|
"-libname=box",
|
|
|
|
}
|
|
|
|
if !debugEnabled {
|
2023-03-09 16:53:25 +08:00
|
|
|
args = append(args, sharedFlags...)
|
2023-02-22 20:52:57 +08:00
|
|
|
} else {
|
2023-03-09 16:53:25 +08:00
|
|
|
args = append(args, debugFlags...)
|
2023-02-22 20:52:57 +08:00
|
|
|
}
|
|
|
|
|
2023-06-18 10:00:32 +08:00
|
|
|
tags := append(sharedTags, iosTags...)
|
2023-03-09 16:53:25 +08:00
|
|
|
args = append(args, "-tags")
|
|
|
|
if !debugEnabled {
|
2023-04-28 11:35:57 +08:00
|
|
|
args = append(args, strings.Join(tags, ","))
|
2023-03-09 16:53:25 +08:00
|
|
|
} else {
|
2023-04-28 11:35:57 +08:00
|
|
|
args = append(args, strings.Join(append(tags, debugTags...), ","))
|
2023-03-09 16:53:25 +08:00
|
|
|
}
|
2023-02-22 20:52:57 +08:00
|
|
|
args = append(args, "./experimental/libbox")
|
|
|
|
|
|
|
|
command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
|
|
|
|
command.Stdout = os.Stdout
|
|
|
|
command.Stderr = os.Stderr
|
|
|
|
err := command.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-07-02 16:45:30 +08:00
|
|
|
copyPath := filepath.Join("..", "sing-box-for-apple")
|
2023-02-22 20:52:57 +08:00
|
|
|
if rw.FileExists(copyPath) {
|
|
|
|
targetDir := filepath.Join(copyPath, "Libbox.xcframework")
|
|
|
|
targetDir, _ = filepath.Abs(targetDir)
|
|
|
|
os.RemoveAll(targetDir)
|
|
|
|
os.Rename("Libbox.xcframework", targetDir)
|
|
|
|
log.Info("copied to ", targetDir)
|
|
|
|
}
|
|
|
|
}
|