detect.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 xext --modversion > /dev/null ")
  28. if x11_error:
  29. print("xext not found.. x11 disabled.")
  30. return False
  31. x11_error = os.system("pkg-config xrandr --modversion > /dev/null ")
  32. if x11_error:
  33. print("xrandr not found.. x11 disabled.")
  34. return False
  35. x11_error = os.system("pkg-config xrender --modversion > /dev/null ")
  36. if x11_error:
  37. print("xrender not found.. x11 disabled.")
  38. return False
  39. x11_error = os.system("pkg-config xi --modversion > /dev/null ")
  40. if x11_error:
  41. print("xi not found.. Aborting.")
  42. return False
  43. return True
  44. def get_opts():
  45. from SCons.Variables import BoolVariable, EnumVariable
  46. return [
  47. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  48. BoolVariable("use_lld", "Use the LLD linker", False),
  49. BoolVariable("use_thinlto", "Use ThinLTO", False),
  50. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  51. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  52. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False),
  53. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN))", False),
  54. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False),
  55. BoolVariable("use_msan", "Use LLVM/GCC compiler memory sanitizer (MSAN))", False),
  56. BoolVariable("pulseaudio", "Detect and use PulseAudio", True),
  57. BoolVariable("udev", "Use udev for gamepad connection callbacks", True),
  58. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  59. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  60. BoolVariable("touch", "Enable touch events", True),
  61. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  62. ]
  63. def get_flags():
  64. return []
  65. def configure(env):
  66. ## Build type
  67. if env["target"] == "release":
  68. if env["optimize"] == "speed": # optimize for speed (default)
  69. env.Prepend(CCFLAGS=["-O3"])
  70. elif env["optimize"] == "size": # optimize for size
  71. env.Prepend(CCFLAGS=["-Os"])
  72. if env["debug_symbols"]:
  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. elif env["optimize"] == "size": # optimize for size
  78. env.Prepend(CCFLAGS=["-Os"])
  79. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  80. if env["debug_symbols"]:
  81. env.Prepend(CCFLAGS=["-g2"])
  82. elif env["target"] == "debug":
  83. env.Prepend(CCFLAGS=["-ggdb"])
  84. env.Prepend(CCFLAGS=["-g3"])
  85. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  86. env.Append(LINKFLAGS=["-rdynamic"])
  87. ## Architecture
  88. is64 = sys.maxsize > 2**32
  89. if env["bits"] == "default":
  90. env["bits"] = "64" if is64 else "32"
  91. ## Compiler configuration
  92. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  93. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  94. env["use_llvm"] = True
  95. if env["use_llvm"]:
  96. if "clang++" not in os.path.basename(env["CXX"]):
  97. env["CC"] = "clang"
  98. env["CXX"] = "clang++"
  99. env.extra_suffix = ".llvm" + env.extra_suffix
  100. if env["use_lld"]:
  101. if env["use_llvm"]:
  102. env.Append(LINKFLAGS=["-fuse-ld=lld"])
  103. if env["use_thinlto"]:
  104. # A convenience so you don't need to write use_lto too when using SCons
  105. env["use_lto"] = True
  106. else:
  107. print("Using LLD with GCC is not supported yet, try compiling with 'use_llvm=yes'.")
  108. sys.exit(255)
  109. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  110. env.extra_suffix += "s"
  111. if env["use_ubsan"]:
  112. env.Append(
  113. CCFLAGS=[
  114. "-fsanitize=undefined,shift,shift-exponent,integer-divide-by-zero,unreachable,vla-bound,null,return,signed-integer-overflow,bounds,float-divide-by-zero,float-cast-overflow,nonnull-attribute,returns-nonnull-attribute,bool,enum,vptr,pointer-overflow,builtin"
  115. ]
  116. )
  117. if env["use_llvm"]:
  118. env.Append(
  119. CCFLAGS=[
  120. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change,implicit-signed-integer-truncation,implicit-unsigned-integer-truncation"
  121. ]
  122. )
  123. else:
  124. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  125. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  126. if env["use_asan"]:
  127. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  128. env.Append(LINKFLAGS=["-fsanitize=address"])
  129. if env["use_lsan"]:
  130. env.Append(CCFLAGS=["-fsanitize=leak"])
  131. env.Append(LINKFLAGS=["-fsanitize=leak"])
  132. if env["use_tsan"]:
  133. env.Append(CCFLAGS=["-fsanitize=thread"])
  134. env.Append(LINKFLAGS=["-fsanitize=thread"])
  135. if env["use_msan"]:
  136. env.Append(CCFLAGS=["-fsanitize=memory"])
  137. env.Append(LINKFLAGS=["-fsanitize=memory"])
  138. if env["use_lto"]:
  139. if not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  140. env.Append(CCFLAGS=["-flto"])
  141. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  142. else:
  143. if env["use_lld"] and env["use_thinlto"]:
  144. env.Append(CCFLAGS=["-flto=thin"])
  145. env.Append(LINKFLAGS=["-flto=thin"])
  146. else:
  147. env.Append(CCFLAGS=["-flto"])
  148. env.Append(LINKFLAGS=["-flto"])
  149. if not env["use_llvm"]:
  150. env["RANLIB"] = "gcc-ranlib"
  151. env["AR"] = "gcc-ar"
  152. env.Append(CCFLAGS=["-pipe"])
  153. env.Append(LINKFLAGS=["-pipe"])
  154. # Check for gcc version >= 6 before adding -no-pie
  155. version = get_compiler_version(env) or [-1, -1]
  156. if using_gcc(env):
  157. if version[0] >= 6:
  158. env.Append(CCFLAGS=["-fpie"])
  159. env.Append(LINKFLAGS=["-no-pie"])
  160. # Do the same for clang should be fine with Clang 4 and higher
  161. if using_clang(env):
  162. if version[0] >= 4:
  163. env.Append(CCFLAGS=["-fpie"])
  164. env.Append(LINKFLAGS=["-no-pie"])
  165. ## Dependencies
  166. env.ParseConfig("pkg-config x11 --cflags --libs")
  167. env.ParseConfig("pkg-config xcursor --cflags --libs")
  168. env.ParseConfig("pkg-config xinerama --cflags --libs")
  169. env.ParseConfig("pkg-config xext --cflags --libs")
  170. env.ParseConfig("pkg-config xrandr --cflags --libs")
  171. env.ParseConfig("pkg-config xrender --cflags --libs")
  172. env.ParseConfig("pkg-config xi --cflags --libs")
  173. if env["touch"]:
  174. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  175. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  176. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  177. # as shared libraries leads to weird issues
  178. if env["builtin_freetype"] or env["builtin_libpng"] or env["builtin_zlib"]:
  179. env["builtin_freetype"] = True
  180. env["builtin_libpng"] = True
  181. env["builtin_zlib"] = True
  182. if not env["builtin_freetype"]:
  183. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  184. if not env["builtin_libpng"]:
  185. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  186. if not env["builtin_bullet"]:
  187. # We need at least version 2.89
  188. import subprocess
  189. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  190. if str(bullet_version) < "2.89":
  191. # Abort as system bullet was requested but too old
  192. print(
  193. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  194. bullet_version, "2.89"
  195. )
  196. )
  197. sys.exit(255)
  198. env.ParseConfig("pkg-config bullet --cflags --libs")
  199. if False: # not env['builtin_assimp']:
  200. # FIXME: Add min version check
  201. env.ParseConfig("pkg-config assimp --cflags --libs")
  202. if not env["builtin_enet"]:
  203. env.ParseConfig("pkg-config libenet --cflags --libs")
  204. if not env["builtin_squish"]:
  205. env.ParseConfig("pkg-config libsquish --cflags --libs")
  206. if not env["builtin_zstd"]:
  207. env.ParseConfig("pkg-config libzstd --cflags --libs")
  208. # Sound and video libraries
  209. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  210. if not env["builtin_libtheora"]:
  211. env["builtin_libogg"] = False # Needed to link against system libtheora
  212. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  213. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  214. else:
  215. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  216. if any(platform.machine() in s for s in list_of_x86):
  217. env["x86_libtheora_opt_gcc"] = True
  218. if not env["builtin_libvpx"]:
  219. env.ParseConfig("pkg-config vpx --cflags --libs")
  220. if not env["builtin_libvorbis"]:
  221. env["builtin_libogg"] = False # Needed to link against system libvorbis
  222. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  223. if not env["builtin_opus"]:
  224. env["builtin_libogg"] = False # Needed to link against system opus
  225. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  226. if not env["builtin_libogg"]:
  227. env.ParseConfig("pkg-config ogg --cflags --libs")
  228. if not env["builtin_libwebp"]:
  229. env.ParseConfig("pkg-config libwebp --cflags --libs")
  230. if not env["builtin_mbedtls"]:
  231. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  232. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  233. if not env["builtin_wslay"]:
  234. env.ParseConfig("pkg-config libwslay --cflags --libs")
  235. if not env["builtin_miniupnpc"]:
  236. # No pkgconfig file so far, hardcode default paths.
  237. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  238. env.Append(LIBS=["miniupnpc"])
  239. # On Linux wchar_t should be 32-bits
  240. # 16-bit library shouldn't be required due to compiler optimisations
  241. if not env["builtin_pcre2"]:
  242. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  243. # Embree is only used in tools build on x86_64 and aarch64.
  244. if env["tools"] and not env["builtin_embree"] and is64:
  245. # No pkgconfig file so far, hardcode expected lib name.
  246. env.Append(LIBS=["embree3"])
  247. ## Flags
  248. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  249. print("Enabling ALSA")
  250. env["alsa"] = True
  251. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  252. else:
  253. print("ALSA libraries not found, disabling driver")
  254. if env["pulseaudio"]:
  255. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  256. print("Enabling PulseAudio")
  257. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  258. env.ParseConfig("pkg-config --cflags libpulse")
  259. else:
  260. print("PulseAudio development libraries not found, disabling driver")
  261. if platform.system() == "Linux":
  262. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  263. if env["udev"]:
  264. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  265. print("Enabling udev support")
  266. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  267. else:
  268. print("libudev development libraries not found, disabling udev support")
  269. else:
  270. env["udev"] = False # Linux specific
  271. # Linkflags below this line should typically stay the last ones
  272. if not env["builtin_zlib"]:
  273. env.ParseConfig("pkg-config zlib --cflags --libs")
  274. env.Prepend(CPPPATH=["#platform/x11"])
  275. env.Append(CPPDEFINES=["X11_ENABLED", "UNIX_ENABLED", "OPENGL_ENABLED", "GLES_ENABLED"])
  276. env.Append(LIBS=["GL", "pthread"])
  277. if platform.system() == "Linux":
  278. env.Append(LIBS=["dl"])
  279. if platform.system().find("BSD") >= 0:
  280. env["execinfo"] = True
  281. if env["execinfo"]:
  282. env.Append(LIBS=["execinfo"])
  283. if not env["tools"]:
  284. import subprocess
  285. import re
  286. linker_version_str = subprocess.check_output([env.subst(env["LINK"]), "-Wl,--version"]).decode("utf-8")
  287. gnu_ld_version = re.search(r"^GNU ld [^$]*(\d+\.\d+)$", linker_version_str, re.MULTILINE)
  288. if not gnu_ld_version:
  289. print(
  290. "Warning: Creating template binaries enabled for PCK embedding is currently only supported with GNU ld"
  291. )
  292. else:
  293. if float(gnu_ld_version.group(1)) >= 2.30:
  294. env.Append(LINKFLAGS=["-T", "platform/x11/pck_embed.ld"])
  295. else:
  296. env.Append(LINKFLAGS=["-T", "platform/x11/pck_embed.legacy.ld"])
  297. ## Cross-compilation
  298. if is64 and env["bits"] == "32":
  299. env.Append(CCFLAGS=["-m32"])
  300. env.Append(LINKFLAGS=["-m32", "-L/usr/lib/i386-linux-gnu"])
  301. elif not is64 and env["bits"] == "64":
  302. env.Append(CCFLAGS=["-m64"])
  303. env.Append(LINKFLAGS=["-m64", "-L/usr/lib/i686-linux-gnu"])
  304. # Link those statically for portability
  305. if env["use_static_cpp"]:
  306. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  307. if env["use_llvm"]:
  308. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  309. else:
  310. if env["use_llvm"]:
  311. env.Append(LIBS=["atomic"])