detect.py 10 KB

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