detect.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import os
  2. import sys
  3. import string
  4. import platform
  5. from distutils.version import LooseVersion
  6. def is_active():
  7. return True
  8. def get_name():
  9. return "Android"
  10. def can_build():
  11. return ("ANDROID_NDK_ROOT" in os.environ)
  12. def get_platform(platform):
  13. return int(platform.split("-")[1])
  14. def get_opts():
  15. return [
  16. ('ANDROID_NDK_ROOT', 'the path to Android NDK',
  17. os.environ.get("ANDROID_NDK_ROOT", 0)),
  18. ('ndk_platform', 'compile for platform: (android-<api> , example: android-18)', "android-18"),
  19. ('android_arch', 'select compiler architecture: (armv7/armv6/arm64v8/x86/x86_64)', "armv7"),
  20. ('android_neon', 'enable neon (armv7 only)', "yes"),
  21. ('android_stl', 'enable STL support in android port (for modules)', "no")
  22. ]
  23. def get_flags():
  24. return [
  25. ('tools', 'no'),
  26. ]
  27. def create(env):
  28. tools = env['TOOLS']
  29. if "mingw" in tools:
  30. tools.remove('mingw')
  31. if "applelink" in tools:
  32. tools.remove("applelink")
  33. env.Tool('gcc')
  34. return env.Clone(tools=tools)
  35. def configure(env):
  36. # Workaround for MinGW. See:
  37. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  38. import os
  39. if (os.name == "nt"):
  40. import subprocess
  41. def mySubProcess(cmdline, env):
  42. # print("SPAWNED : " + cmdline)
  43. startupinfo = subprocess.STARTUPINFO()
  44. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  45. proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  46. stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, env=env)
  47. data, err = proc.communicate()
  48. rv = proc.wait()
  49. if rv:
  50. print("=====")
  51. print(err)
  52. print("=====")
  53. return rv
  54. def mySpawn(sh, escape, cmd, args, env):
  55. newargs = ' '.join(args[1:])
  56. cmdline = cmd + " " + newargs
  57. rv = 0
  58. if len(cmdline) > 32000 and cmd.endswith("ar"):
  59. cmdline = cmd + " " + args[1] + " " + args[2] + " "
  60. for i in range(3, len(args)):
  61. rv = mySubProcess(cmdline + args[i], env)
  62. if rv:
  63. break
  64. else:
  65. rv = mySubProcess(cmdline, env)
  66. return rv
  67. env['SPAWN'] = mySpawn
  68. if env['android_arch'] not in ['armv7', 'armv6', 'arm64v8', 'x86', 'x86_64']:
  69. env['android_arch'] = 'armv7'
  70. if env['PLATFORM'] == 'win32':
  71. env.Tool('gcc')
  72. env['SHLIBSUFFIX'] = '.so'
  73. neon_text = ""
  74. if env["android_arch"] == "armv7" and env['android_neon'] == 'yes':
  75. neon_text = " (with neon)"
  76. print("Godot Android!!!!! (" + env['android_arch'] + ")" + neon_text)
  77. env.Append(CPPPATH=['#platform/android'])
  78. if env['android_arch'] == 'x86':
  79. env['ARCH'] = 'arch-x86'
  80. env.extra_suffix = ".x86" + env.extra_suffix
  81. target_subpath = "x86-4.9"
  82. abi_subpath = "i686-linux-android"
  83. arch_subpath = "x86"
  84. env["x86_libtheora_opt_gcc"] = True
  85. elif env['android_arch'] == 'x86_64':
  86. if get_platform(env['ndk_platform']) < 21:
  87. print("WARNING: android_arch=x86_64 is not supported by ndk_platform lower than android-21; setting ndk_platform=android-21")
  88. env['ndk_platform'] = "android-21"
  89. env['ARCH'] = 'arch-x86_64'
  90. env.extra_suffix = ".x86_64" + env.extra_suffix
  91. target_subpath = "x86_64-4.9"
  92. abi_subpath = "x86_64-linux-android"
  93. arch_subpath = "x86_64"
  94. env["x86_libtheora_opt_gcc"] = True
  95. elif env['android_arch'] == 'armv6':
  96. env['ARCH'] = 'arch-arm'
  97. env.extra_suffix = ".armv6" + env.extra_suffix
  98. target_subpath = "arm-linux-androideabi-4.9"
  99. abi_subpath = "arm-linux-androideabi"
  100. arch_subpath = "armeabi"
  101. elif env["android_arch"] == "armv7":
  102. env['ARCH'] = 'arch-arm'
  103. target_subpath = "arm-linux-androideabi-4.9"
  104. abi_subpath = "arm-linux-androideabi"
  105. arch_subpath = "armeabi-v7a"
  106. if env['android_neon'] == 'yes':
  107. env.extra_suffix = ".armv7.neon" + env.extra_suffix
  108. else:
  109. env.extra_suffix = ".armv7" + env.extra_suffix
  110. elif env["android_arch"] == "arm64v8":
  111. if get_platform(env['ndk_platform']) < 21:
  112. print("WARNING: android_arch=arm64v8 is not supported by ndk_platform lower than android-21; setting ndk_platform=android-21")
  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. mt_link = True
  120. if (sys.platform.startswith("linux")):
  121. host_subpath = "linux-x86_64"
  122. elif (sys.platform.startswith("darwin")):
  123. host_subpath = "darwin-x86_64"
  124. elif (sys.platform.startswith('win')):
  125. if (platform.machine().endswith('64')):
  126. host_subpath = "windows-x86_64"
  127. else:
  128. mt_link = False
  129. host_subpath = "windows"
  130. if env["android_arch"] == "arm64v8":
  131. mt_link = False
  132. compiler_path = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/" + host_subpath + "/bin"
  133. gcc_toolchain_path = env["ANDROID_NDK_ROOT"] + "/toolchains/" + target_subpath + "/prebuilt/" + host_subpath
  134. tools_path = gcc_toolchain_path + "/" + abi_subpath + "/bin"
  135. # For Clang to find NDK tools in preference of those system-wide
  136. env.PrependENVPath('PATH', tools_path)
  137. ccache_path = os.environ.get("CCACHE")
  138. if ccache_path == None:
  139. env['CC'] = compiler_path + '/clang'
  140. env['CXX'] = compiler_path + '/clang++'
  141. else:
  142. # there aren't any ccache wrappers available for Android,
  143. # to enable caching we need to prepend the path to the ccache binary
  144. env['CC'] = ccache_path + ' ' + compiler_path + '/clang'
  145. env['CXX'] = ccache_path + ' ' + compiler_path + '/clang++'
  146. env['AR'] = tools_path + "/ar"
  147. env['RANLIB'] = tools_path + "/ranlib"
  148. env['AS'] = tools_path + "/as"
  149. common_opts = ['-fno-integrated-as', '-gcc-toolchain', gcc_toolchain_path]
  150. if env['android_stl'] == 'yes':
  151. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/include"])
  152. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++abi/include"])
  153. env.Append(CXXFLAGS=['-frtti',"-std=gnu++14"])
  154. else:
  155. env.Append(CXXFLAGS=['-fno-rtti', '-fno-exceptions', '-DNO_SAFE_CAST'])
  156. lib_sysroot = env["ANDROID_NDK_ROOT"] + "/platforms/" + env['ndk_platform'] + "/" + env['ARCH']
  157. ndk_version = get_ndk_version(env["ANDROID_NDK_ROOT"])
  158. if ndk_version != None and LooseVersion(ndk_version) >= LooseVersion("15.0.4075724"):
  159. print("Using NDK unified headers")
  160. sysroot = env["ANDROID_NDK_ROOT"] + "/sysroot"
  161. env.Append(CPPFLAGS=["--sysroot="+sysroot])
  162. env.Append(CPPFLAGS=["-isystem", sysroot + "/usr/include/" + abi_subpath])
  163. env.Append(CPPFLAGS=["-isystem", env["ANDROID_NDK_ROOT"] + "/sources/android/support/include"])
  164. # For unified headers this define has to be set manually
  165. env.Append(CPPFLAGS=["-D__ANDROID_API__=" + str(get_platform(env['ndk_platform']))])
  166. else:
  167. print("Using NDK deprecated headers")
  168. env.Append(CPPFLAGS=["-isystem", lib_sysroot + "/usr/include"])
  169. env.Append(CPPFLAGS='-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden -fno-strict-aliasing'.split())
  170. env.Append(CPPFLAGS='-DNO_STATVFS -DGLES2_ENABLED'.split())
  171. env['neon_enabled'] = False
  172. if env['android_arch'] == 'x86':
  173. can_vectorize = True
  174. target_opts = ['-target', 'i686-none-linux-android']
  175. # The NDK adds this if targeting API < 21, so we can drop it when Godot targets it at least
  176. env.Append(CPPFLAGS=['-mstackrealign'])
  177. elif env['android_arch'] == 'x86_64':
  178. can_vectorize = True
  179. target_opts = ['-target', 'x86_64-none-linux-android']
  180. elif env["android_arch"] == "armv6":
  181. can_vectorize = False
  182. target_opts = ['-target', 'armv6-none-linux-androideabi']
  183. env.Append(CPPFLAGS='-D__ARM_ARCH_6__ -march=armv6 -mfpu=vfp -mfloat-abi=softfp'.split())
  184. elif env["android_arch"] == "armv7":
  185. can_vectorize = True
  186. target_opts = ['-target', 'armv7-none-linux-androideabi']
  187. env.Append(CPPFLAGS='-D__ARM_ARCH_7__ -D__ARM_ARCH_7A__ -march=armv7-a -mfloat-abi=softfp'.split())
  188. if env['android_neon'] == 'yes':
  189. env['neon_enabled'] = True
  190. env.Append(CPPFLAGS=['-mfpu=neon', '-D__ARM_NEON__'])
  191. else:
  192. env.Append(CPPFLAGS=['-mfpu=vfpv3-d16'])
  193. elif env["android_arch"] == "arm64v8":
  194. can_vectorize = True
  195. target_opts = ['-target', 'aarch64-none-linux-android']
  196. env.Append(CPPFLAGS=['-D__ARM_ARCH_8A__'])
  197. env.Append(CPPFLAGS=['-mfix-cortex-a53-835769'])
  198. env.Append(CPPFLAGS=target_opts)
  199. env.Append(CPPFLAGS=common_opts)
  200. env.Append(LIBS=['OpenSLES'])
  201. env.Append(LIBS=['EGL', 'OpenSLES', 'android'])
  202. env.Append(LIBS=['log', 'GLESv1_CM', 'GLESv2', 'z'])
  203. if (sys.platform.startswith("darwin")):
  204. env['SHLIBSUFFIX'] = '.so'
  205. if ndk_version != None and LooseVersion(ndk_version) >= LooseVersion("15.0.4075724"):
  206. if env['android_stl'] == 'yes':
  207. if LooseVersion(ndk_version) >= LooseVersion("17.1.4828580"):
  208. env.Append(LINKFLAGS=['-Wl,--exclude-libs,libgcc.a','-Wl,--exclude-libs,libatomic.a','-nostdlib++'])
  209. else:
  210. env.Append(LINKFLAGS=[env["ANDROID_NDK_ROOT"] +"/sources/cxx-stl/llvm-libc++/libs/"+arch_subpath+"/libandroid_support.a"])
  211. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/llvm-libc++/libs/"+arch_subpath+"/"])
  212. env.Append(LINKFLAGS=[env["ANDROID_NDK_ROOT"] +"/sources/cxx-stl/llvm-libc++/libs/"+arch_subpath+"/libc++_shared.so"])
  213. else:
  214. # This is the legacy and minimal 'System STL' with support for basic features like new and delete
  215. env.Append(LINKFLAGS=['-stdlib=libstdc++'])
  216. else:
  217. if mt_link:
  218. env.Append(LINKFLAGS=['-Wl,--threads'])
  219. env.Append(LINKFLAGS=['-shared', '--sysroot=' + lib_sysroot, '-Wl,--warn-shared-textrel'])
  220. if env["android_arch"] == "armv7":
  221. env.Append(LINKFLAGS='-Wl,--fix-cortex-a8'.split())
  222. env.Append(LINKFLAGS='-Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now'.split())
  223. env.Append(LINKFLAGS='-Wl,-soname,libgodot_android.so -Wl,--gc-sections'.split())
  224. env.Append(LINKFLAGS=target_opts)
  225. env.Append(LINKFLAGS=common_opts)
  226. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + '/toolchains/' + target_subpath + '/prebuilt/' +
  227. host_subpath + '/lib/gcc/' + abi_subpath + '/4.9.x'])
  228. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] +
  229. '/toolchains/' + target_subpath + '/prebuilt/' + host_subpath + '/' + abi_subpath + '/lib'])
  230. if (env["target"].startswith("release")):
  231. env.Append(LINKFLAGS=['-O2'])
  232. env.Append(CPPFLAGS=['-O2', '-DNDEBUG', '-ffast-math',
  233. '-funsafe-math-optimizations', '-fomit-frame-pointer'])
  234. if (can_vectorize):
  235. env.Append(CPPFLAGS=['-ftree-vectorize'])
  236. if (env["target"] == "release_debug"):
  237. env.Append(CPPFLAGS=['-DDEBUG_ENABLED'])
  238. elif (env["target"] == "debug"):
  239. env.Append(LINKFLAGS=['-O0'])
  240. env.Append(CPPFLAGS=['-O0', '-D_DEBUG', '-UNDEBUG', '-DDEBUG_ENABLED',
  241. '-DDEBUG_MEMORY_ENABLED', '-g', '-fno-limit-debug-info'])
  242. env.Append(CPPFLAGS=['-DANDROID_ENABLED',
  243. '-DUNIX_ENABLED', '-DNO_FCNTL', '-DMPC_FIXED_POINT'])
  244. # TODO: Move that to opus module's config
  245. if("module_opus_enabled" in env and env["module_opus_enabled"] != "no"):
  246. if (env["android_arch"] == "armv6" or env["android_arch"] == "armv7"):
  247. env.Append(CFLAGS=["-DOPUS_ARM_OPT"])
  248. env.opus_fixed_point = "yes"
  249. import methods
  250. env.Append(BUILDERS={'GLSL120': env.Builder(
  251. action=methods.build_legacygl_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
  252. env.Append(BUILDERS={'GLSL': env.Builder(
  253. action=methods.build_glsl_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
  254. env.Append(BUILDERS={'GLSL120GLES': env.Builder(
  255. action=methods.build_gles2_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
  256. env.use_windows_spawn_fix()
  257. # Return NDK version string in source.properties (adapted from the Chromium project).
  258. def get_ndk_version(path):
  259. if path == None:
  260. return None
  261. prop_file_path = os.path.join(path, "source.properties")
  262. try:
  263. with open(prop_file_path) as prop_file:
  264. for line in prop_file:
  265. key_value = list(map(lambda x: x.strip(), line.split("=")))
  266. if key_value[0] == "Pkg.Revision":
  267. return key_value[1]
  268. except:
  269. print("Could not read source prop file '%s'" % prop_file_path)
  270. return None