detect.py 11 KB

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