detect.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. import os
  11. if (not os.environ.has_key("ANDROID_NDK_ROOT")):
  12. return False
  13. return True
  14. def get_opts():
  15. return [
  16. ('ANDROID_NDK_ROOT', 'the path to Android NDK',
  17. os.environ.get("ANDROID_NDK_ROOT", 0)),
  18. ('ndk_platform', 'compile for platform: (android-<api> , example: android-14)', "android-14"),
  19. ('android_arch', 'select compiler architecture: (armv7/armv6/x86)', "armv7"),
  20. ('android_neon', 'enable neon (armv7 only)', "yes"),
  21. ('android_stl', 'enable STL support in android port (for modules)', "no")
  22. ]
  23. def get_flags():
  24. return [
  25. ('tools', 'no'),
  26. ('openssl', 'builtin'), # use builtin openssl
  27. ]
  28. def create(env):
  29. tools = env['TOOLS']
  30. if "mingw" in tools:
  31. tools.remove('mingw')
  32. if "applelink" in tools:
  33. tools.remove("applelink")
  34. env.Tool('gcc')
  35. return env.Clone(tools=tools)
  36. def configure(env):
  37. # Workaround for MinGW. See:
  38. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  39. import os
  40. if (os.name == "nt"):
  41. import subprocess
  42. def mySubProcess(cmdline, env):
  43. # print "SPAWNED : " + cmdline
  44. startupinfo = subprocess.STARTUPINFO()
  45. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  46. proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  47. stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, env=env)
  48. data, err = proc.communicate()
  49. rv = proc.wait()
  50. if rv:
  51. print "====="
  52. print err
  53. print "====="
  54. return rv
  55. def mySpawn(sh, escape, cmd, args, env):
  56. newargs = ' '.join(args[1:])
  57. cmdline = cmd + " " + newargs
  58. rv = 0
  59. if len(cmdline) > 32000 and cmd.endswith("ar"):
  60. cmdline = cmd + " " + args[1] + " " + args[2] + " "
  61. for i in range(3, len(args)):
  62. rv = mySubProcess(cmdline + args[i], env)
  63. if rv:
  64. break
  65. else:
  66. rv = mySubProcess(cmdline, env)
  67. return rv
  68. env['SPAWN'] = mySpawn
  69. ndk_platform = env['ndk_platform']
  70. if env['android_arch'] not in ['armv7', 'armv6', 'x86']:
  71. env['android_arch'] = 'armv7'
  72. if env['android_arch'] == 'x86':
  73. env["x86_libtheora_opt_gcc"] = True
  74. if env['PLATFORM'] == 'win32':
  75. env.Tool('gcc')
  76. env['SHLIBSUFFIX'] = '.so'
  77. neon_text = ""
  78. if env["android_arch"] == "armv7" and env['android_neon'] == 'yes':
  79. neon_text = " (with neon)"
  80. print("Godot Android!!!!! (" + env['android_arch'] + ")" + neon_text)
  81. env.Append(CPPPATH=['#platform/android'])
  82. if env['android_arch'] == 'x86':
  83. env.extra_suffix = ".x86" + env.extra_suffix
  84. target_subpath = "x86-4.9"
  85. abi_subpath = "i686-linux-android"
  86. arch_subpath = "x86"
  87. elif env['android_arch'] == 'armv6':
  88. env.extra_suffix = ".armv6" + env.extra_suffix
  89. target_subpath = "arm-linux-androideabi-4.9"
  90. abi_subpath = "arm-linux-androideabi"
  91. arch_subpath = "armeabi"
  92. elif env["android_arch"] == "armv7":
  93. target_subpath = "arm-linux-androideabi-4.9"
  94. abi_subpath = "arm-linux-androideabi"
  95. arch_subpath = "armeabi-v7a"
  96. if env['android_neon'] == 'yes':
  97. env.extra_suffix = ".armv7.neon" + env.extra_suffix
  98. else:
  99. env.extra_suffix = ".armv7" + env.extra_suffix
  100. if (sys.platform.startswith("linux")):
  101. if (platform.machine().endswith('64')):
  102. host_subpath = "linux-x86_64"
  103. else:
  104. host_subpath = "linux-x86"
  105. elif (sys.platform.startswith("darwin")):
  106. host_subpath = "darwin-x86_64"
  107. elif (sys.platform.startswith('win')):
  108. if (platform.machine().endswith('64')):
  109. host_subpath = "windows-x86_64"
  110. else:
  111. host_subpath = "windows-x86"
  112. compiler_path = env["ANDROID_NDK_ROOT"] + \
  113. "/toolchains/llvm/prebuilt/" + host_subpath + "/bin"
  114. gcc_toolchain_path = env["ANDROID_NDK_ROOT"] + \
  115. "/toolchains/" + target_subpath + "/prebuilt/" + host_subpath
  116. tools_path = gcc_toolchain_path + "/" + abi_subpath + "/bin"
  117. # For Clang to find NDK tools in preference of those system-wide
  118. env.PrependENVPath('PATH', tools_path)
  119. env['CC'] = compiler_path + '/clang'
  120. env['CXX'] = compiler_path + '/clang++'
  121. env['AR'] = tools_path + "/ar"
  122. env['RANLIB'] = tools_path + "/ranlib"
  123. env['AS'] = tools_path + "/as"
  124. if env['android_arch'] == 'x86':
  125. env['ARCH'] = 'arch-x86'
  126. else:
  127. env['ARCH'] = 'arch-arm'
  128. sysroot = env["ANDROID_NDK_ROOT"] + \
  129. "/platforms/" + ndk_platform + "/" + env['ARCH']
  130. common_opts = ['-fno-integrated-as', '-gcc-toolchain', gcc_toolchain_path]
  131. env.Append(CPPFLAGS=["-isystem", sysroot + "/usr/include"])
  132. env.Append(CPPFLAGS=string.split(
  133. '-Wno-invalid-command-line-argument -Wno-unused-command-line-argument'))
  134. env.Append(CPPFLAGS=string.split(
  135. '-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -fvisibility=hidden -fno-strict-aliasing -Wa,--noexecstack'))
  136. env.Append(CPPFLAGS=string.split('-DANDROID -DNO_STATVFS -DGLES2_ENABLED'))
  137. env['neon_enabled'] = False
  138. if env['android_arch'] == 'x86':
  139. can_vectorize = True
  140. target_opts = ['-target', 'i686-none-linux-android']
  141. elif env["android_arch"] == "armv6":
  142. can_vectorize = False
  143. target_opts = ['-target', 'armv6-none-linux-androideabi']
  144. env.Append(CPPFLAGS=string.split(
  145. '-D__ARM_ARCH_6__ -march=armv6 -mfpu=vfp -mfloat-abi=softfp'))
  146. elif env["android_arch"] == "armv7":
  147. can_vectorize = True
  148. target_opts = ['-target', 'armv7-none-linux-androideabi']
  149. env.Append(CPPFLAGS=string.split(
  150. '-D__ARM_ARCH_7__ -D__ARM_ARCH_7A__ -march=armv7-a -mfloat-abi=softfp'))
  151. if env['android_neon'] == 'yes':
  152. env['neon_enabled'] = True
  153. env.Append(CPPFLAGS=['-mfpu=neon', '-D__ARM_NEON__'])
  154. else:
  155. env.Append(CPPFLAGS=['-mfpu=vfpv3-d16'])
  156. env.Append(CPPFLAGS=target_opts)
  157. env.Append(CPPFLAGS=common_opts)
  158. env.Append(LIBS=['OpenSLES'])
  159. env.Append(LIBS=['EGL', 'OpenSLES', 'android'])
  160. env.Append(LIBS=['log', 'GLESv1_CM', 'GLESv2', 'z'])
  161. if (sys.platform.startswith("darwin")):
  162. env['SHLIBSUFFIX'] = '.so'
  163. env['LINKFLAGS'] = ['-shared', '--sysroot=' +
  164. sysroot, '-Wl,--warn-shared-textrel',
  165. '-Wl,--threads']
  166. env.Append(LINKFLAGS=string.split(
  167. '-Wl,--fix-cortex-a8'))
  168. env.Append(LINKFLAGS=string.split(
  169. '-Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now'))
  170. env.Append(LINKFLAGS=string.split(
  171. '-Wl,-soname,libgodot_android.so -Wl,--gc-sections'))
  172. env.Append(LINKFLAGS=target_opts)
  173. env.Append(LINKFLAGS=common_opts)
  174. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] + '/toolchains/arm-linux-androideabi-4.9/prebuilt/' +
  175. host_subpath + '/lib/gcc/' + abi_subpath + '/4.9.x'])
  176. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] +
  177. '/toolchains/arm-linux-androideabi-4.9/prebuilt/' + host_subpath + '/' + abi_subpath + '/lib'])
  178. if (env["target"].startswith("release")):
  179. env.Append(LINKFLAGS=['-O2'])
  180. env.Append(CPPFLAGS=['-O2', '-DNDEBUG', '-ffast-math',
  181. '-funsafe-math-optimizations', '-fomit-frame-pointer'])
  182. if (can_vectorize):
  183. env.Append(CPPFLAGS=['-ftree-vectorize'])
  184. if (env["target"] == "release_debug"):
  185. env.Append(CPPFLAGS=['-DDEBUG_ENABLED'])
  186. elif (env["target"] == "debug"):
  187. env.Append(LINKFLAGS=['-O0'])
  188. env.Append(CPPFLAGS=['-O0', '-D_DEBUG', '-UNDEBUG', '-DDEBUG_ENABLED',
  189. '-DDEBUG_MEMORY_ALLOC', '-g', '-fno-limit-debug-info'])
  190. env.Append(CPPFLAGS=['-DANDROID_ENABLED',
  191. '-DUNIX_ENABLED', '-DNO_FCNTL', '-DMPC_FIXED_POINT'])
  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"
  197. if (env['android_stl'] == 'yes'):
  198. env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] +
  199. "/sources/cxx-stl/gnu-libstdc++/4.9/include"])
  200. env.Append(CPPPATH=[env["ANDROID_NDK_ROOT"] +
  201. "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath + "/include"])
  202. env.Append(LIBPATH=[env["ANDROID_NDK_ROOT"] +
  203. "/sources/cxx-stl/gnu-libstdc++/4.9/libs/" + arch_subpath])
  204. env.Append(LIBS=["gnustl_static"])
  205. else:
  206. env.Append(CXXFLAGS=['-fno-rtti', '-fno-exceptions', '-DNO_SAFE_CAST'])
  207. import methods
  208. env.Append(BUILDERS={'GLSL120': env.Builder(
  209. action=methods.build_legacygl_headers, suffix='glsl.h', src_suffix='.glsl')})
  210. env.Append(BUILDERS={'GLSL': env.Builder(
  211. action=methods.build_glsl_headers, suffix='glsl.h', src_suffix='.glsl')})
  212. env.Append(BUILDERS={'GLSL120GLES': env.Builder(
  213. action=methods.build_gles2_headers, suffix='glsl.h', src_suffix='.glsl')})
  214. env.use_windows_spawn_fix()