Browse Source

waifulib: xcompile: enhance compiling using host clang, refactoring

pull/2/head
Alibek Omarov 5 years ago
parent
commit
4f4c63b1dd
  1. 113
      scripts/waifulib/xcompile.py

113
scripts/waifulib/xcompile.py

@ -34,6 +34,8 @@ class Android:
def __init__(self, ctx, arch, toolchain, api): def __init__(self, ctx, arch, toolchain, api):
self.ctx = ctx self.ctx = ctx
self.api = api self.api = api
self.toolchain = toolchain
self.arch = arch
for i in ['ANDROID_NDK_HOME', 'ANDROID_NDK']: for i in ['ANDROID_NDK_HOME', 'ANDROID_NDK']:
self.ndk_home = os.getenv(i) self.ndk_home = os.getenv(i)
@ -59,20 +61,20 @@ class Android:
self.ndk_rev = 10 self.ndk_rev = 10
if self.ndk_rev not in [10, 19, 20]: if self.ndk_rev not in [10, 19, 20]:
ctx.fatal('Unknown NDK revision: {}'.format(self.ndk_rev)) ctx.fatal('Unknown NDK revision: %d' % (self.ndk_rev))
if self.is_host() and self.ndk_rev < 20:
ctx.fatal('Using host toolchain with this NDK revision is untested. You can comment this check, but you\'re on your own!')
if self.ndk_rev >= 19 or 'clang' in self.toolchain:
self.clang = True
self.arch = arch
if self.arch == 'armeabi-v7a-hard': if self.arch == 'armeabi-v7a-hard':
if self.ndk_rev <= 10: if self.ndk_rev <= 10:
self.arch = 'armeabi-v7a' # Only armeabi-v7a have hard float ABI self.arch = 'armeabi-v7a' # Only armeabi-v7a have hard float ABI
self.is_hardfloat = True self.is_hardfloat = True
else: else:
raise Exception('NDK does not support hardfloat ABI') ctx.fatal('NDK does not support hardfloat ABI')
self.toolchain = toolchain
if self.ndk_rev >= 19 or 'clang' in self.toolchain:
self.clang = True
if self.is_arm64() or self.is_amd64() and self.api < 21: if self.is_arm64() or self.is_amd64() and self.api < 21:
Logs.warn('API level for 64-bit target automatically was set to 21') Logs.warn('API level for 64-bit target automatically was set to 21')
@ -82,10 +84,6 @@ class Android:
self.api = 16 self.api = 16
self.toolchain_path = self.gen_toolchain_path() self.toolchain_path = self.gen_toolchain_path()
# TODO: Crystax support?
# TODO: Support for everything else than linux-x86_64?
# TODO: Determine if I actually need to implement listed above
def is_host(self): def is_host(self):
''' '''
Checks if we using host compiler(implies clang) Checks if we using host compiler(implies clang)
@ -125,19 +123,26 @@ class Android:
def is_hardfp(self): def is_hardfp(self):
return self.is_hardfloat return self.is_hardfloat
def ndk_triplet(self): def ndk_triplet(self, llvm_toolchain = False, toolchain_folder = False):
if self.is_x86(): if self.is_x86():
triplet = 'i686-linux-android' if toolchain_folder:
return 'x86'
else:
return 'i686-linux-android'
elif self.is_arm(): elif self.is_arm():
triplet = 'arm-linux-androideabi' if llvm_toolchain:
return 'armv7a-linux-androideabi'
else:
return 'arm-linux-androideabi'
elif self.is_amd64() and toolchain_folder:
return 'x86_64'
else: else:
triplet = self.arch + '-linux-android' return self.arch + '-linux-android'
return triplet
def gen_gcc_toolchain_path(self): def gen_gcc_toolchain_path(self):
path = 'toolchains' path = 'toolchains'
if sys.platform.startswith('linux'): if sys.platform.startswith('linux') or self.is_host():
toolchain_host = 'linux' toolchain_host = 'linux'
elif sys.platform.startswith('darwin'): elif sys.platform.startswith('darwin'):
toolchain_host = 'darwin' toolchain_host = 'darwin'
@ -158,23 +163,13 @@ class Android:
toolchain_folder = 'llvm' toolchain_folder = 'llvm'
else: else:
if self.is_x86() or self.is_amd64(): toolchain_folder = '%s-%s' % (self.ndk_triplet(toolchain_folder = True), self.toolchain)
toolchain_folder = self.arch + '-' + self.toolchain
elif self.is_arm():
toolchain_folder = 'arm-linux-androideabi-' + self.toolchain
else:
toolchain_folder = self.arch + '-linux-android-' + self.toolchain
return os.path.abspath(os.path.join(self.ndk_home, path, toolchain_folder, 'prebuilt', toolchain_host)) return os.path.abspath(os.path.join(self.ndk_home, path, toolchain_folder, 'prebuilt', toolchain_host))
def gen_toolchain_path(self): def gen_toolchain_path(self):
if self.is_clang(): if self.is_clang():
if self.is_x86(): triplet = '%s%d-' % (self.ndk_triplet(llvm_toolchain = True), self.api)
triplet = 'i686-linux-android{}-'.format(self.api)
elif self.is_arm():
triplet = 'armv7a-linux-androideabi{}-'.format(self.api)
else:
triplet = self.arch + '-linux-android{}-'.format(self.api)
else: else:
triplet = self.ndk_triplet() + '-' triplet = self.ndk_triplet() + '-'
return os.path.join(self.gen_gcc_toolchain_path(), 'bin', triplet) return os.path.join(self.gen_gcc_toolchain_path(), 'bin', triplet)
@ -184,12 +179,12 @@ class Android:
def cc(self): def cc(self):
if self.is_host(): if self.is_host():
return 'clang --target=%s' % self.clang_host_triplet() return 'clang --target=%s%d' % (self.ndk_triplet(), self.api)
return self.toolchain_path + ('clang' if self.is_clang() else 'gcc') return self.toolchain_path + ('clang' if self.is_clang() else 'gcc')
def cxx(self): def cxx(self):
if self.is_host(): if self.is_host():
return 'clang++ --target=%s' % self.clang_host_triplet() return 'clang++ --target=%s%d' % (self.ndk_triplet(), self.api)
return self.toolchain_path + ('clang++' if self.is_clang() else 'g++') return self.toolchain_path + ('clang++' if self.is_clang() else 'g++')
def strip(self): def strip(self):
@ -207,7 +202,7 @@ class Android:
arch = 'arm' arch = 'arm'
elif self.is_arm64(): elif self.is_arm64():
arch = 'arm64' arch = 'arm64'
path = 'platforms/android-{}/arch-{}'.format(self.api, arch) path = 'platforms/android-%s/arch-%s' % (self.api, arch)
return os.path.abspath(os.path.join(self.ndk_home, path)) return os.path.abspath(os.path.join(self.ndk_home, path))
@ -217,34 +212,25 @@ class Android:
else: else:
return self.libsysroot() return self.libsysroot()
def clang_host_triplet(self):
triplet = ''
if self.is_arm():
triplet += 'arm'
elif self.is_x86():
triplet += 'i686'
else:
triplet += self.arch
triplet += '-linux-android'
return triplet
def cflags(self): def cflags(self):
cflags = [] cflags = []
if self.is_host(): if self.is_host():
cflags += ['-nostdlib'] cflags += [
'--sysroot=%s/sysroot' % (self.gen_gcc_toolchain_path()),
if self.ndk_rev < 20: '-I%s/usr/include/' % (self.sysroot())
cflags += ['--sysroot={0}'.format(self.sysroot())] ]
elif self.is_host(): elif self.ndk_rev < 20:
cflags += ['-isysroot={0}'.format(self.sysroot())] cflags += ['--sysroot=%s' % (self.sysroot())]
cflags += ['-DANDROID', '-D__ANDROID__']
cflags += ['-I{0}'.format(self.system_stl())] cflags += ['-I%s' % (self.system_stl()), '-DANDROID', '-D__ANDROID__']
if self.is_arm(): if self.is_arm():
if self.arch == 'armeabi-v7a': if self.arch == 'armeabi-v7a':
# ARMv7 support # ARMv7 support
cflags += ['-mthumb', '-mfpu=neon', '-mcpu=cortex-a9', '-DHAVE_EFFICIENT_UNALIGNED_ACCESS', '-DVECTORIZE_SINCOS'] cflags += ['-mthumb', '-mfpu=neon', '-mcpu=cortex-a9', '-DHAVE_EFFICIENT_UNALIGNED_ACCESS', '-DVECTORIZE_SINCOS']
if not self.is_clang(): if not self.is_clang():
cflags += [ '-mvectorize-with-neon-quad' ] cflags += [ '-mvectorize-with-neon-quad' ]
if self.is_hardfloat: if self.is_hardfloat:
cflags += ['-D_NDK_MATH_NO_SOFTFP=1', '-mhard-float', '-mfloat-abi=hard', '-DLOAD_HARDFP', '-DSOFTFP_LINK'] cflags += ['-D_NDK_MATH_NO_SOFTFP=1', '-mhard-float', '-mfloat-abi=hard', '-DLOAD_HARDFP', '-DSOFTFP_LINK']
else: else:
@ -260,25 +246,22 @@ class Android:
def linkflags(self): def linkflags(self):
linkflags = [] linkflags = []
if self.is_host(): if self.is_host():
linkflags += ['-fuse-ld=lld', '-nostdlib', '--target=%s' % self.clang_host_triplet(), linkflags += ['--gcc-toolchain=%s' % self.gen_gcc_toolchain_path()]
'--gcc-toolchain=%s' % self.gen_gcc_toolchain_path()]
if self.ndk_rev < 20 or self.is_host(): if self.ndk_rev < 20 or self.is_host():
linkflags += ['--sysroot={0}'.format(self.libsysroot())] linkflags += ['--sysroot=%s/sysroot' % (self.gen_gcc_toolchain_path())]
if self.is_host(): if self.is_clang():
linkflags += ['-L{0}/usr/lib/{1}'.format(self.sysroot(), self.ndk_triplet()), linkflags += ['-fuse-ld=lld']
'-L{0}/sysroot/usr/lib/{1}/'.format(self.gen_gcc_toolchain_path(), self.ndk_triplet())]
return linkflags return linkflags
def ldflags(self): def ldflags(self):
ldflags = ['-stdlib=libstdc++', '-lgcc', '-no-canonical-prefixes'] ldflags = ['-stdlib=libstdc++', '-lgcc', '-no-canonical-prefixes']
if self.is_arm(): if self.is_arm():
if self.arch == 'armeabi-v7a': if self.arch == 'armeabi-v7a':
ldflags += ['-march=armv7-a'] ldflags += ['-march=armv7-a', '-mthumb']
if not self.is_host(): # lld only if not self.is_clang(): # lld only
ldflags += ['-Wl,--fix-cortex-a8'] ldflags += ['-Wl,--fix-cortex-a8']
ldflags += ['-mthumb']
if self.is_hardfloat: if self.is_hardfloat:
ldflags += ['-Wl,--no-warn-mismatch', '-lm_hard'] ldflags += ['-Wl,--no-warn-mismatch', '-lm_hard']
else: else:
@ -296,10 +279,10 @@ def configure(conf):
if len(values) != 3: if len(values) != 3:
conf.fatal('Invalid --android paramater value!') conf.fatal('Invalid --android paramater value!')
valid_archs = ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'aarch64', 'mipsel', 'mips64el'] valid_archs = ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'aarch64']
if values[0] not in valid_archs: if values[0] not in valid_archs:
conf.fatal('Unknown arch: {}. Supported: {}'.format(values[0], ', '.join(valid_archs))) conf.fatal('Unknown arch: %s. Supported: %r' % (values[0], ', '.join(valid_archs)))
conf.android = android = Android(conf, values[0], values[1], int(values[2])) conf.android = android = Android(conf, values[0], values[1], int(values[2]))
conf.environ['CC'] = android.cc() conf.environ['CC'] = android.cc()
@ -315,9 +298,9 @@ def configure(conf):
conf.env.LIB_M = ['m_hard'] conf.env.LIB_M = ['m_hard']
else: conf.env.LIB_M = ['m'] else: conf.env.LIB_M = ['m']
conf.env.PREFIX = '/lib/{}'.format(android.arch) conf.env.PREFIX = '/lib/%s' % (android.arch)
conf.msg('Selected Android NDK', '{}, version: {}'.format(android.ndk_home, android.ndk_rev)) conf.msg('Selected Android NDK', '%s, version: %d' % (android.ndk_home, android.ndk_rev))
# no need to print C/C++ compiler, as it would be printed by compiler_c/cxx # no need to print C/C++ compiler, as it would be printed by compiler_c/cxx
conf.msg('... C/C++ flags', ' '.join(android.cflags()).replace(android.ndk_home, '$NDK')) conf.msg('... C/C++ flags', ' '.join(android.cflags()).replace(android.ndk_home, '$NDK'))
conf.msg('... link flags', ' '.join(android.linkflags()).replace(android.ndk_home, '$NDK')) conf.msg('... link flags', ' '.join(android.linkflags()).replace(android.ndk_home, '$NDK'))

Loading…
Cancel
Save