summaryrefslogtreecommitdiffstats
path: root/tools/android.py
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2022-06-06 15:09:32 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2022-06-27 20:44:12 +0200
commit93f2091185ff4390ca8fc8901ebc68ebc35a218f (patch)
treec69f995ae6b3e20995747861c595ca1a8fef9942 /tools/android.py
parenta0fcd8a7353ab692d864d3807974a93c097fb362 (diff)
downloadredot-cpp-93f2091185ff4390ca8fc8901ebc68ebc35a218f.tar.gz
[SCons] Move toolchains logic to tools folder.
Diffstat (limited to 'tools/android.py')
-rw-r--r--tools/android.py110
1 files changed, 110 insertions, 0 deletions
diff --git a/tools/android.py b/tools/android.py
new file mode 100644
index 0000000..98c3b3f
--- /dev/null
+++ b/tools/android.py
@@ -0,0 +1,110 @@
+import os
+import sys
+import my_spawn
+from SCons.Script import ARGUMENTS
+
+
+def options(opts):
+ opts.Add(
+ "android_api_level",
+ "Target Android API level",
+ "18" if "32" in ARGUMENTS.get("arch", "arm64") else "21",
+ )
+ opts.Add(
+ "ANDROID_NDK_ROOT",
+ "Path to your Android NDK installation. By default, uses ANDROID_NDK_ROOT from your defined environment variables.",
+ os.environ.get("ANDROID_NDK_ROOT", None),
+ )
+
+
+def exists(env):
+ if not "ANDROID_NDK_ROOT" in os.environ:
+ return False
+ return True
+
+
+def generate(env):
+ if not "ANDROID_NDK_ROOT" in os.environ:
+ raise ValueError(
+ "To build for Android, ANDROID_NDK_ROOT must be defined. Please set ANDROID_NDK_ROOT to the root folder of your Android NDK installation."
+ )
+
+ env["ANDROID_NDK_ROOT"] = os.environ["ANDROID_NDK_ROOT"]
+
+ 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.")
+ Exit()
+
+ if sys.platform == "win32" or sys.platform == "msys":
+ my_spawn.configure(env)
+
+ # Validate API level
+ api_level = int(env["android_api_level"])
+ if "64" in env["arch"] and api_level < 21:
+ print("WARN: 64-bit Android architectures require an API level of at least 21; setting android_api_level=21")
+ env["android_api_level"] = "21"
+ api_level = 21
+
+ # Setup toolchain
+ toolchain = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/"
+ 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"
+ 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"]
+ ) # , '-fPIE', '-fno-addrsig', '-Oz'])
+ env.Append(CCFLAGS=arch_info["ccflags"])
+ env.Append(LINKFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"]])
+
+ if env["target"] == "debug":
+ env.Append(CCFLAGS=["-Og", "-g"])
+ elif env["target"] == "release":
+ env.Append(CCFLAGS=["-O3"])