detect.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import os
  2. import sys
  3. import platform
  4. from distutils.version import LooseVersion
  5. def is_active():
  6. return True
  7. def get_name():
  8. return "Android"
  9. def can_build():
  10. return ("ANDROID_SDK_ROOT" in os.environ) or ("ANDROID_HOME" in os.environ)
  11. def get_platform(platform):
  12. return int(platform.split("-")[1])
  13. def get_opts():
  14. from SCons.Variables import BoolVariable, EnumVariable
  15. return [
  16. ("ANDROID_NDK_ROOT", "Path to the Android NDK", get_android_ndk_root()),
  17. ("ANDROID_SDK_ROOT", "Path to the Android SDK", get_android_sdk_root()),
  18. ("ndk_platform", 'Target platform (android-<api>, e.g. "android-24")', "android-24"),
  19. EnumVariable("android_arch", "Target architecture", "arm64v8", ("armv7", "arm64v8", "x86", "x86_64")),
  20. ]
  21. # Return the ANDROID_SDK_ROOT environment variable.
  22. # While ANDROID_HOME has been deprecated, it's used as a fallback for backward
  23. # compatibility purposes.
  24. def get_android_sdk_root():
  25. if "ANDROID_SDK_ROOT" in os.environ:
  26. return os.environ.get("ANDROID_SDK_ROOT", 0)
  27. else:
  28. return os.environ.get("ANDROID_HOME", 0)
  29. # Return the ANDROID_NDK_ROOT environment variable.
  30. # We generate one for this build using the ANDROID_SDK_ROOT env
  31. # variable and the project ndk version.
  32. # If the env variable is already defined, we override it with
  33. # our own to match what the project expects.
  34. def get_android_ndk_root():
  35. return get_android_sdk_root() + "/ndk/" + get_project_ndk_version()
  36. def get_flags():
  37. return [
  38. ("tools", False),
  39. ]
  40. def create(env):
  41. tools = env["TOOLS"]
  42. if "mingw" in tools:
  43. tools.remove("mingw")
  44. if "applelink" in tools:
  45. tools.remove("applelink")
  46. env.Tool("gcc")
  47. return env.Clone(tools=tools)
  48. # Check if ANDROID_NDK_ROOT is valid.
  49. # If not, install the ndk using ANDROID_SDK_ROOT and sdkmanager.
  50. def install_ndk_if_needed(env):
  51. print("Checking for Android NDK...")
  52. env_ndk_version = get_env_ndk_version(env["ANDROID_NDK_ROOT"])
  53. if env_ndk_version is None:
  54. # Reinstall the ndk and update ANDROID_NDK_ROOT.
  55. print("Installing Android NDK...")
  56. if env["ANDROID_SDK_ROOT"] is None:
  57. raise Exception("Invalid ANDROID_SDK_ROOT environment variable.")
  58. import subprocess
  59. extension = ".bat" if os.name == "nt" else ""
  60. sdkmanager_path = env["ANDROID_SDK_ROOT"] + "/cmdline-tools/latest/bin/sdkmanager" + extension
  61. ndk_download_args = "ndk;" + get_project_ndk_version()
  62. subprocess.check_call([sdkmanager_path, ndk_download_args])
  63. env["ANDROID_NDK_ROOT"] = env["ANDROID_SDK_ROOT"] + "/ndk/" + get_project_ndk_version()
  64. print("ANDROID_NDK_ROOT: " + env["ANDROID_NDK_ROOT"])
  65. def configure(env):
  66. install_ndk_if_needed(env)
  67. # Workaround for MinGW. See:
  68. # https://www.scons.org/wiki/LongCmdLinesOnWin32
  69. if os.name == "nt":
  70. import subprocess
  71. def mySubProcess(cmdline, env):
  72. # print("SPAWNED : " + cmdline)
  73. startupinfo = subprocess.STARTUPINFO()
  74. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  75. proc = subprocess.Popen(
  76. cmdline,
  77. stdin=subprocess.PIPE,
  78. stdout=subprocess.PIPE,
  79. stderr=subprocess.PIPE,
  80. startupinfo=startupinfo,
  81. shell=False,
  82. env=env,
  83. )
  84. data, err = proc.communicate()
  85. rv = proc.wait()
  86. if rv:
  87. print("=====")
  88. print(err)
  89. print("=====")
  90. return rv
  91. def mySpawn(sh, escape, cmd, args, env):
  92. newargs = " ".join(args[1:])
  93. cmdline = cmd + " " + newargs
  94. rv = 0
  95. if len(cmdline) > 32000 and cmd.endswith("ar"):
  96. cmdline = cmd + " " + args[1] + " " + args[2] + " "
  97. for i in range(3, len(args)):
  98. rv = mySubProcess(cmdline + args[i], env)
  99. if rv:
  100. break
  101. else:
  102. rv = mySubProcess(cmdline, env)
  103. return rv
  104. env["SPAWN"] = mySpawn
  105. # Architecture
  106. if env["android_arch"] not in ["armv7", "arm64v8", "x86", "x86_64"]:
  107. env["android_arch"] = "armv7"
  108. print("Building for Android, platform " + env["ndk_platform"] + " (" + env["android_arch"] + ")")
  109. can_vectorize = True
  110. if env["android_arch"] == "x86":
  111. env["ARCH"] = "arch-x86"
  112. env.extra_suffix = ".x86" + env.extra_suffix
  113. target_subpath = "x86-4.9"
  114. abi_subpath = "i686-linux-android"
  115. arch_subpath = "x86"
  116. env["x86_libtheora_opt_gcc"] = True
  117. if env["android_arch"] == "x86_64":
  118. if get_platform(env["ndk_platform"]) < 21:
  119. print(
  120. "WARNING: android_arch=x86_64 is not supported by ndk_platform lower than android-21; setting"
  121. " ndk_platform=android-21"
  122. )
  123. env["ndk_platform"] = "android-21"
  124. env["ARCH"] = "arch-x86_64"
  125. env.extra_suffix = ".x86_64" + env.extra_suffix
  126. target_subpath = "x86_64-4.9"
  127. abi_subpath = "x86_64-linux-android"
  128. arch_subpath = "x86_64"
  129. env["x86_libtheora_opt_gcc"] = True
  130. elif env["android_arch"] == "armv7":
  131. env["ARCH"] = "arch-arm"
  132. target_subpath = "arm-linux-androideabi-4.9"
  133. abi_subpath = "arm-linux-androideabi"
  134. arch_subpath = "armeabi-v7a"
  135. env.extra_suffix = ".armv7" + env.extra_suffix
  136. elif env["android_arch"] == "arm64v8":
  137. if get_platform(env["ndk_platform"]) < 21:
  138. print(
  139. "WARNING: android_arch=arm64v8 is not supported by ndk_platform lower than android-21; setting"
  140. " ndk_platform=android-21"
  141. )
  142. env["ndk_platform"] = "android-21"
  143. env["ARCH"] = "arch-arm64"
  144. target_subpath = "aarch64-linux-android-4.9"
  145. abi_subpath = "aarch64-linux-android"
  146. arch_subpath = "arm64-v8a"
  147. env.extra_suffix = ".armv8" + env.extra_suffix
  148. # Build type
  149. if env["target"].startswith("release"):
  150. if env["optimize"] == "speed": # optimize for speed (default)
  151. env.Append(LINKFLAGS=["-O2"])
  152. env.Append(CCFLAGS=["-O2", "-fomit-frame-pointer"])
  153. elif env["optimize"] == "size": # optimize for size
  154. env.Append(CCFLAGS=["-Os"])
  155. env.Append(LINKFLAGS=["-Os"])
  156. env.Append(CPPDEFINES=["NDEBUG"])
  157. if can_vectorize:
  158. env.Append(CCFLAGS=["-ftree-vectorize"])
  159. if env["target"] == "release_debug":
  160. env.Append(CPPDEFINES=["DEBUG_ENABLED"])
  161. elif env["target"] == "debug":
  162. env.Append(LINKFLAGS=["-O0"])
  163. env.Append(CCFLAGS=["-O0", "-g", "-fno-limit-debug-info"])
  164. env.Append(CPPDEFINES=["_DEBUG", "DEBUG_ENABLED"])
  165. env.Append(CPPDEFINES=["DEV_ENABLED"])
  166. env.Append(CPPFLAGS=["-UNDEBUG"])
  167. # Compiler configuration
  168. env["SHLIBSUFFIX"] = ".so"
  169. if env["PLATFORM"] == "win32":
  170. env.Tool("gcc")
  171. env.use_windows_spawn_fix()
  172. if sys.platform.startswith("linux"):
  173. host_subpath = "linux-x86_64"
  174. elif sys.platform.startswith("darwin"):
  175. host_subpath = "darwin-x86_64"
  176. elif sys.platform.startswith("win"):
  177. if platform.machine().endswith("64"):
  178. host_subpath = "windows-x86_64"
  179. else:
  180. host_subpath = "windows"
  181. compiler_path = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/" + host_subpath + "/bin"
  182. gcc_toolchain_path = env["ANDROID_NDK_ROOT"] + "/toolchains/" + target_subpath + "/prebuilt/" + host_subpath
  183. tools_path = gcc_toolchain_path + "/" + abi_subpath + "/bin"
  184. # For Clang to find NDK tools in preference of those system-wide
  185. env.PrependENVPath("PATH", tools_path)
  186. ccache_path = os.environ.get("CCACHE")
  187. if ccache_path is None:
  188. env["CC"] = compiler_path + "/clang"
  189. env["CXX"] = compiler_path + "/clang++"
  190. else:
  191. # there aren't any ccache wrappers available for Android,
  192. # to enable caching we need to prepend the path to the ccache binary
  193. env["CC"] = ccache_path + " " + compiler_path + "/clang"
  194. env["CXX"] = ccache_path + " " + compiler_path + "/clang++"
  195. env["AR"] = tools_path + "/ar"
  196. env["RANLIB"] = tools_path + "/ranlib"
  197. env["AS"] = tools_path + "/as"
  198. common_opts = ["-gcc-toolchain", gcc_toolchain_path]
  199. # Compile flags
  200. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/include"])
  201. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++abi/include"])
  202. # Disable exceptions and rtti on non-tools (template) builds
  203. if env["tools"]:
  204. env.Append(CXXFLAGS=["-frtti"])
  205. elif env["builtin_icu"]:
  206. env.Append(CXXFLAGS=["-frtti", "-fno-exceptions"])
  207. else:
  208. env.Append(CXXFLAGS=["-fno-rtti", "-fno-exceptions"])
  209. # Don't use dynamic_cast, necessary with no-rtti.
  210. env.Append(CPPDEFINES=["NO_SAFE_CAST"])
  211. lib_sysroot = env["ANDROID_NDK_ROOT"] + "/platforms/" + env["ndk_platform"] + "/" + env["ARCH"]
  212. # Using NDK unified headers (NDK r15+)
  213. sysroot = env["ANDROID_NDK_ROOT"] + "/sysroot"
  214. env.Append(CPPFLAGS=["--sysroot=" + sysroot])
  215. env.Append(CPPFLAGS=["-isystem", sysroot + "/usr/include/" + abi_subpath])
  216. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/android/support/include"])
  217. # For unified headers this define has to be set manually
  218. env.Append(CPPDEFINES=[("__ANDROID_API__", str(get_platform(env["ndk_platform"])))])
  219. env.Append(
  220. CCFLAGS=(
  221. "-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden"
  222. " -fno-strict-aliasing".split()
  223. )
  224. )
  225. env.Append(CPPDEFINES=["NO_STATVFS", "GLES_ENABLED"])
  226. if get_platform(env["ndk_platform"]) >= 24:
  227. env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
  228. if env["android_arch"] == "x86":
  229. target_opts = ["-target", "i686-none-linux-android"]
  230. # The NDK adds this if targeting API < 21, so we can drop it when Godot targets it at least
  231. env.Append(CCFLAGS=["-mstackrealign"])
  232. elif env["android_arch"] == "x86_64":
  233. target_opts = ["-target", "x86_64-none-linux-android"]
  234. elif env["android_arch"] == "armv7":
  235. target_opts = ["-target", "armv7-none-linux-androideabi"]
  236. env.Append(CCFLAGS="-march=armv7-a -mfloat-abi=softfp".split())
  237. env.Append(CPPDEFINES=["__ARM_ARCH_7__", "__ARM_ARCH_7A__"])
  238. # Enable ARM NEON instructions to compile more optimized code.
  239. env.Append(CCFLAGS=["-mfpu=neon"])
  240. env.Append(CPPDEFINES=["__ARM_NEON__"])
  241. elif env["android_arch"] == "arm64v8":
  242. target_opts = ["-target", "aarch64-none-linux-android"]
  243. env.Append(CCFLAGS=["-mfix-cortex-a53-835769"])
  244. env.Append(CPPDEFINES=["__ARM_ARCH_8A__"])
  245. env.Append(CCFLAGS=target_opts)
  246. env.Append(CCFLAGS=common_opts)
  247. # Link flags
  248. ndk_version = get_env_ndk_version(env["ANDROID_NDK_ROOT"])
  249. if ndk_version != None and LooseVersion(ndk_version) >= LooseVersion("17.1.4828580"):
  250. env.Append(LINKFLAGS=["-Wl,--exclude-libs,libgcc.a", "-Wl,--exclude-libs,libatomic.a", "-nostdlib++"])
  251. else:
  252. env.Append(
  253. LINKFLAGS=[
  254. env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/libs/" + arch_subpath + "/libandroid_support.a"
  255. ]
  256. )
  257. env.Append(LINKFLAGS=["-shared", "--sysroot=" + lib_sysroot, "-Wl,--warn-shared-textrel"])
  258. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/libs/" + arch_subpath + "/"])
  259. env.Append(
  260. LINKFLAGS=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/libs/" + arch_subpath + "/libc++_shared.so"]
  261. )
  262. if env["android_arch"] == "armv7":
  263. env.Append(LINKFLAGS="-Wl,--fix-cortex-a8".split())
  264. env.Append(LINKFLAGS="-Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now".split())
  265. env.Append(LINKFLAGS="-Wl,-soname,libgodot_android.so -Wl,--gc-sections".split())
  266. env.Append(LINKFLAGS=target_opts)
  267. env.Append(LINKFLAGS=common_opts)
  268. env.Append(
  269. LIBPATH=[
  270. env["ANDROID_NDK_ROOT"]
  271. + "/toolchains/"
  272. + target_subpath
  273. + "/prebuilt/"
  274. + host_subpath
  275. + "/lib/gcc/"
  276. + abi_subpath
  277. + "/4.9.x"
  278. ]
  279. )
  280. env.Append(
  281. LIBPATH=[
  282. env["ANDROID_NDK_ROOT"]
  283. + "/toolchains/"
  284. + target_subpath
  285. + "/prebuilt/"
  286. + host_subpath
  287. + "/"
  288. + abi_subpath
  289. + "/lib"
  290. ]
  291. )
  292. env.Prepend(CPPPATH=["#platform/android"])
  293. env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED", "NO_FCNTL"])
  294. env.Append(LIBS=["OpenSLES", "EGL", "GLESv2", "android", "log", "z", "dl"])
  295. if env["vulkan"]:
  296. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  297. if not env["use_volk"]:
  298. env.Append(LIBS=["vulkan"])
  299. # Return the project NDK version.
  300. # This is kept in sync with the value in 'platform/android/java/app/config.gradle'.
  301. def get_project_ndk_version():
  302. return "21.4.7075529"
  303. # Return NDK version string in source.properties (adapted from the Chromium project).
  304. def get_env_ndk_version(path):
  305. if path is None:
  306. return None
  307. prop_file_path = os.path.join(path, "source.properties")
  308. try:
  309. with open(prop_file_path) as prop_file:
  310. for line in prop_file:
  311. key_value = list(map(lambda x: x.strip(), line.split("=")))
  312. if key_value[0] == "Pkg.Revision":
  313. return key_value[1]
  314. except Exception:
  315. print("Could not read source prop file '%s'" % prop_file_path)
  316. return None