detect.py 11 KB

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