detect.py 12 KB

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