detect.py 13 KB

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