2022-10-25 12:55:00 +08:00
|
|
|
package build_shared
|
2022-12-11 14:38:01 +08:00
|
|
|
|
|
|
|
import (
|
2022-10-25 12:55:00 +08:00
|
|
|
"go/build"
|
2022-12-11 14:38:01 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-02-02 16:30:50 +08:00
|
|
|
"runtime"
|
2022-12-11 14:38:01 +08:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing/common"
|
2024-02-16 13:28:58 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-12-11 14:38:01 +08:00
|
|
|
"github.com/sagernet/sing/common/rw"
|
2024-02-16 13:28:58 +08:00
|
|
|
"github.com/sagernet/sing/common/shell"
|
2022-12-11 14:38:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
androidSDKPath string
|
|
|
|
androidNDKPath string
|
|
|
|
)
|
|
|
|
|
2022-10-25 12:55:00 +08:00
|
|
|
func FindSDK() {
|
2022-12-11 14:38:01 +08:00
|
|
|
searchPath := []string{
|
|
|
|
"$ANDROID_HOME",
|
|
|
|
"$HOME/Android/Sdk",
|
|
|
|
"$HOME/.local/lib/android/sdk",
|
|
|
|
"$HOME/Library/Android/sdk",
|
|
|
|
}
|
|
|
|
for _, path := range searchPath {
|
|
|
|
path = os.ExpandEnv(path)
|
|
|
|
if rw.FileExists(path + "/licenses/android-sdk-license") {
|
|
|
|
androidSDKPath = path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if androidSDKPath == "" {
|
|
|
|
log.Fatal("android SDK not found")
|
|
|
|
}
|
|
|
|
if !findNDK() {
|
|
|
|
log.Fatal("android NDK not found")
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:28:58 +08:00
|
|
|
javaVersion, err := shell.Exec("java", "--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")
|
|
|
|
}
|
|
|
|
|
2022-12-11 14:38:01 +08:00
|
|
|
os.Setenv("ANDROID_HOME", androidSDKPath)
|
|
|
|
os.Setenv("ANDROID_SDK_HOME", androidSDKPath)
|
|
|
|
os.Setenv("ANDROID_NDK_HOME", androidNDKPath)
|
|
|
|
os.Setenv("NDK", androidNDKPath)
|
2023-02-02 16:30:50 +08:00
|
|
|
os.Setenv("PATH", os.Getenv("PATH")+":"+filepath.Join(androidNDKPath, "toolchains", "llvm", "prebuilt", runtime.GOOS+"-x86_64", "bin"))
|
2022-12-11 14:38:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func findNDK() bool {
|
|
|
|
if rw.FileExists(androidSDKPath + "/ndk/25.1.8937393") {
|
|
|
|
androidNDKPath = androidSDKPath + "/ndk/25.1.8937393"
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
ndkVersions, err := os.ReadDir(androidSDKPath + "/ndk")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
versionNames := common.Map(ndkVersions, os.DirEntry.Name)
|
|
|
|
if len(versionNames) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sort.Slice(versionNames, func(i, j int) bool {
|
|
|
|
iVersions := strings.Split(versionNames[i], ".")
|
|
|
|
jVersions := strings.Split(versionNames[j], ".")
|
|
|
|
for k := 0; k < len(iVersions) && k < len(jVersions); k++ {
|
|
|
|
iVersion, _ := strconv.Atoi(iVersions[k])
|
|
|
|
jVersion, _ := strconv.Atoi(jVersions[k])
|
|
|
|
if iVersion != jVersion {
|
|
|
|
return iVersion > jVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
for _, versionName := range versionNames {
|
|
|
|
if rw.FileExists(androidSDKPath + "/ndk/" + versionName) {
|
|
|
|
androidNDKPath = androidSDKPath + "/ndk/" + versionName
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-10-25 12:55:00 +08:00
|
|
|
|
|
|
|
var GoBinPath string
|
|
|
|
|
|
|
|
func FindMobile() {
|
|
|
|
goBin := filepath.Join(build.Default.GOPATH, "bin")
|
2024-02-01 10:44:29 +08:00
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if !rw.FileExists(goBin + "/" + "gobind.exe") {
|
|
|
|
log.Fatal("missing gomobile.exe installation")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !rw.FileExists(goBin + "/" + "gobind") {
|
|
|
|
log.Fatal("missing gomobile installation")
|
|
|
|
}
|
2022-10-25 12:55:00 +08:00
|
|
|
}
|
|
|
|
GoBinPath = goBin
|
|
|
|
}
|