detect.py 12 KB

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