sing-box/cmd/internal/build_libbox/main.go

114 lines
2.4 KiB
Go
Raw Normal View History

2022-10-25 12:55:00 +08:00
package main
import (
"flag"
"os"
"os/exec"
"path/filepath"
2023-03-02 00:18:35 +08:00
_ "github.com/sagernet/gomobile/event/key"
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()
}
}
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 {
args = append(args,
"-trimpath", "-ldflags=-s -w -buildid=",
2023-02-22 20:52:57 +08:00
"-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api",
2022-10-25 12:55:00 +08:00
)
} else {
2023-02-22 20:52:57 +08:00
args = append(args, "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug")
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",
"-target", "ios,iossimulator,macos",
"-libname=box",
}
if !debugEnabled {
2023-03-01 10:37:47 +08:00
args = append(
args, "-trimpath", "-ldflags=-s -w -buildid=",
"-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api",
2023-02-22 20:52:57 +08:00
)
} else {
2023-03-01 10:37:47 +08:00
args = append(args, "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug")
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)
}
copyPath := filepath.Join("..", "sfi")
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)
}
}