detect.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import os
  2. import platform
  3. import sys
  4. from methods import get_compiler_version, using_gcc, using_clang
  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_thinlto', 'Use ThinLTO', 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 and 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. def configure(env):
  60. ## Build type
  61. if (env["target"] == "release"):
  62. if (env["optimize"] == "speed"): #optimize for speed (default)
  63. env.Prepend(CCFLAGS=['-O3'])
  64. else: #optimize for size
  65. env.Prepend(CCFLAGS=['-Os'])
  66. if (env["debug_symbols"] == "yes"):
  67. env.Prepend(CCFLAGS=['-g1'])
  68. if (env["debug_symbols"] == "full"):
  69. env.Prepend(CCFLAGS=['-g2'])
  70. elif (env["target"] == "release_debug"):
  71. if (env["optimize"] == "speed"): #optimize for speed (default)
  72. env.Prepend(CCFLAGS=['-O2'])
  73. else: #optimize for size
  74. env.Prepend(CCFLAGS=['-Os'])
  75. env.Prepend(CPPDEFINES=['DEBUG_ENABLED'])
  76. if (env["debug_symbols"] == "yes"):
  77. env.Prepend(CCFLAGS=['-g1'])
  78. if (env["debug_symbols"] == "full"):
  79. env.Prepend(CCFLAGS=['-g2'])
  80. elif (env["target"] == "debug"):
  81. env.Prepend(CCFLAGS=['-g3'])
  82. env.Prepend(CPPDEFINES=['DEBUG_ENABLED', 'DEBUG_MEMORY_ENABLED'])
  83. env.Append(LINKFLAGS=['-rdynamic'])
  84. ## Architecture
  85. is64 = sys.maxsize > 2**32
  86. if (env["bits"] == "default"):
  87. env["bits"] = "64" if is64 else "32"
  88. ## Compiler configuration
  89. if 'CXX' in env and 'clang' in os.path.basename(env['CXX']):
  90. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  91. env['use_llvm'] = True
  92. if env['use_llvm']:
  93. if ('clang++' not in os.path.basename(env['CXX'])):
  94. env["CC"] = "clang"
  95. env["CXX"] = "clang++"
  96. env["LINK"] = "clang++"
  97. env.Append(CPPDEFINES=['TYPED_METHOD_BIND'])
  98. env.extra_suffix = ".llvm" + env.extra_suffix
  99. if env['use_lld']:
  100. if env['use_llvm']:
  101. env.Append(LINKFLAGS=['-fuse-ld=lld'])
  102. if env['use_thinlto']:
  103. # A convenience so you don't need to write use_lto too when using SCons
  104. env['use_lto'] = True
  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. if not env['use_llvm'] and env.GetOption("num_jobs") > 1:
  121. env.Append(CCFLAGS=['-flto'])
  122. env.Append(LINKFLAGS=['-flto=' + str(env.GetOption("num_jobs"))])
  123. else:
  124. if env['use_lld'] and env['use_thinlto']:
  125. env.Append(CCFLAGS=['-flto=thin'])
  126. env.Append(LINKFLAGS=['-flto=thin'])
  127. else:
  128. env.Append(CCFLAGS=['-flto'])
  129. env.Append(LINKFLAGS=['-flto'])
  130. if not env['use_llvm']:
  131. env['RANLIB'] = 'gcc-ranlib'
  132. env['AR'] = 'gcc-ar'
  133. env.Append(CCFLAGS=['-pipe'])
  134. env.Append(LINKFLAGS=['-pipe'])
  135. # Check for gcc version >= 6 before adding -no-pie
  136. if using_gcc(env):
  137. version = get_compiler_version(env)
  138. if version != None and version[0] >= '6':
  139. env.Append(CCFLAGS=['-fpie'])
  140. env.Append(LINKFLAGS=['-no-pie'])
  141. # Do the same for clang should be fine with Clang 4 and higher
  142. if using_clang(env):
  143. version = get_compiler_version(env)
  144. if version != None and version[0] >= '4':
  145. env.Append(CCFLAGS=['-fpie'])
  146. env.Append(LINKFLAGS=['-no-pie'])
  147. ## Dependencies
  148. env.ParseConfig('pkg-config x11 --cflags --libs')
  149. env.ParseConfig('pkg-config xcursor --cflags --libs')
  150. env.ParseConfig('pkg-config xinerama --cflags --libs')
  151. env.ParseConfig('pkg-config xrandr --cflags --libs')
  152. env.ParseConfig('pkg-config xrender --cflags --libs')
  153. env.ParseConfig('pkg-config xi --cflags --libs')
  154. if (env['touch']):
  155. env.Append(CPPDEFINES=['TOUCH_ENABLED'])
  156. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  157. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  158. # as shared libraries leads to weird issues
  159. if env['builtin_freetype'] or env['builtin_libpng'] or env['builtin_zlib']:
  160. env['builtin_freetype'] = True
  161. env['builtin_libpng'] = True
  162. env['builtin_zlib'] = True
  163. if not env['builtin_freetype']:
  164. env.ParseConfig('pkg-config freetype2 --cflags --libs')
  165. if not env['builtin_libpng']:
  166. env.ParseConfig('pkg-config libpng16 --cflags --libs')
  167. if not env['builtin_bullet']:
  168. # We need at least version 2.89
  169. import subprocess
  170. bullet_version = subprocess.check_output(['pkg-config', 'bullet', '--modversion']).strip()
  171. if str(bullet_version) < "2.89":
  172. # Abort as system bullet was requested but too old
  173. print("Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(bullet_version, "2.89"))
  174. sys.exit(255)
  175. env.ParseConfig('pkg-config bullet --cflags --libs')
  176. if not env['builtin_enet']:
  177. env.ParseConfig('pkg-config libenet --cflags --libs')
  178. if not env['builtin_squish'] and env['tools']:
  179. env.ParseConfig('pkg-config libsquish --cflags --libs')
  180. if not env['builtin_zstd']:
  181. env.ParseConfig('pkg-config libzstd --cflags --libs')
  182. # Sound and video libraries
  183. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  184. if not env['builtin_libtheora']:
  185. env['builtin_libogg'] = False # Needed to link against system libtheora
  186. env['builtin_libvorbis'] = False # Needed to link against system libtheora
  187. env.ParseConfig('pkg-config theora theoradec --cflags --libs')
  188. else:
  189. list_of_x86 = ['x86_64', 'x86', 'i386', 'i586']
  190. if any(platform.machine() in s for s in list_of_x86):
  191. env["x86_libtheora_opt_gcc"] = True
  192. if not env['builtin_libvpx']:
  193. env.ParseConfig('pkg-config vpx --cflags --libs')
  194. if not env['builtin_libvorbis']:
  195. env['builtin_libogg'] = False # Needed to link against system libvorbis
  196. env.ParseConfig('pkg-config vorbis vorbisfile --cflags --libs')
  197. if not env['builtin_opus']:
  198. env['builtin_libogg'] = False # Needed to link against system opus
  199. env.ParseConfig('pkg-config opus opusfile --cflags --libs')
  200. if not env['builtin_libogg']:
  201. env.ParseConfig('pkg-config ogg --cflags --libs')
  202. if not env['builtin_libwebp']:
  203. env.ParseConfig('pkg-config libwebp --cflags --libs')
  204. if not env['builtin_mbedtls']:
  205. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  206. env.Append(LIBS=['mbedtls', 'mbedcrypto', 'mbedx509'])
  207. if not env['builtin_libwebsockets']:
  208. env.ParseConfig('pkg-config libwebsockets --cflags --libs')
  209. if not env['builtin_miniupnpc']:
  210. # No pkgconfig file so far, hardcode default paths.
  211. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  212. env.Append(LIBS=["miniupnpc"])
  213. # On Linux wchar_t should be 32-bits
  214. # 16-bit library shouldn't be required due to compiler optimisations
  215. if not env['builtin_pcre2']:
  216. env.ParseConfig('pkg-config libpcre2-32 --cflags --libs')
  217. ## Flags
  218. if (os.system("pkg-config --exists alsa") == 0): # 0 means found
  219. print("Enabling ALSA")
  220. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  221. # Don't parse --cflags, we don't need to add /usr/include/alsa to include path
  222. env.ParseConfig('pkg-config alsa --libs')
  223. else:
  224. print("ALSA libraries not found, disabling driver")
  225. if env['pulseaudio']:
  226. if (os.system("pkg-config --exists libpulse") == 0): # 0 means found
  227. print("Enabling PulseAudio")
  228. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  229. env.ParseConfig('pkg-config --cflags --libs libpulse')
  230. else:
  231. print("PulseAudio development libraries not found, disabling driver")
  232. if (platform.system() == "Linux"):
  233. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  234. if env['udev']:
  235. if (os.system("pkg-config --exists libudev") == 0): # 0 means found
  236. print("Enabling udev support")
  237. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  238. env.ParseConfig('pkg-config libudev --cflags --libs')
  239. else:
  240. print("libudev development libraries not found, disabling udev support")
  241. # Linkflags below this line should typically stay the last ones
  242. if not env['builtin_zlib']:
  243. env.ParseConfig('pkg-config zlib --cflags --libs')
  244. env.Prepend(CPPPATH=['#platform/x11'])
  245. env.Append(CPPDEFINES=['X11_ENABLED', 'UNIX_ENABLED', 'OPENGL_ENABLED', 'GLES_ENABLED'])
  246. env.Append(LIBS=['GL', 'pthread'])
  247. if (platform.system() == "Linux"):
  248. env.Append(LIBS=['dl'])
  249. if (platform.system().find("BSD") >= 0):
  250. env["execinfo"] = True
  251. if env["execinfo"]:
  252. env.Append(LIBS=['execinfo'])
  253. ## Cross-compilation
  254. if (is64 and env["bits"] == "32"):
  255. env.Append(CCFLAGS=['-m32'])
  256. env.Append(LINKFLAGS=['-m32', '-L/usr/lib/i386-linux-gnu'])
  257. elif (not is64 and env["bits"] == "64"):
  258. env.Append(CCFLAGS=['-m64'])
  259. env.Append(LINKFLAGS=['-m64', '-L/usr/lib/i686-linux-gnu'])
  260. # Link those statically for portability
  261. if env['use_static_cpp']:
  262. env.Append(LINKFLAGS=['-static-libgcc', '-static-libstdc++'])