detect.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. print("Error: pkg-config not found. Aborting.")
  16. return False
  17. x11_error = os.system("pkg-config x11 --modversion > /dev/null")
  18. if x11_error:
  19. print("Error: X11 libraries not found. Aborting.")
  20. return False
  21. x11_error = os.system("pkg-config xcursor --modversion > /dev/null")
  22. if x11_error:
  23. print("Error: Xcursor library not found. Aborting.")
  24. return False
  25. x11_error = os.system("pkg-config xinerama --modversion > /dev/null")
  26. if x11_error:
  27. print("Error: Xinerama library not found. Aborting.")
  28. return False
  29. x11_error = os.system("pkg-config xext --modversion > /dev/null")
  30. if x11_error:
  31. print("Error: Xext library not found. Aborting.")
  32. return False
  33. x11_error = os.system("pkg-config xrandr --modversion > /dev/null")
  34. if x11_error:
  35. print("Error: XrandR library not found. Aborting.")
  36. return False
  37. x11_error = os.system("pkg-config xrender --modversion > /dev/null")
  38. if x11_error:
  39. print("Error: XRender library not found. Aborting.")
  40. return False
  41. x11_error = os.system("pkg-config xi --modversion > /dev/null")
  42. if x11_error:
  43. print("Error: Xi library not found. Aborting.")
  44. return False
  45. return True
  46. def get_opts():
  47. from SCons.Variables import BoolVariable, EnumVariable
  48. return [
  49. EnumVariable("linker", "Linker program", "default", ("default", "bfd", "gold", "lld", "mold")),
  50. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  51. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  52. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  53. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False),
  54. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN))", False),
  55. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False),
  56. BoolVariable("use_msan", "Use LLVM/GCC compiler memory sanitizer (MSAN))", False),
  57. BoolVariable("pulseaudio", "Detect and use PulseAudio", True),
  58. BoolVariable("speechd", "Detect and use Speech Dispatcher for Text-to-Speech support", True),
  59. BoolVariable("udev", "Use udev for gamepad connection callbacks", True),
  60. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  61. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  62. BoolVariable("touch", "Enable touch events", True),
  63. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  64. ]
  65. def get_flags():
  66. return []
  67. def configure(env):
  68. ## Build type
  69. if env["target"] == "release":
  70. if env["optimize"] == "speed": # optimize for speed (default)
  71. env.Prepend(CCFLAGS=["-O3"])
  72. elif env["optimize"] == "size": # optimize for size
  73. env.Prepend(CCFLAGS=["-Os"])
  74. if env["debug_symbols"]:
  75. env.Prepend(CCFLAGS=["-g2"])
  76. elif env["target"] == "release_debug":
  77. if env["optimize"] == "speed": # optimize for speed (default)
  78. env.Prepend(CCFLAGS=["-O2"])
  79. elif env["optimize"] == "size": # optimize for size
  80. env.Prepend(CCFLAGS=["-Os"])
  81. if env["debug_symbols"]:
  82. env.Prepend(CCFLAGS=["-g2"])
  83. elif env["target"] == "debug":
  84. env.Prepend(CCFLAGS=["-ggdb"])
  85. env.Prepend(CCFLAGS=["-g3"])
  86. env.Append(LINKFLAGS=["-rdynamic"])
  87. if env["debug_symbols"]:
  88. # Adding dwarf-4 explicitly makes stacktraces work with clang builds,
  89. # otherwise addr2line doesn't understand them
  90. env.Append(CCFLAGS=["-gdwarf-4"])
  91. ## Architecture
  92. # Cross-compilation
  93. # TODO: Support cross-compilation on architectures other than x86.
  94. host_is_64_bit = sys.maxsize > 2**32
  95. if env["bits"] == "default":
  96. env["bits"] = "64" if host_is_64_bit else "32"
  97. if host_is_64_bit and (env["bits"] == "32" or env["arch"] == "x86"):
  98. env.Append(CCFLAGS=["-m32"])
  99. env.Append(LINKFLAGS=["-m32"])
  100. elif not host_is_64_bit and (env["bits"] == "64" or env["arch"] == "x86_64"):
  101. env.Append(CCFLAGS=["-m64"])
  102. env.Append(LINKFLAGS=["-m64"])
  103. machines = {
  104. "riscv64": "rv64",
  105. "ppc64le": "ppc64",
  106. "ppc64": "ppc64",
  107. "ppcle": "ppc",
  108. "ppc": "ppc",
  109. }
  110. if env["arch"] == "" and platform.machine() in machines:
  111. env["arch"] = machines[platform.machine()]
  112. if env["arch"] == "rv64":
  113. # G = General-purpose extensions, C = Compression extension (very common).
  114. env.Append(CCFLAGS=["-march=rv64gc"])
  115. ## Compiler configuration
  116. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  117. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  118. env["use_llvm"] = True
  119. if env["use_llvm"]:
  120. if "clang++" not in os.path.basename(env["CXX"]):
  121. env["CC"] = "clang"
  122. env["CXX"] = "clang++"
  123. env.extra_suffix = ".llvm" + env.extra_suffix
  124. # Linker
  125. if env["linker"] != "default":
  126. print("Using linker program: " + env["linker"])
  127. if env["linker"] == "mold" and using_gcc(env): # GCC < 12.1 doesn't support -fuse-ld=mold.
  128. cc_semver = tuple(get_compiler_version(env))
  129. if cc_semver < (12, 1):
  130. found_wrapper = False
  131. for path in ["/usr/libexec", "/usr/local/libexec", "/usr/lib", "/usr/local/lib"]:
  132. if os.path.isfile(path + "/mold/ld"):
  133. env.Append(LINKFLAGS=["-B" + path + "/mold"])
  134. found_wrapper = True
  135. break
  136. if not found_wrapper:
  137. print("Couldn't locate mold installation path. Make sure it's installed in /usr or /usr/local.")
  138. sys.exit(255)
  139. else:
  140. env.Append(LINKFLAGS=["-fuse-ld=mold"])
  141. else:
  142. env.Append(LINKFLAGS=["-fuse-ld=%s" % env["linker"]])
  143. # Sanitizers
  144. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  145. env.extra_suffix += "s"
  146. if env["use_ubsan"]:
  147. env.Append(
  148. CCFLAGS=[
  149. "-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"
  150. ]
  151. )
  152. if env["use_llvm"]:
  153. env.Append(
  154. CCFLAGS=[
  155. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change,implicit-signed-integer-truncation,implicit-unsigned-integer-truncation"
  156. ]
  157. )
  158. else:
  159. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  160. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  161. if env["use_asan"]:
  162. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  163. env.Append(LINKFLAGS=["-fsanitize=address"])
  164. if env["use_lsan"]:
  165. env.Append(CCFLAGS=["-fsanitize=leak"])
  166. env.Append(LINKFLAGS=["-fsanitize=leak"])
  167. if env["use_tsan"]:
  168. env.Append(CCFLAGS=["-fsanitize=thread"])
  169. env.Append(LINKFLAGS=["-fsanitize=thread"])
  170. if env["use_msan"]:
  171. env.Append(CCFLAGS=["-fsanitize=memory"])
  172. env.Append(LINKFLAGS=["-fsanitize=memory"])
  173. # LTO
  174. if env["lto"] == "auto": # Full LTO for production.
  175. env["lto"] = "full"
  176. if env["lto"] != "none":
  177. if env["lto"] == "thin":
  178. if not env["use_llvm"]:
  179. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  180. sys.exit(255)
  181. env.Append(CCFLAGS=["-flto=thin"])
  182. env.Append(LINKFLAGS=["-flto=thin"])
  183. elif not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  184. env.Append(CCFLAGS=["-flto"])
  185. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  186. else:
  187. env.Append(CCFLAGS=["-flto"])
  188. env.Append(LINKFLAGS=["-flto"])
  189. if not env["use_llvm"]:
  190. env["RANLIB"] = "gcc-ranlib"
  191. env["AR"] = "gcc-ar"
  192. env.Append(CCFLAGS=["-pipe"])
  193. # Check for gcc version >= 6 before adding -no-pie
  194. version = get_compiler_version(env) or [-1, -1]
  195. if using_gcc(env):
  196. if version[0] >= 6:
  197. env.Append(CCFLAGS=["-fpie"])
  198. env.Append(LINKFLAGS=["-no-pie"])
  199. # Do the same for clang should be fine with Clang 4 and higher
  200. if using_clang(env):
  201. if version[0] >= 4:
  202. env.Append(CCFLAGS=["-fpie"])
  203. env.Append(LINKFLAGS=["-no-pie"])
  204. ## Dependencies
  205. env.ParseConfig("pkg-config x11 --cflags --libs")
  206. env.ParseConfig("pkg-config xcursor --cflags --libs")
  207. env.ParseConfig("pkg-config xinerama --cflags --libs")
  208. env.ParseConfig("pkg-config xext --cflags --libs")
  209. env.ParseConfig("pkg-config xrandr --cflags --libs")
  210. env.ParseConfig("pkg-config xrender --cflags --libs")
  211. env.ParseConfig("pkg-config xi --cflags --libs")
  212. if env["touch"]:
  213. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  214. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  215. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  216. # as shared libraries leads to weird issues
  217. if env["builtin_freetype"] or env["builtin_libpng"] or env["builtin_zlib"]:
  218. env["builtin_freetype"] = True
  219. env["builtin_libpng"] = True
  220. env["builtin_zlib"] = True
  221. if not env["builtin_freetype"]:
  222. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  223. if not env["builtin_libpng"]:
  224. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  225. if not env["builtin_bullet"]:
  226. # We need at least version 2.90
  227. min_bullet_version = "2.90"
  228. import subprocess
  229. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  230. if str(bullet_version) < min_bullet_version:
  231. # Abort as system bullet was requested but too old
  232. print(
  233. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  234. bullet_version, min_bullet_version
  235. )
  236. )
  237. sys.exit(255)
  238. env.ParseConfig("pkg-config bullet --cflags --libs")
  239. if False: # not env['builtin_assimp']:
  240. # FIXME: Add min version check
  241. env.ParseConfig("pkg-config assimp --cflags --libs")
  242. if not env["builtin_enet"]:
  243. env.ParseConfig("pkg-config libenet --cflags --libs")
  244. if not env["builtin_squish"]:
  245. env.ParseConfig("pkg-config libsquish --cflags --libs")
  246. if not env["builtin_zstd"]:
  247. env.ParseConfig("pkg-config libzstd --cflags --libs")
  248. # Sound and video libraries
  249. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  250. if not env["builtin_libtheora"]:
  251. env["builtin_libogg"] = False # Needed to link against system libtheora
  252. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  253. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  254. else:
  255. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  256. if (env["arch"].startswith("x86") or env["arch"] == "") and any(platform.machine() in s for s in list_of_x86):
  257. env["x86_libtheora_opt_gcc"] = True
  258. if not env["builtin_libvpx"]:
  259. env.ParseConfig("pkg-config vpx --cflags --libs")
  260. if not env["builtin_libvorbis"]:
  261. env["builtin_libogg"] = False # Needed to link against system libvorbis
  262. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  263. if not env["builtin_opus"]:
  264. env["builtin_libogg"] = False # Needed to link against system opus
  265. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  266. if not env["builtin_libogg"]:
  267. env.ParseConfig("pkg-config ogg --cflags --libs")
  268. if not env["builtin_libwebp"]:
  269. env.ParseConfig("pkg-config libwebp --cflags --libs")
  270. if not env["builtin_mbedtls"]:
  271. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  272. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  273. if not env["builtin_wslay"]:
  274. env.ParseConfig("pkg-config libwslay --cflags --libs")
  275. if not env["builtin_miniupnpc"]:
  276. # No pkgconfig file so far, hardcode default paths.
  277. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  278. env.Append(LIBS=["miniupnpc"])
  279. # On Linux wchar_t should be 32-bits
  280. # 16-bit library shouldn't be required due to compiler optimisations
  281. if not env["builtin_pcre2"]:
  282. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  283. # Embree is only used in tools build on x86_64 and aarch64.
  284. if env["tools"] and not env["builtin_embree"] and host_is_64_bit:
  285. # No pkgconfig file so far, hardcode expected lib name.
  286. env.Append(LIBS=["embree3"])
  287. ## Flags
  288. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  289. env["alsa"] = True
  290. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  291. env.ParseConfig("pkg-config alsa --cflags") # Only cflags, we dlopen the library.
  292. else:
  293. print("Warning: ALSA libraries not found. Disabling the ALSA audio driver.")
  294. if env["pulseaudio"]:
  295. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  296. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  297. env.ParseConfig("pkg-config libpulse --cflags") # Only cflags, we dlopen the library.
  298. else:
  299. env["pulseaudio"] = False
  300. print("Warning: PulseAudio development libraries not found. Disabling the PulseAudio audio driver.")
  301. if env["speechd"]:
  302. if os.system("pkg-config --exists speech-dispatcher") == 0: # 0 means found
  303. env.Append(CPPDEFINES=["SPEECHD_ENABLED"])
  304. env.ParseConfig("pkg-config speech-dispatcher --cflags") # Only cflags, we dlopen the library.
  305. else:
  306. env["speechd"] = False
  307. print("Warning: Speech Dispatcher development libraries not found. Disabling Text-to-Speech support.")
  308. if platform.system() == "Linux":
  309. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  310. if env["udev"]:
  311. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  312. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  313. env.ParseConfig("pkg-config libudev --cflags") # Only cflags, we dlopen the library.
  314. else:
  315. env["udev"] = False
  316. print("Warning: libudev development libraries not found. Disabling controller hotplugging support.")
  317. else:
  318. env["udev"] = False # Linux specific
  319. # Linkflags below this line should typically stay the last ones
  320. if not env["builtin_zlib"]:
  321. env.ParseConfig("pkg-config zlib --cflags --libs")
  322. env.Prepend(CPPPATH=["#platform/x11"])
  323. env.Append(CPPDEFINES=["X11_ENABLED", "UNIX_ENABLED", "OPENGL_ENABLED", "GLES_ENABLED", ("_FILE_OFFSET_BITS", 64)])
  324. env.ParseConfig("pkg-config gl --cflags --libs")
  325. env.Append(LIBS=["pthread"])
  326. if platform.system() == "Linux":
  327. env.Append(LIBS=["dl"])
  328. if not env["execinfo"] and platform.libc_ver()[0] != "glibc":
  329. # The default crash handler depends on glibc, so if the host uses
  330. # a different libc (BSD libc, musl), fall back to libexecinfo.
  331. print("Note: Using `execinfo=yes` for the crash handler as required on platforms where glibc is missing.")
  332. env["execinfo"] = True
  333. if env["execinfo"]:
  334. env.Append(LIBS=["execinfo"])
  335. if not env["tools"]:
  336. import subprocess
  337. import re
  338. linker_version_str = subprocess.check_output(
  339. [env.subst(env["LINK"]), "-Wl,--version"] + env.subst(env["LINKFLAGS"])
  340. ).decode("utf-8")
  341. gnu_ld_version = re.search(r"^GNU ld [^$]*(\d+\.\d+)$", linker_version_str, re.MULTILINE)
  342. if not gnu_ld_version:
  343. print(
  344. "Warning: Creating export template binaries enabled for PCK embedding is currently only supported with GNU ld, not gold, LLD or mold."
  345. )
  346. else:
  347. if float(gnu_ld_version.group(1)) >= 2.30:
  348. env.Append(LINKFLAGS=["-T", "platform/x11/pck_embed.ld"])
  349. else:
  350. env.Append(LINKFLAGS=["-T", "platform/x11/pck_embed.legacy.ld"])
  351. # Link those statically for portability
  352. if env["use_static_cpp"]:
  353. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  354. if env["use_llvm"] and platform.system() != "FreeBSD":
  355. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  356. else:
  357. if env["use_llvm"] and platform.system() != "FreeBSD":
  358. env.Append(LIBS=["atomic"])