github.com/sagernet/sing-box@v1.2.7/cmd/internal/build_libbox/main.go (about)

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