detect.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 (os.environ.has_key("ANDROID_NDK_ROOT"))
  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/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', '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.extra_suffix = ".x86" + env.extra_suffix
  73. target_subpath = "x86-4.9"
  74. abi_subpath = "i686-linux-android"
  75. arch_subpath = "x86"
  76. env["x86_libtheora_opt_gcc"] = True
  77. elif env['android_arch'] == 'armv6':
  78. env.extra_suffix = ".armv6" + env.extra_suffix
  79. target_subpath = "arm-linux-androideabi-4.9"
  80. abi_subpath = "arm-linux-androideabi"
  81. arch_subpath = "armeabi"
  82. can_vectorize = False
  83. elif env["android_arch"] == "armv7":
  84. target_subpath = "arm-linux-androideabi-4.9"
  85. abi_subpath = "arm-linux-androideabi"
  86. arch_subpath = "armeabi-v7a"
  87. if env['android_neon'] == 'yes':
  88. env.extra_suffix = ".armv7.neon" + env.extra_suffix
  89. else:
  90. env.extra_suffix = ".armv7" + env.extra_suffix
  91. ## Build type
  92. if (env["target"].startswith("release")):
  93. env.Append(LINKFLAGS=['-O2'])
  94. env.Append(CPPFLAGS=['-O2', '-DNDEBUG', '-ffast-math', '-funsafe-math-optimizations', '-fomit-frame-pointer'])
  95. if (can_vectorize):
  96. env.Append(CPPFLAGS=['-ftree-vectorize'])
  97. if (env["target"] == "release_debug"):
  98. env.Append(CPPFLAGS=['-DDEBUG_ENABLED'])
  99. elif (env["target"] == "debug"):
  100. env.Append(LINKFLAGS=['-O0'])
  101. env.Append(CPPFLAGS=['-O0', '-D_DEBUG', '-UNDEBUG', '-DDEBUG_ENABLED',
  102. '-DDEBUG_MEMORY_ENABLED', '-g', '-fno-limit-debug-info'])
  103. ## Compiler configuration
  104. env['SHLIBSUFFIX'] = '.so'
  105. if env['PLATFORM'] == 'win32':
  106. env.Tool('gcc')
  107. env.use_windows_spawn_fix()
  108. mt_link = True
  109. if (sys.platform.startswith("linux")):
  110. host_subpath = "linux-x86_64"
  111. elif (sys.platform.startswith("darwin")):
  112. host_subpath = "darwin-x86_64"
  113. elif (sys.platform.startswith('win')):
  114. if (platform.machine().endswith('64')):
  115. host_subpath = "windows-x86_64"
  116. else:
  117. mt_link = False
  118. host_subpath = "windows"
  119. compiler_path = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/" + host_subpath + "/bin"
  120. gcc_toolchain_path = env["ANDROID_NDK_ROOT"] + "/toolchains/" + target_subpath + "/prebuilt/" + host_subpath
  121. tools_path = gcc_toolchain_path + "/" + abi_subpath + "/bin"
  122. # For Clang to find NDK tools in preference of those system-wide
  123. env.PrependENVPath('PATH', tools_path)
  124. env['CC'] = compiler_path + '/clang'
  125. env['CXX'] = compiler_path + '/clang++'
  126. env['AR'] = tools_path + "/ar"
  127. env['RANLIB'] = tools_path + "/ranlib"
  128. env['AS'] = tools_path + "/as"
  129. if env['android_arch'] == 'x86':
  130. env['ARCH'] = 'arch-x86'
  131. else:
  132. env['ARCH'] = 'arch-arm'
  133. sysroot = env["ANDROID_NDK_ROOT"] + "/platforms/" + env['ndk_platform'] + "/" + env['ARCH']
  134. common_opts = ['-fno-integrated-as', '-gcc-toolchain', gcc_toolchain_path]
  135. ## Compile flags
  136. env.Append(CPPFLAGS=["-isystem", sysroot + "/usr/include"])
  137. env.Append(CPPFLAGS=string.split('-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden -fno-strict-aliasing'))
  138. env.Append(CPPFLAGS=string.split('-DNO_STATVFS -DGLES2_ENABLED'))
  139. env['neon_enabled'] = False
  140. if env['android_arch'] == 'x86':
  141. target_opts = ['-target', 'i686-none-linux-android']
  142. # The NDK adds this if targeting API < 21, so we can drop it when Godot targets it at least
  143. env.Append(CPPFLAGS=['-mstackrealign'])
  144. elif env["android_arch"] == "armv6":
  145. target_opts = ['-target', 'armv6-none-linux-androideabi']
  146. env.Append(CPPFLAGS=string.split('-D__ARM_ARCH_6__ -march=armv6 -mfpu=vfp -mfloat-abi=softfp'))
  147. elif env["android_arch"] == "armv7":
  148. target_opts = ['-target', 'armv7-none-linux-androideabi']
  149. env.Append(CPPFLAGS=string.split('-D__ARM_ARCH_7__ -D__ARM_ARCH_7A__ -march=armv7-a -mfloat-abi=softfp'))
  150. if env['android_neon'] == 'yes':
  151. env['neon_enabled'] = True
  152. env.Append(CPPFLAGS=['-mfpu=neon', '-D__ARM_NEON__'])
  153. else:
  154. env.Append(CPPFLAGS=['-mfpu=vfpv3-d16'])
  155. env.Append(CPPFLAGS=target_opts)
  156. env.Append(CPPFLAGS=common_opts)
  157. if (env['android_stl'] == 'yes'):
  158. env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/include"])
  159. env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath + "/include"])
  160. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath])
  161. env.Append(LIBS=["gnustl_static"])
  162. else:
  163. env.Append(CXXFLAGS=['-fno-rtti', '-fno-exceptions', '-DNO_SAFE_CAST'])
  164. ## Link flags
  165. env['LINKFLAGS'] = ['-shared', '--sysroot=' + sysroot, '-Wl,--warn-shared-textrel']
  166. env.Append(LINKFLAGS=string.split('-Wl,--fix-cortex-a8'))
  167. env.Append(LINKFLAGS=string.split('-Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now'))
  168. env.Append(LINKFLAGS=string.split('-Wl,-soname,libgodot_android.so -Wl,--gc-sections'))
  169. if mt_link:
  170. env.Append(LINKFLAGS=['-Wl,--threads'])
  171. env.Append(LINKFLAGS=target_opts)
  172. env.Append(LINKFLAGS=common_opts)
  173. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + '/toolchains/arm-linux-androideabi-4.9/prebuilt/' +
  174. host_subpath + '/lib/gcc/' + abi_subpath + '/4.9.x'])
  175. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] +
  176. '/toolchains/arm-linux-androideabi-4.9/prebuilt/' + host_subpath + '/' + abi_subpath + '/lib'])
  177. env.Append(CPPPATH=['#platform/android'])
  178. env.Append(CPPFLAGS=['-DANDROID_ENABLED', '-DUNIX_ENABLED', '-DNO_FCNTL', '-DMPC_FIXED_POINT'])
  179. env.Append(LIBS=['OpenSLES', 'EGL', 'GLESv3', 'android', 'log', 'z'])
  180. # TODO: Move that to opus module's config
  181. if("module_opus_enabled" in env and env["module_opus_enabled"] != "no"):
  182. if (env["android_arch"] == "armv6" or env["android_arch"] == "armv7"):
  183. env.Append(CFLAGS=["-DOPUS_ARM_OPT"])
  184. env.opus_fixed_point = "yes"