4
4
# Android ndk version must be ndk-r9b.
5
5
6
6
7
+ import argparse
7
8
import sys
8
9
import os , os .path
9
10
import shutil
10
11
import subprocess
11
12
import re
12
13
from contextlib import contextmanager
13
14
15
+ g_ndk_root = None
14
16
15
17
def _check_ndk_root_env ():
16
18
''' Checking the environment ANDROID_NDK, which will be used for building
17
19
'''
18
20
19
21
try :
20
22
21
- sdkRoot = os .environ ['ANDROID_SDK_ROOT' ]
23
+ ANDROID_NDK = None
24
+
25
+ sdkRoot = os .environ .get ('ANDROID_SDK_ROOT' , None )
22
26
for _ , ndkVers , _ in os .walk ("{0}{1}ndk" .format (sdkRoot , os .path .sep )):
23
27
for ndkVer in ndkVers :
24
28
if (ndkVer == '19.2.5345600' ):
@@ -27,12 +31,10 @@ def _check_ndk_root_env():
27
31
break
28
32
29
33
if ANDROID_NDK == None :
30
- ANDROID_NDK = os .environ ['ANDROID_NDK' ]
31
- if not os .path .isdir (ANDROID_NDK ): raise
34
+ ANDROID_NDK = os .environ .get ('ANDROID_NDK' , None )
32
35
33
- except Exception :
34
- print ("The ndk-r19c not installed in '{0}{1}ndk', please install via cmdline-tools/bin/sdkmanager --verbose --sdk_root=D:\\ dev\\ adt\\ sdk \" ndk;19.2.5345600\" " .format (sdkRoot , os .path .sep ))
35
- sys .exit (1 )
36
+ except :
37
+ print ('Exception occurred when check_ndk_root_env!' )
36
38
37
39
return ANDROID_NDK
38
40
@@ -71,12 +73,13 @@ def _find_all_files_match(dir, cond, all):
71
73
72
74
73
75
def _find_toolchain_include_path ():
76
+ global g_ndk_root
74
77
'''
75
78
Search gcc prebuilt include path
76
79
for instance: "$ANDROID_NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/include"
77
80
'''
78
81
foundFiles = []
79
- _find_all_files_match (os .path .join (_check_ndk_root_env () , "toolchains" ), lambda x : os .path .basename (x ) == "stdarg.h" and "arm-linux-androideabi" in x , foundFiles )
82
+ _find_all_files_match (os .path .join (g_ndk_root , "toolchains" ), lambda x : os .path .basename (x ) == "stdarg.h" and "arm-linux-androideabi" in x , foundFiles )
80
83
if len (foundFiles ) == 0 :
81
84
return ""
82
85
else :
@@ -87,7 +90,8 @@ def _find_llvm_include_path():
87
90
Search llvm prebuilt include path.
88
91
for instance: "$ANDROID_NDK/toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/6.0.2/include"
89
92
'''
90
- versionFile = _find_first_file_in_dir (_check_ndk_root_env (), "AndroidVersion.txt" )
93
+ global g_ndk_root
94
+ versionFile = _find_first_file_in_dir (g_ndk_root , "AndroidVersion.txt" )
91
95
if versionFile is None :
92
96
return ""
93
97
versionDir = os .path .dirname (versionFile )
@@ -123,12 +127,19 @@ def _run_cmd(command):
123
127
raise CmdError (message )
124
128
125
129
def main ():
130
+ global g_ndk_root
126
131
127
132
cur_platform = '??'
128
133
llvm_path = '??'
129
- ndk_root = _check_ndk_root_env ()
134
+ if (g_ndk_root == None or not os .path .isdir (g_ndk_root )):
135
+ g_ndk_root = _check_ndk_root_env ()
136
+
137
+ if not os .path .isdir (g_ndk_root ):
138
+ print ("The ndk-r19c root not specified, please specifiy via --ndk_root '/path/to/ndk'" )
139
+ sys .exit (1 )
140
+
130
141
# del the " in the path
131
- ndk_root = re .sub (r"\"" , "" , ndk_root )
142
+ g_ndk_root = re .sub (r"\"" , "" , g_ndk_root )
132
143
python_bin = _check_python_bin_env ()
133
144
134
145
platform = sys .platform
@@ -143,11 +154,11 @@ def main():
143
154
sys .exit (1 )
144
155
145
156
x86_llvm_path = ""
146
- x64_llvm_path = os .path .abspath (os .path .join (ndk_root , 'toolchains/llvm/prebuilt' , '%s-%s' % (cur_platform , 'x86_64' )))
157
+ x64_llvm_path = os .path .abspath (os .path .join (g_ndk_root , 'toolchains/llvm/prebuilt' , '%s-%s' % (cur_platform , 'x86_64' )))
147
158
if not os .path .exists (x64_llvm_path ):
148
- x86_llvm_path = os .path .abspath (os .path .join (ndk_root , 'toolchains/llvm/prebuilt' , '%s' % (cur_platform )))
159
+ x86_llvm_path = os .path .abspath (os .path .join (g_ndk_root , 'toolchains/llvm/prebuilt' , '%s' % (cur_platform )))
149
160
if not os .path .exists (x86_llvm_path ):
150
- x86_llvm_path = os .path .abspath (os .path .join (ndk_root , 'toolchains/llvm/prebuilt' , '%s-%s' % (cur_platform , 'x86' )))
161
+ x86_llvm_path = os .path .abspath (os .path .join (g_ndk_root , 'toolchains/llvm/prebuilt' , '%s-%s' % (cur_platform , 'x86' )))
151
162
152
163
if os .path .isdir (x64_llvm_path ):
153
164
llvm_path = x64_llvm_path
@@ -159,11 +170,11 @@ def main():
159
170
sys .exit (1 )
160
171
161
172
x86_gcc_toolchain_path = ""
162
- x64_gcc_toolchain_path = os .path .abspath (os .path .join (ndk_root , 'toolchains/arm-linux-androideabi-4.9/prebuilt' , '%s-%s' % (cur_platform , 'x86_64' )))
173
+ x64_gcc_toolchain_path = os .path .abspath (os .path .join (g_ndk_root , 'toolchains/arm-linux-androideabi-4.9/prebuilt' , '%s-%s' % (cur_platform , 'x86_64' )))
163
174
if not os .path .exists (x64_gcc_toolchain_path ):
164
- x86_gcc_toolchain_path = os .path .abspath (os .path .join (ndk_root , 'toolchains/arm-linux-androideabi-4.9/prebuilt' , '%s' % (cur_platform )))
175
+ x86_gcc_toolchain_path = os .path .abspath (os .path .join (g_ndk_root , 'toolchains/arm-linux-androideabi-4.9/prebuilt' , '%s' % (cur_platform )))
165
176
if not os .path .exists (x86_gcc_toolchain_path ):
166
- x86_gcc_toolchain_path = os .path .abspath (os .path .join (ndk_root , 'toolchains/arm-linux-androideabi-4.9/prebuilt' , '%s-%s' % (cur_platform , 'x86' )))
177
+ x86_gcc_toolchain_path = os .path .abspath (os .path .join (g_ndk_root , 'toolchains/arm-linux-androideabi-4.9/prebuilt' , '%s-%s' % (cur_platform , 'x86' )))
167
178
168
179
if os .path .isdir (x64_gcc_toolchain_path ):
169
180
gcc_toolchain_path = x64_gcc_toolchain_path
@@ -189,7 +200,7 @@ def main():
189
200
import ConfigParser
190
201
config = ConfigParser .ConfigParser ()
191
202
192
- config .set ('DEFAULT' , 'androidndkdir' , ndk_root )
203
+ config .set ('DEFAULT' , 'androidndkdir' , g_ndk_root )
193
204
config .set ('DEFAULT' , 'clangllvmdir' , llvm_path )
194
205
config .set ('DEFAULT' , 'gcc_toolchain_dir' , gcc_toolchain_path )
195
206
config .set ('DEFAULT' , 'axysdir' , axys_root )
@@ -259,4 +270,8 @@ def main():
259
270
260
271
# -------------- main --------------
261
272
if __name__ == '__main__' :
273
+ parser = argparse .ArgumentParser (description = 'Install android sdk/ndk' )
274
+ parser .add_argument ("--ndk_root" , help = "Specificy ndk root" )
275
+ args = parser .parse_args ()
276
+ g_ndk_root = args .ndk_root
262
277
main ()
0 commit comments