detect.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import os
  2. import platform
  3. import sys
  4. from methods import get_compiler_version, using_gcc
  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. return False
  16. x11_error = os.system("pkg-config x11 --modversion > /dev/null ")
  17. if (x11_error):
  18. return False
  19. x11_error = os.system("pkg-config xcursor --modversion > /dev/null ")
  20. if (x11_error):
  21. print("xcursor not found.. x11 disabled.")
  22. return False
  23. x11_error = os.system("pkg-config xinerama --modversion > /dev/null ")
  24. if (x11_error):
  25. print("xinerama not found.. x11 disabled.")
  26. return False
  27. x11_error = os.system("pkg-config xrandr --modversion > /dev/null ")
  28. if (x11_error):
  29. print("xrandr not found.. x11 disabled.")
  30. return False
  31. x11_error = os.system("pkg-config xrender --modversion > /dev/null ")
  32. if (x11_error):
  33. print("xrender not found.. x11 disabled.")
  34. return False
  35. x11_error = os.system("pkg-config xi --modversion > /dev/null ")
  36. if (x11_error):
  37. print("xi not found.. Aborting.")
  38. return False
  39. return True
  40. def get_opts():
  41. from SCons.Variables import BoolVariable, EnumVariable
  42. return [
  43. BoolVariable('use_llvm', 'Use the LLVM compiler', False),
  44. BoolVariable('use_lld', 'Use the LLD linker', False),
  45. BoolVariable('use_static_cpp', 'Link libgcc and libstdc++ statically for better portability', False),
  46. BoolVariable('use_ubsan', 'Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)', False),
  47. BoolVariable('use_asan', 'Use LLVM/GCC compiler address sanitizer (ASAN))', False),
  48. BoolVariable('use_lsan', 'Use LLVM/GCC compiler leak sanitizer (LSAN))', False),
  49. BoolVariable('pulseaudio', 'Detect and 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. if (env["optimize"] == "speed"): #optimize for speed (default)
  66. env.Prepend(CCFLAGS=['-O3'])
  67. else: #optimize for size
  68. env.Prepend(CCFLAGS=['-Os'])
  69. if (env["debug_symbols"] == "yes"):
  70. env.Prepend(CCFLAGS=['-g1'])
  71. if (env["debug_symbols"] == "full"):
  72. env.Prepend(CCFLAGS=['-g2'])
  73. elif (env["target"] == "release_debug"):
  74. if (env["optimize"] == "speed"): #optimize for speed (default)
  75. env.Prepend(CCFLAGS=['-O2'])
  76. else: #optimize for size
  77. env.Prepend(CCFLAGS=['-Os'])
  78. env.Prepend(CPPFLAGS=['-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'])
  85. env.Prepend(CPPFLAGS=['-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. if env['use_lld']:
  103. if env['use_llvm']:
  104. env.Append(LINKFLAGS=['-fuse-ld=lld'])
  105. else:
  106. print("Using LLD with GCC is not supported yet, try compiling with 'use_llvm=yes'.")
  107. sys.exit(255)
  108. if env['use_ubsan'] or env['use_asan'] or env['use_lsan']:
  109. env.extra_suffix += "s"
  110. if env['use_ubsan']:
  111. env.Append(CCFLAGS=['-fsanitize=undefined'])
  112. env.Append(LINKFLAGS=['-fsanitize=undefined'])
  113. if env['use_asan']:
  114. env.Append(CCFLAGS=['-fsanitize=address'])
  115. env.Append(LINKFLAGS=['-fsanitize=address'])
  116. if env['use_lsan']:
  117. env.Append(CCFLAGS=['-fsanitize=leak'])
  118. env.Append(LINKFLAGS=['-fsanitize=leak'])
  119. if env['use_lto']:
  120. env.Append(CCFLAGS=['-flto'])
  121. if not env['use_llvm'] and env.GetOption("num_jobs") > 1:
  122. env.Append(LINKFLAGS=['-flto=' + str(env.GetOption("num_jobs"))])
  123. else:
  124. env.Append(LINKFLAGS=['-flto'])
  125. if not env['use_llvm']:
  126. env['RANLIB'] = 'gcc-ranlib'
  127. env['AR'] = 'gcc-ar'
  128. env.Append(CCFLAGS=['-pipe'])
  129. env.Append(LINKFLAGS=['-pipe'])
  130. # Check for gcc version >= 6 before adding -no-pie
  131. if using_gcc(env):
  132. version = get_compiler_version(env)
  133. if version != None and version[0] >= '6':
  134. env.Append(CCFLAGS=['-fpie'])
  135. env.Append(LINKFLAGS=['-no-pie'])
  136. ## Dependencies
  137. env.ParseConfig('pkg-config x11 --cflags --libs')
  138. env.ParseConfig('pkg-config xcursor --cflags --libs')
  139. env.ParseConfig('pkg-config xinerama --cflags --libs')
  140. env.ParseConfig('pkg-config xrandr --cflags --libs')
  141. env.ParseConfig('pkg-config xrender --cflags --libs')
  142. env.ParseConfig('pkg-config xi --cflags --libs')
  143. if (env['touch']):
  144. env.Append(CPPFLAGS=['-DTOUCH_ENABLED'])
  145. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  146. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  147. # as shared libraries leads to weird issues
  148. if env['builtin_freetype'] or env['builtin_libpng'] or env['builtin_zlib']:
  149. env['builtin_freetype'] = True
  150. env['builtin_libpng'] = True
  151. env['builtin_zlib'] = True
  152. if not env['builtin_freetype']:
  153. env.ParseConfig('pkg-config freetype2 --cflags --libs')
  154. if not env['builtin_libpng']:
  155. env.ParseConfig('pkg-config libpng --cflags --libs')
  156. if not env['builtin_bullet']:
  157. # We need at least version 2.88
  158. import subprocess
  159. bullet_version = subprocess.check_output(['pkg-config', 'bullet', '--modversion']).strip()
  160. if str(bullet_version) < "2.88":
  161. # Abort as system bullet was requested but too old
  162. print("Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(bullet_version, "2.88"))
  163. sys.exit(255)
  164. env.ParseConfig('pkg-config bullet --cflags --libs')
  165. if not env['builtin_enet']:
  166. env.ParseConfig('pkg-config libenet --cflags --libs')
  167. if not env['builtin_squish'] and env['tools']:
  168. env.ParseConfig('pkg-config libsquish --cflags --libs')
  169. if not env['builtin_zstd']:
  170. env.ParseConfig('pkg-config libzstd --cflags --libs')
  171. # Sound and video libraries
  172. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  173. if not env['builtin_libtheora']:
  174. env['builtin_libogg'] = False # Needed to link against system libtheora
  175. env['builtin_libvorbis'] = False # Needed to link against system libtheora
  176. env.ParseConfig('pkg-config theora theoradec --cflags --libs')
  177. else:
  178. list_of_x86 = ['x86_64', 'x86', 'i386', 'i586']
  179. if any(platform.machine() in s for s in list_of_x86):
  180. env["x86_libtheora_opt_gcc"] = True
  181. if not env['builtin_libvpx']:
  182. env.ParseConfig('pkg-config vpx --cflags --libs')
  183. if not env['builtin_libvorbis']:
  184. env['builtin_libogg'] = False # Needed to link against system libvorbis
  185. env.ParseConfig('pkg-config vorbis vorbisfile --cflags --libs')
  186. if not env['builtin_opus']:
  187. env['builtin_libogg'] = False # Needed to link against system opus
  188. env.ParseConfig('pkg-config opus opusfile --cflags --libs')
  189. if not env['builtin_libogg']:
  190. env.ParseConfig('pkg-config ogg --cflags --libs')
  191. if not env['builtin_libwebp']:
  192. env.ParseConfig('pkg-config libwebp --cflags --libs')
  193. if not env['builtin_mbedtls']:
  194. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  195. env.Append(LIBS=['mbedtls', 'mbedcrypto', 'mbedx509'])
  196. if not env['builtin_libwebsockets']:
  197. env.ParseConfig('pkg-config libwebsockets --cflags --libs')
  198. if not env['builtin_miniupnpc']:
  199. # No pkgconfig file so far, hardcode default paths.
  200. env.Append(CPPPATH=["/usr/include/miniupnpc"])
  201. env.Append(LIBS=["miniupnpc"])
  202. # On Linux wchar_t should be 32-bits
  203. # 16-bit library shouldn't be required due to compiler optimisations
  204. if not env['builtin_pcre2']:
  205. env.ParseConfig('pkg-config libpcre2-32 --cflags --libs')
  206. ## Flags
  207. if (os.system("pkg-config --exists alsa") == 0): # 0 means found
  208. print("Enabling ALSA")
  209. env.Append(CPPFLAGS=["-DALSA_ENABLED", "-DALSAMIDI_ENABLED"])
  210. # Don't parse --cflags, we don't need to add /usr/include/alsa to include path
  211. env.ParseConfig('pkg-config alsa --libs')
  212. else:
  213. print("ALSA libraries not found, disabling driver")
  214. if env['pulseaudio']:
  215. if (os.system("pkg-config --exists libpulse") == 0): # 0 means found
  216. print("Enabling PulseAudio")
  217. env.Append(CPPFLAGS=["-DPULSEAUDIO_ENABLED"])
  218. env.ParseConfig('pkg-config --cflags --libs libpulse')
  219. else:
  220. print("PulseAudio development libraries not found, disabling driver")
  221. if (platform.system() == "Linux"):
  222. env.Append(CPPFLAGS=["-DJOYDEV_ENABLED"])
  223. if env['udev']:
  224. if (os.system("pkg-config --exists libudev") == 0): # 0 means found
  225. print("Enabling udev support")
  226. env.Append(CPPFLAGS=["-DUDEV_ENABLED"])
  227. env.ParseConfig('pkg-config libudev --cflags --libs')
  228. else:
  229. print("libudev development libraries not found, disabling udev support")
  230. # Linkflags below this line should typically stay the last ones
  231. if not env['builtin_zlib']:
  232. env.ParseConfig('pkg-config zlib --cflags --libs')
  233. env.Append(CPPPATH=['#platform/x11'])
  234. env.Append(CPPFLAGS=['-DX11_ENABLED', '-DUNIX_ENABLED', '-DOPENGL_ENABLED', '-DGLES_ENABLED'])
  235. env.Append(LIBS=['GL', 'pthread'])
  236. if (platform.system() == "Linux"):
  237. env.Append(LIBS=['dl'])
  238. if (platform.system().find("BSD") >= 0):
  239. env["execinfo"] = True
  240. if env["execinfo"]:
  241. env.Append(LIBS=['execinfo'])
  242. ## Cross-compilation
  243. if (is64 and env["bits"] == "32"):
  244. env.Append(CCFLAGS=['-m32'])
  245. env.Append(LINKFLAGS=['-m32', '-L/usr/lib/i386-linux-gnu'])
  246. elif (not is64 and env["bits"] == "64"):
  247. env.Append(CCFLAGS=['-m64'])
  248. env.Append(LINKFLAGS=['-m64', '-L/usr/lib/i686-linux-gnu'])
  249. # Link those statically for portability
  250. if env['use_static_cpp']:
  251. env.Append(LINKFLAGS=['-static-libgcc', '-static-libstdc++'])