detect.py 17 KB

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