detect.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import os
  2. import platform
  3. import sys
  4. from compat import decode_utf8
  5. def is_active():
  6. return True
  7. def get_name():
  8. return "X11"
  9. def can_build():
  10. if (os.name != "posix" or sys.platform == "darwin"):
  11. return False
  12. # Check the minimal dependencies
  13. x11_error = os.system("pkg-config --version > /dev/null")
  14. if (x11_error):
  15. print("pkg-config not found.. x11 disabled.")
  16. return False
  17. x11_error = os.system("pkg-config x11 --modversion > /dev/null ")
  18. if (x11_error):
  19. print("X11 not found.. x11 disabled.")
  20. return False
  21. x11_error = os.system("pkg-config xcursor --modversion > /dev/null ")
  22. if (x11_error):
  23. print("xcursor not found.. x11 disabled.")
  24. return False
  25. x11_error = os.system("pkg-config xinerama --modversion > /dev/null ")
  26. if (x11_error):
  27. print("xinerama not found.. x11 disabled.")
  28. return False
  29. x11_error = os.system("pkg-config xrandr --modversion > /dev/null ")
  30. if (x11_error):
  31. print("xrandr not found.. x11 disabled.")
  32. return False
  33. x11_error = os.system("pkg-config xrender --modversion > /dev/null ")
  34. if (x11_error):
  35. print("xrender not found.. x11 disabled.")
  36. return False
  37. x11_error = os.system("pkg-config xi --modversion > /dev/null ")
  38. if (x11_error):
  39. print("xi not found.. Aborting.")
  40. return False
  41. return True
  42. def get_opts():
  43. from SCons.Variables import BoolVariable, EnumVariable
  44. return [
  45. BoolVariable('use_llvm', 'Use the LLVM compiler', False),
  46. BoolVariable('use_static_cpp', 'Link libgcc and libstdc++ statically for better portability', False),
  47. BoolVariable('use_sanitizer', 'Use LLVM compiler address sanitizer', False),
  48. BoolVariable('use_leak_sanitizer', 'Use LLVM compiler memory leaks sanitizer (implies use_sanitizer)', False),
  49. BoolVariable('pulseaudio', 'Detect & use pulseaudio', True),
  50. BoolVariable('udev', 'Use udev for gamepad connection callbacks', False),
  51. EnumVariable('debug_symbols', 'Add debugging symbols to release builds', 'yes', ('yes', 'no', 'full')),
  52. BoolVariable('separate_debug_symbols', 'Create a separate file containing debugging symbols', False),
  53. BoolVariable('touch', 'Enable touch events', True),
  54. BoolVariable('execinfo', 'Use libexecinfo on systems where glibc is not available', False),
  55. ]
  56. def get_flags():
  57. return [
  58. ('builtin_freetype', False),
  59. ('builtin_libpng', False),
  60. ('builtin_zlib', False),
  61. ]
  62. def configure(env):
  63. ## Build type
  64. if (env["target"] == "release"):
  65. # -O3 -ffast-math is identical to -Ofast. We need to split it out so we can selectively disable
  66. # -ffast-math in code for which it generates wrong results.
  67. if (env["optimize"] == "speed"): #optimize for speed (default)
  68. env.Prepend(CCFLAGS=['-O3', '-ffast-math'])
  69. else: #optimize for size
  70. env.Prepend(CCFLAGS=['-Os'])
  71. if (env["debug_symbols"] == "yes"):
  72. env.Prepend(CCFLAGS=['-g1'])
  73. if (env["debug_symbols"] == "full"):
  74. env.Prepend(CCFLAGS=['-g2'])
  75. elif (env["target"] == "release_debug"):
  76. if (env["optimize"] == "speed"): #optimize for speed (default)
  77. env.Prepend(CCFLAGS=['-O2', '-ffast-math', '-DDEBUG_ENABLED'])
  78. else: #optimize for size
  79. env.Prepend(CCFLAGS=['-Os', '-DDEBUG_ENABLED'])
  80. if (env["debug_symbols"] == "yes"):
  81. env.Prepend(CCFLAGS=['-g1'])
  82. if (env["debug_symbols"] == "full"):
  83. env.Prepend(CCFLAGS=['-g2'])
  84. elif (env["target"] == "debug"):
  85. env.Prepend(CCFLAGS=['-g3', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED'])
  86. env.Append(LINKFLAGS=['-rdynamic'])
  87. ## Architecture
  88. is64 = sys.maxsize > 2**32
  89. if (env["bits"] == "default"):
  90. env["bits"] = "64" if is64 else "32"
  91. ## Compiler configuration
  92. if 'CXX' in env and 'clang' in os.path.basename(env['CXX']):
  93. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  94. env['use_llvm'] = True
  95. if env['use_llvm']:
  96. if ('clang++' not in os.path.basename(env['CXX'])):
  97. env["CC"] = "clang"
  98. env["CXX"] = "clang++"
  99. env["LINK"] = "clang++"
  100. env.Append(CPPFLAGS=['-DTYPED_METHOD_BIND'])
  101. env.extra_suffix = ".llvm" + env.extra_suffix
  102. # leak sanitizer requires (address) sanitizer
  103. if env['use_sanitizer'] or env['use_leak_sanitizer']:
  104. env.Append(CCFLAGS=['-fsanitize=address', '-fno-omit-frame-pointer'])
  105. env.Append(LINKFLAGS=['-fsanitize=address'])
  106. env.extra_suffix += "s"
  107. if env['use_leak_sanitizer']:
  108. env.Append(CCFLAGS=['-fsanitize=leak'])
  109. env.Append(LINKFLAGS=['-fsanitize=leak'])
  110. if env['use_lto']:
  111. env.Append(CCFLAGS=['-flto'])
  112. if not env['use_llvm'] and env.GetOption("num_jobs") > 1:
  113. env.Append(LINKFLAGS=['-flto=' + str(env.GetOption("num_jobs"))])
  114. else:
  115. env.Append(LINKFLAGS=['-flto'])
  116. if not env['use_llvm']:
  117. env['RANLIB'] = 'gcc-ranlib'
  118. env['AR'] = 'gcc-ar'
  119. env.Append(CCFLAGS=['-pipe'])
  120. env.Append(LINKFLAGS=['-pipe'])
  121. # Check for gcc version > 5 before adding -no-pie
  122. import re
  123. import subprocess
  124. proc = subprocess.Popen([env['CXX'], '--version'], stdout=subprocess.PIPE)
  125. (stdout, _) = proc.communicate()
  126. stdout = decode_utf8(stdout)
  127. match = re.search('[0-9][0-9.]*', stdout)
  128. if match is not None:
  129. version = match.group().split('.')
  130. if (version[0] > '5'):
  131. env.Append(CCFLAGS=['-fpie'])
  132. env.Append(LINKFLAGS=['-no-pie'])
  133. ## Dependencies
  134. env.ParseConfig('pkg-config x11 --cflags --libs')
  135. env.ParseConfig('pkg-config xcursor --cflags --libs')
  136. env.ParseConfig('pkg-config xinerama --cflags --libs')
  137. env.ParseConfig('pkg-config xrandr --cflags --libs')
  138. env.ParseConfig('pkg-config xrender --cflags --libs')
  139. env.ParseConfig('pkg-config xi --cflags --libs')
  140. if (env['touch']):
  141. env.Append(CPPFLAGS=['-DTOUCH_ENABLED'])
  142. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  143. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  144. # as shared libraries leads to weird issues
  145. if env['builtin_freetype'] or env['builtin_libpng'] or env['builtin_zlib']:
  146. env['builtin_freetype'] = True
  147. env['builtin_libpng'] = True
  148. env['builtin_zlib'] = True
  149. if not env['builtin_freetype']:
  150. env.ParseConfig('pkg-config freetype2 --cflags --libs')
  151. if not env['builtin_libpng']:
  152. env.ParseConfig('pkg-config libpng --cflags --libs')
  153. if not env['builtin_bullet']:
  154. # We need at least version 2.88
  155. import subprocess
  156. bullet_version = subprocess.check_output(['pkg-config', 'bullet', '--modversion']).strip()
  157. if bullet_version < "2.88":
  158. # Abort as system bullet was requested but too old
  159. print("Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(bullet_version, "2.88"))
  160. sys.exit(255)
  161. env.ParseConfig('pkg-config bullet --cflags --libs')
  162. if not env['builtin_enet']:
  163. env.ParseConfig('pkg-config libenet --cflags --libs')
  164. if not env['builtin_squish'] and env['tools']:
  165. env.ParseConfig('pkg-config libsquish --cflags --libs')
  166. if not env['builtin_zstd']:
  167. env.ParseConfig('pkg-config libzstd --cflags --libs')
  168. # Sound and video libraries
  169. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  170. if not env['builtin_libtheora']:
  171. env['builtin_libogg'] = False # Needed to link against system libtheora
  172. env['builtin_libvorbis'] = False # Needed to link against system libtheora
  173. env.ParseConfig('pkg-config theora theoradec --cflags --libs')
  174. else:
  175. list_of_x86 = ['x86_64', 'x86', 'i386', 'i586']
  176. if any(platform.machine() in s for s in list_of_x86):
  177. env["x86_libtheora_opt_gcc"] = True
  178. if not env['builtin_libvpx']:
  179. env.ParseConfig('pkg-config vpx --cflags --libs')
  180. if not env['builtin_libvorbis']:
  181. env['builtin_libogg'] = False # Needed to link against system libvorbis
  182. env.ParseConfig('pkg-config vorbis vorbisfile --cflags --libs')
  183. if not env['builtin_opus']:
  184. env['builtin_libogg'] = False # Needed to link against system opus
  185. env.ParseConfig('pkg-config opus opusfile --cflags --libs')
  186. if not env['builtin_libogg']:
  187. env.ParseConfig('pkg-config ogg --cflags --libs')
  188. if not env['builtin_libwebp']:
  189. env.ParseConfig('pkg-config libwebp --cflags --libs')
  190. if not env['builtin_mbedtls']:
  191. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  192. env.Append(LIBS=['mbedtls', 'mbedcrypto', 'mbedx509'])
  193. if not env['builtin_libwebsockets']:
  194. env.ParseConfig('pkg-config libwebsockets --cflags --libs')
  195. if not env['builtin_miniupnpc']:
  196. # No pkgconfig file so far, hardcode default paths.
  197. env.Append(CPPPATH=["/usr/include/miniupnpc"])
  198. env.Append(LIBS=["miniupnpc"])
  199. # On Linux wchar_t should be 32-bits
  200. # 16-bit library shouldn't be required due to compiler optimisations
  201. if not env['builtin_pcre2']:
  202. env.ParseConfig('pkg-config libpcre2-32 --cflags --libs')
  203. ## Flags
  204. if (os.system("pkg-config --exists alsa") == 0): # 0 means found
  205. print("Enabling ALSA")
  206. env.Append(CPPFLAGS=["-DALSA_ENABLED", "-DALSAMIDI_ENABLED"])
  207. # Don't parse --cflags, we don't need to add /usr/include/alsa to include path
  208. env.ParseConfig('pkg-config alsa --libs')
  209. else:
  210. print("ALSA libraries not found, disabling driver")
  211. if env['pulseaudio']:
  212. if (os.system("pkg-config --exists libpulse") == 0): # 0 means found
  213. print("Enabling PulseAudio")
  214. env.Append(CPPFLAGS=["-DPULSEAUDIO_ENABLED"])
  215. env.ParseConfig('pkg-config --cflags --libs libpulse')
  216. else:
  217. print("PulseAudio development libraries not found, disabling driver")
  218. if (platform.system() == "Linux"):
  219. env.Append(CPPFLAGS=["-DJOYDEV_ENABLED"])
  220. if env['udev']:
  221. if (os.system("pkg-config --exists libudev") == 0): # 0 means found
  222. print("Enabling udev support")
  223. env.Append(CPPFLAGS=["-DUDEV_ENABLED"])
  224. env.ParseConfig('pkg-config libudev --cflags --libs')
  225. else:
  226. print("libudev development libraries not found, disabling udev support")
  227. # Linkflags below this line should typically stay the last ones
  228. if not env['builtin_zlib']:
  229. env.ParseConfig('pkg-config zlib --cflags --libs')
  230. env.Append(CPPPATH=['#platform/x11'])
  231. env.Append(CPPFLAGS=['-DX11_ENABLED', '-DUNIX_ENABLED', '-DOPENGL_ENABLED', '-DGLES_ENABLED'])
  232. env.Append(LIBS=['GL', 'pthread'])
  233. if (platform.system() == "Linux"):
  234. env.Append(LIBS=['dl'])
  235. if (platform.system().find("BSD") >= 0):
  236. env["execinfo"] = True
  237. if env["execinfo"]:
  238. env.Append(LIBS=['execinfo'])
  239. ## Cross-compilation
  240. if (is64 and env["bits"] == "32"):
  241. env.Append(CPPFLAGS=['-m32'])
  242. env.Append(LINKFLAGS=['-m32', '-L/usr/lib/i386-linux-gnu'])
  243. elif (not is64 and env["bits"] == "64"):
  244. env.Append(CPPFLAGS=['-m64'])
  245. env.Append(LINKFLAGS=['-m64', '-L/usr/lib/i686-linux-gnu'])
  246. # Link those statically for portability
  247. if env['use_static_cpp']:
  248. env.Append(LINKFLAGS=['-static-libgcc', '-static-libstdc++'])