2014-02-10 02:10:30 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
import platform
|
2017-11-09 16:58:29 +01:00
|
|
|
from distutils.version import LooseVersion
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
def is_active():
|
2016-10-30 18:44:57 +01:00
|
|
|
return True
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
def get_name():
|
2016-10-30 18:44:57 +01:00
|
|
|
return "Android"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
def can_build():
|
|
|
|
|
2017-08-26 18:53:49 +02:00
|
|
|
return ("ANDROID_NDK_ROOT" in os.environ)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
def get_opts():
|
2017-09-25 06:37:17 +02:00
|
|
|
from SCons.Variables import BoolVariable, EnumVariable
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 18:44:57 +01:00
|
|
|
return [
|
2017-06-30 19:21:38 +02:00
|
|
|
('ANDROID_NDK_ROOT', 'Path to the Android NDK', os.environ.get("ANDROID_NDK_ROOT", 0)),
|
|
|
|
('ndk_platform', 'Target platform (android-<api>, e.g. "android-18")', "android-18"),
|
2017-09-25 05:06:45 +02:00
|
|
|
EnumVariable('android_arch', 'Target architecture', "armv7", ('armv7', 'armv6', 'arm64v8', 'x86')),
|
2017-09-25 06:37:17 +02:00
|
|
|
BoolVariable('android_neon', 'Enable NEON support (armv7 only)', True),
|
2017-11-08 14:26:43 +01:00
|
|
|
BoolVariable('android_stl', 'Enable Android STL support (for modules)', True)
|
2016-10-30 18:44:57 +01:00
|
|
|
]
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-20 10:22:48 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
def get_flags():
|
|
|
|
|
2016-10-30 18:44:57 +01:00
|
|
|
return [
|
2017-09-25 06:04:49 +02:00
|
|
|
('tools', False),
|
2016-10-30 18:44:57 +01:00
|
|
|
]
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create(env):
|
2017-06-30 19:21:38 +02:00
|
|
|
|
2016-10-30 18:44:57 +01:00
|
|
|
tools = env['TOOLS']
|
|
|
|
if "mingw" in tools:
|
|
|
|
tools.remove('mingw')
|
|
|
|
if "applelink" in tools:
|
|
|
|
tools.remove("applelink")
|
|
|
|
env.Tool('gcc')
|
2016-11-01 00:24:30 +01:00
|
|
|
return env.Clone(tools=tools)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
def configure(env):
|
|
|
|
|
2016-10-30 18:44:57 +01:00
|
|
|
# Workaround for MinGW. See:
|
|
|
|
# http://www.scons.org/wiki/LongCmdLinesOnWin32
|
2016-10-30 18:57:40 +01:00
|
|
|
if (os.name == "nt"):
|
2016-10-30 18:44:57 +01:00
|
|
|
|
|
|
|
import subprocess
|
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
def mySubProcess(cmdline, env):
|
2017-08-26 18:53:49 +02:00
|
|
|
# print("SPAWNED : " + cmdline)
|
2016-10-30 18:44:57 +01:00
|
|
|
startupinfo = subprocess.STARTUPINFO()
|
|
|
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
|
|
|
proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
2016-10-30 18:57:40 +01:00
|
|
|
stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, env=env)
|
2016-10-30 18:44:57 +01:00
|
|
|
data, err = proc.communicate()
|
|
|
|
rv = proc.wait()
|
|
|
|
if rv:
|
2017-08-26 18:53:49 +02:00
|
|
|
print("=====")
|
|
|
|
print(err)
|
|
|
|
print("=====")
|
2016-10-30 18:44:57 +01:00
|
|
|
return rv
|
|
|
|
|
|
|
|
def mySpawn(sh, escape, cmd, args, env):
|
|
|
|
|
|
|
|
newargs = ' '.join(args[1:])
|
|
|
|
cmdline = cmd + " " + newargs
|
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
rv = 0
|
|
|
|
if len(cmdline) > 32000 and cmd.endswith("ar"):
|
2016-10-30 18:44:57 +01:00
|
|
|
cmdline = cmd + " " + args[1] + " " + args[2] + " "
|
2016-10-30 18:57:40 +01:00
|
|
|
for i in range(3, len(args)):
|
|
|
|
rv = mySubProcess(cmdline + args[i], env)
|
|
|
|
if rv:
|
2016-10-30 18:44:57 +01:00
|
|
|
break
|
|
|
|
else:
|
2016-10-30 18:57:40 +01:00
|
|
|
rv = mySubProcess(cmdline, env)
|
2016-10-30 18:44:57 +01:00
|
|
|
|
|
|
|
return rv
|
|
|
|
|
|
|
|
env['SPAWN'] = mySpawn
|
|
|
|
|
2017-06-30 19:21:38 +02:00
|
|
|
## Architecture
|
2016-10-30 18:44:57 +01:00
|
|
|
|
2017-07-25 12:28:31 +02:00
|
|
|
if env['android_arch'] not in ['armv7', 'armv6', 'arm64v8', 'x86']:
|
2017-06-30 19:21:38 +02:00
|
|
|
env['android_arch'] = 'armv7'
|
2016-10-30 18:44:57 +01:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
neon_text = ""
|
2017-09-25 06:37:17 +02:00
|
|
|
if env["android_arch"] == "armv7" and env['android_neon']:
|
2017-06-30 19:21:38 +02:00
|
|
|
neon_text = " (with NEON)"
|
|
|
|
print("Building for Android (" + env['android_arch'] + ")" + neon_text)
|
2016-10-30 18:44:57 +01:00
|
|
|
|
2017-06-30 19:21:38 +02:00
|
|
|
can_vectorize = True
|
2016-10-30 18:57:40 +01:00
|
|
|
if env['android_arch'] == 'x86':
|
2017-07-25 12:28:31 +02:00
|
|
|
env['ARCH'] = 'arch-x86'
|
2016-10-30 18:57:40 +01:00
|
|
|
env.extra_suffix = ".x86" + env.extra_suffix
|
2016-11-02 10:54:51 +01:00
|
|
|
target_subpath = "x86-4.9"
|
|
|
|
abi_subpath = "i686-linux-android"
|
|
|
|
arch_subpath = "x86"
|
2017-06-30 19:21:38 +02:00
|
|
|
env["x86_libtheora_opt_gcc"] = True
|
2016-10-30 18:57:40 +01:00
|
|
|
elif env['android_arch'] == 'armv6':
|
2017-07-25 12:28:31 +02:00
|
|
|
env['ARCH'] = 'arch-arm'
|
2016-10-30 18:57:40 +01:00
|
|
|
env.extra_suffix = ".armv6" + env.extra_suffix
|
2016-11-02 10:54:51 +01:00
|
|
|
target_subpath = "arm-linux-androideabi-4.9"
|
|
|
|
abi_subpath = "arm-linux-androideabi"
|
|
|
|
arch_subpath = "armeabi"
|
2017-06-30 19:21:38 +02:00
|
|
|
can_vectorize = False
|
2016-10-30 18:57:40 +01:00
|
|
|
elif env["android_arch"] == "armv7":
|
2017-07-25 12:28:31 +02:00
|
|
|
env['ARCH'] = 'arch-arm'
|
2016-11-02 10:54:51 +01:00
|
|
|
target_subpath = "arm-linux-androideabi-4.9"
|
|
|
|
abi_subpath = "arm-linux-androideabi"
|
|
|
|
arch_subpath = "armeabi-v7a"
|
2017-09-25 06:37:17 +02:00
|
|
|
if env['android_neon']:
|
2016-10-30 18:57:40 +01:00
|
|
|
env.extra_suffix = ".armv7.neon" + env.extra_suffix
|
2016-10-30 18:44:57 +01:00
|
|
|
else:
|
2016-10-30 18:57:40 +01:00
|
|
|
env.extra_suffix = ".armv7" + env.extra_suffix
|
2017-07-25 12:28:31 +02:00
|
|
|
elif env["android_arch"] == "arm64v8":
|
|
|
|
env['ARCH'] = 'arch-arm64'
|
|
|
|
target_subpath = "aarch64-linux-android-4.9"
|
|
|
|
abi_subpath = "aarch64-linux-android"
|
|
|
|
arch_subpath = "arm64-v8a"
|
|
|
|
env.extra_suffix = ".armv8" + env.extra_suffix
|
2016-10-30 18:44:57 +01:00
|
|
|
|
2017-07-19 20:27:42 +02:00
|
|
|
## Build type
|
|
|
|
|
|
|
|
if (env["target"].startswith("release")):
|
|
|
|
env.Append(LINKFLAGS=['-O2'])
|
|
|
|
env.Append(CPPFLAGS=['-O2', '-DNDEBUG', '-ffast-math', '-funsafe-math-optimizations', '-fomit-frame-pointer'])
|
|
|
|
if (can_vectorize):
|
|
|
|
env.Append(CPPFLAGS=['-ftree-vectorize'])
|
|
|
|
if (env["target"] == "release_debug"):
|
|
|
|
env.Append(CPPFLAGS=['-DDEBUG_ENABLED'])
|
|
|
|
elif (env["target"] == "debug"):
|
|
|
|
env.Append(LINKFLAGS=['-O0'])
|
|
|
|
env.Append(CPPFLAGS=['-O0', '-D_DEBUG', '-UNDEBUG', '-DDEBUG_ENABLED',
|
|
|
|
'-DDEBUG_MEMORY_ENABLED', '-g', '-fno-limit-debug-info'])
|
|
|
|
|
2017-06-30 19:21:38 +02:00
|
|
|
## Compiler configuration
|
|
|
|
|
|
|
|
env['SHLIBSUFFIX'] = '.so'
|
|
|
|
|
|
|
|
if env['PLATFORM'] == 'win32':
|
|
|
|
env.Tool('gcc')
|
|
|
|
env.use_windows_spawn_fix()
|
|
|
|
|
2016-11-13 23:54:06 +01:00
|
|
|
mt_link = True
|
2016-10-30 18:44:57 +01:00
|
|
|
if (sys.platform.startswith("linux")):
|
2016-11-13 23:54:06 +01:00
|
|
|
host_subpath = "linux-x86_64"
|
2016-10-30 18:44:57 +01:00
|
|
|
elif (sys.platform.startswith("darwin")):
|
2016-11-02 10:54:51 +01:00
|
|
|
host_subpath = "darwin-x86_64"
|
2016-10-30 18:44:57 +01:00
|
|
|
elif (sys.platform.startswith('win')):
|
|
|
|
if (platform.machine().endswith('64')):
|
2016-11-02 10:54:51 +01:00
|
|
|
host_subpath = "windows-x86_64"
|
2017-07-25 12:28:31 +02:00
|
|
|
if env["android_arch"] == "arm64v8":
|
|
|
|
mt_link = False
|
2016-10-30 18:44:57 +01:00
|
|
|
else:
|
2016-11-13 23:54:06 +01:00
|
|
|
mt_link = False
|
|
|
|
host_subpath = "windows"
|
2016-10-30 18:57:40 +01:00
|
|
|
|
2017-06-30 19:21:38 +02:00
|
|
|
compiler_path = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/" + host_subpath + "/bin"
|
|
|
|
gcc_toolchain_path = env["ANDROID_NDK_ROOT"] + "/toolchains/" + target_subpath + "/prebuilt/" + host_subpath
|
2016-11-02 10:54:51 +01:00
|
|
|
tools_path = gcc_toolchain_path + "/" + abi_subpath + "/bin"
|
|
|
|
|
|
|
|
# For Clang to find NDK tools in preference of those system-wide
|
|
|
|
env.PrependENVPath('PATH', tools_path)
|
|
|
|
|
2017-11-10 01:07:28 +01:00
|
|
|
ccache_path = os.environ.get("CCACHE")
|
|
|
|
if ccache_path == None:
|
|
|
|
env['CC'] = compiler_path + '/clang'
|
|
|
|
env['CXX'] = compiler_path + '/clang++'
|
|
|
|
else:
|
|
|
|
# there aren't any ccache wrappers available for Android,
|
|
|
|
# to enable caching we need to prepend the path to the ccache binary
|
|
|
|
env['CC'] = ccache_path + ' ' + compiler_path + '/clang'
|
|
|
|
env['CXX'] = ccache_path + ' ' + compiler_path + '/clang++'
|
2016-11-02 10:54:51 +01:00
|
|
|
env['AR'] = tools_path + "/ar"
|
|
|
|
env['RANLIB'] = tools_path + "/ranlib"
|
|
|
|
env['AS'] = tools_path + "/as"
|
2016-10-30 18:44:57 +01:00
|
|
|
|
2016-11-02 10:54:51 +01:00
|
|
|
common_opts = ['-fno-integrated-as', '-gcc-toolchain', gcc_toolchain_path]
|
|
|
|
|
2017-10-31 19:44:25 +01:00
|
|
|
lib_sysroot = env["ANDROID_NDK_ROOT"] + "/platforms/" + env['ndk_platform'] + "/" + env['ARCH']
|
|
|
|
|
2017-06-30 19:21:38 +02:00
|
|
|
## Compile flags
|
|
|
|
|
2017-11-08 14:26:43 +01:00
|
|
|
ndk_version = get_ndk_version(env["ANDROID_NDK_ROOT"])
|
|
|
|
if ndk_version != None and LooseVersion(ndk_version) >= LooseVersion("15.0.4075724"):
|
|
|
|
print("Using NDK unified headers")
|
2017-10-31 19:44:25 +01:00
|
|
|
sysroot = env["ANDROID_NDK_ROOT"] + "/sysroot"
|
|
|
|
env.Append(CPPFLAGS=["-isystem", sysroot + "/usr/include"])
|
|
|
|
env.Append(CPPFLAGS=["-isystem", sysroot + "/usr/include/" + abi_subpath])
|
|
|
|
# For unified headers this define has to be set manually
|
|
|
|
env.Append(CPPFLAGS=["-D__ANDROID_API__=" + str(int(env['ndk_platform'].split("-")[1]))])
|
|
|
|
else:
|
2017-11-08 14:26:43 +01:00
|
|
|
print("Using NDK deprecated headers")
|
2017-10-31 19:44:25 +01:00
|
|
|
env.Append(CPPFLAGS=["-isystem", lib_sysroot + "/usr/include"])
|
|
|
|
|
2017-08-26 18:53:49 +02:00
|
|
|
env.Append(CPPFLAGS='-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden -fno-strict-aliasing'.split())
|
2017-11-19 17:52:18 +01:00
|
|
|
env.Append(CPPFLAGS='-DNO_STATVFS -DGLES_ENABLED'.split())
|
2014-10-07 06:31:49 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
env['neon_enabled'] = False
|
|
|
|
if env['android_arch'] == 'x86':
|
2016-11-02 10:54:51 +01:00
|
|
|
target_opts = ['-target', 'i686-none-linux-android']
|
2017-04-05 22:16:04 +02:00
|
|
|
# The NDK adds this if targeting API < 21, so we can drop it when Godot targets it at least
|
|
|
|
env.Append(CPPFLAGS=['-mstackrealign'])
|
2017-06-30 19:21:38 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
elif env["android_arch"] == "armv6":
|
2016-11-02 10:54:51 +01:00
|
|
|
target_opts = ['-target', 'armv6-none-linux-androideabi']
|
2017-08-26 18:53:49 +02:00
|
|
|
env.Append(CPPFLAGS='-D__ARM_ARCH_6__ -march=armv6 -mfpu=vfp -mfloat-abi=softfp'.split())
|
2017-06-30 19:21:38 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
elif env["android_arch"] == "armv7":
|
2016-11-02 10:54:51 +01:00
|
|
|
target_opts = ['-target', 'armv7-none-linux-androideabi']
|
2017-08-26 18:53:49 +02:00
|
|
|
env.Append(CPPFLAGS='-D__ARM_ARCH_7__ -D__ARM_ARCH_7A__ -march=armv7-a -mfloat-abi=softfp'.split())
|
2017-09-25 06:37:17 +02:00
|
|
|
if env['android_neon']:
|
2016-10-30 18:57:40 +01:00
|
|
|
env['neon_enabled'] = True
|
2016-11-02 10:54:51 +01:00
|
|
|
env.Append(CPPFLAGS=['-mfpu=neon', '-D__ARM_NEON__'])
|
2016-10-30 18:44:57 +01:00
|
|
|
else:
|
2016-11-02 10:54:51 +01:00
|
|
|
env.Append(CPPFLAGS=['-mfpu=vfpv3-d16'])
|
|
|
|
|
2017-07-25 12:28:31 +02:00
|
|
|
elif env["android_arch"] == "arm64v8":
|
|
|
|
target_opts = ['-target', 'aarch64-none-linux-android']
|
|
|
|
env.Append(CPPFLAGS=['-D__ARM_ARCH_8A__'])
|
|
|
|
env.Append(CPPFLAGS=['-mfix-cortex-a53-835769'])
|
|
|
|
|
2016-11-02 10:54:51 +01:00
|
|
|
env.Append(CPPFLAGS=target_opts)
|
|
|
|
env.Append(CPPFLAGS=common_opts)
|
2016-10-30 18:44:57 +01:00
|
|
|
|
2017-09-25 06:37:17 +02:00
|
|
|
if env['android_stl']:
|
2017-06-30 19:21:38 +02:00
|
|
|
env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/include"])
|
|
|
|
env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath + "/include"])
|
|
|
|
env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath])
|
|
|
|
env.Append(LIBS=["gnustl_static"])
|
|
|
|
else:
|
|
|
|
env.Append(CXXFLAGS=['-fno-rtti', '-fno-exceptions', '-DNO_SAFE_CAST'])
|
|
|
|
|
|
|
|
## Link flags
|
|
|
|
|
2017-10-31 19:44:25 +01:00
|
|
|
env['LINKFLAGS'] = ['-shared', '--sysroot=' + lib_sysroot, '-Wl,--warn-shared-textrel']
|
2017-07-25 12:28:31 +02:00
|
|
|
if env["android_arch"] == "armv7":
|
2017-08-26 18:53:49 +02:00
|
|
|
env.Append(LINKFLAGS='-Wl,--fix-cortex-a8'.split())
|
|
|
|
env.Append(LINKFLAGS='-Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now'.split())
|
|
|
|
env.Append(LINKFLAGS='-Wl,-soname,libgodot_android.so -Wl,--gc-sections'.split())
|
2016-11-13 23:54:06 +01:00
|
|
|
if mt_link:
|
|
|
|
env.Append(LINKFLAGS=['-Wl,--threads'])
|
2016-11-02 10:54:51 +01:00
|
|
|
env.Append(LINKFLAGS=target_opts)
|
|
|
|
env.Append(LINKFLAGS=common_opts)
|
|
|
|
|
|
|
|
env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + '/toolchains/arm-linux-androideabi-4.9/prebuilt/' +
|
|
|
|
host_subpath + '/lib/gcc/' + abi_subpath + '/4.9.x'])
|
|
|
|
env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] +
|
|
|
|
'/toolchains/arm-linux-androideabi-4.9/prebuilt/' + host_subpath + '/' + abi_subpath + '/lib'])
|
|
|
|
|
2017-06-30 19:21:38 +02:00
|
|
|
env.Append(CPPPATH=['#platform/android'])
|
|
|
|
env.Append(CPPFLAGS=['-DANDROID_ENABLED', '-DUNIX_ENABLED', '-DNO_FCNTL', '-DMPC_FIXED_POINT'])
|
2017-08-18 16:17:35 +02:00
|
|
|
env.Append(LIBS=['OpenSLES', 'EGL', 'GLESv3', 'android', 'log', 'z', 'dl'])
|
2014-10-07 06:31:49 +02:00
|
|
|
|
2016-10-30 18:44:57 +01:00
|
|
|
# TODO: Move that to opus module's config
|
2017-09-25 06:27:32 +02:00
|
|
|
if 'module_opus_enabled' in env and env['module_opus_enabled']:
|
2016-10-30 18:57:40 +01:00
|
|
|
if (env["android_arch"] == "armv6" or env["android_arch"] == "armv7"):
|
2016-10-30 18:44:57 +01:00
|
|
|
env.Append(CFLAGS=["-DOPUS_ARM_OPT"])
|
2016-10-30 18:57:40 +01:00
|
|
|
env.opus_fixed_point = "yes"
|
2017-10-31 19:44:25 +01:00
|
|
|
|
2017-11-08 14:26:43 +01:00
|
|
|
# Return NDK version string in source.properties (adapted from the Chromium project).
|
|
|
|
def get_ndk_version(path):
|
2017-10-31 19:44:25 +01:00
|
|
|
if path == None:
|
|
|
|
return None
|
|
|
|
prop_file_path = os.path.join(path, "source.properties")
|
|
|
|
try:
|
|
|
|
with open(prop_file_path) as prop_file:
|
|
|
|
for line in prop_file:
|
2017-12-17 13:01:16 +01:00
|
|
|
key_value = list(map(lambda x: x.strip(), line.split("=")))
|
2017-10-31 19:44:25 +01:00
|
|
|
if key_value[0] == "Pkg.Revision":
|
2017-11-08 14:26:43 +01:00
|
|
|
return key_value[1]
|
2017-10-31 19:44:25 +01:00
|
|
|
except:
|
|
|
|
print("Could not read source prop file '%s'" % prop_file_path)
|
|
|
|
return None
|