android.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import os
  2. import sys
  3. import my_spawn
  4. import common_compiler_flags
  5. from SCons.Script import ARGUMENTS
  6. def options(opts):
  7. opts.Add(
  8. "android_api_level",
  9. "Target Android API level",
  10. "21",
  11. )
  12. opts.Add(
  13. "ANDROID_HOME",
  14. "Path to your Android SDK installation. By default, uses ANDROID_HOME from your defined environment variables.",
  15. os.environ.get("ANDROID_HOME", os.environ.get("ANDROID_SDK_ROOT")),
  16. )
  17. def exists(env):
  18. return get_android_ndk_root(env) is not None
  19. # This must be kept in sync with the value in https://github.com/godotengine/godot/blob/master/platform/android/detect.py#L58.
  20. def get_ndk_version():
  21. return "23.2.8568313"
  22. def get_android_ndk_root(env):
  23. if env["ANDROID_HOME"]:
  24. return env["ANDROID_HOME"] + "/ndk/" + get_ndk_version()
  25. else:
  26. return os.environ.get("ANDROID_NDK_ROOT")
  27. def generate(env):
  28. if get_android_ndk_root(env) is None:
  29. raise ValueError(
  30. "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."
  31. )
  32. if env["arch"] not in ("arm64", "x86_64", "arm32", "x86_32"):
  33. print("Only arm64, x86_64, arm32, and x86_32 are supported on Android. Exiting.")
  34. env.Exit(1)
  35. if sys.platform == "win32" or sys.platform == "msys":
  36. my_spawn.configure(env)
  37. # Validate API level
  38. if int(env["android_api_level"]) < 21:
  39. print("WARNING: minimum supported Android target api is 21. Forcing target api 21.")
  40. env["android_api_level"] = "21"
  41. # Setup toolchain
  42. toolchain = get_android_ndk_root(env) + "/toolchains/llvm/prebuilt/"
  43. if sys.platform == "win32" or sys.platform == "msys":
  44. toolchain += "windows"
  45. import platform as pltfm
  46. if pltfm.machine().endswith("64"):
  47. toolchain += "-x86_64"
  48. elif sys.platform.startswith("linux"):
  49. toolchain += "linux-x86_64"
  50. elif sys.platform == "darwin":
  51. toolchain += "darwin-x86_64"
  52. env.Append(LINKFLAGS=["-shared"])
  53. if not os.path.exists(toolchain):
  54. print("ERROR: Could not find NDK toolchain at " + toolchain + ".")
  55. print("Make sure NDK version " + get_ndk_version() + " is installed.")
  56. env.Exit(1)
  57. env.PrependENVPath("PATH", toolchain + "/bin") # This does nothing half of the time, but we'll put it here anyways
  58. # Get architecture info
  59. arch_info_table = {
  60. "arm32": {
  61. "march": "armv7-a",
  62. "target": "armv7a-linux-androideabi",
  63. "compiler_path": "armv7a-linux-androideabi",
  64. "ccflags": ["-mfpu=neon"],
  65. },
  66. "arm64": {
  67. "march": "armv8-a",
  68. "target": "aarch64-linux-android",
  69. "compiler_path": "aarch64-linux-android",
  70. "ccflags": [],
  71. },
  72. "x86_32": {
  73. "march": "i686",
  74. "target": "i686-linux-android",
  75. "compiler_path": "i686-linux-android",
  76. "ccflags": ["-mstackrealign"],
  77. },
  78. "x86_64": {
  79. "march": "x86-64",
  80. "target": "x86_64-linux-android",
  81. "compiler_path": "x86_64-linux-android",
  82. "ccflags": [],
  83. },
  84. }
  85. arch_info = arch_info_table[env["arch"]]
  86. # Setup tools
  87. env["CC"] = toolchain + "/bin/clang"
  88. env["CXX"] = toolchain + "/bin/clang++"
  89. env["LINK"] = toolchain + "/bin/clang++"
  90. env["AR"] = toolchain + "/bin/llvm-ar"
  91. env["AS"] = toolchain + "/bin/llvm-as"
  92. env["STRIP"] = toolchain + "/bin/llvm-strip"
  93. env["RANLIB"] = toolchain + "/bin/llvm-ranlib"
  94. env["SHLIBSUFFIX"] = ".so"
  95. env.Append(
  96. CCFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"], "-fPIC"]
  97. )
  98. env.Append(CCFLAGS=arch_info["ccflags"])
  99. env.Append(LINKFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"]])
  100. env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED"])
  101. common_compiler_flags.generate(env)