detect.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 stdc++ statically', 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('use_lto', 'Use link time optimization', False),
  41. BoolVariable('pulseaudio', 'Detect & use pulseaudio', True),
  42. BoolVariable('udev', 'Use udev for gamepad connection callbacks', False),
  43. EnumVariable('debug_symbols', 'Add debug symbols to release version', 'yes', ('yes', 'no', 'full')),
  44. ]
  45. def get_flags():
  46. return [
  47. ('builtin_freetype', False),
  48. ('builtin_libpng', False),
  49. ('builtin_openssl', False),
  50. ('builtin_zlib', False),
  51. ]
  52. def configure(env):
  53. ## Build type
  54. if (env["target"] == "release"):
  55. # -O3 -ffast-math is identical to -Ofast. We need to split it out so we can selectively disable
  56. # -ffast-math in code for which it generates wrong results.
  57. env.Prepend(CCFLAGS=['-O3', '-ffast-math'])
  58. if (env["debug_symbols"] == "yes"):
  59. env.Prepend(CCFLAGS=['-g1'])
  60. if (env["debug_symbols"] == "full"):
  61. env.Prepend(CCFLAGS=['-g2'])
  62. elif (env["target"] == "release_debug"):
  63. env.Prepend(CCFLAGS=['-O2', '-ffast-math', '-DDEBUG_ENABLED'])
  64. if (env["debug_symbols"] == "yes"):
  65. env.Prepend(CCFLAGS=['-g1'])
  66. if (env["debug_symbols"] == "full"):
  67. env.Prepend(CCFLAGS=['-g2'])
  68. elif (env["target"] == "debug"):
  69. env.Prepend(CCFLAGS=['-g3', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED'])
  70. env.Append(LINKFLAGS=['-rdynamic'])
  71. ## Architecture
  72. is64 = sys.maxsize > 2**32
  73. if (env["bits"] == "default"):
  74. env["bits"] = "64" if is64 else "32"
  75. ## Compiler configuration
  76. if env['use_llvm']:
  77. if ('clang++' not in env['CXX']):
  78. env["CC"] = "clang"
  79. env["CXX"] = "clang++"
  80. env["LD"] = "clang++"
  81. env.Append(CPPFLAGS=['-DTYPED_METHOD_BIND'])
  82. env.extra_suffix = ".llvm" + env.extra_suffix
  83. # leak sanitizer requires (address) sanitizer
  84. if env['use_sanitizer'] or env['use_leak_sanitizer']:
  85. env.Append(CCFLAGS=['-fsanitize=address', '-fno-omit-frame-pointer'])
  86. env.Append(LINKFLAGS=['-fsanitize=address'])
  87. env.extra_suffix += "s"
  88. if env['use_leak_sanitizer']:
  89. env.Append(CCFLAGS=['-fsanitize=leak'])
  90. env.Append(LINKFLAGS=['-fsanitize=leak'])
  91. if env['use_lto']:
  92. env.Append(CCFLAGS=['-flto'])
  93. if not env['use_llvm'] and env.GetOption("num_jobs") > 1:
  94. env.Append(LINKFLAGS=['-flto=' + str(env.GetOption("num_jobs"))])
  95. else:
  96. env.Append(LINKFLAGS=['-flto'])
  97. if not env['use_llvm']:
  98. env['RANLIB'] = 'gcc-ranlib'
  99. env['AR'] = 'gcc-ar'
  100. env.Append(CCFLAGS=['-pipe'])
  101. env.Append(LINKFLAGS=['-pipe'])
  102. ## Dependencies
  103. env.ParseConfig('pkg-config x11 --cflags --libs')
  104. env.ParseConfig('pkg-config xcursor --cflags --libs')
  105. env.ParseConfig('pkg-config xinerama --cflags --libs')
  106. env.ParseConfig('pkg-config xrandr --cflags --libs')
  107. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  108. if not env['builtin_openssl']:
  109. env.ParseConfig('pkg-config openssl --cflags --libs')
  110. if not env['builtin_libwebp']:
  111. env.ParseConfig('pkg-config libwebp --cflags --libs')
  112. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  113. # as shared libraries leads to weird issues
  114. if env['builtin_freetype'] or env['builtin_libpng'] or env['builtin_zlib']:
  115. env['builtin_freetype'] = True
  116. env['builtin_libpng'] = True
  117. env['builtin_zlib'] = True
  118. if not env['builtin_freetype']:
  119. env.ParseConfig('pkg-config freetype2 --cflags --libs')
  120. if not env['builtin_libpng']:
  121. env.ParseConfig('pkg-config libpng --cflags --libs')
  122. if not env['builtin_enet']:
  123. env.ParseConfig('pkg-config libenet --cflags --libs')
  124. if not env['builtin_squish'] and env['tools']:
  125. env.ParseConfig('pkg-config libsquish --cflags --libs')
  126. if not env['builtin_zstd']:
  127. env.ParseConfig('pkg-config libzstd --cflags --libs')
  128. # Sound and video libraries
  129. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  130. if not env['builtin_libtheora']:
  131. env['builtin_libogg'] = False # Needed to link against system libtheora
  132. env['builtin_libvorbis'] = False # Needed to link against system libtheora
  133. env.ParseConfig('pkg-config theora theoradec --cflags --libs')
  134. if not env['builtin_libvpx']:
  135. env.ParseConfig('pkg-config vpx --cflags --libs')
  136. if not env['builtin_libvorbis']:
  137. env['builtin_libogg'] = False # Needed to link against system libvorbis
  138. env.ParseConfig('pkg-config vorbis vorbisfile --cflags --libs')
  139. if not env['builtin_opus']:
  140. env['builtin_libogg'] = False # Needed to link against system opus
  141. env.ParseConfig('pkg-config opus opusfile --cflags --libs')
  142. if not env['builtin_libogg']:
  143. env.ParseConfig('pkg-config ogg --cflags --libs')
  144. if env['builtin_libtheora']:
  145. list_of_x86 = ['x86_64', 'x86', 'i386', 'i586']
  146. if any(platform.machine() in s for s in list_of_x86):
  147. env["x86_libtheora_opt_gcc"] = True
  148. # On Linux wchar_t should be 32-bits
  149. # 16-bit library shouldn't be required due to compiler optimisations
  150. if not env['builtin_pcre2']:
  151. env.ParseConfig('pkg-config libpcre2-32 --cflags --libs')
  152. ## Flags
  153. if (os.system("pkg-config --exists alsa") == 0): # 0 means found
  154. print("Enabling ALSA")
  155. env.Append(CPPFLAGS=["-DALSA_ENABLED"])
  156. env.ParseConfig('pkg-config alsa --cflags --libs')
  157. else:
  158. print("ALSA libraries not found, disabling driver")
  159. if env['pulseaudio']:
  160. if (os.system("pkg-config --exists libpulse-simple") == 0): # 0 means found
  161. print("Enabling PulseAudio")
  162. env.Append(CPPFLAGS=["-DPULSEAUDIO_ENABLED"])
  163. env.ParseConfig('pkg-config --cflags --libs libpulse-simple')
  164. else:
  165. print("PulseAudio development libraries not found, disabling driver")
  166. if (platform.system() == "Linux"):
  167. env.Append(CPPFLAGS=["-DJOYDEV_ENABLED"])
  168. if env['udev']:
  169. if (os.system("pkg-config --exists libudev") == 0): # 0 means found
  170. print("Enabling udev support")
  171. env.Append(CPPFLAGS=["-DUDEV_ENABLED"])
  172. env.ParseConfig('pkg-config libudev --cflags --libs')
  173. else:
  174. print("libudev development libraries not found, disabling udev support")
  175. # Linkflags below this line should typically stay the last ones
  176. if not env['builtin_zlib']:
  177. env.ParseConfig('pkg-config zlib --cflags --libs')
  178. env.Append(CPPPATH=['#platform/x11'])
  179. env.Append(CPPFLAGS=['-DX11_ENABLED', '-DUNIX_ENABLED', '-DOPENGL_ENABLED', '-DGLES2_ENABLED', '-DGLES_OVER_GL'])
  180. env.Append(LIBS=['GL', 'pthread'])
  181. if (platform.system() == "Linux"):
  182. env.Append(LIBS=['dl'])
  183. if (platform.system().find("BSD") >= 0):
  184. env.Append(LIBS=['execinfo'])
  185. ## Cross-compilation
  186. if (is64 and env["bits"] == "32"):
  187. env.Append(CPPFLAGS=['-m32'])
  188. env.Append(LINKFLAGS=['-m32', '-L/usr/lib/i386-linux-gnu'])
  189. elif (not is64 and env["bits"] == "64"):
  190. env.Append(CPPFLAGS=['-m64'])
  191. env.Append(LINKFLAGS=['-m64', '-L/usr/lib/i686-linux-gnu'])
  192. if env['use_static_cpp']:
  193. env.Append(LINKFLAGS=['-static-libstdc++'])