detect.py 12 KB

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