detect.py 11 KB

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