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

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