2019-05-03 18:19:39 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
# xcompile.py -- crosscompiling utils
|
|
|
|
# Copyright (C) 2018 a1batross
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
2019-06-06 02:37:08 +02:00
|
|
|
try: from fwgslib import get_flags_by_compiler
|
|
|
|
except: from waflib.extras.fwgslib import get_flags_by_compiler
|
2019-09-21 02:53:32 +02:00
|
|
|
from waflib import Logs, TaskGen
|
2019-09-19 16:18:57 +02:00
|
|
|
from waflib.Tools import c_config
|
2019-09-20 15:35:33 +02:00
|
|
|
from collections import OrderedDict
|
2019-05-03 18:19:39 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
ANDROID_NDK_ENVVARS = ['ANDROID_NDK_HOME', 'ANDROID_NDK']
|
2022-08-01 13:43:42 +02:00
|
|
|
ANDROID_NDK_SUPPORTED = [10, 19, 20, 23, 25]
|
2019-10-09 02:50:25 +02:00
|
|
|
ANDROID_NDK_HARDFP_MAX = 11 # latest version that supports hardfp
|
|
|
|
ANDROID_NDK_GCC_MAX = 17 # latest NDK that ships with GCC
|
|
|
|
ANDROID_NDK_UNIFIED_SYSROOT_MIN = 15
|
|
|
|
ANDROID_NDK_SYSROOT_FLAG_MAX = 19 # latest NDK that need --sysroot flag
|
2022-08-01 13:43:42 +02:00
|
|
|
ANDROID_NDK_API_MIN = { 10: 3, 19: 16, 20: 16, 23: 16, 25: 19 } # minimal API level ndk revision supports
|
2021-12-17 01:16:35 +01:00
|
|
|
|
|
|
|
ANDROID_STPCPY_API_MIN = 21 # stpcpy() introduced in SDK 21
|
2019-10-09 02:50:25 +02:00
|
|
|
ANDROID_64BIT_API_MIN = 21 # minimal API level that supports 64-bit targets
|
|
|
|
|
2023-02-05 01:59:50 +01:00
|
|
|
NSWITCH_ENVVARS = ['DEVKITPRO']
|
|
|
|
|
2023-02-25 22:44:58 +01:00
|
|
|
PSVITA_ENVVARS = ['VITASDK']
|
|
|
|
|
2019-09-19 16:18:57 +02:00
|
|
|
# This class does support ONLY r10e and r19c/r20 NDK
|
2019-05-03 18:19:39 +02:00
|
|
|
class Android:
|
2019-05-28 03:17:05 +02:00
|
|
|
ctx = None # waf context
|
2019-05-03 18:19:39 +02:00
|
|
|
arch = None
|
|
|
|
toolchain = None
|
|
|
|
api = None
|
|
|
|
ndk_home = None
|
2019-05-28 03:17:05 +02:00
|
|
|
ndk_rev = 0
|
2019-05-06 03:12:12 +02:00
|
|
|
is_hardfloat = False
|
2019-05-28 03:17:05 +02:00
|
|
|
clang = False
|
2019-09-10 07:25:51 +02:00
|
|
|
|
2019-05-28 03:17:05 +02:00
|
|
|
def __init__(self, ctx, arch, toolchain, api):
|
|
|
|
self.ctx = ctx
|
2019-09-10 07:25:51 +02:00
|
|
|
self.api = api
|
2019-09-20 17:50:24 +02:00
|
|
|
self.toolchain = toolchain
|
|
|
|
self.arch = arch
|
2019-09-10 07:25:51 +02:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
for i in ANDROID_NDK_ENVVARS:
|
2019-05-28 03:17:05 +02:00
|
|
|
self.ndk_home = os.getenv(i)
|
|
|
|
if self.ndk_home != None:
|
|
|
|
break
|
2019-10-09 02:50:25 +02:00
|
|
|
else:
|
|
|
|
ctx.fatal('Set %s environment variable pointing to the root of Android NDK!' %
|
|
|
|
' or '.join(ANDROID_NDK_ENVVARS))
|
2019-05-28 03:17:05 +02:00
|
|
|
|
|
|
|
# TODO: this were added at some point of NDK development
|
|
|
|
# but I don't know at which version
|
|
|
|
# r10e don't have it
|
|
|
|
source_prop = os.path.join(self.ndk_home, 'source.properties')
|
|
|
|
if os.path.exists(source_prop):
|
|
|
|
with open(source_prop) as ndk_props_file:
|
|
|
|
for line in ndk_props_file.readlines():
|
|
|
|
tokens = line.split('=')
|
|
|
|
trimed_tokens = [token.strip() for token in tokens]
|
|
|
|
|
|
|
|
if 'Pkg.Revision' in trimed_tokens:
|
|
|
|
self.ndk_rev = int(trimed_tokens[1].split('.')[0])
|
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.ndk_rev not in ANDROID_NDK_SUPPORTED:
|
|
|
|
ctx.fatal('Unknown NDK revision: %d' % (self.ndk_rev))
|
|
|
|
else:
|
|
|
|
self.ndk_rev = ANDROID_NDK_SUPPORTED[0]
|
2019-09-20 17:50:24 +02:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
if 'clang' in self.toolchain or self.ndk_rev > ANDROID_NDK_GCC_MAX:
|
2019-09-20 17:50:24 +02:00
|
|
|
self.clang = True
|
2019-05-28 03:17:05 +02:00
|
|
|
|
|
|
|
if self.arch == 'armeabi-v7a-hard':
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.ndk_rev <= ANDROID_NDK_HARDFP_MAX:
|
2019-05-28 03:17:05 +02:00
|
|
|
self.arch = 'armeabi-v7a' # Only armeabi-v7a have hard float ABI
|
|
|
|
self.is_hardfloat = True
|
|
|
|
else:
|
2019-09-20 17:50:24 +02:00
|
|
|
ctx.fatal('NDK does not support hardfloat ABI')
|
2019-05-28 03:17:05 +02:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.api < ANDROID_NDK_API_MIN[self.ndk_rev]:
|
|
|
|
self.api = ANDROID_NDK_API_MIN[self.ndk_rev]
|
|
|
|
Logs.warn('API level automatically was set to %d due to NDK support' % self.api)
|
|
|
|
|
|
|
|
if self.is_arm64() or self.is_amd64() and self.api < ANDROID_64BIT_API_MIN:
|
|
|
|
self.api = ANDROID_64BIT_API_MIN
|
|
|
|
Logs.warn('API level for 64-bit target automatically was set to %d' % self.api)
|
2019-05-03 18:19:39 +02:00
|
|
|
|
2019-09-10 07:25:51 +02:00
|
|
|
def is_host(self):
|
|
|
|
'''
|
|
|
|
Checks if we using host compiler(implies clang)
|
|
|
|
'''
|
|
|
|
return self.toolchain == 'host'
|
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
def is_arm(self):
|
|
|
|
'''
|
|
|
|
Checks if selected architecture is **32-bit** ARM
|
|
|
|
'''
|
|
|
|
return self.arch.startswith('armeabi')
|
|
|
|
|
|
|
|
def is_x86(self):
|
|
|
|
'''
|
|
|
|
Checks if selected architecture is **32-bit** or **64-bit** x86
|
|
|
|
'''
|
2019-05-28 03:17:05 +02:00
|
|
|
return self.arch == 'x86'
|
|
|
|
|
|
|
|
def is_amd64(self):
|
|
|
|
'''
|
|
|
|
Checks if selected architecture is **64-bit** x86
|
|
|
|
'''
|
|
|
|
return self.arch == 'x86_64'
|
2019-05-03 18:19:39 +02:00
|
|
|
|
|
|
|
def is_arm64(self):
|
|
|
|
'''
|
|
|
|
Checks if selected architecture is AArch64
|
|
|
|
'''
|
|
|
|
return self.arch == 'aarch64'
|
|
|
|
|
|
|
|
def is_clang(self):
|
|
|
|
'''
|
|
|
|
Checks if selected toolchain is Clang (TODO)
|
|
|
|
'''
|
2019-05-28 03:17:05 +02:00
|
|
|
return self.clang
|
2019-05-03 18:19:39 +02:00
|
|
|
|
|
|
|
def is_hardfp(self):
|
2019-05-06 03:12:12 +02:00
|
|
|
return self.is_hardfloat
|
2019-05-03 18:19:39 +02:00
|
|
|
|
2019-09-20 17:50:24 +02:00
|
|
|
def ndk_triplet(self, llvm_toolchain = False, toolchain_folder = False):
|
2019-09-10 07:25:51 +02:00
|
|
|
if self.is_x86():
|
2019-09-20 17:50:24 +02:00
|
|
|
if toolchain_folder:
|
|
|
|
return 'x86'
|
|
|
|
else:
|
|
|
|
return 'i686-linux-android'
|
2019-09-10 07:25:51 +02:00
|
|
|
elif self.is_arm():
|
2019-09-20 17:50:24 +02:00
|
|
|
if llvm_toolchain:
|
|
|
|
return 'armv7a-linux-androideabi'
|
|
|
|
else:
|
|
|
|
return 'arm-linux-androideabi'
|
|
|
|
elif self.is_amd64() and toolchain_folder:
|
|
|
|
return 'x86_64'
|
2019-09-10 07:25:51 +02:00
|
|
|
else:
|
2019-09-20 17:50:24 +02:00
|
|
|
return self.arch + '-linux-android'
|
2019-09-10 07:25:51 +02:00
|
|
|
|
2019-10-09 04:52:13 +02:00
|
|
|
def apk_arch(self):
|
|
|
|
if self.is_arm64():
|
|
|
|
return 'arm64-v8a'
|
|
|
|
return self.arch
|
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
def gen_host_toolchain(self):
|
|
|
|
# With host toolchain we don't care about OS
|
|
|
|
# so just download NDK for Linux x86_64
|
2021-07-03 18:21:13 +02:00
|
|
|
if 'HOST_TOOLCHAIN' in self.ctx.environ:
|
|
|
|
return self.ctx.environ['HOST_TOOLCHAIN']
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.is_host():
|
|
|
|
return 'linux-x86_64'
|
2019-05-28 03:17:05 +02:00
|
|
|
|
2019-09-21 02:53:32 +02:00
|
|
|
if sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
|
2019-10-09 02:50:25 +02:00
|
|
|
osname = 'windows'
|
2019-05-28 03:17:05 +02:00
|
|
|
elif sys.platform.startswith('darwin'):
|
2019-10-09 02:50:25 +02:00
|
|
|
osname = 'darwin'
|
|
|
|
elif sys.platform.startswith('linux'):
|
|
|
|
osname = 'linux'
|
2019-09-21 02:53:32 +02:00
|
|
|
else:
|
|
|
|
self.ctx.fatal('Unsupported by NDK host platform')
|
2019-05-28 03:17:05 +02:00
|
|
|
|
2019-09-11 19:44:05 +02:00
|
|
|
if sys.maxsize > 2**32:
|
2019-10-09 02:50:25 +02:00
|
|
|
arch = 'x86_64'
|
|
|
|
else: arch = 'x86'
|
2019-05-28 03:17:05 +02:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
return '%s-%s' % (osname, arch)
|
2019-05-28 03:17:05 +02:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
def gen_gcc_toolchain_path(self):
|
|
|
|
path = 'toolchains'
|
|
|
|
toolchain_host = self.gen_host_toolchain()
|
|
|
|
|
|
|
|
if self.is_clang():
|
2019-05-28 03:17:05 +02:00
|
|
|
toolchain_folder = 'llvm'
|
|
|
|
else:
|
2019-09-21 02:53:32 +02:00
|
|
|
if self.is_host():
|
|
|
|
toolchain = '4.9'
|
|
|
|
else:
|
|
|
|
toolchain = self.toolchain
|
|
|
|
|
|
|
|
toolchain_folder = '%s-%s' % (self.ndk_triplet(toolchain_folder = True), toolchain)
|
2019-05-03 18:19:39 +02:00
|
|
|
|
2019-09-10 07:25:51 +02:00
|
|
|
return os.path.abspath(os.path.join(self.ndk_home, path, toolchain_folder, 'prebuilt', toolchain_host))
|
|
|
|
|
|
|
|
def gen_toolchain_path(self):
|
|
|
|
if self.is_clang():
|
2019-09-20 17:50:24 +02:00
|
|
|
triplet = '%s%d-' % (self.ndk_triplet(llvm_toolchain = True), self.api)
|
2019-09-10 07:25:51 +02:00
|
|
|
else:
|
|
|
|
triplet = self.ndk_triplet() + '-'
|
|
|
|
return os.path.join(self.gen_gcc_toolchain_path(), 'bin', triplet)
|
2019-05-03 18:19:39 +02:00
|
|
|
|
2019-09-11 19:44:05 +02:00
|
|
|
def gen_binutils_path(self):
|
2021-12-07 06:30:54 +01:00
|
|
|
if self.ndk_rev >= 23:
|
|
|
|
return os.path.join(self.gen_gcc_toolchain_path(), 'bin')
|
2019-09-11 19:44:05 +02:00
|
|
|
return os.path.join(self.gen_gcc_toolchain_path(), self.ndk_triplet(), 'bin')
|
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
def cc(self):
|
2019-09-10 07:25:51 +02:00
|
|
|
if self.is_host():
|
2021-07-03 18:21:13 +02:00
|
|
|
s = 'clang'
|
|
|
|
environ = getattr(self.ctx, 'environ', os.environ)
|
|
|
|
|
|
|
|
if 'CC' in environ:
|
|
|
|
s = environ['CC']
|
|
|
|
|
|
|
|
return '%s --target=%s%d' % (s, self.ndk_triplet(), self.api)
|
2019-10-09 02:50:25 +02:00
|
|
|
return self.gen_toolchain_path() + ('clang' if self.is_clang() else 'gcc')
|
2019-05-03 18:19:39 +02:00
|
|
|
|
|
|
|
def cxx(self):
|
2019-09-10 07:25:51 +02:00
|
|
|
if self.is_host():
|
2021-07-03 18:21:13 +02:00
|
|
|
s = 'clang++'
|
|
|
|
environ = getattr(self.ctx, 'environ', os.environ)
|
|
|
|
|
|
|
|
if 'CXX' in environ:
|
|
|
|
s = environ['CXX']
|
|
|
|
|
|
|
|
return '%s --target=%s%d' % (s, self.ndk_triplet(), self.api)
|
2019-10-09 02:50:25 +02:00
|
|
|
return self.gen_toolchain_path() + ('clang++' if self.is_clang() else 'g++')
|
2019-05-03 18:19:39 +02:00
|
|
|
|
2019-09-11 19:44:05 +02:00
|
|
|
def strip(self):
|
|
|
|
if self.is_host():
|
2021-07-03 18:21:13 +02:00
|
|
|
environ = getattr(self.ctx, 'environ', os.environ)
|
|
|
|
|
|
|
|
if 'STRIP' in environ:
|
|
|
|
return environ['STRIP']
|
2019-09-19 17:20:11 +02:00
|
|
|
return 'llvm-strip'
|
2021-12-07 06:30:54 +01:00
|
|
|
|
|
|
|
if self.ndk_rev >= 23:
|
|
|
|
return os.path.join(self.gen_binutils_path(), 'llvm-strip')
|
2019-09-11 19:44:05 +02:00
|
|
|
return os.path.join(self.gen_binutils_path(), 'strip')
|
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
def system_stl(self):
|
|
|
|
# TODO: proper STL support
|
|
|
|
return os.path.abspath(os.path.join(self.ndk_home, 'sources', 'cxx-stl', 'system', 'include'))
|
|
|
|
|
2019-09-10 07:25:51 +02:00
|
|
|
def libsysroot(self):
|
|
|
|
arch = self.arch
|
|
|
|
if self.is_arm():
|
|
|
|
arch = 'arm'
|
|
|
|
elif self.is_arm64():
|
|
|
|
arch = 'arm64'
|
2019-09-20 17:50:24 +02:00
|
|
|
path = 'platforms/android-%s/arch-%s' % (self.api, arch)
|
2019-09-10 07:25:51 +02:00
|
|
|
|
|
|
|
return os.path.abspath(os.path.join(self.ndk_home, path))
|
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
def sysroot(self):
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.ndk_rev >= ANDROID_NDK_UNIFIED_SYSROOT_MIN:
|
2019-05-28 03:17:05 +02:00
|
|
|
return os.path.abspath(os.path.join(self.ndk_home, 'sysroot'))
|
|
|
|
else:
|
2019-09-10 07:25:51 +02:00
|
|
|
return self.libsysroot()
|
2019-05-03 18:19:39 +02:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
def cflags(self, cxx = False):
|
2019-07-29 05:52:43 +02:00
|
|
|
cflags = []
|
2019-10-09 02:50:25 +02:00
|
|
|
|
|
|
|
if self.ndk_rev <= ANDROID_NDK_SYSROOT_FLAG_MAX:
|
|
|
|
cflags += ['--sysroot=%s' % (self.sysroot())]
|
|
|
|
else:
|
|
|
|
if self.is_host():
|
2019-09-21 02:53:32 +02:00
|
|
|
cflags += [
|
|
|
|
'--sysroot=%s/sysroot' % (self.gen_gcc_toolchain_path()),
|
2019-10-11 09:05:07 +02:00
|
|
|
'-isystem', '%s/usr/include/' % (self.sysroot())
|
2019-09-21 02:53:32 +02:00
|
|
|
]
|
2019-09-20 17:50:24 +02:00
|
|
|
|
2021-12-17 01:16:35 +01:00
|
|
|
cflags += ['-I%s' % (self.system_stl())]
|
|
|
|
if not self.is_clang():
|
|
|
|
cflags += ['-DANDROID', '-D__ANDROID__']
|
2019-10-09 02:50:25 +02:00
|
|
|
|
|
|
|
if cxx and not self.is_clang() and self.toolchain not in ['4.8','4.9']:
|
|
|
|
cflags += ['-fno-sized-deallocation']
|
|
|
|
|
2021-12-17 01:16:35 +01:00
|
|
|
if self.is_clang():
|
|
|
|
# stpcpy() isn't available in early Android versions
|
|
|
|
# disable it here so Clang won't use it
|
|
|
|
if self.api < ANDROID_STPCPY_API_MIN:
|
|
|
|
cflags += ['-fno-builtin-stpcpy']
|
2019-11-07 05:49:44 +01:00
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
if self.is_arm():
|
2019-05-06 03:12:12 +02:00
|
|
|
if self.arch == 'armeabi-v7a':
|
2019-05-03 18:19:39 +02:00
|
|
|
# ARMv7 support
|
2021-12-17 01:16:35 +01:00
|
|
|
cflags += ['-mthumb', '-mfpu=neon', '-mcpu=cortex-a9']
|
2019-11-07 05:49:44 +01:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.is_hardfp():
|
|
|
|
cflags += ['-D_NDK_MATH_NO_SOFTFP=1', '-mfloat-abi=hard', '-DLOAD_HARDFP', '-DSOFTFP_LINK']
|
2021-12-17 01:16:35 +01:00
|
|
|
|
|
|
|
if self.is_host():
|
|
|
|
# Clang builtin redefine w/ different calling convention bug
|
|
|
|
# NOTE: I did not added complex.h functions here, despite
|
|
|
|
# that NDK devs forgot to put __NDK_FPABI_MATH__ for complex
|
|
|
|
# math functions
|
|
|
|
# I personally don't need complex numbers support, but if you want it
|
|
|
|
# just run sed to patch header
|
|
|
|
for f in ['strtod', 'strtof', 'strtold']:
|
|
|
|
cflags += ['-fno-builtin-%s' % f]
|
2019-05-03 18:19:39 +02:00
|
|
|
else:
|
2019-05-28 03:17:05 +02:00
|
|
|
cflags += ['-mfloat-abi=softfp']
|
2019-05-03 18:19:39 +02:00
|
|
|
else:
|
|
|
|
# ARMv5 support
|
2021-07-03 18:21:13 +02:00
|
|
|
cflags += ['-march=armv5te', '-msoft-float']
|
2019-05-03 18:19:39 +02:00
|
|
|
elif self.is_x86():
|
2021-12-17 01:16:35 +01:00
|
|
|
cflags += ['-mtune=atom', '-march=atom', '-mssse3', '-mfpmath=sse']
|
2019-05-03 18:19:39 +02:00
|
|
|
return cflags
|
|
|
|
|
2019-05-28 03:17:05 +02:00
|
|
|
# they go before object list
|
|
|
|
def linkflags(self):
|
2019-07-29 05:52:43 +02:00
|
|
|
linkflags = []
|
2019-09-10 07:25:51 +02:00
|
|
|
if self.is_host():
|
2019-09-20 17:50:24 +02:00
|
|
|
linkflags += ['--gcc-toolchain=%s' % self.gen_gcc_toolchain_path()]
|
2019-09-10 07:25:51 +02:00
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.ndk_rev <= ANDROID_NDK_SYSROOT_FLAG_MAX:
|
2019-09-21 02:53:32 +02:00
|
|
|
linkflags += ['--sysroot=%s' % (self.sysroot())]
|
|
|
|
elif self.is_host():
|
2019-09-20 17:50:24 +02:00
|
|
|
linkflags += ['--sysroot=%s/sysroot' % (self.gen_gcc_toolchain_path())]
|
2019-09-10 07:25:51 +02:00
|
|
|
|
2019-09-21 02:53:32 +02:00
|
|
|
if self.is_clang() or self.is_host():
|
2019-09-20 17:50:24 +02:00
|
|
|
linkflags += ['-fuse-ld=lld']
|
2021-12-17 01:16:35 +01:00
|
|
|
else: linkflags += ['-no-canonical-prefixes']
|
2019-09-21 02:53:32 +02:00
|
|
|
|
2021-12-17 01:16:35 +01:00
|
|
|
linkflags += ['-Wl,--hash-style=sysv', '-Wl,--no-undefined']
|
2019-05-28 03:17:05 +02:00
|
|
|
return linkflags
|
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
def ldflags(self):
|
2021-12-07 06:30:54 +01:00
|
|
|
ldflags = []
|
|
|
|
|
|
|
|
if self.ndk_rev < 23:
|
|
|
|
ldflags += ['-lgcc']
|
|
|
|
|
2019-10-09 02:50:25 +02:00
|
|
|
if self.is_clang() or self.is_host():
|
|
|
|
ldflags += ['-stdlib=libstdc++']
|
2021-12-17 01:16:35 +01:00
|
|
|
else: ldflags += ['-no-canonical-prefixes']
|
2021-12-07 06:30:54 +01:00
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
if self.is_arm():
|
2019-05-06 03:12:12 +02:00
|
|
|
if self.arch == 'armeabi-v7a':
|
2019-09-20 17:50:24 +02:00
|
|
|
ldflags += ['-march=armv7-a', '-mthumb']
|
2019-10-09 02:50:25 +02:00
|
|
|
|
2019-09-21 02:53:32 +02:00
|
|
|
if not self.is_clang() and not self.is_host(): # lld only
|
2019-09-10 07:25:51 +02:00
|
|
|
ldflags += ['-Wl,--fix-cortex-a8']
|
2019-10-09 02:50:25 +02:00
|
|
|
|
|
|
|
if self.is_hardfp():
|
2019-05-28 03:17:05 +02:00
|
|
|
ldflags += ['-Wl,--no-warn-mismatch', '-lm_hard']
|
2019-05-03 18:19:39 +02:00
|
|
|
else:
|
|
|
|
ldflags += ['-march=armv5te']
|
|
|
|
return ldflags
|
|
|
|
|
2023-02-05 01:59:50 +01:00
|
|
|
class NintendoSwitch:
|
|
|
|
ctx = None # waf context
|
2023-02-08 01:01:26 +01:00
|
|
|
arch = "arm64"
|
2023-02-05 01:59:50 +01:00
|
|
|
dkp_dir = None
|
|
|
|
portlibs_dir = None
|
|
|
|
dka64_dir = None
|
|
|
|
libnx_dir = None
|
|
|
|
|
|
|
|
def __init__(self, ctx):
|
|
|
|
self.ctx = ctx
|
|
|
|
|
|
|
|
for i in NSWITCH_ENVVARS:
|
|
|
|
self.dkp_dir = os.getenv(i)
|
|
|
|
if self.dkp_dir != None:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
ctx.fatal('Set %s environment variable pointing to the DEVKITPRO home!' %
|
|
|
|
' or '.join(NSWITCH_ENVVARS))
|
|
|
|
|
|
|
|
self.dkp_dir = os.path.abspath(self.dkp_dir)
|
|
|
|
|
|
|
|
self.dka64_dir = os.path.join(self.dkp_dir, 'devkitA64')
|
|
|
|
if not os.path.exists(self.dka64_dir):
|
|
|
|
ctx.fatal('devkitA64 not found in `%s`. Install devkitA64!' % self.dka64_dir)
|
|
|
|
|
|
|
|
self.libnx_dir = os.path.join(self.dkp_dir, 'libnx')
|
|
|
|
if not os.path.exists(self.libnx_dir):
|
|
|
|
ctx.fatal('libnx not found in `%s`. Install libnx!' % self.libnx_dir)
|
|
|
|
|
|
|
|
self.portlibs_dir = os.path.join(self.dkp_dir, 'portlibs', 'switch')
|
|
|
|
if not os.path.exists(self.portlibs_dir):
|
|
|
|
ctx.fatal('No Switch libraries found in `%s`!' % self.portlibs_dir)
|
|
|
|
|
|
|
|
def gen_toolchain_prefix(self):
|
|
|
|
return 'aarch64-none-elf-'
|
|
|
|
|
|
|
|
def gen_gcc_toolchain_path(self):
|
|
|
|
return os.path.join(self.dka64_dir, 'bin', self.gen_toolchain_prefix())
|
|
|
|
|
|
|
|
def cc(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'gcc'
|
|
|
|
|
|
|
|
def cxx(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'g++'
|
|
|
|
|
|
|
|
def strip(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'strip'
|
|
|
|
|
|
|
|
def pkgconfig(self):
|
2023-02-25 22:44:58 +01:00
|
|
|
# counter-intuitively, this motherfucker is in $DEVKITPRO/portlibs/switch/bin
|
2023-02-05 01:59:50 +01:00
|
|
|
return os.path.join(self.portlibs_dir, 'bin', self.gen_toolchain_prefix() + 'pkg-config')
|
|
|
|
|
|
|
|
def cflags(self, cxx = False):
|
|
|
|
cflags = []
|
|
|
|
# arch flags
|
|
|
|
cflags += ['-D__SWITCH__', '-march=armv8-a+crc+crypto', '-mtune=cortex-a57', '-mtp=soft', '-ftls-model=local-exec', '-fPIE']
|
|
|
|
# help the linker out
|
|
|
|
cflags += ['-ffunction-sections', '-fdata-sections']
|
|
|
|
# base include dirs
|
|
|
|
cflags += ['-isystem %s/include' % self.libnx_dir, '-I%s/include' % self.portlibs_dir]
|
2023-02-25 22:44:58 +01:00
|
|
|
# the game wants GNU extensions
|
2023-02-05 01:59:50 +01:00
|
|
|
if cxx:
|
|
|
|
cflags += ['-std=gnu++17', '-D_GNU_SOURCE']
|
|
|
|
else:
|
|
|
|
cflags += ['-std=gnu11', '-D_GNU_SOURCE']
|
|
|
|
return cflags
|
|
|
|
|
|
|
|
# they go before object list
|
|
|
|
def linkflags(self):
|
|
|
|
linkflags = ['-fPIE', '-specs=%s/switch.specs' % self.libnx_dir]
|
|
|
|
# libsolder only supports sysv hashes and we need to build everything with -rdynamic
|
|
|
|
linkflags += ['-Wl,--hash-style=sysv', '-rdynamic']
|
|
|
|
# avoid pulling in and exposing mesa's internals, that crashes it for some god forsaken reason
|
|
|
|
linkflags += ['-Wl,--exclude-libs=libglapi.a', '-Wl,--exclude-libs=libdrm_nouveau.a']
|
|
|
|
return linkflags
|
|
|
|
|
|
|
|
def ldflags(self):
|
2023-02-08 00:53:45 +01:00
|
|
|
# NOTE: shared libraries should be built without standard libs, so that they could import their contents from the NRO,
|
|
|
|
# but executables, including the SDL2 sanity check, will generally require libstdc++ and libm, which we will add manually
|
|
|
|
ldflags = [] # ['-lm', '-lstdc++']
|
2023-02-05 01:59:50 +01:00
|
|
|
return ldflags
|
|
|
|
|
2023-02-25 22:44:58 +01:00
|
|
|
class PSVita:
|
|
|
|
ctx = None # waf context
|
|
|
|
arch ='armeabi-v7a-hard'
|
|
|
|
vitasdk_dir = None
|
|
|
|
|
|
|
|
def __init__(self, ctx):
|
|
|
|
self.ctx = ctx
|
|
|
|
|
|
|
|
for i in PSVITA_ENVVARS:
|
|
|
|
self.vitasdk_dir = os.getenv(i)
|
|
|
|
if self.vitasdk_dir != None:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
ctx.fatal('Set %s environment variable pointing to the VitaSDK directory!' %
|
|
|
|
' or '.join(PSVITA_ENVVARS))
|
|
|
|
|
|
|
|
def gen_toolchain_prefix(self):
|
|
|
|
return 'arm-vita-eabi-'
|
|
|
|
|
|
|
|
def gen_gcc_toolchain_path(self):
|
|
|
|
return os.path.join(self.vitasdk_dir, 'bin', self.gen_toolchain_prefix())
|
|
|
|
|
|
|
|
def cc(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'gcc'
|
|
|
|
|
|
|
|
def cxx(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'g++'
|
|
|
|
|
|
|
|
def strip(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'strip'
|
|
|
|
|
|
|
|
def ar(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'ar'
|
|
|
|
|
|
|
|
def pkgconfig(self):
|
|
|
|
return self.gen_gcc_toolchain_path() + 'pkg-config'
|
|
|
|
|
|
|
|
def cflags(self, cxx = False):
|
|
|
|
cflags = []
|
|
|
|
# arch flags
|
|
|
|
cflags += ['-D__vita__', '-mtune=cortex-a9', '-mfpu=neon']
|
|
|
|
# necessary linker flags
|
|
|
|
cflags += ['-Wl,-q', '-Wl,-z,nocopyreloc']
|
|
|
|
# this optimization is broken in vitasdk
|
|
|
|
cflags += ['-fno-optimize-sibling-calls']
|
|
|
|
# disable some ARM bullshit
|
2023-03-09 18:38:13 +01:00
|
|
|
cflags += ['-fno-short-enums', '-Wno-attributes']
|
2023-02-25 22:44:58 +01:00
|
|
|
# base include dir
|
|
|
|
cflags += ['-isystem %s/arm-vita-eabi/include' % self.vitasdk_dir]
|
|
|
|
# SDL include dir
|
|
|
|
cflags += ['-I%s/arm-vita-eabi/include/SDL2' % self.vitasdk_dir]
|
|
|
|
return cflags
|
|
|
|
|
|
|
|
# they go before object list
|
|
|
|
def linkflags(self):
|
|
|
|
linkflags = ['-Wl,--hash-style=sysv', '-Wl,-q', '-Wl,-z,nocopyreloc', '-mtune=cortex-a9', '-mfpu=neon']
|
|
|
|
# enforce no-short-enums again
|
|
|
|
linkflags += ['-Wl,-no-enum-size-warning', '-fno-short-enums']
|
|
|
|
return linkflags
|
|
|
|
|
|
|
|
def ldflags(self):
|
|
|
|
ldflags = []
|
|
|
|
return ldflags
|
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
def options(opt):
|
2023-02-05 01:59:50 +01:00
|
|
|
xc = opt.add_option_group('Cross compile options')
|
|
|
|
xc.add_option('--android', action='store', dest='ANDROID_OPTS', default=None,
|
2019-05-03 18:19:39 +02:00
|
|
|
help='enable building for android, format: --android=<arch>,<toolchain>,<api>, example: --android=armeabi-v7a-hard,4.9,9')
|
2023-02-05 01:59:50 +01:00
|
|
|
xc.add_option('--enable-magx', action='store_true', dest='MAGX', default=False,
|
2024-08-10 13:23:13 +02:00
|
|
|
help='enable building for Motorola MAGX [default: %(default)s]')
|
2023-02-05 01:59:50 +01:00
|
|
|
xc.add_option('--enable-msvc-wine', action='store_true', dest='MSVC_WINE', default=False,
|
2024-08-10 13:23:13 +02:00
|
|
|
help='enable building with MSVC using Wine [default: %(default)s]')
|
2023-02-05 01:59:50 +01:00
|
|
|
xc.add_option('--nswitch', action='store_true', dest='NSWITCH', default = False,
|
2024-08-10 13:23:13 +02:00
|
|
|
help='enable building for Nintendo Switch [default: %(default)s]')
|
2023-02-25 22:44:58 +01:00
|
|
|
xc.add_option('--psvita', action='store_true', dest='PSVITA', default = False,
|
2024-08-10 13:23:13 +02:00
|
|
|
help='enable building for PlayStation Vita [default: %(default)s]')
|
|
|
|
xc.add_option('--sailfish', action='store', dest='SAILFISH', default = None,
|
|
|
|
help='enable building for Sailfish/Aurora')
|
2021-07-03 18:21:13 +02:00
|
|
|
|
2019-05-03 18:19:39 +02:00
|
|
|
def configure(conf):
|
|
|
|
if conf.options.ANDROID_OPTS:
|
|
|
|
values = conf.options.ANDROID_OPTS.split(',')
|
|
|
|
if len(values) != 3:
|
|
|
|
conf.fatal('Invalid --android paramater value!')
|
|
|
|
|
2019-09-20 17:50:24 +02:00
|
|
|
valid_archs = ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'aarch64']
|
2019-05-03 18:19:39 +02:00
|
|
|
|
|
|
|
if values[0] not in valid_archs:
|
2019-09-20 17:50:24 +02:00
|
|
|
conf.fatal('Unknown arch: %s. Supported: %r' % (values[0], ', '.join(valid_archs)))
|
2019-05-03 18:19:39 +02:00
|
|
|
|
2019-09-19 16:18:57 +02:00
|
|
|
conf.android = android = Android(conf, values[0], values[1], int(values[2]))
|
2019-05-03 18:19:39 +02:00
|
|
|
conf.environ['CC'] = android.cc()
|
|
|
|
conf.environ['CXX'] = android.cxx()
|
2019-09-11 19:44:05 +02:00
|
|
|
conf.environ['STRIP'] = android.strip()
|
2019-05-03 18:19:39 +02:00
|
|
|
conf.env.CFLAGS += android.cflags()
|
2019-10-09 02:50:25 +02:00
|
|
|
conf.env.CXXFLAGS += android.cflags(True)
|
2019-05-28 03:17:05 +02:00
|
|
|
conf.env.LINKFLAGS += android.linkflags()
|
|
|
|
conf.env.LDFLAGS += android.ldflags()
|
2019-05-03 18:19:39 +02:00
|
|
|
|
|
|
|
conf.env.HAVE_M = True
|
|
|
|
if android.is_hardfp():
|
|
|
|
conf.env.LIB_M = ['m_hard']
|
|
|
|
else: conf.env.LIB_M = ['m']
|
|
|
|
|
2019-10-09 04:52:13 +02:00
|
|
|
conf.env.PREFIX = '/lib/%s' % android.apk_arch()
|
2019-05-06 03:12:12 +02:00
|
|
|
|
2019-09-20 17:50:24 +02:00
|
|
|
conf.msg('Selected Android NDK', '%s, version: %d' % (android.ndk_home, android.ndk_rev))
|
2019-05-03 18:19:39 +02:00
|
|
|
# no need to print C/C++ compiler, as it would be printed by compiler_c/cxx
|
2019-10-09 02:50:25 +02:00
|
|
|
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('... ld flags', ' '.join(android.ldflags()).replace(android.ndk_home, '$NDK/'))
|
2021-07-03 18:21:13 +02:00
|
|
|
elif conf.options.MAGX:
|
|
|
|
# useless to change toolchain path, as toolchain meant to be placed in this path
|
|
|
|
toolchain_path = '/opt/toolchains/motomagx/arm-eabi2/lib/'
|
|
|
|
conf.env.INCLUDES_MAGX = [toolchain_path + i for i in ['ezx-z6/include', 'qt-2.3.8/include']]
|
|
|
|
conf.env.LIBPATH_MAGX = [toolchain_path + i for i in ['ezx-z6/lib', 'qt-2.3.8/lib']]
|
|
|
|
conf.env.LINKFLAGS_MAGX = ['-Wl,-rpath-link=' + i for i in conf.env.LIBPATH_MAGX]
|
2023-02-05 01:59:50 +01:00
|
|
|
elif conf.options.MSVC_WINE:
|
|
|
|
try:
|
|
|
|
toolchain_path = conf.environ['MSVC_WINE_PATH']
|
|
|
|
except KeyError:
|
|
|
|
conf.fatal('Set MSVC_WINE_PATH environment variable to the MSVC toolchain root!')
|
|
|
|
|
|
|
|
conf.environ['CC'] = conf.environ['CXX'] = os.path.join(toolchain_path, 'bin', conf.env.MSVC_TARGETS[0], 'cl')
|
|
|
|
conf.environ['LINK_CXX'] = os.path.join(toolchain_path, 'bin', conf.env.MSVC_TARGETS[0], 'link')
|
|
|
|
conf.environ['AR'] = os.path.join(toolchain_path, 'bin', conf.env.MSVC_TARGETS[0], 'lib')
|
|
|
|
conf.environ['WINRC'] = os.path.join(toolchain_path, 'bin', conf.env.MSVC_TARGETS[0], 'rc')
|
|
|
|
conf.env.DEST_OS = 'win32'
|
|
|
|
conf.env.DEST_CPU = conf.env.MSVC_TARGETS[0]
|
|
|
|
conf.env.COMPILER_CXX = conf.env.COMPILER_CC = 'msvc'
|
|
|
|
elif conf.options.NSWITCH:
|
|
|
|
conf.nswitch = nswitch = NintendoSwitch(conf)
|
|
|
|
conf.environ['CC'] = nswitch.cc()
|
|
|
|
conf.environ['CXX'] = nswitch.cxx()
|
|
|
|
conf.environ['STRIP'] = nswitch.strip()
|
|
|
|
conf.env.PKGCONFIG = nswitch.pkgconfig()
|
|
|
|
conf.env.CFLAGS += nswitch.cflags()
|
|
|
|
conf.env.CXXFLAGS += nswitch.cflags(True)
|
|
|
|
conf.env.LINKFLAGS += nswitch.linkflags()
|
|
|
|
conf.env.LDFLAGS += nswitch.ldflags()
|
|
|
|
conf.env.HAVE_M = True
|
|
|
|
conf.env.LIB_M = ['m']
|
|
|
|
conf.env.DEST_OS = 'nswitch'
|
2023-02-25 22:44:58 +01:00
|
|
|
elif conf.options.PSVITA:
|
|
|
|
conf.psvita = psvita = PSVita(conf)
|
|
|
|
conf.environ['CC'] = psvita.cc()
|
|
|
|
conf.environ['CXX'] = psvita.cxx()
|
|
|
|
conf.environ['STRIP'] = psvita.strip()
|
|
|
|
conf.environ['AR'] = psvita.ar()
|
|
|
|
conf.env.PKGCONFIG = psvita.pkgconfig()
|
|
|
|
conf.env.CFLAGS += psvita.cflags()
|
|
|
|
conf.env.CXXFLAGS += psvita.cflags(True)
|
|
|
|
conf.env.LINKFLAGS += psvita.linkflags()
|
|
|
|
conf.env.LDFLAGS += psvita.ldflags()
|
|
|
|
conf.env.HAVE_M = True
|
|
|
|
conf.env.LIB_M = ['m']
|
|
|
|
conf.env.VRTLD = ['vrtld']
|
|
|
|
conf.env.DEST_OS = 'psvita'
|
2021-07-03 18:21:13 +02:00
|
|
|
|
|
|
|
conf.env.MAGX = conf.options.MAGX
|
2023-02-05 01:59:50 +01:00
|
|
|
conf.env.MSVC_WINE = conf.options.MSVC_WINE
|
2024-08-10 13:23:13 +02:00
|
|
|
conf.env.SAILFISH = conf.options.SAILFISH
|
2023-02-25 22:44:58 +01:00
|
|
|
MACRO_TO_DESTOS = OrderedDict({ '__ANDROID__' : 'android', '__SWITCH__' : 'nswitch', '__vita__' : 'psvita' })
|
2019-09-20 15:35:33 +02:00
|
|
|
for k in c_config.MACRO_TO_DESTOS:
|
|
|
|
MACRO_TO_DESTOS[k] = c_config.MACRO_TO_DESTOS[k] # ordering is important
|
2019-09-19 16:18:57 +02:00
|
|
|
c_config.MACRO_TO_DESTOS = MACRO_TO_DESTOS
|
2019-05-28 03:17:05 +02:00
|
|
|
|
|
|
|
def post_compiler_cxx_configure(conf):
|
2019-09-19 16:18:57 +02:00
|
|
|
conf.msg('Target OS', conf.env.DEST_OS)
|
|
|
|
conf.msg('Target CPU', conf.env.DEST_CPU)
|
|
|
|
conf.msg('Target binfmt', conf.env.DEST_BINFMT)
|
|
|
|
|
2019-05-28 03:17:05 +02:00
|
|
|
if conf.options.ANDROID_OPTS:
|
2019-07-29 05:52:43 +02:00
|
|
|
if conf.android.ndk_rev == 19:
|
2019-05-28 03:17:05 +02:00
|
|
|
conf.env.CXXFLAGS_cxxshlib += ['-static-libstdc++']
|
|
|
|
conf.env.LDFLAGS_cxxshlib += ['-static-libstdc++']
|
2021-07-03 18:35:19 +02:00
|
|
|
elif conf.options.MAGX:
|
|
|
|
for lib in ['qte-mt', 'ezxappbase', 'ezxpm', 'log_util']:
|
|
|
|
conf.check_cc(lib=lib, use='MAGX', uselib_store='MAGX')
|
|
|
|
|
2019-05-28 03:17:05 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
def post_compiler_c_configure(conf):
|
2019-09-19 16:18:57 +02:00
|
|
|
conf.msg('Target OS', conf.env.DEST_OS)
|
|
|
|
conf.msg('Target CPU', conf.env.DEST_CPU)
|
|
|
|
conf.msg('Target binfmt', conf.env.DEST_BINFMT)
|
|
|
|
|
2019-05-28 03:17:05 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
from waflib.Tools import compiler_cxx, compiler_c
|
|
|
|
|
|
|
|
compiler_cxx_configure = getattr(compiler_cxx, 'configure')
|
|
|
|
compiler_c_configure = getattr(compiler_c, 'configure')
|
|
|
|
|
|
|
|
def patch_compiler_cxx_configure(conf):
|
2023-02-05 01:59:50 +01:00
|
|
|
if not conf.env.MSVC_WINE:
|
|
|
|
compiler_cxx_configure(conf)
|
|
|
|
else:
|
|
|
|
conf.load('msvc', funs='no_autodetect')
|
2019-05-28 03:17:05 +02:00
|
|
|
post_compiler_cxx_configure(conf)
|
|
|
|
|
|
|
|
def patch_compiler_c_configure(conf):
|
2023-02-05 01:59:50 +01:00
|
|
|
if not conf.env.MSVC_WINE:
|
|
|
|
compiler_c_configure(conf)
|
|
|
|
else:
|
|
|
|
conf.load('msvc', funs='no_autodetect')
|
2019-05-28 03:17:05 +02:00
|
|
|
post_compiler_c_configure(conf)
|
|
|
|
|
|
|
|
setattr(compiler_cxx, 'configure', patch_compiler_cxx_configure)
|
|
|
|
setattr(compiler_c, 'configure', patch_compiler_c_configure)
|
2019-09-21 02:53:32 +02:00
|
|
|
|
|
|
|
@TaskGen.feature('cshlib', 'cxxshlib', 'dshlib', 'fcshlib', 'vnum')
|
|
|
|
@TaskGen.after_method('apply_link', 'propagate_uselib_vars')
|
|
|
|
@TaskGen.before_method('apply_vnum')
|
|
|
|
def apply_android_soname(self):
|
|
|
|
"""
|
|
|
|
Enforce SONAME on Android
|
|
|
|
"""
|
|
|
|
if self.env.DEST_OS != 'android':
|
|
|
|
return
|
|
|
|
|
|
|
|
setattr(self, 'vnum', None) # remove vnum, so SONAME would not be overwritten
|
|
|
|
link = self.link_task
|
|
|
|
node = link.outputs[0]
|
|
|
|
libname = node.name
|
|
|
|
v = self.env.SONAME_ST % libname
|
|
|
|
self.env.append_value('LINKFLAGS', v.split())
|