detect.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import os
  2. import sys
  3. import string
  4. import platform
  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_opts():
  12. return [
  13. ('ANDROID_NDK_ROOT', 'Path to the Android NDK', os.environ.get("ANDROID_NDK_ROOT", 0)),
  14. ('ndk_platform', 'Target platform (android-<api>, e.g. "android-18")', "android-18"),
  15. ('android_arch', 'Target architecture (armv7/armv6/arm64v8/x86)', "armv7"),
  16. ('android_neon', 'Enable NEON support (armv7 only)', "yes"),
  17. ('android_stl', 'Enable Android STL support (for modules)', "no")
  18. ]
  19. def get_flags():
  20. return [
  21. ('tools', 'no'),
  22. ]
  23. def create(env):
  24. tools = env['TOOLS']
  25. if "mingw" in tools:
  26. tools.remove('mingw')
  27. if "applelink" in tools:
  28. tools.remove("applelink")
  29. env.Tool('gcc')
  30. return env.Clone(tools=tools)
  31. def configure(env):
  32. # Workaround for MinGW. See:
  33. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  34. if (os.name == "nt"):
  35. import subprocess
  36. def mySubProcess(cmdline, env):
  37. # print("SPAWNED : " + cmdline)
  38. startupinfo = subprocess.STARTUPINFO()
  39. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  40. proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  41. stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, env=env)
  42. data, err = proc.communicate()
  43. rv = proc.wait()
  44. if rv:
  45. print("=====")
  46. print(err)
  47. print("=====")
  48. return rv
  49. def mySpawn(sh, escape, cmd, args, env):
  50. newargs = ' '.join(args[1:])
  51. cmdline = cmd + " " + newargs
  52. rv = 0
  53. if len(cmdline) > 32000 and cmd.endswith("ar"):
  54. cmdline = cmd + " " + args[1] + " " + args[2] + " "
  55. for i in range(3, len(args)):
  56. rv = mySubProcess(cmdline + args[i], env)
  57. if rv:
  58. break
  59. else:
  60. rv = mySubProcess(cmdline, env)
  61. return rv
  62. env['SPAWN'] = mySpawn
  63. ## Architecture
  64. if env['android_arch'] not in ['armv7', 'armv6', 'arm64v8', 'x86']:
  65. env['android_arch'] = 'armv7'
  66. neon_text = ""
  67. if env["android_arch"] == "armv7" and env['android_neon'] == 'yes':
  68. neon_text = " (with NEON)"
  69. print("Building for Android (" + env['android_arch'] + ")" + neon_text)
  70. can_vectorize = True
  71. if env['android_arch'] == 'x86':
  72. env['ARCH'] = 'arch-x86'
  73. env.extra_suffix = ".x86" + env.extra_suffix
  74. target_subpath = "x86-4.9"
  75. abi_subpath = "i686-linux-android"
  76. arch_subpath = "x86"
  77. env["x86_libtheora_opt_gcc"] = True
  78. elif env['android_arch'] == 'armv6':
  79. env['ARCH'] = 'arch-arm'
  80. env.extra_suffix = ".armv6" + env.extra_suffix
  81. target_subpath = "arm-linux-androideabi-4.9"
  82. abi_subpath = "arm-linux-androideabi"
  83. arch_subpath = "armeabi"
  84. can_vectorize = False
  85. elif env["android_arch"] == "armv7":
  86. env['ARCH'] = 'arch-arm'
  87. target_subpath = "arm-linux-androideabi-4.9"
  88. abi_subpath = "arm-linux-androideabi"
  89. arch_subpath = "armeabi-v7a"
  90. if env['android_neon'] == 'yes':
  91. env.extra_suffix = ".armv7.neon" + env.extra_suffix
  92. else:
  93. env.extra_suffix = ".armv7" + env.extra_suffix
  94. elif env["android_arch"] == "arm64v8":
  95. env['ARCH'] = 'arch-arm64'
  96. target_subpath = "aarch64-linux-android-4.9"
  97. abi_subpath = "aarch64-linux-android"
  98. arch_subpath = "arm64-v8a"
  99. env.extra_suffix = ".armv8" + env.extra_suffix
  100. ## Build type
  101. if (env["target"].startswith("release")):
  102. env.Append(LINKFLAGS=['-O2'])
  103. env.Append(CPPFLAGS=['-O2', '-DNDEBUG', '-ffast-math', '-funsafe-math-optimizations', '-fomit-frame-pointer'])
  104. if (can_vectorize):
  105. env.Append(CPPFLAGS=['-ftree-vectorize'])
  106. if (env["target"] == "release_debug"):
  107. env.Append(CPPFLAGS=['-DDEBUG_ENABLED'])
  108. elif (env["target"] == "debug"):
  109. env.Append(LINKFLAGS=['-O0'])
  110. env.Append(CPPFLAGS=['-O0', '-D_DEBUG', '-UNDEBUG', '-DDEBUG_ENABLED',
  111. '-DDEBUG_MEMORY_ENABLED', '-g', '-fno-limit-debug-info'])
  112. ## Compiler configuration
  113. env['SHLIBSUFFIX'] = '.so'
  114. if env['PLATFORM'] == 'win32':
  115. env.Tool('gcc')
  116. env.use_windows_spawn_fix()
  117. mt_link = True
  118. if (sys.platform.startswith("linux")):
  119. host_subpath = "linux-x86_64"
  120. elif (sys.platform.startswith("darwin")):
  121. host_subpath = "darwin-x86_64"
  122. elif (sys.platform.startswith('win')):
  123. if (platform.machine().endswith('64')):
  124. host_subpath = "windows-x86_64"
  125. if env["android_arch"] == "arm64v8":
  126. mt_link = False
  127. else:
  128. mt_link = False
  129. host_subpath = "windows"
  130. compiler_path = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/" + host_subpath + "/bin"
  131. gcc_toolchain_path = env["ANDROID_NDK_ROOT"] + "/toolchains/" + target_subpath + "/prebuilt/" + host_subpath
  132. tools_path = gcc_toolchain_path + "/" + abi_subpath + "/bin"
  133. # For Clang to find NDK tools in preference of those system-wide
  134. env.PrependENVPath('PATH', tools_path)
  135. env['CC'] = compiler_path + '/clang'
  136. env['CXX'] = compiler_path + '/clang++'
  137. env['AR'] = tools_path + "/ar"
  138. env['RANLIB'] = tools_path + "/ranlib"
  139. env['AS'] = tools_path + "/as"
  140. sysroot = env["ANDROID_NDK_ROOT"] + "/platforms/" + env['ndk_platform'] + "/" + env['ARCH']
  141. common_opts = ['-fno-integrated-as', '-gcc-toolchain', gcc_toolchain_path]
  142. ## Compile flags
  143. env.Append(CPPFLAGS=["-isystem", 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. target_opts = ['-target', 'i686-none-linux-android']
  149. # The NDK adds this if targeting API < 21, so we can drop it when Godot targets it at least
  150. env.Append(CPPFLAGS=['-mstackrealign'])
  151. elif env["android_arch"] == "armv6":
  152. target_opts = ['-target', 'armv6-none-linux-androideabi']
  153. env.Append(CPPFLAGS='-D__ARM_ARCH_6__ -march=armv6 -mfpu=vfp -mfloat-abi=softfp'.split())
  154. elif env["android_arch"] == "armv7":
  155. target_opts = ['-target', 'armv7-none-linux-androideabi']
  156. env.Append(CPPFLAGS='-D__ARM_ARCH_7__ -D__ARM_ARCH_7A__ -march=armv7-a -mfloat-abi=softfp'.split())
  157. if env['android_neon'] == 'yes':
  158. env['neon_enabled'] = True
  159. env.Append(CPPFLAGS=['-mfpu=neon', '-D__ARM_NEON__'])
  160. else:
  161. env.Append(CPPFLAGS=['-mfpu=vfpv3-d16'])
  162. elif env["android_arch"] == "arm64v8":
  163. target_opts = ['-target', 'aarch64-none-linux-android']
  164. env.Append(CPPFLAGS=['-D__ARM_ARCH_8A__'])
  165. env.Append(CPPFLAGS=['-mfix-cortex-a53-835769'])
  166. env.Append(CPPFLAGS=target_opts)
  167. env.Append(CPPFLAGS=common_opts)
  168. if (env['android_stl'] == 'yes'):
  169. env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/include"])
  170. env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath + "/include"])
  171. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath])
  172. env.Append(LIBS=["gnustl_static"])
  173. else:
  174. env.Append(CXXFLAGS=['-fno-rtti', '-fno-exceptions', '-DNO_SAFE_CAST'])
  175. ## Link flags
  176. env['LINKFLAGS'] = ['-shared', '--sysroot=' + sysroot, '-Wl,--warn-shared-textrel']
  177. if env["android_arch"] == "armv7":
  178. env.Append(LINKFLAGS='-Wl,--fix-cortex-a8'.split())
  179. env.Append(LINKFLAGS='-Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now'.split())
  180. env.Append(LINKFLAGS='-Wl,-soname,libgodot_android.so -Wl,--gc-sections'.split())
  181. if mt_link:
  182. env.Append(LINKFLAGS=['-Wl,--threads'])
  183. env.Append(LINKFLAGS=target_opts)
  184. env.Append(LINKFLAGS=common_opts)
  185. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + '/toolchains/arm-linux-androideabi-4.9/prebuilt/' +
  186. host_subpath + '/lib/gcc/' + abi_subpath + '/4.9.x'])
  187. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] +
  188. '/toolchains/arm-linux-androideabi-4.9/prebuilt/' + host_subpath + '/' + abi_subpath + '/lib'])
  189. env.Append(CPPPATH=['#platform/android'])
  190. env.Append(CPPFLAGS=['-DANDROID_ENABLED', '-DUNIX_ENABLED', '-DNO_FCNTL', '-DMPC_FIXED_POINT'])
  191. env.Append(LIBS=['OpenSLES', 'EGL', 'GLESv3', 'android', 'log', 'z'])
  192. # TODO: Move that to opus module's config
  193. if("module_opus_enabled" in env and env["module_opus_enabled"] != "no"):
  194. if (env["android_arch"] == "armv6" or env["android_arch"] == "armv7"):
  195. env.Append(CFLAGS=["-DOPUS_ARM_OPT"])
  196. env.opus_fixed_point = "yes"