| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | 
							- package main
 
- import (
 
- 	"flag"
 
- 	"os"
 
- 	"os/exec"
 
- 	"path/filepath"
 
- 	"strings"
 
- 	_ "github.com/sagernet/gomobile"
 
- 	"github.com/sagernet/sing-box/cmd/internal/build_shared"
 
- 	"github.com/sagernet/sing-box/log"
 
- 	E "github.com/sagernet/sing/common/exceptions"
 
- 	"github.com/sagernet/sing/common/rw"
 
- 	"github.com/sagernet/sing/common/shell"
 
- )
 
- var (
 
- 	debugEnabled bool
 
- 	target       string
 
- 	platform     string
 
- )
 
- func init() {
 
- 	flag.BoolVar(&debugEnabled, "debug", false, "enable debug")
 
- 	flag.StringVar(&target, "target", "android", "target platform")
 
- 	flag.StringVar(&platform, "platform", "", "specify platform")
 
- }
 
- func main() {
 
- 	flag.Parse()
 
- 	build_shared.FindMobile()
 
- 	switch target {
 
- 	case "android":
 
- 		buildAndroid()
 
- 	case "apple":
 
- 		buildApple()
 
- 	}
 
- }
 
- var (
 
- 	sharedFlags []string
 
- 	debugFlags  []string
 
- 	sharedTags  []string
 
- 	iosTags     []string
 
- 	memcTags    []string
 
- 	debugTags   []string
 
- )
 
- func init() {
 
- 	sharedFlags = append(sharedFlags, "-trimpath")
 
- 	sharedFlags = append(sharedFlags, "-buildvcs=false")
 
- 	currentTag, err := build_shared.ReadTag()
 
- 	if err != nil {
 
- 		currentTag = "unknown"
 
- 	}
 
- 	sharedFlags = append(sharedFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -s -w -buildid=")
 
- 	debugFlags = append(debugFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag)
 
- 	sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_ech", "with_utls", "with_clash_api")
 
- 	iosTags = append(iosTags, "with_dhcp", "with_low_memory", "with_conntrack")
 
- 	memcTags = append(memcTags, "with_tailscale")
 
- 	debugTags = append(debugTags, "debug")
 
- }
 
- func buildAndroid() {
 
- 	build_shared.FindSDK()
 
- 	var javaPath string
 
- 	javaHome := os.Getenv("JAVA_HOME")
 
- 	if javaHome == "" {
 
- 		javaPath = "java"
 
- 	} else {
 
- 		javaPath = filepath.Join(javaHome, "bin", "java")
 
- 	}
 
- 	javaVersion, err := shell.Exec(javaPath, "--version").ReadOutput()
 
- 	if err != nil {
 
- 		log.Fatal(E.Cause(err, "check java version"))
 
- 	}
 
- 	if !strings.Contains(javaVersion, "openjdk 17") {
 
- 		log.Fatal("java version should be openjdk 17")
 
- 	}
 
- 	var bindTarget string
 
- 	if platform != "" {
 
- 		bindTarget = platform
 
- 	} else if debugEnabled {
 
- 		bindTarget = "android/arm64"
 
- 	} else {
 
- 		bindTarget = "android"
 
- 	}
 
- 	args := []string{
 
- 		"bind",
 
- 		"-v",
 
- 		"-target", bindTarget,
 
- 		"-androidapi", "21",
 
- 		"-javapkg=io.nekohasekai",
 
- 		"-libname=box",
 
- 	}
 
- 	if !debugEnabled {
 
- 		args = append(args, sharedFlags...)
 
- 	} else {
 
- 		args = append(args, debugFlags...)
 
- 	}
 
- 	tags := append(sharedTags, memcTags...)
 
- 	if debugEnabled {
 
- 		tags = append(tags, debugTags...)
 
- 	}
 
- 	args = append(args, "-tags", strings.Join(tags, ","))
 
- 	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.IsDir(copyPath) {
 
- 		copyPath, _ = filepath.Abs(copyPath)
 
- 		err = rw.CopyFile(name, filepath.Join(copyPath, name))
 
- 		if err != nil {
 
- 			log.Fatal(err)
 
- 		}
 
- 		log.Info("copied to ", copyPath)
 
- 	}
 
- }
 
- func buildApple() {
 
- 	var bindTarget string
 
- 	if platform != "" {
 
- 		bindTarget = platform
 
- 	} else if debugEnabled {
 
- 		bindTarget = "ios"
 
- 	} else {
 
- 		bindTarget = "ios,tvos,macos"
 
- 	}
 
- 	args := []string{
 
- 		"bind",
 
- 		"-v",
 
- 		"-target", bindTarget,
 
- 		"-libname=box",
 
- 		"-tags-macos=" + strings.Join(memcTags, ","),
 
- 	}
 
- 	if !debugEnabled {
 
- 		args = append(args, sharedFlags...)
 
- 	} else {
 
- 		args = append(args, debugFlags...)
 
- 	}
 
- 	tags := append(sharedTags, iosTags...)
 
- 	if debugEnabled {
 
- 		tags = append(tags, debugTags...)
 
- 	}
 
- 	args = append(args, "-tags", strings.Join(tags, ","))
 
- 	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("..", "sing-box-for-apple")
 
- 	if rw.IsDir(copyPath) {
 
- 		targetDir := filepath.Join(copyPath, "Libbox.xcframework")
 
- 		targetDir, _ = filepath.Abs(targetDir)
 
- 		os.RemoveAll(targetDir)
 
- 		os.Rename("Libbox.xcframework", targetDir)
 
- 		log.Info("copied to ", targetDir)
 
- 	}
 
- }
 
 
  |