detect.py 6.2 KB

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