2022-06-06 13:09:32 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import my_spawn
|
2024-02-14 14:32:27 +00:00
|
|
|
import common_compiler_flags
|
2022-06-06 13:09:32 +00:00
|
|
|
from SCons.Script import ARGUMENTS
|
|
|
|
|
|
|
|
|
|
|
|
def options(opts):
|
|
|
|
opts.Add(
|
|
|
|
"android_api_level",
|
|
|
|
"Target Android API level",
|
2023-11-22 21:08:13 +00:00
|
|
|
"21",
|
2022-06-06 13:09:32 +00:00
|
|
|
)
|
|
|
|
opts.Add(
|
2023-11-01 16:09:05 +00:00
|
|
|
"ANDROID_HOME",
|
|
|
|
"Path to your Android SDK installation. By default, uses ANDROID_HOME from your defined environment variables.",
|
|
|
|
os.environ.get("ANDROID_HOME", os.environ.get("ANDROID_SDK_ROOT")),
|
2022-06-06 13:09:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def exists(env):
|
2023-11-01 16:09:05 +00:00
|
|
|
return get_android_ndk_root(env) is not None
|
|
|
|
|
|
|
|
|
|
|
|
# This must be kept in sync with the value in https://github.com/godotengine/godot/blob/master/platform/android/detect.py#L58.
|
|
|
|
def get_ndk_version():
|
|
|
|
return "23.2.8568313"
|
|
|
|
|
|
|
|
|
|
|
|
def get_android_ndk_root(env):
|
|
|
|
if env["ANDROID_HOME"]:
|
|
|
|
return env["ANDROID_HOME"] + "/ndk/" + get_ndk_version()
|
|
|
|
else:
|
|
|
|
return os.environ.get("ANDROID_NDK_ROOT")
|
2022-06-06 13:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate(env):
|
2023-11-01 16:09:05 +00:00
|
|
|
if get_android_ndk_root(env) is None:
|
2022-06-06 13:09:32 +00:00
|
|
|
raise ValueError(
|
2023-11-01 16:09:05 +00:00
|
|
|
"To build for Android, the path to the NDK must be defined. Please set ANDROID_HOME to the root folder of your Android SDK installation."
|
2022-06-06 13:09:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if env["arch"] not in ("arm64", "x86_64", "arm32", "x86_32"):
|
|
|
|
print("Only arm64, x86_64, arm32, and x86_32 are supported on Android. Exiting.")
|
2023-09-09 08:55:53 +00:00
|
|
|
env.Exit(1)
|
2022-06-06 13:09:32 +00:00
|
|
|
|
|
|
|
if sys.platform == "win32" or sys.platform == "msys":
|
|
|
|
my_spawn.configure(env)
|
|
|
|
|
|
|
|
# Validate API level
|
2023-11-22 21:08:13 +00:00
|
|
|
if int(env["android_api_level"]) < 21:
|
|
|
|
print("WARNING: minimum supported Android target api is 21. Forcing target api 21.")
|
2022-06-06 13:09:32 +00:00
|
|
|
env["android_api_level"] = "21"
|
|
|
|
|
|
|
|
# Setup toolchain
|
2023-11-01 16:09:05 +00:00
|
|
|
toolchain = get_android_ndk_root(env) + "/toolchains/llvm/prebuilt/"
|
2022-06-06 13:09:32 +00:00
|
|
|
if sys.platform == "win32" or sys.platform == "msys":
|
|
|
|
toolchain += "windows"
|
|
|
|
import platform as pltfm
|
|
|
|
|
|
|
|
if pltfm.machine().endswith("64"):
|
|
|
|
toolchain += "-x86_64"
|
|
|
|
elif sys.platform.startswith("linux"):
|
|
|
|
toolchain += "linux-x86_64"
|
|
|
|
elif sys.platform == "darwin":
|
|
|
|
toolchain += "darwin-x86_64"
|
2022-11-07 11:03:54 +00:00
|
|
|
env.Append(LINKFLAGS=["-shared"])
|
2023-12-29 14:32:14 +00:00
|
|
|
|
|
|
|
if not os.path.exists(toolchain):
|
|
|
|
print("ERROR: Could not find NDK toolchain at " + toolchain + ".")
|
|
|
|
print("Make sure NDK version " + get_ndk_version() + " is installed.")
|
|
|
|
env.Exit(1)
|
|
|
|
|
2022-06-06 13:09:32 +00:00
|
|
|
env.PrependENVPath("PATH", toolchain + "/bin") # This does nothing half of the time, but we'll put it here anyways
|
|
|
|
|
|
|
|
# Get architecture info
|
|
|
|
arch_info_table = {
|
|
|
|
"arm32": {
|
|
|
|
"march": "armv7-a",
|
|
|
|
"target": "armv7a-linux-androideabi",
|
|
|
|
"compiler_path": "armv7a-linux-androideabi",
|
|
|
|
"ccflags": ["-mfpu=neon"],
|
|
|
|
},
|
|
|
|
"arm64": {
|
|
|
|
"march": "armv8-a",
|
|
|
|
"target": "aarch64-linux-android",
|
|
|
|
"compiler_path": "aarch64-linux-android",
|
|
|
|
"ccflags": [],
|
|
|
|
},
|
|
|
|
"x86_32": {
|
|
|
|
"march": "i686",
|
|
|
|
"target": "i686-linux-android",
|
|
|
|
"compiler_path": "i686-linux-android",
|
|
|
|
"ccflags": ["-mstackrealign"],
|
|
|
|
},
|
|
|
|
"x86_64": {
|
|
|
|
"march": "x86-64",
|
|
|
|
"target": "x86_64-linux-android",
|
|
|
|
"compiler_path": "x86_64-linux-android",
|
|
|
|
"ccflags": [],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
arch_info = arch_info_table[env["arch"]]
|
|
|
|
|
|
|
|
# Setup tools
|
|
|
|
env["CC"] = toolchain + "/bin/clang"
|
|
|
|
env["CXX"] = toolchain + "/bin/clang++"
|
|
|
|
env["LINK"] = toolchain + "/bin/clang++"
|
|
|
|
env["AR"] = toolchain + "/bin/llvm-ar"
|
|
|
|
env["AS"] = toolchain + "/bin/llvm-as"
|
|
|
|
env["STRIP"] = toolchain + "/bin/llvm-strip"
|
|
|
|
env["RANLIB"] = toolchain + "/bin/llvm-ranlib"
|
|
|
|
env["SHLIBSUFFIX"] = ".so"
|
|
|
|
|
|
|
|
env.Append(
|
|
|
|
CCFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"], "-fPIC"]
|
2022-09-11 17:25:22 +00:00
|
|
|
)
|
2022-06-06 13:09:32 +00:00
|
|
|
env.Append(CCFLAGS=arch_info["ccflags"])
|
|
|
|
env.Append(LINKFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"]])
|
2023-07-22 12:46:14 +00:00
|
|
|
|
|
|
|
env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED"])
|
2024-02-14 14:32:27 +00:00
|
|
|
|
|
|
|
common_compiler_flags.generate(env)
|