detect.py 12 KB

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