detect.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import os
  2. import sys
  3. import platform
  4. import subprocess
  5. from typing import TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from SCons.Script.SConscript import SConsEnvironment
  8. def get_name():
  9. return "Android"
  10. def can_build():
  11. return os.path.exists(get_env_android_sdk_root())
  12. def get_opts():
  13. from SCons.Variables import BoolVariable
  14. return [
  15. ("ANDROID_HOME", "Path to the Android SDK", get_env_android_sdk_root()),
  16. (
  17. "ndk_platform",
  18. 'Target platform (android-<api>, e.g. "android-' + str(get_min_target_api()) + '")',
  19. "android-" + str(get_min_target_api()),
  20. ),
  21. BoolVariable("store_release", "Editor build for Google Play Store (for official builds only)", False),
  22. BoolVariable("generate_apk", "Generate an APK/AAB after building Android library by calling Gradle", False),
  23. ]
  24. def get_doc_classes():
  25. return [
  26. "EditorExportPlatformAndroid",
  27. ]
  28. def get_doc_path():
  29. return "doc_classes"
  30. # Return the ANDROID_HOME environment variable.
  31. def get_env_android_sdk_root():
  32. return os.environ.get("ANDROID_HOME", os.environ.get("ANDROID_SDK_ROOT", ""))
  33. def get_min_sdk_version(platform):
  34. return int(platform.split("-")[1])
  35. def get_android_ndk_root(env: "SConsEnvironment"):
  36. return env["ANDROID_HOME"] + "/ndk/" + get_ndk_version()
  37. # This is kept in sync with the value in 'platform/android/java/app/config.gradle'.
  38. def get_ndk_version():
  39. return "23.2.8568313"
  40. # This is kept in sync with the value in 'platform/android/java/app/config.gradle'.
  41. def get_min_target_api():
  42. return 21
  43. def get_flags():
  44. return [
  45. ("arch", "arm64"), # Default for convenience.
  46. ("target", "template_debug"),
  47. ("supported", ["mono"]),
  48. ]
  49. # Check if Android NDK version is installed
  50. # If not, install it.
  51. def install_ndk_if_needed(env: "SConsEnvironment"):
  52. print("Checking for Android NDK...")
  53. sdk_root = env["ANDROID_HOME"]
  54. if not os.path.exists(get_android_ndk_root(env)):
  55. extension = ".bat" if os.name == "nt" else ""
  56. sdkmanager = sdk_root + "/cmdline-tools/latest/bin/sdkmanager" + extension
  57. if os.path.exists(sdkmanager):
  58. # Install the Android NDK
  59. print("Installing Android NDK...")
  60. ndk_download_args = "ndk;" + get_ndk_version()
  61. subprocess.check_call([sdkmanager, ndk_download_args])
  62. else:
  63. print("Cannot find " + sdkmanager)
  64. print(
  65. "Please ensure ANDROID_HOME is correct and cmdline-tools are installed, or install NDK version "
  66. + get_ndk_version()
  67. + " manually."
  68. )
  69. sys.exit()
  70. env["ANDROID_NDK_ROOT"] = get_android_ndk_root(env)
  71. def configure(env: "SConsEnvironment"):
  72. # Validate arch.
  73. supported_arches = ["x86_32", "x86_64", "arm32", "arm64"]
  74. if env["arch"] not in supported_arches:
  75. print(
  76. 'Unsupported CPU architecture "%s" for Android. Supported architectures are: %s.'
  77. % (env["arch"], ", ".join(supported_arches))
  78. )
  79. sys.exit()
  80. if get_min_sdk_version(env["ndk_platform"]) < get_min_target_api():
  81. print(
  82. "WARNING: minimum supported Android target api is %d. Forcing target api %d."
  83. % (get_min_target_api(), get_min_target_api())
  84. )
  85. env["ndk_platform"] = "android-" + str(get_min_target_api())
  86. install_ndk_if_needed(env)
  87. ndk_root = env["ANDROID_NDK_ROOT"]
  88. # Architecture
  89. if env["arch"] == "arm32":
  90. target_triple = "armv7a-linux-androideabi"
  91. elif env["arch"] == "arm64":
  92. target_triple = "aarch64-linux-android"
  93. elif env["arch"] == "x86_32":
  94. target_triple = "i686-linux-android"
  95. elif env["arch"] == "x86_64":
  96. target_triple = "x86_64-linux-android"
  97. target_option = ["-target", target_triple + str(get_min_sdk_version(env["ndk_platform"]))]
  98. env.Append(ASFLAGS=[target_option, "-c"])
  99. env.Append(CCFLAGS=target_option)
  100. env.Append(LINKFLAGS=target_option)
  101. # LTO
  102. if env["lto"] == "auto": # LTO benefits for Android (size, performance) haven't been clearly established yet.
  103. env["lto"] = "none"
  104. if env["lto"] != "none":
  105. if env["lto"] == "thin":
  106. env.Append(CCFLAGS=["-flto=thin"])
  107. env.Append(LINKFLAGS=["-flto=thin"])
  108. else:
  109. env.Append(CCFLAGS=["-flto"])
  110. env.Append(LINKFLAGS=["-flto"])
  111. # Compiler configuration
  112. env["SHLIBSUFFIX"] = ".so"
  113. if env["PLATFORM"] == "win32":
  114. env.use_windows_spawn_fix()
  115. if sys.platform.startswith("linux"):
  116. host_subpath = "linux-x86_64"
  117. elif sys.platform.startswith("darwin"):
  118. host_subpath = "darwin-x86_64"
  119. elif sys.platform.startswith("win"):
  120. if platform.machine().endswith("64"):
  121. host_subpath = "windows-x86_64"
  122. else:
  123. host_subpath = "windows"
  124. toolchain_path = ndk_root + "/toolchains/llvm/prebuilt/" + host_subpath
  125. compiler_path = toolchain_path + "/bin"
  126. env["CC"] = compiler_path + "/clang"
  127. env["CXX"] = compiler_path + "/clang++"
  128. env["AR"] = compiler_path + "/llvm-ar"
  129. env["RANLIB"] = compiler_path + "/llvm-ranlib"
  130. env["AS"] = compiler_path + "/clang"
  131. env.Append(
  132. CCFLAGS=(
  133. "-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden -fno-strict-aliasing".split()
  134. )
  135. )
  136. if get_min_sdk_version(env["ndk_platform"]) >= 24:
  137. env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
  138. if env["arch"] == "x86_32":
  139. # The NDK adds this if targeting API < 24, so we can drop it when Godot targets it at least
  140. env.Append(CCFLAGS=["-mstackrealign"])
  141. elif env["arch"] == "arm32":
  142. env.Append(CCFLAGS="-march=armv7-a -mfloat-abi=softfp".split())
  143. env.Append(CPPDEFINES=["__ARM_ARCH_7__", "__ARM_ARCH_7A__"])
  144. env.Append(CPPDEFINES=["__ARM_NEON__"])
  145. elif env["arch"] == "arm64":
  146. env.Append(CCFLAGS=["-mfix-cortex-a53-835769"])
  147. env.Append(CPPDEFINES=["__ARM_ARCH_8A__"])
  148. # Link flags
  149. env.Append(LINKFLAGS="-Wl,--gc-sections -Wl,--no-undefined -Wl,-z,now".split())
  150. env.Append(LINKFLAGS="-Wl,-soname,libgodot_android.so")
  151. env.Prepend(CPPPATH=["#platform/android"])
  152. env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED"])
  153. env.Append(LIBS=["OpenSLES", "EGL", "android", "log", "z", "dl"])
  154. if env["vulkan"]:
  155. env.Append(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  156. if not env["use_volk"]:
  157. env.Append(LIBS=["vulkan"])
  158. if env["opengl3"]:
  159. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  160. env.Append(LIBS=["GLESv3"])