detect.py 7.1 KB

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