2014-02-10 01:10:30 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import platform
|
2022-06-01 07:54:08 +00:00
|
|
|
import subprocess
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-10-30 18:05:14 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
def is_active():
|
2016-10-30 17:44:57 +00:00
|
|
|
return True
|
2016-04-02 18:26:12 +00:00
|
|
|
|
2016-10-30 18:05:14 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
def get_name():
|
2016-10-30 17:44:57 +00:00
|
|
|
return "Android"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-10-30 18:05:14 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
def can_build():
|
2022-06-01 07:54:08 +00:00
|
|
|
return os.path.exists(get_env_android_sdk_root())
|
2017-12-18 15:39:09 +00:00
|
|
|
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
def get_opts():
|
2017-09-25 04:37:17 +00:00
|
|
|
from SCons.Variables import BoolVariable, EnumVariable
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-10-30 17:44:57 +00:00
|
|
|
return [
|
2022-06-01 07:54:08 +00:00
|
|
|
("ANDROID_SDK_ROOT", "Path to the Android SDK", get_env_android_sdk_root()),
|
2020-03-05 18:00:28 +00:00
|
|
|
("ndk_platform", 'Target platform (android-<api>, e.g. "android-24")', "android-24"),
|
2021-10-12 13:27:30 +00:00
|
|
|
EnumVariable("android_arch", "Target architecture", "arm64v8", ("armv7", "arm64v8", "x86", "x86_64")),
|
2016-10-30 17:44:57 +00:00
|
|
|
]
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-03-20 09:22:48 +00:00
|
|
|
|
2021-01-05 21:40:42 +00:00
|
|
|
# Return the ANDROID_SDK_ROOT environment variable.
|
2022-06-01 07:54:08 +00:00
|
|
|
def get_env_android_sdk_root():
|
|
|
|
return os.environ.get("ANDROID_SDK_ROOT", -1)
|
2021-01-05 21:40:42 +00:00
|
|
|
|
|
|
|
|
2022-06-01 07:54:08 +00:00
|
|
|
def get_min_sdk_version(platform):
|
|
|
|
return int(platform.split("-")[1])
|
|
|
|
|
|
|
|
|
|
|
|
def get_android_ndk_root(env):
|
|
|
|
return env["ANDROID_SDK_ROOT"] + "/ndk/" + get_ndk_version()
|
|
|
|
|
|
|
|
|
|
|
|
# This is kept in sync with the value in 'platform/android/java/app/config.gradle'.
|
|
|
|
def get_ndk_version():
|
|
|
|
return "23.2.8568313"
|
2021-01-05 21:40:42 +00:00
|
|
|
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
def get_flags():
|
2016-10-30 17:44:57 +00:00
|
|
|
return [
|
2020-03-30 06:28:32 +00:00
|
|
|
("tools", False),
|
2016-10-30 17:44:57 +00:00
|
|
|
]
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
|
2022-06-01 07:54:08 +00:00
|
|
|
# Check if Android NDK version is installed
|
|
|
|
# If not, install it.
|
2021-01-05 21:40:42 +00:00
|
|
|
def install_ndk_if_needed(env):
|
|
|
|
print("Checking for Android NDK...")
|
2022-06-01 07:54:08 +00:00
|
|
|
sdk_root = env["ANDROID_SDK_ROOT"]
|
|
|
|
if not os.path.exists(get_android_ndk_root(env)):
|
2021-01-05 21:40:42 +00:00
|
|
|
extension = ".bat" if os.name == "nt" else ""
|
2022-06-01 07:54:08 +00:00
|
|
|
sdkmanager = sdk_root + "/cmdline-tools/latest/bin/sdkmanager" + extension
|
|
|
|
if os.path.exists(sdkmanager):
|
|
|
|
# Install the Android NDK
|
|
|
|
print("Installing Android NDK...")
|
|
|
|
ndk_download_args = "ndk;" + get_ndk_version()
|
|
|
|
subprocess.check_call([sdkmanager, ndk_download_args])
|
|
|
|
else:
|
|
|
|
print("Cannot find " + sdkmanager)
|
|
|
|
print(
|
|
|
|
"Please ensure ANDROID_SDK_ROOT is correct and cmdline-tools are installed, or install NDK version "
|
|
|
|
+ get_ndk_version()
|
|
|
|
+ " manually."
|
|
|
|
)
|
|
|
|
sys.exit()
|
|
|
|
env["ANDROID_NDK_ROOT"] = get_android_ndk_root(env)
|
2021-01-05 21:40:42 +00:00
|
|
|
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
def configure(env):
|
2021-01-05 21:40:42 +00:00
|
|
|
install_ndk_if_needed(env)
|
2022-06-01 07:54:08 +00:00
|
|
|
ndk_root = env["ANDROID_NDK_ROOT"]
|
2016-10-30 17:44:57 +00:00
|
|
|
|
2019-08-27 09:16:33 +00:00
|
|
|
# Architecture
|
2016-10-30 17:44:57 +00:00
|
|
|
|
2020-03-30 06:28:32 +00:00
|
|
|
if env["android_arch"] not in ["armv7", "arm64v8", "x86", "x86_64"]:
|
2022-06-01 07:54:08 +00:00
|
|
|
env["android_arch"] = "arm64v8"
|
2016-10-30 17:44:57 +00:00
|
|
|
|
2021-10-12 13:27:30 +00:00
|
|
|
print("Building for Android, platform " + env["ndk_platform"] + " (" + env["android_arch"] + ")")
|
2016-10-30 17:44:57 +00:00
|
|
|
|
2022-06-01 07:54:08 +00:00
|
|
|
if get_min_sdk_version(env["ndk_platform"]) < 21:
|
|
|
|
if env["android_arch"] == "x86_64" or env["android_arch"] == "arm64v8":
|
2020-03-30 06:28:32 +00:00
|
|
|
print(
|
2022-06-01 07:54:08 +00:00
|
|
|
"WARNING: android_arch="
|
|
|
|
+ env["android_arch"]
|
|
|
|
+ " is not supported by ndk_platform lower than android-21; setting ndk_platform=android-21"
|
2020-03-30 06:28:32 +00:00
|
|
|
)
|
2019-01-16 10:16:00 +00:00
|
|
|
env["ndk_platform"] = "android-21"
|
2022-06-01 07:54:08 +00:00
|
|
|
|
|
|
|
if env["android_arch"] == "armv7":
|
|
|
|
target_triple = "armv7a-linux-androideabi"
|
|
|
|
bin_utils = "arm-linux-androideabi"
|
2021-10-12 13:27:30 +00:00
|
|
|
env.extra_suffix = ".armv7" + env.extra_suffix
|
2017-07-25 10:28:31 +00:00
|
|
|
elif env["android_arch"] == "arm64v8":
|
2022-06-01 07:54:08 +00:00
|
|
|
target_triple = "aarch64-linux-android"
|
|
|
|
bin_utils = target_triple
|
2017-07-25 10:28:31 +00:00
|
|
|
env.extra_suffix = ".armv8" + env.extra_suffix
|
2022-06-01 07:54:08 +00:00
|
|
|
elif env["android_arch"] == "x86":
|
|
|
|
target_triple = "i686-linux-android"
|
|
|
|
bin_utils = target_triple
|
|
|
|
env.extra_suffix = ".x86" + env.extra_suffix
|
|
|
|
elif env["android_arch"] == "x86_64":
|
|
|
|
target_triple = "x86_64-linux-android"
|
|
|
|
bin_utils = target_triple
|
|
|
|
env.extra_suffix = ".x86_64" + env.extra_suffix
|
|
|
|
|
|
|
|
target_option = ["-target", target_triple + str(get_min_sdk_version(env["ndk_platform"]))]
|
|
|
|
env.Append(CCFLAGS=target_option)
|
|
|
|
env.Append(LINKFLAGS=target_option)
|
2016-10-30 17:44:57 +00:00
|
|
|
|
2019-08-27 09:16:33 +00:00
|
|
|
# Build type
|
2017-07-19 18:27:42 +00:00
|
|
|
|
2020-03-30 06:28:32 +00:00
|
|
|
if env["target"].startswith("release"):
|
|
|
|
if env["optimize"] == "speed": # optimize for speed (default)
|
2022-04-18 19:04:24 +00:00
|
|
|
# `-O2` is more friendly to debuggers than `-O3`, leading to better crash backtraces
|
|
|
|
# when using `target=release_debug`.
|
|
|
|
opt = "-O3" if env["target"] == "release" else "-O2"
|
|
|
|
env.Append(CCFLAGS=[opt, "-fomit-frame-pointer"])
|
2021-03-13 14:29:55 +00:00
|
|
|
elif env["optimize"] == "size": # optimize for size
|
2022-06-01 07:54:08 +00:00
|
|
|
env.Append(CCFLAGS=["-Oz"])
|
2021-03-13 14:29:55 +00:00
|
|
|
env.Append(CPPDEFINES=["NDEBUG"])
|
2022-06-01 07:54:08 +00:00
|
|
|
env.Append(CCFLAGS=["-ftree-vectorize"])
|
2020-03-30 06:28:32 +00:00
|
|
|
elif env["target"] == "debug":
|
|
|
|
env.Append(LINKFLAGS=["-O0"])
|
|
|
|
env.Append(CCFLAGS=["-O0", "-g", "-fno-limit-debug-info"])
|
2021-10-15 06:39:29 +00:00
|
|
|
env.Append(CPPDEFINES=["_DEBUG"])
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(CPPFLAGS=["-UNDEBUG"])
|
2017-07-19 18:27:42 +00:00
|
|
|
|
2019-08-27 09:16:33 +00:00
|
|
|
# Compiler configuration
|
2017-06-30 17:21:38 +00:00
|
|
|
|
2020-03-30 06:28:32 +00:00
|
|
|
env["SHLIBSUFFIX"] = ".so"
|
2017-06-30 17:21:38 +00:00
|
|
|
|
2020-03-30 06:28:32 +00:00
|
|
|
if env["PLATFORM"] == "win32":
|
2017-06-30 17:21:38 +00:00
|
|
|
env.use_windows_spawn_fix()
|
|
|
|
|
2020-03-30 06:28:32 +00:00
|
|
|
if sys.platform.startswith("linux"):
|
2016-11-13 22:54:06 +00:00
|
|
|
host_subpath = "linux-x86_64"
|
2020-03-30 06:28:32 +00:00
|
|
|
elif sys.platform.startswith("darwin"):
|
2016-11-02 09:54:51 +00:00
|
|
|
host_subpath = "darwin-x86_64"
|
2020-03-30 06:28:32 +00:00
|
|
|
elif sys.platform.startswith("win"):
|
|
|
|
if platform.machine().endswith("64"):
|
2016-11-02 09:54:51 +00:00
|
|
|
host_subpath = "windows-x86_64"
|
2016-10-30 17:44:57 +00:00
|
|
|
else:
|
2016-11-13 22:54:06 +00:00
|
|
|
host_subpath = "windows"
|
2016-10-30 17:57:40 +00:00
|
|
|
|
2022-06-01 07:54:08 +00:00
|
|
|
toolchain_path = ndk_root + "/toolchains/llvm/prebuilt/" + host_subpath
|
|
|
|
compiler_path = toolchain_path + "/bin"
|
|
|
|
bin_utils_path = toolchain_path + "/" + bin_utils + "/bin"
|
2016-10-30 17:44:57 +00:00
|
|
|
|
2022-06-01 07:54:08 +00:00
|
|
|
env["CC"] = compiler_path + "/clang"
|
|
|
|
env["CXX"] = compiler_path + "/clang++"
|
|
|
|
env["AR"] = compiler_path + "/llvm-ar"
|
|
|
|
env["RANLIB"] = compiler_path + "/llvm-ranlib"
|
|
|
|
env["AS"] = bin_utils_path + "/as"
|
2019-07-30 13:33:24 +00:00
|
|
|
|
2019-05-20 14:47:32 +00:00
|
|
|
# Disable exceptions and rtti on non-tools (template) builds
|
2021-04-20 18:50:48 +00:00
|
|
|
if env["tools"]:
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(CXXFLAGS=["-frtti"])
|
2021-04-20 18:50:48 +00:00
|
|
|
elif env["builtin_icu"]:
|
|
|
|
env.Append(CXXFLAGS=["-frtti", "-fno-exceptions"])
|
2018-08-22 22:02:47 +00:00
|
|
|
else:
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(CXXFLAGS=["-fno-rtti", "-fno-exceptions"])
|
2019-05-20 14:47:32 +00:00
|
|
|
# Don't use dynamic_cast, necessary with no-rtti.
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(CPPDEFINES=["NO_SAFE_CAST"])
|
2018-08-22 22:02:47 +00:00
|
|
|
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(
|
2020-07-26 17:38:10 +00:00
|
|
|
CCFLAGS=(
|
2022-06-01 07:54:08 +00:00
|
|
|
"-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden -fno-strict-aliasing".split()
|
2020-07-26 17:38:10 +00:00
|
|
|
)
|
2020-03-30 06:28:32 +00:00
|
|
|
)
|
|
|
|
env.Append(CPPDEFINES=["NO_STATVFS", "GLES_ENABLED"])
|
2014-10-07 04:31:49 +00:00
|
|
|
|
2022-06-01 07:54:08 +00:00
|
|
|
if get_min_sdk_version(env["ndk_platform"]) >= 24:
|
2019-03-26 17:51:13 +00:00
|
|
|
env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
|
|
|
|
|
2020-03-30 06:28:32 +00:00
|
|
|
if env["android_arch"] == "x86":
|
2022-06-01 07:54:08 +00:00
|
|
|
# The NDK adds this if targeting API < 24, so we can drop it when Godot targets it at least
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(CCFLAGS=["-mstackrealign"])
|
2016-10-30 17:57:40 +00:00
|
|
|
elif env["android_arch"] == "armv7":
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(CCFLAGS="-march=armv7-a -mfloat-abi=softfp".split())
|
|
|
|
env.Append(CPPDEFINES=["__ARM_ARCH_7__", "__ARM_ARCH_7A__"])
|
2021-10-12 13:27:30 +00:00
|
|
|
env.Append(CPPDEFINES=["__ARM_NEON__"])
|
2017-07-25 10:28:31 +00:00
|
|
|
elif env["android_arch"] == "arm64v8":
|
2020-03-30 06:28:32 +00:00
|
|
|
env.Append(CCFLAGS=["-mfix-cortex-a53-835769"])
|
|
|
|
env.Append(CPPDEFINES=["__ARM_ARCH_8A__"])
|
2017-07-25 10:28:31 +00:00
|
|
|
|
2019-08-27 09:16:33 +00:00
|
|
|
# Link flags
|
2019-07-30 13:49:31 +00:00
|
|
|
|
2022-06-01 07:54:08 +00:00
|
|
|
env.Append(LINKFLAGS="-Wl,--gc-sections -Wl,--no-undefined -Wl,-z,now".split())
|
|
|
|
env.Append(LINKFLAGS="-Wl,-soname,libgodot_android.so")
|
2020-03-30 06:28:32 +00:00
|
|
|
|
|
|
|
env.Prepend(CPPPATH=["#platform/android"])
|
2021-08-12 11:24:54 +00:00
|
|
|
env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED", "NO_FCNTL"])
|
|
|
|
env.Append(LIBS=["OpenSLES", "EGL", "GLESv2", "android", "log", "z", "dl"])
|
|
|
|
|
|
|
|
if env["vulkan"]:
|
|
|
|
env.Append(CPPDEFINES=["VULKAN_ENABLED"])
|
|
|
|
if not env["use_volk"]:
|
|
|
|
env.Append(LIBS=["vulkan"])
|