/
android.go
160 lines (137 loc) · 4.22 KB
/
android.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package utils
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/sirupsen/logrus"
)
func JDK_DIR() string {
if dir, ok := os.LookupEnv("JDK_DIR"); ok {
return filepath.Clean(dir)
}
if dir, ok := os.LookupEnv("JAVA_HOME"); ok {
return filepath.Clean(dir)
}
for _, l := range strings.Split(RunCmd(exec.Command("java", "-XshowSettings:properties", "-version"), "deploy.jdk"), "\n") {
l = strings.TrimSpace(l)
if strings.HasPrefix(l, "java.home") {
return strings.Split(l, " = ")[1]
}
}
return filepath.Join(os.Getenv("HOME"), "jdk")
}
func ANDROID_SDK_DIR() string {
if dir, ok := os.LookupEnv("ANDROID_SDK_DIR"); ok {
return filepath.Clean(dir)
}
if dir, ok := os.LookupEnv("ANDROID_SDK_ROOT"); ok {
return filepath.Clean(dir)
}
switch runtime.GOOS {
case "windows":
return windowsSystemDrive() + "\\android-sdk-windows"
case "darwin":
return filepath.Join(os.Getenv("HOME"), "android-sdk-macosx")
default:
return filepath.Join(os.Getenv("HOME"), "android-sdk-linux")
}
}
func ANDROID_NDK_DIR() string {
if dir, ok := os.LookupEnv("ANDROID_NDK_DIR"); ok {
return filepath.Clean(dir)
}
if dir, ok := os.LookupEnv("ANDROID_NDK_ROOT"); ok {
return filepath.Clean(dir)
}
version := "r18b" //TODO: test r19c and 20b
if QT_VERSION_NUM() >= 5140 {
version = "r21"
}
if runtime.GOOS == "windows" {
return windowsSystemDrive() + "\\android-ndk-" + version
}
return filepath.Join(os.Getenv("HOME"), "android-ndk-"+version)
}
func ANDROID_NDK_MAJOR_VERSION() int {
var (
propsRegex = regexp.MustCompile(`[#].*\\n|\\s+\\n|\\S+[=]|.*\n`)
pairRegex = regexp.MustCompile(`\s+=\s+`)
)
source := filepath.Join(ANDROID_NDK_DIR(), "source.properties")
props, err := ioutil.ReadFile(source)
if err != nil {
fields := logrus.Fields{"_func": "ANDROID_NDK_MAJOR_VERSION", "source": source}
Log.WithError(err).WithFields(fields).Error("Failed to read source.properties file of Android NDK")
os.Exit(1)
}
var version_string string
pairs := propsRegex.FindAll(props, -1)
for _, pair := range pairs {
str_pair := strings.TrimSpace(string(pair))
if strings.HasPrefix(str_pair, "Pkg.Revision") {
if aux := pairRegex.Split(str_pair, -1); len(aux) == 2 {
version_string = aux[1]
break
} else {
fields := logrus.Fields{"_func": "ANDROID_NDK_MAJOR_VERSION", "property_pair": str_pair}
Log.WithError(err).WithFields(fields).Error("Failed to proccess source.properties file of Android NDK")
os.Exit(1)
}
}
}
rawVersion := strings.Split(version_string, ".")
major, err := strconv.Atoi(rawVersion[0])
if err != nil {
fields := logrus.Fields{"_func": "ANDROID_NDK_MAJOR_VERSION", "major_version": rawVersion[0]}
Log.WithError(err).WithFields(fields).Error("Failed to parse major version of Android NDK")
os.Exit(1)
}
return major
}
// ANDROID_NDK_REQUIRE_NOSTDLIBPP_LDFLAG returns true if the -nostdlib++ flag is required by Android NDK and false otherwise
func ANDROID_NDK_REQUIRE_NOSTDLIBPP_LDFLAG() bool {
if ANDROID_NDK_MAJOR_VERSION() > 19 && QT_VERSION_NUM() < 5140 {
return true
}
return false
}
// ANDROID_NDK_NOSTDLIBPP_LDFLAG return -nostdlib++ or empty string if that flag is no required by Android NDK
func ANDROID_NDK_NOSTDLIBPP_LDFLAG() string {
if ANDROID_NDK_REQUIRE_NOSTDLIBPP_LDFLAG() {
return "-nostdlib++"
}
return ""
}
// ANDROID_NDK_PLATFORM returns the Android API level to use in the building process
func ANDROID_NDK_PLATFORM() string {
// default value as set in cmd.go BuildEnv function
if QT_FELGO() {
return "android-16"
}
// if env var exists and is not empty use env var of the system
if value, exist := os.LookupEnv("ANDROID_NDK_PLATFORM"); exist && value != "" {
return value
}
// default value as recommended in https://wiki.qt.io/Qt_for_Android_known_issues
// to workaround problems with arm64 android builds
return "android-21"
}
func ANDROID_EXTRA_LIBS() string {
return os.Getenv("ANDROID_EXTRA_LIBS")
}
func ANDROID_MODULES_INCLUDE() string {
return os.Getenv("ANDROID_MODULES_INCLUDE")
}
func ADD_ANDROID_MODULES_INCLUDE(module string) {
val := os.Getenv("ANDROID_MODULES_INCLUDE")
mod := module
if val != "" {
mod = "," + mod
}
os.Setenv("ANDROID_MODULES_INCLUDE", val+mod)
}