2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-28 13:09:13 +01:00

waifulib: xcompile: enhance compiling using host clang, refactoring

This commit is contained in:
Alibek Omarov 2019-09-20 18:50:24 +03:00
parent c93aa8c568
commit 70c4279c25

View File

@ -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:
elif self.is_arm(): return 'x86'
triplet = 'arm-linux-androideabi'
else: else:
triplet = self.arch + '-linux-android' return 'i686-linux-android'
return triplet elif self.is_arm():
if llvm_toolchain:
return 'armv7a-linux-androideabi'
else:
return 'arm-linux-androideabi'
elif self.is_amd64() and toolchain_folder:
return 'x86_64'
else:
return self.arch + '-linux-android'
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()),
'-I%s/usr/include/' % (self.sysroot())
]
elif self.ndk_rev < 20:
cflags += ['--sysroot=%s' % (self.sysroot())]
if self.ndk_rev < 20: cflags += ['-I%s' % (self.system_stl()), '-DANDROID', '-D__ANDROID__']
cflags += ['--sysroot={0}'.format(self.sysroot())]
elif self.is_host():
cflags += ['-isysroot={0}'.format(self.sysroot())]
cflags += ['-DANDROID', '-D__ANDROID__']
cflags += ['-I{0}'.format(self.system_stl())]
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'))