detect.py 6.4 KB

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