detect.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import os
  2. import platform
  3. import sys
  4. def is_active():
  5. return True
  6. def get_name():
  7. return "X11"
  8. def can_build():
  9. if (os.name != "posix" or sys.platform == "darwin"):
  10. return False
  11. # Check the minimal dependencies
  12. x11_error = os.system("pkg-config --version > /dev/null")
  13. if (x11_error):
  14. print("pkg-config not found.. x11 disabled.")
  15. return False
  16. x11_error = os.system("pkg-config x11 --modversion > /dev/null ")
  17. if (x11_error):
  18. print("X11 not found.. x11 disabled.")
  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. return True
  33. def get_opts():
  34. from SCons.Variables import BoolVariable, EnumVariable
  35. return [
  36. BoolVariable('use_llvm', 'Use the LLVM compiler', False),
  37. BoolVariable('use_static_cpp', 'Link libgcc and libstdc++ statically for better portability', False),
  38. BoolVariable('use_sanitizer', 'Use LLVM compiler address sanitizer', False),
  39. BoolVariable('use_leak_sanitizer', 'Use LLVM compiler memory leaks sanitizer (implies use_sanitizer)', False),
  40. BoolVariable('pulseaudio', 'Detect & use pulseaudio', True),
  41. BoolVariable('udev', 'Use udev for gamepad connection callbacks', False),
  42. EnumVariable('debug_symbols', 'Add debug symbols to release version', 'yes', ('yes', 'no', 'full')),
  43. BoolVariable('separate_debug_symbols', 'Create a separate file with the debug symbols', False),
  44. BoolVariable('touch', 'Enable touch events', True),
  45. ]
  46. def get_flags():
  47. return [
  48. ('builtin_freetype', False),
  49. ('builtin_libpng', False),
  50. ('builtin_openssl', False),
  51. ('builtin_zlib', False),
  52. ]
  53. def configure(env):
  54. ## Build type
  55. if (env["target"] == "release"):
  56. # -O3 -ffast-math is identical to -Ofast. We need to split it out so we can selectively disable
  57. # -ffast-math in code for which it generates wrong results.
  58. env.Prepend(CCFLAGS=['-O3', '-ffast-math'])
  59. if (env["debug_symbols"] == "yes"):
  60. env.Prepend(CCFLAGS=['-g1'])
  61. if (env["debug_symbols"] == "full"):
  62. env.Prepend(CCFLAGS=['-g2'])
  63. elif (env["target"] == "release_debug"):
  64. env.Prepend(CCFLAGS=['-O2', '-ffast-math', '-DDEBUG_ENABLED'])
  65. if (env["debug_symbols"] == "yes"):
  66. env.Prepend(CCFLAGS=['-g1'])
  67. if (env["debug_symbols"] == "full"):
  68. env.Prepend(CCFLAGS=['-g2'])
  69. elif (env["target"] == "debug"):
  70. env.Prepend(CCFLAGS=['-g3', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED'])
  71. env.Append(LINKFLAGS=['-rdynamic'])
  72. ## Architecture
  73. is64 = sys.maxsize > 2**32
  74. if (env["bits"] == "default"):
  75. env["bits"] = "64" if is64 else "32"
  76. ## Compiler configuration
  77. if 'CXX' in env and 'clang' in env['CXX']:
  78. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  79. env['use_llvm'] = True
  80. if env['use_llvm']:
  81. if ('clang++' not in env['CXX']):
  82. env["CC"] = "clang"
  83. env["CXX"] = "clang++"
  84. env["LINK"] = "clang++"
  85. env.Append(CPPFLAGS=['-DTYPED_METHOD_BIND'])
  86. env.extra_suffix = ".llvm" + env.extra_suffix
  87. # leak sanitizer requires (address) sanitizer
  88. if env['use_sanitizer'] or env['use_leak_sanitizer']:
  89. env.Append(CCFLAGS=['-fsanitize=address', '-fno-omit-frame-pointer'])
  90. env.Append(LINKFLAGS=['-fsanitize=address'])
  91. env.extra_suffix += "s"
  92. if env['use_leak_sanitizer']:
  93. env.Append(CCFLAGS=['-fsanitize=leak'])
  94. env.Append(LINKFLAGS=['-fsanitize=leak'])
  95. if env['use_lto']:
  96. env.Append(CCFLAGS=['-flto'])
  97. if not env['use_llvm'] and env.GetOption("num_jobs") > 1:
  98. env.Append(LINKFLAGS=['-flto=' + str(env.GetOption("num_jobs"))])
  99. else:
  100. env.Append(LINKFLAGS=['-flto'])
  101. if not env['use_llvm']:
  102. env['RANLIB'] = 'gcc-ranlib'
  103. env['AR'] = 'gcc-ar'
  104. env.Append(CCFLAGS=['-pipe'])
  105. env.Append(LINKFLAGS=['-pipe'])
  106. ## Dependencies
  107. env.ParseConfig('pkg-config x11 --cflags --libs')
  108. env.ParseConfig('pkg-config xcursor --cflags --libs')
  109. env.ParseConfig('pkg-config xinerama --cflags --libs')
  110. env.ParseConfig('pkg-config xrandr --cflags --libs')
  111. if (env['touch']):
  112. x11_error = os.system("pkg-config xi --modversion > /dev/null ")
  113. if (x11_error):
  114. print("xi not found.. cannot build with touch. Aborting.")
  115. sys.exit(255)
  116. env.ParseConfig('pkg-config xi --cflags --libs')
  117. env.Append(CPPFLAGS=['-DTOUCH_ENABLED'])
  118. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  119. if not env['builtin_openssl']:
  120. env.ParseConfig('pkg-config openssl --cflags --libs')
  121. if not env['builtin_libwebp']:
  122. env.ParseConfig('pkg-config libwebp --cflags --libs')
  123. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  124. # as shared libraries leads to weird issues
  125. if env['builtin_freetype'] or env['builtin_libpng'] or env['builtin_zlib']:
  126. env['builtin_freetype'] = True
  127. env['builtin_libpng'] = True
  128. env['builtin_zlib'] = True
  129. if not env['builtin_freetype']:
  130. env.ParseConfig('pkg-config freetype2 --cflags --libs')
  131. if not env['builtin_libpng']:
  132. env.ParseConfig('pkg-config libpng --cflags --libs')
  133. if not env['builtin_bullet']:
  134. # We need at least version 2.88
  135. import subprocess
  136. bullet_version = subprocess.check_output(['pkg-config', 'bullet', '--modversion']).strip()
  137. if bullet_version < "2.88":
  138. # Abort as system bullet was requested but too old
  139. print("Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(bullet_version, "2.88"))
  140. sys.exit(255)
  141. env.ParseConfig('pkg-config bullet --cflags --libs')
  142. if not env['builtin_enet']:
  143. env.ParseConfig('pkg-config libenet --cflags --libs')
  144. if not env['builtin_squish'] and env['tools']:
  145. env.ParseConfig('pkg-config libsquish --cflags --libs')
  146. if not env['builtin_zstd']:
  147. env.ParseConfig('pkg-config libzstd --cflags --libs')
  148. # Sound and video libraries
  149. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  150. if not env['builtin_libtheora']:
  151. env['builtin_libogg'] = False # Needed to link against system libtheora
  152. env['builtin_libvorbis'] = False # Needed to link against system libtheora
  153. env.ParseConfig('pkg-config theora theoradec --cflags --libs')
  154. if not env['builtin_libvpx']:
  155. env.ParseConfig('pkg-config vpx --cflags --libs')
  156. if not env['builtin_libvorbis']:
  157. env['builtin_libogg'] = False # Needed to link against system libvorbis
  158. env.ParseConfig('pkg-config vorbis vorbisfile --cflags --libs')
  159. if not env['builtin_opus']:
  160. env['builtin_libogg'] = False # Needed to link against system opus
  161. env.ParseConfig('pkg-config opus opusfile --cflags --libs')
  162. if not env['builtin_libogg']:
  163. env.ParseConfig('pkg-config ogg --cflags --libs')
  164. if env['builtin_libtheora']:
  165. list_of_x86 = ['x86_64', 'x86', 'i386', 'i586']
  166. if any(platform.machine() in s for s in list_of_x86):
  167. env["x86_libtheora_opt_gcc"] = True
  168. # On Linux wchar_t should be 32-bits
  169. # 16-bit library shouldn't be required due to compiler optimisations
  170. if not env['builtin_pcre2']:
  171. env.ParseConfig('pkg-config libpcre2-32 --cflags --libs')
  172. ## Flags
  173. if (os.system("pkg-config --exists alsa") == 0): # 0 means found
  174. print("Enabling ALSA")
  175. env.Append(CPPFLAGS=["-DALSA_ENABLED"])
  176. env.ParseConfig('pkg-config alsa --cflags --libs')
  177. else:
  178. print("ALSA libraries not found, disabling driver")
  179. if env['pulseaudio']:
  180. if (os.system("pkg-config --exists libpulse") == 0): # 0 means found
  181. print("Enabling PulseAudio")
  182. env.Append(CPPFLAGS=["-DPULSEAUDIO_ENABLED"])
  183. env.ParseConfig('pkg-config --cflags --libs libpulse')
  184. else:
  185. print("PulseAudio development libraries not found, disabling driver")
  186. if (platform.system() == "Linux"):
  187. env.Append(CPPFLAGS=["-DJOYDEV_ENABLED"])
  188. if env['udev']:
  189. if (os.system("pkg-config --exists libudev") == 0): # 0 means found
  190. print("Enabling udev support")
  191. env.Append(CPPFLAGS=["-DUDEV_ENABLED"])
  192. env.ParseConfig('pkg-config libudev --cflags --libs')
  193. else:
  194. print("libudev development libraries not found, disabling udev support")
  195. # Linkflags below this line should typically stay the last ones
  196. if not env['builtin_zlib']:
  197. env.ParseConfig('pkg-config zlib --cflags --libs')
  198. env.Append(CPPPATH=['#platform/x11'])
  199. env.Append(CPPFLAGS=['-DX11_ENABLED', '-DUNIX_ENABLED', '-DOPENGL_ENABLED', '-DGLES_ENABLED', '-DGLES_OVER_GL'])
  200. env.Append(LIBS=['GL', 'pthread'])
  201. if (platform.system() == "Linux"):
  202. env.Append(LIBS=['dl'])
  203. if (platform.system().find("BSD") >= 0):
  204. env.Append(LIBS=['execinfo'])
  205. ## Cross-compilation
  206. if (is64 and env["bits"] == "32"):
  207. env.Append(CPPFLAGS=['-m32'])
  208. env.Append(LINKFLAGS=['-m32', '-L/usr/lib/i386-linux-gnu'])
  209. elif (not is64 and env["bits"] == "64"):
  210. env.Append(CPPFLAGS=['-m64'])
  211. env.Append(LINKFLAGS=['-m64', '-L/usr/lib/i686-linux-gnu'])
  212. # Link those statically for portability
  213. if env['use_static_cpp']:
  214. env.Append(LINKFLAGS=['-static-libgcc', '-static-libstdc++'])