detect.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import os
  2. import sys
  3. import platform
  4. import subprocess
  5. def is_active():
  6. return True
  7. def get_name():
  8. return "Android"
  9. def can_build():
  10. return os.path.exists(get_env_android_sdk_root())
  11. def get_opts():
  12. from SCons.Variables import BoolVariable, EnumVariable
  13. return [
  14. ("ANDROID_SDK_ROOT", "Path to the Android SDK", get_env_android_sdk_root()),
  15. ("ndk_platform", 'Target platform (android-<api>, e.g. "android-24")', "android-24"),
  16. ]
  17. # Return the ANDROID_SDK_ROOT environment variable.
  18. def get_env_android_sdk_root():
  19. return os.environ.get("ANDROID_SDK_ROOT", -1)
  20. def get_min_sdk_version(platform):
  21. return int(platform.split("-")[1])
  22. def get_android_ndk_root(env):
  23. return env["ANDROID_SDK_ROOT"] + "/ndk/" + get_ndk_version()
  24. # This is kept in sync with the value in 'platform/android/java/app/config.gradle'.
  25. def get_ndk_version():
  26. return "23.2.8568313"
  27. def get_flags():
  28. return [
  29. ("arch", "arm64"), # Default for convenience.
  30. ("tools", False),
  31. # Benefits of LTO for Android (size, performance) haven't been clearly established yet.
  32. # So for now we override the default value which may be set when using `production=yes`.
  33. ("lto", "none"),
  34. ]
  35. # Check if Android NDK version is installed
  36. # If not, install it.
  37. def install_ndk_if_needed(env):
  38. print("Checking for Android NDK...")
  39. sdk_root = env["ANDROID_SDK_ROOT"]
  40. if not os.path.exists(get_android_ndk_root(env)):
  41. extension = ".bat" if os.name == "nt" else ""
  42. sdkmanager = sdk_root + "/cmdline-tools/latest/bin/sdkmanager" + extension
  43. if os.path.exists(sdkmanager):
  44. # Install the Android NDK
  45. print("Installing Android NDK...")
  46. ndk_download_args = "ndk;" + get_ndk_version()
  47. subprocess.check_call([sdkmanager, ndk_download_args])
  48. else:
  49. print("Cannot find " + sdkmanager)
  50. print(
  51. "Please ensure ANDROID_SDK_ROOT is correct and cmdline-tools are installed, or install NDK version "
  52. + get_ndk_version()
  53. + " manually."
  54. )
  55. sys.exit()
  56. env["ANDROID_NDK_ROOT"] = get_android_ndk_root(env)
  57. def configure(env):
  58. # Validate arch.
  59. supported_arches = ["x86_32", "x86_64", "arm32", "arm64"]
  60. if env["arch"] not in supported_arches:
  61. print(
  62. 'Unsupported CPU architecture "%s" for Android. Supported architectures are: %s.'
  63. % (env["arch"], ", ".join(supported_arches))
  64. )
  65. sys.exit()
  66. install_ndk_if_needed(env)
  67. ndk_root = env["ANDROID_NDK_ROOT"]
  68. # Architecture
  69. if get_min_sdk_version(env["ndk_platform"]) < 21 and env["arch"] in ["x86_64", "arm64"]:
  70. print(
  71. 'WARNING: arch="%s" is not supported with "ndk_platform" lower than "android-21". Forcing platform 21.'
  72. % env["arch"]
  73. )
  74. env["ndk_platform"] = "android-21"
  75. if env["arch"] == "arm32":
  76. target_triple = "armv7a-linux-androideabi"
  77. env.extra_suffix = ".armv7" + env.extra_suffix
  78. elif env["arch"] == "arm64":
  79. target_triple = "aarch64-linux-android"
  80. env.extra_suffix = ".armv8" + env.extra_suffix
  81. elif env["arch"] == "x86_32":
  82. target_triple = "i686-linux-android"
  83. env.extra_suffix = ".x86" + env.extra_suffix
  84. elif env["arch"] == "x86_64":
  85. target_triple = "x86_64-linux-android"
  86. env.extra_suffix = ".x86_64" + env.extra_suffix
  87. target_option = ["-target", target_triple + str(get_min_sdk_version(env["ndk_platform"]))]
  88. env.Append(ASFLAGS=[target_option, "-c"])
  89. env.Append(CCFLAGS=target_option)
  90. env.Append(LINKFLAGS=target_option)
  91. # Build type
  92. if env["target"].startswith("release"):
  93. if env["optimize"] == "speed": # optimize for speed (default)
  94. # `-O2` is more friendly to debuggers than `-O3`, leading to better crash backtraces
  95. # when using `target=release_debug`.
  96. opt = "-O3" if env["target"] == "release" else "-O2"
  97. env.Append(CCFLAGS=[opt, "-fomit-frame-pointer"])
  98. elif env["optimize"] == "size": # optimize for size
  99. env.Append(CCFLAGS=["-Oz"])
  100. env.Append(CPPDEFINES=["NDEBUG"])
  101. env.Append(CCFLAGS=["-ftree-vectorize"])
  102. elif env["target"] == "debug":
  103. env.Append(LINKFLAGS=["-O0"])
  104. env.Append(CCFLAGS=["-O0", "-g", "-fno-limit-debug-info"])
  105. env.Append(CPPDEFINES=["_DEBUG"])
  106. env.Append(CPPFLAGS=["-UNDEBUG"])
  107. # LTO
  108. if env["lto"] != "none":
  109. if env["lto"] == "thin":
  110. env.Append(CCFLAGS=["-flto=thin"])
  111. env.Append(LINKFLAGS=["-flto=thin"])
  112. else:
  113. env.Append(CCFLAGS=["-flto"])
  114. env.Append(LINKFLAGS=["-flto"])
  115. # Compiler configuration
  116. env["SHLIBSUFFIX"] = ".so"
  117. if env["PLATFORM"] == "win32":
  118. env.use_windows_spawn_fix()
  119. if sys.platform.startswith("linux"):
  120. host_subpath = "linux-x86_64"
  121. elif sys.platform.startswith("darwin"):
  122. host_subpath = "darwin-x86_64"
  123. elif sys.platform.startswith("win"):
  124. if platform.machine().endswith("64"):
  125. host_subpath = "windows-x86_64"
  126. else:
  127. host_subpath = "windows"
  128. toolchain_path = ndk_root + "/toolchains/llvm/prebuilt/" + host_subpath
  129. compiler_path = toolchain_path + "/bin"
  130. env["CC"] = compiler_path + "/clang"
  131. env["CXX"] = compiler_path + "/clang++"
  132. env["AR"] = compiler_path + "/llvm-ar"
  133. env["RANLIB"] = compiler_path + "/llvm-ranlib"
  134. env["AS"] = compiler_path + "/clang"
  135. # Disable exceptions and rtti on non-tools (template) builds
  136. if env["tools"]:
  137. env.Append(CXXFLAGS=["-frtti"])
  138. elif env["builtin_icu"]:
  139. env.Append(CXXFLAGS=["-frtti", "-fno-exceptions"])
  140. else:
  141. env.Append(CXXFLAGS=["-fno-rtti", "-fno-exceptions"])
  142. # Don't use dynamic_cast, necessary with no-rtti.
  143. env.Append(CPPDEFINES=["NO_SAFE_CAST"])
  144. env.Append(
  145. CCFLAGS=(
  146. "-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden -fno-strict-aliasing".split()
  147. )
  148. )
  149. env.Append(CPPDEFINES=["NO_STATVFS", "GLES_ENABLED"])
  150. if get_min_sdk_version(env["ndk_platform"]) >= 24:
  151. env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
  152. if env["arch"] == "x86_32":
  153. # The NDK adds this if targeting API < 24, so we can drop it when Godot targets it at least
  154. env.Append(CCFLAGS=["-mstackrealign"])
  155. elif env["arch"] == "arm32":
  156. env.Append(CCFLAGS="-march=armv7-a -mfloat-abi=softfp".split())
  157. env.Append(CPPDEFINES=["__ARM_ARCH_7__", "__ARM_ARCH_7A__"])
  158. env.Append(CPPDEFINES=["__ARM_NEON__"])
  159. elif env["arch"] == "arm64":
  160. env.Append(CCFLAGS=["-mfix-cortex-a53-835769"])
  161. env.Append(CPPDEFINES=["__ARM_ARCH_8A__"])
  162. # Link flags
  163. env.Append(LINKFLAGS="-Wl,--gc-sections -Wl,--no-undefined -Wl,-z,now".split())
  164. env.Append(LINKFLAGS="-Wl,-soname,libgodot_android.so")
  165. env.Prepend(CPPPATH=["#platform/android"])
  166. env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED", "NO_FCNTL"])
  167. env.Append(LIBS=["OpenSLES", "EGL", "GLESv2", "android", "log", "z", "dl"])
  168. if env["vulkan"]:
  169. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  170. if not env["use_volk"]:
  171. env.Append(LIBS=["vulkan"])