github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/cmd/AndroidNDKBuild/main.go (about)

     1  // AndroidNDKBuild
     2  // go build -ldflags "-X main.APILevel=15 -X main.Arch=x86_64"
     3  // env ANDROID_API_LEVEL NDK ANDROID_NDK_ROOT GOARCH
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"runtime"
    12  	"syscall"
    13  )
    14  
    15  var (
    16  	// NDKPath path to Android NDK
    17  	NDKPath string
    18  	// APILevel Android api level
    19  	APILevel string
    20  	// Arch arch
    21  	Arch string
    22  )
    23  
    24  func getNDKPath() string {
    25  	ndkPath, ok := os.LookupEnv("NDK")
    26  	if ok {
    27  		return ndkPath
    28  	}
    29  	ndkPath, ok = os.LookupEnv("ANDROID_NDK_ROOT")
    30  	if ok {
    31  		return ndkPath
    32  	}
    33  	ndkPath, ok = os.LookupEnv("ANDROID_NDK_DIR")
    34  	if ok {
    35  		return ndkPath
    36  	}
    37  	return ""
    38  }
    39  
    40  func getAPILevel() string {
    41  	apiLevelStr, ok := os.LookupEnv("ANDROID_API_LEVEL")
    42  	if ok {
    43  		return apiLevelStr
    44  	}
    45  	return "21"
    46  }
    47  
    48  func getGoarch() string {
    49  	arch, ok := os.LookupEnv("GOARCH")
    50  	if ok {
    51  		return arch
    52  	}
    53  
    54  	return runtime.GOARCH
    55  }
    56  
    57  func getArch() string {
    58  	if Arch != "" {
    59  		return Arch
    60  	}
    61  	goarch := getGoarch()
    62  	switch goarch {
    63  	case "386":
    64  		return "x86"
    65  	case "amd64":
    66  		return "x86_64"
    67  	case "arm64":
    68  		return "aarch64"
    69  	}
    70  	return goarch
    71  }
    72  
    73  func getPlatformsArch() string {
    74  	arch := getArch()
    75  	switch arch {
    76  	case "aarch64":
    77  		return "arm64"
    78  	}
    79  	return arch
    80  }
    81  
    82  func main() {
    83  	if NDKPath == "" {
    84  		NDKPath = getNDKPath()
    85  	}
    86  	if APILevel == "" {
    87  		APILevel = getAPILevel()
    88  	}
    89  	if Arch == "" {
    90  		Arch = getArch()
    91  	}
    92  
    93  	lastPattern := "*-gcc"
    94  	if runtime.GOOS == "windows" {
    95  		lastPattern += ".exe"
    96  	}
    97  
    98  	gccPaths, err := filepath.Glob(filepath.Join(NDKPath, "toolchains", getArch()+"-*", "prebuilt", runtime.GOOS+"-*", "bin", lastPattern))
    99  	checkErr(err)
   100  	if len(gccPaths) == 0 {
   101  		panic("no match gcc")
   102  	}
   103  
   104  	args := make([]string, len(os.Args))
   105  	copy(args[1:], os.Args[1:])
   106  	args[0] = "--sysroot=" + filepath.Join(NDKPath, "platforms", "android-"+APILevel, "arch-"+getPlatformsArch())
   107  
   108  	gccExec := exec.Command(gccPaths[0], args...)
   109  	gccExec.Stdout = os.Stdout
   110  	gccExec.Stderr = os.Stderr
   111  
   112  	err = gccExec.Run()
   113  	exitError, ok := err.(*exec.ExitError)
   114  	if ok {
   115  		status := exitError.ProcessState.Sys().(syscall.WaitStatus)
   116  		os.Exit(status.ExitStatus())
   117  	}
   118  
   119  	if err != nil {
   120  		println(err.Error())
   121  	}
   122  
   123  	return
   124  }
   125  
   126  func checkErr(err error) {
   127  	if err != nil {
   128  		panic(err)
   129  	}
   130  }