detect.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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(CPPFLAGS=["-UNDEBUG"])
  166. # Compiler configuration
  167. env["SHLIBSUFFIX"] = ".so"
  168. if env["PLATFORM"] == "win32":
  169. env.Tool("gcc")
  170. env.use_windows_spawn_fix()
  171. if sys.platform.startswith("linux"):
  172. host_subpath = "linux-x86_64"
  173. elif sys.platform.startswith("darwin"):
  174. host_subpath = "darwin-x86_64"
  175. elif sys.platform.startswith("win"):
  176. if platform.machine().endswith("64"):
  177. host_subpath = "windows-x86_64"
  178. else:
  179. host_subpath = "windows"
  180. compiler_path = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/" + host_subpath + "/bin"
  181. gcc_toolchain_path = env["ANDROID_NDK_ROOT"] + "/toolchains/" + target_subpath + "/prebuilt/" + host_subpath
  182. tools_path = gcc_toolchain_path + "/" + abi_subpath + "/bin"
  183. # For Clang to find NDK tools in preference of those system-wide
  184. env.PrependENVPath("PATH", tools_path)
  185. ccache_path = os.environ.get("CCACHE")
  186. if ccache_path is None:
  187. env["CC"] = compiler_path + "/clang"
  188. env["CXX"] = compiler_path + "/clang++"
  189. else:
  190. # there aren't any ccache wrappers available for Android,
  191. # to enable caching we need to prepend the path to the ccache binary
  192. env["CC"] = ccache_path + " " + compiler_path + "/clang"
  193. env["CXX"] = ccache_path + " " + compiler_path + "/clang++"
  194. env["AR"] = tools_path + "/ar"
  195. env["RANLIB"] = tools_path + "/ranlib"
  196. env["AS"] = tools_path + "/as"
  197. common_opts = ["-gcc-toolchain", gcc_toolchain_path]
  198. # Compile flags
  199. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/include"])
  200. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++abi/include"])
  201. # Disable exceptions and rtti on non-tools (template) builds
  202. if env["tools"]:
  203. env.Append(CXXFLAGS=["-frtti"])
  204. elif env["builtin_icu"]:
  205. env.Append(CXXFLAGS=["-frtti", "-fno-exceptions"])
  206. else:
  207. env.Append(CXXFLAGS=["-fno-rtti", "-fno-exceptions"])
  208. # Don't use dynamic_cast, necessary with no-rtti.
  209. env.Append(CPPDEFINES=["NO_SAFE_CAST"])
  210. lib_sysroot = env["ANDROID_NDK_ROOT"] + "/platforms/" + env["ndk_platform"] + "/" + env["ARCH"]
  211. # Using NDK unified headers (NDK r15+)
  212. sysroot = env["ANDROID_NDK_ROOT"] + "/sysroot"
  213. env.Append(CPPFLAGS=["--sysroot=" + sysroot])
  214. env.Append(CPPFLAGS=["-isystem", sysroot + "/usr/include/" + abi_subpath])
  215. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/android/support/include"])
  216. # For unified headers this define has to be set manually
  217. env.Append(CPPDEFINES=[("__ANDROID_API__", str(get_platform(env["ndk_platform"])))])
  218. env.Append(
  219. CCFLAGS=(
  220. "-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden"
  221. " -fno-strict-aliasing".split()
  222. )
  223. )
  224. env.Append(CPPDEFINES=["NO_STATVFS", "GLES_ENABLED"])
  225. if get_platform(env["ndk_platform"]) >= 24:
  226. env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
  227. if env["android_arch"] == "x86":
  228. target_opts = ["-target", "i686-none-linux-android"]
  229. # The NDK adds this if targeting API < 21, so we can drop it when Godot targets it at least
  230. env.Append(CCFLAGS=["-mstackrealign"])
  231. elif env["android_arch"] == "x86_64":
  232. target_opts = ["-target", "x86_64-none-linux-android"]
  233. elif env["android_arch"] == "armv7":
  234. target_opts = ["-target", "armv7-none-linux-androideabi"]
  235. env.Append(CCFLAGS="-march=armv7-a -mfloat-abi=softfp".split())
  236. env.Append(CPPDEFINES=["__ARM_ARCH_7__", "__ARM_ARCH_7A__"])
  237. # Enable ARM NEON instructions to compile more optimized code.
  238. env.Append(CCFLAGS=["-mfpu=neon"])
  239. env.Append(CPPDEFINES=["__ARM_NEON__"])
  240. elif env["android_arch"] == "arm64v8":
  241. target_opts = ["-target", "aarch64-none-linux-android"]
  242. env.Append(CCFLAGS=["-mfix-cortex-a53-835769"])
  243. env.Append(CPPDEFINES=["__ARM_ARCH_8A__"])
  244. env.Append(CCFLAGS=target_opts)
  245. env.Append(CCFLAGS=common_opts)
  246. # Link flags
  247. ndk_version = get_env_ndk_version(env["ANDROID_NDK_ROOT"])
  248. if ndk_version != None and LooseVersion(ndk_version) >= LooseVersion("17.1.4828580"):
  249. env.Append(LINKFLAGS=["-Wl,--exclude-libs,libgcc.a", "-Wl,--exclude-libs,libatomic.a", "-nostdlib++"])
  250. else:
  251. env.Append(
  252. LINKFLAGS=[
  253. env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/libs/" + arch_subpath + "/libandroid_support.a"
  254. ]
  255. )
  256. env.Append(LINKFLAGS=["-shared", "--sysroot=" + lib_sysroot, "-Wl,--warn-shared-textrel"])
  257. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/libs/" + arch_subpath + "/"])
  258. env.Append(
  259. LINKFLAGS=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/libs/" + arch_subpath + "/libc++_shared.so"]
  260. )
  261. if env["android_arch"] == "armv7":
  262. env.Append(LINKFLAGS="-Wl,--fix-cortex-a8".split())
  263. env.Append(LINKFLAGS="-Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now".split())
  264. env.Append(LINKFLAGS="-Wl,-soname,libgodot_android.so -Wl,--gc-sections".split())
  265. env.Append(LINKFLAGS=target_opts)
  266. env.Append(LINKFLAGS=common_opts)
  267. env.Append(
  268. LIBPATH=[
  269. env["ANDROID_NDK_ROOT"]
  270. + "/toolchains/"
  271. + target_subpath
  272. + "/prebuilt/"
  273. + host_subpath
  274. + "/lib/gcc/"
  275. + abi_subpath
  276. + "/4.9.x"
  277. ]
  278. )
  279. env.Append(
  280. LIBPATH=[
  281. env["ANDROID_NDK_ROOT"]
  282. + "/toolchains/"
  283. + target_subpath
  284. + "/prebuilt/"
  285. + host_subpath
  286. + "/"
  287. + abi_subpath
  288. + "/lib"
  289. ]
  290. )
  291. env.Prepend(CPPPATH=["#platform/android"])
  292. env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED", "NO_FCNTL"])
  293. env.Append(LIBS=["OpenSLES", "EGL", "GLESv2", "android", "log", "z", "dl"])
  294. if env["vulkan"]:
  295. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  296. if not env["use_volk"]:
  297. env.Append(LIBS=["vulkan"])
  298. # Return the project NDK version.
  299. # This is kept in sync with the value in 'platform/android/java/app/config.gradle'.
  300. def get_project_ndk_version():
  301. return "21.4.7075529"
  302. # Return NDK version string in source.properties (adapted from the Chromium project).
  303. def get_env_ndk_version(path):
  304. if path is None:
  305. return None
  306. prop_file_path = os.path.join(path, "source.properties")
  307. try:
  308. with open(prop_file_path) as prop_file:
  309. for line in prop_file:
  310. key_value = list(map(lambda x: x.strip(), line.split("=")))
  311. if key_value[0] == "Pkg.Revision":
  312. return key_value[1]
  313. except Exception:
  314. print("Could not read source prop file '%s'" % prop_file_path)
  315. return None