1- from os .path import (exists , join , dirname )
1+ from os .path import (exists , join , dirname , split )
22from os import environ , uname
3+ from glob import glob
34import sys
45from distutils .spawn import find_executable
56
@@ -30,13 +31,30 @@ def include_dirs(self):
3031 d .format (arch = self ))
3132 for d in self .ctx .include_dirs ]
3233
33- def get_env (self , with_flags_in_cc = True ):
34+ @property
35+ def target (self ):
36+ target_data = self .command_prefix .split ('-' )
37+ return '-' .join (
38+ [target_data [0 ], 'none' , target_data [1 ], target_data [2 ]])
39+
40+ def get_env (self , with_flags_in_cc = True , clang = False ):
3441 env = {}
3542
36- env ['CFLAGS' ] = ' ' .join ([
37- '-DANDROID' , '-mandroid' , '-fomit-frame-pointer'
38- ' -D__ANDROID_API__={}' .format (self .ctx .ndk_api ),
39- ])
43+ cflags = [
44+ '-DANDROID' ,
45+ '-fomit-frame-pointer' ,
46+ '-D__ANDROID_API__={}' .format (self .ctx .ndk_api )]
47+ if not clang :
48+ cflags .append ('-mandroid' )
49+ else :
50+ cflags .append ('-target ' + self .target )
51+ toolchain = '{android_host}-{toolchain_version}' .format (
52+ android_host = self .ctx .toolchain_prefix ,
53+ toolchain_version = self .ctx .toolchain_version )
54+ toolchain = join (self .ctx .ndk_dir , 'toolchains' , toolchain , 'prebuilt' , 'linux-x86_64' )
55+ cflags .append ('-gcc-toolchain {}' .format (toolchain ))
56+
57+ env ['CFLAGS' ] = ' ' .join (cflags )
4058 env ['LDFLAGS' ] = ' '
4159
4260 sysroot = join (self .ctx ._ndk_dir , 'sysroot' )
@@ -45,6 +63,8 @@ def get_env(self, with_flags_in_cc=True):
4563 # https://android.googlesource.com/platform/ndk/+/ndk-r15-release/docs/UnifiedHeaders.md
4664 env ['CFLAGS' ] += ' -isystem {}/sysroot/usr/include/{}' .format (
4765 self .ctx .ndk_dir , self .ctx .toolchain_prefix )
66+ env ['CFLAGS' ] += ' -I{}/sysroot/usr/include/{}' .format (
67+ self .ctx .ndk_dir , self .command_prefix )
4868 else :
4969 sysroot = self .ctx .ndk_platform
5070 env ['CFLAGS' ] += ' -I{}' .format (self .ctx .ndk_platform )
@@ -82,8 +102,20 @@ def get_env(self, with_flags_in_cc=True):
82102 env ['NDK_CCACHE' ] = self .ctx .ccache
83103 env .update ({k : v for k , v in environ .items () if k .startswith ('CCACHE_' )})
84104
85- cc = find_executable ('{command_prefix}-gcc' .format (
86- command_prefix = command_prefix ), path = environ ['PATH' ])
105+ if clang :
106+ llvm_dirname = split (
107+ glob (join (self .ctx .ndk_dir , 'toolchains' , 'llvm*' ))[- 1 ])[- 1 ]
108+ clang_path = join (self .ctx .ndk_dir , 'toolchains' , llvm_dirname ,
109+ 'prebuilt' , 'linux-x86_64' , 'bin' )
110+ environ ['PATH' ] = '{clang_path}:{path}' .format (
111+ clang_path = clang_path , path = environ ['PATH' ])
112+ exe = join (clang_path , 'clang' )
113+ execxx = join (clang_path , 'clang++' )
114+ else :
115+ exe = '{command_prefix}-gcc' .format (command_prefix = command_prefix )
116+ execxx = '{command_prefix}-g++' .format (command_prefix = command_prefix )
117+
118+ cc = find_executable (exe , path = environ ['PATH' ])
87119 if cc is None :
88120 print ('Searching path are: {!r}' .format (environ ['PATH' ]))
89121 raise BuildInterruptingException (
@@ -93,20 +125,20 @@ def get_env(self, with_flags_in_cc=True):
93125 'installed. Exiting.' )
94126
95127 if with_flags_in_cc :
96- env ['CC' ] = '{ccache}{command_prefix}-gcc {cflags}' .format (
97- command_prefix = command_prefix ,
128+ env ['CC' ] = '{ccache}{exe} {cflags}' .format (
129+ exe = exe ,
98130 ccache = ccache ,
99131 cflags = env ['CFLAGS' ])
100- env ['CXX' ] = '{ccache}{command_prefix}-g++ {cxxflags}' .format (
101- command_prefix = command_prefix ,
132+ env ['CXX' ] = '{ccache}{execxx} {cxxflags}' .format (
133+ execxx = execxx ,
102134 ccache = ccache ,
103135 cxxflags = env ['CXXFLAGS' ])
104136 else :
105- env ['CC' ] = '{ccache}{command_prefix}-gcc ' .format (
106- command_prefix = command_prefix ,
137+ env ['CC' ] = '{ccache}{exe} ' .format (
138+ exe = exe ,
107139 ccache = ccache )
108- env ['CXX' ] = '{ccache}{command_prefix}-g++ ' .format (
109- command_prefix = command_prefix ,
140+ env ['CXX' ] = '{ccache}{execxx} ' .format (
141+ execxx = execxx ,
110142 ccache = ccache )
111143
112144 env ['AR' ] = '{}-ar' .format (command_prefix )
@@ -123,12 +155,13 @@ def get_env(self, with_flags_in_cc=True):
123155 env ['READELF' ] = '{}-readelf' .format (command_prefix )
124156 env ['NM' ] = '{}-nm' .format (command_prefix )
125157
126- hostpython_recipe = Recipe .get_recipe ('hostpython2' , self .ctx )
127-
128- # This hardcodes python version 2.7, needs fixing
158+ hostpython_recipe = Recipe .get_recipe (
159+ 'host' + self .ctx .python_recipe .name , self .ctx )
129160 env ['BUILDLIB_PATH' ] = join (
130161 hostpython_recipe .get_build_dir (self .arch ),
131- 'build' , 'lib.linux-{}-2.7' .format (uname ()[- 1 ]))
162+ 'build' , 'lib.linux-{}-{}' .format (
163+ uname ()[- 1 ], self .ctx .python_recipe .major_minor_version_string )
164+ )
132165
133166 env ['PATH' ] = environ ['PATH' ]
134167
@@ -147,12 +180,18 @@ class ArchARM(Arch):
147180 command_prefix = 'arm-linux-androideabi'
148181 platform_dir = 'arch-arm'
149182
183+ @property
184+ def target (self ):
185+ target_data = self .command_prefix .split ('-' )
186+ return '-' .join (
187+ ['armv7a' , 'none' , target_data [1 ], target_data [2 ]])
188+
150189
151190class ArchARMv7_a (ArchARM ):
152191 arch = 'armeabi-v7a'
153192
154- def get_env (self , with_flags_in_cc = True ):
155- env = super (ArchARMv7_a , self ).get_env (with_flags_in_cc )
193+ def get_env (self , with_flags_in_cc = True , clang = False ):
194+ env = super (ArchARMv7_a , self ).get_env (with_flags_in_cc , clang = clang )
156195 env ['CFLAGS' ] = (env ['CFLAGS' ] +
157196 (' -march=armv7-a -mfloat-abi=softfp '
158197 '-mfpu=vfp -mthumb' ))
@@ -166,8 +205,8 @@ class Archx86(Arch):
166205 command_prefix = 'i686-linux-android'
167206 platform_dir = 'arch-x86'
168207
169- def get_env (self , with_flags_in_cc = True ):
170- env = super (Archx86 , self ).get_env (with_flags_in_cc )
208+ def get_env (self , with_flags_in_cc = True , clang = False ):
209+ env = super (Archx86 , self ).get_env (with_flags_in_cc , clang = clang )
171210 env ['CFLAGS' ] = (env ['CFLAGS' ] +
172211 ' -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32' )
173212 env ['CXXFLAGS' ] = env ['CFLAGS' ]
@@ -180,8 +219,8 @@ class Archx86_64(Arch):
180219 command_prefix = 'x86_64-linux-android'
181220 platform_dir = 'arch-x86_64'
182221
183- def get_env (self , with_flags_in_cc = True ):
184- env = super (Archx86_64 , self ).get_env (with_flags_in_cc )
222+ def get_env (self , with_flags_in_cc = True , clang = False ):
223+ env = super (Archx86_64 , self ).get_env (with_flags_in_cc , clang = clang )
185224 env ['CFLAGS' ] = (env ['CFLAGS' ] +
186225 ' -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel' )
187226 env ['CXXFLAGS' ] = env ['CFLAGS' ]
@@ -194,8 +233,8 @@ class ArchAarch_64(Arch):
194233 command_prefix = 'aarch64-linux-android'
195234 platform_dir = 'arch-arm64'
196235
197- def get_env (self , with_flags_in_cc = True ):
198- env = super (ArchAarch_64 , self ).get_env (with_flags_in_cc )
236+ def get_env (self , with_flags_in_cc = True , clang = False ):
237+ env = super (ArchAarch_64 , self ).get_env (with_flags_in_cc , clang = clang )
199238 incpath = ' -I' + join (dirname (__file__ ), 'includes' , 'arm64-v8a' )
200239 env ['EXTRA_CFLAGS' ] = incpath
201240 env ['CFLAGS' ] += incpath
0 commit comments