detect.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import os
  2. import platform
  3. import sys
  4. from typing import TYPE_CHECKING
  5. from methods import get_compiler_version, print_error, print_warning, using_gcc
  6. from platform_methods import detect_arch
  7. if TYPE_CHECKING:
  8. from SCons.Script.SConscript import SConsEnvironment
  9. def get_name():
  10. return "LinuxBSD"
  11. def can_build():
  12. if os.name != "posix" or sys.platform == "darwin":
  13. return False
  14. pkgconf_error = os.system("pkg-config --version > /dev/null")
  15. if pkgconf_error:
  16. print_error("pkg-config not found. Aborting.")
  17. return False
  18. return True
  19. def get_opts():
  20. from SCons.Variables import BoolVariable, EnumVariable
  21. return [
  22. EnumVariable("linker", "Linker program", "default", ("default", "bfd", "gold", "lld", "mold")),
  23. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  24. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  25. BoolVariable("use_coverage", "Test Godot coverage", False),
  26. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  27. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
  28. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN)", False),
  29. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
  30. BoolVariable("use_msan", "Use LLVM compiler memory sanitizer (MSAN)", False),
  31. BoolVariable("use_sowrap", "Dynamically load system libraries", True),
  32. BoolVariable("alsa", "Use ALSA", True),
  33. BoolVariable("pulseaudio", "Use PulseAudio", True),
  34. BoolVariable("dbus", "Use D-Bus to handle screensaver and portal desktop settings", True),
  35. BoolVariable("speechd", "Use Speech Dispatcher for Text-to-Speech support", True),
  36. BoolVariable("fontconfig", "Use fontconfig for system fonts support", True),
  37. BoolVariable("udev", "Use udev for gamepad connection callbacks", True),
  38. BoolVariable("x11", "Enable X11 display", True),
  39. BoolVariable("wayland", "Enable Wayland display", True),
  40. BoolVariable("libdecor", "Enable libdecor support", True),
  41. BoolVariable("touch", "Enable touch events", True),
  42. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  43. ]
  44. def get_doc_classes():
  45. return [
  46. "EditorExportPlatformLinuxBSD",
  47. ]
  48. def get_doc_path():
  49. return "doc_classes"
  50. def get_flags():
  51. return {
  52. "arch": detect_arch(),
  53. "supported": ["mono"],
  54. }
  55. def configure(env: "SConsEnvironment"):
  56. # Validate arch.
  57. supported_arches = ["x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc32", "ppc64"]
  58. if env["arch"] not in supported_arches:
  59. print_error(
  60. 'Unsupported CPU architecture "%s" for Linux / *BSD. Supported architectures are: %s.'
  61. % (env["arch"], ", ".join(supported_arches))
  62. )
  63. sys.exit(255)
  64. ## Build type
  65. if env.dev_build:
  66. # This is needed for our crash handler to work properly.
  67. # gdb works fine without it though, so maybe our crash handler could too.
  68. env.Append(LINKFLAGS=["-rdynamic"])
  69. # Cross-compilation
  70. # TODO: Support cross-compilation on architectures other than x86.
  71. host_is_64_bit = sys.maxsize > 2**32
  72. if host_is_64_bit and env["arch"] == "x86_32":
  73. env.Append(CCFLAGS=["-m32"])
  74. env.Append(LINKFLAGS=["-m32"])
  75. elif not host_is_64_bit and env["arch"] == "x86_64":
  76. env.Append(CCFLAGS=["-m64"])
  77. env.Append(LINKFLAGS=["-m64"])
  78. # CPU architecture flags.
  79. if env["arch"] == "rv64":
  80. # G = General-purpose extensions, C = Compression extension (very common).
  81. env.Append(CCFLAGS=["-march=rv64gc"])
  82. ## Compiler configuration
  83. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  84. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  85. env["use_llvm"] = True
  86. if env["use_llvm"]:
  87. if "clang++" not in os.path.basename(env["CXX"]):
  88. env["CC"] = "clang"
  89. env["CXX"] = "clang++"
  90. env.extra_suffix = ".llvm" + env.extra_suffix
  91. if env["linker"] != "default":
  92. print("Using linker program: " + env["linker"])
  93. if env["linker"] == "mold" and using_gcc(env): # GCC < 12.1 doesn't support -fuse-ld=mold.
  94. cc_version = get_compiler_version(env)
  95. cc_semver = (cc_version["major"], cc_version["minor"])
  96. if cc_semver < (12, 1):
  97. found_wrapper = False
  98. for path in ["/usr/libexec", "/usr/local/libexec", "/usr/lib", "/usr/local/lib"]:
  99. if os.path.isfile(path + "/mold/ld"):
  100. env.Append(LINKFLAGS=["-B" + path + "/mold"])
  101. found_wrapper = True
  102. break
  103. if not found_wrapper:
  104. print_error(
  105. "Couldn't locate mold installation path. Make sure it's installed in /usr or /usr/local."
  106. )
  107. sys.exit(255)
  108. else:
  109. env.Append(LINKFLAGS=["-fuse-ld=mold"])
  110. else:
  111. env.Append(LINKFLAGS=["-fuse-ld=%s" % env["linker"]])
  112. if env["use_coverage"]:
  113. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  114. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  115. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  116. env.extra_suffix += ".san"
  117. env.Append(CCFLAGS=["-DSANITIZERS_ENABLED"])
  118. if env["use_ubsan"]:
  119. env.Append(
  120. CCFLAGS=[
  121. "-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"
  122. ]
  123. )
  124. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  125. if env["use_llvm"]:
  126. env.Append(
  127. CCFLAGS=[
  128. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change"
  129. ]
  130. )
  131. else:
  132. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  133. if env["use_asan"]:
  134. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  135. env.Append(LINKFLAGS=["-fsanitize=address"])
  136. if env["use_lsan"]:
  137. env.Append(CCFLAGS=["-fsanitize=leak"])
  138. env.Append(LINKFLAGS=["-fsanitize=leak"])
  139. if env["use_tsan"]:
  140. env.Append(CCFLAGS=["-fsanitize=thread"])
  141. env.Append(LINKFLAGS=["-fsanitize=thread"])
  142. if env["use_msan"] and env["use_llvm"]:
  143. env.Append(CCFLAGS=["-fsanitize=memory"])
  144. env.Append(CCFLAGS=["-fsanitize-memory-track-origins"])
  145. env.Append(CCFLAGS=["-fsanitize-recover=memory"])
  146. env.Append(LINKFLAGS=["-fsanitize=memory"])
  147. env.Append(CCFLAGS=["-ffp-contract=off"])
  148. # LTO
  149. if env["lto"] == "auto": # Full LTO for production.
  150. env["lto"] = "full"
  151. if env["lto"] != "none":
  152. if env["lto"] == "thin":
  153. if not env["use_llvm"]:
  154. print_error("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  155. sys.exit(255)
  156. env.Append(CCFLAGS=["-flto=thin"])
  157. env.Append(LINKFLAGS=["-flto=thin"])
  158. elif not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  159. env.Append(CCFLAGS=["-flto"])
  160. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  161. else:
  162. env.Append(CCFLAGS=["-flto"])
  163. env.Append(LINKFLAGS=["-flto"])
  164. if not env["use_llvm"]:
  165. env["RANLIB"] = "gcc-ranlib"
  166. env["AR"] = "gcc-ar"
  167. env.Append(CCFLAGS=["-pipe"])
  168. ## Dependencies
  169. if env["use_sowrap"]:
  170. env.Append(CPPDEFINES=["SOWRAP_ENABLED"])
  171. if env["wayland"]:
  172. if os.system("wayland-scanner -v 2>/dev/null") != 0:
  173. print_warning("wayland-scanner not found. Disabling Wayland support.")
  174. env["wayland"] = False
  175. if env["touch"]:
  176. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  177. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  178. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  179. # as shared libraries leads to weird issues. And graphite and harfbuzz need freetype.
  180. ft_linked_deps = [
  181. env["builtin_freetype"],
  182. env["builtin_libpng"],
  183. env["builtin_zlib"],
  184. env["builtin_graphite"],
  185. env["builtin_harfbuzz"],
  186. ]
  187. if (not all(ft_linked_deps)) and any(ft_linked_deps): # All or nothing.
  188. print_error(
  189. "These libraries should be either all builtin, or all system provided:\n"
  190. "freetype, libpng, zlib, graphite, harfbuzz.\n"
  191. "Please specify `builtin_<name>=no` for all of them, or none."
  192. )
  193. sys.exit(255)
  194. if not env["builtin_freetype"]:
  195. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  196. if not env["builtin_graphite"]:
  197. env.ParseConfig("pkg-config graphite2 --cflags --libs")
  198. if not env["builtin_icu4c"]:
  199. env.ParseConfig("pkg-config icu-i18n icu-uc --cflags --libs")
  200. if not env["builtin_harfbuzz"]:
  201. env.ParseConfig("pkg-config harfbuzz harfbuzz-icu --cflags --libs")
  202. if not env["builtin_libpng"]:
  203. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  204. if not env["builtin_enet"]:
  205. env.ParseConfig("pkg-config libenet --cflags --libs")
  206. if not env["builtin_zstd"]:
  207. env.ParseConfig("pkg-config libzstd --cflags --libs")
  208. if env["brotli"] and not env["builtin_brotli"]:
  209. env.ParseConfig("pkg-config libbrotlicommon libbrotlidec --cflags --libs")
  210. # Sound and video libraries
  211. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  212. if not env["builtin_libtheora"]:
  213. env["builtin_libogg"] = False # Needed to link against system libtheora
  214. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  215. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  216. else:
  217. if env["arch"] in ["x86_64", "x86_32"]:
  218. env["x86_libtheora_opt_gcc"] = True
  219. if not env["builtin_libvorbis"]:
  220. env["builtin_libogg"] = False # Needed to link against system libvorbis
  221. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  222. if not env["builtin_libogg"]:
  223. env.ParseConfig("pkg-config ogg --cflags --libs")
  224. if not env["builtin_libwebp"]:
  225. env.ParseConfig("pkg-config libwebp --cflags --libs")
  226. if not env["builtin_mbedtls"]:
  227. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  228. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  229. if not env["builtin_wslay"]:
  230. env.ParseConfig("pkg-config libwslay --cflags --libs")
  231. if not env["builtin_miniupnpc"]:
  232. # No pkgconfig file so far, hardcode default paths.
  233. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  234. env.Append(LIBS=["miniupnpc"])
  235. # On Linux wchar_t should be 32-bits
  236. # 16-bit library shouldn't be required due to compiler optimizations
  237. if not env["builtin_pcre2"]:
  238. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  239. if not env["builtin_recastnavigation"]:
  240. # No pkgconfig file so far, hardcode default paths.
  241. env.Prepend(CPPPATH=["/usr/include/recastnavigation"])
  242. env.Append(LIBS=["Recast"])
  243. if not env["builtin_embree"] and env["arch"] in ["x86_64", "arm64"]:
  244. # No pkgconfig file so far, hardcode expected lib name.
  245. env.Append(LIBS=["embree4"])
  246. if not env["builtin_openxr"]:
  247. env.ParseConfig("pkg-config openxr --cflags --libs")
  248. if env["fontconfig"]:
  249. if not env["use_sowrap"]:
  250. if os.system("pkg-config --exists fontconfig") == 0: # 0 means found
  251. env.ParseConfig("pkg-config fontconfig --cflags --libs")
  252. env.Append(CPPDEFINES=["FONTCONFIG_ENABLED"])
  253. else:
  254. print_warning("fontconfig development libraries not found. Disabling the system fonts support.")
  255. env["fontconfig"] = False
  256. else:
  257. env.Append(CPPDEFINES=["FONTCONFIG_ENABLED"])
  258. if env["alsa"]:
  259. if not env["use_sowrap"]:
  260. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  261. env.ParseConfig("pkg-config alsa --cflags --libs")
  262. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  263. else:
  264. print_warning("ALSA development libraries not found. Disabling the ALSA audio driver.")
  265. env["alsa"] = False
  266. else:
  267. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  268. if env["pulseaudio"]:
  269. if not env["use_sowrap"]:
  270. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  271. env.ParseConfig("pkg-config libpulse --cflags --libs")
  272. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  273. else:
  274. print_warning("PulseAudio development libraries not found. Disabling the PulseAudio audio driver.")
  275. env["pulseaudio"] = False
  276. else:
  277. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED", "_REENTRANT"])
  278. if env["dbus"]:
  279. if not env["use_sowrap"]:
  280. if os.system("pkg-config --exists dbus-1") == 0: # 0 means found
  281. env.ParseConfig("pkg-config dbus-1 --cflags --libs")
  282. env.Append(CPPDEFINES=["DBUS_ENABLED"])
  283. else:
  284. print_warning("D-Bus development libraries not found. Disabling screensaver prevention.")
  285. env["dbus"] = False
  286. else:
  287. env.Append(CPPDEFINES=["DBUS_ENABLED"])
  288. if env["speechd"]:
  289. if not env["use_sowrap"]:
  290. if os.system("pkg-config --exists speech-dispatcher") == 0: # 0 means found
  291. env.ParseConfig("pkg-config speech-dispatcher --cflags --libs")
  292. env.Append(CPPDEFINES=["SPEECHD_ENABLED"])
  293. else:
  294. print_warning("speech-dispatcher development libraries not found. Disabling text to speech support.")
  295. env["speechd"] = False
  296. else:
  297. env.Append(CPPDEFINES=["SPEECHD_ENABLED"])
  298. if not env["use_sowrap"]:
  299. if os.system("pkg-config --exists xkbcommon") == 0: # 0 means found
  300. env.ParseConfig("pkg-config xkbcommon --cflags --libs")
  301. env.Append(CPPDEFINES=["XKB_ENABLED"])
  302. else:
  303. if env["wayland"]:
  304. print_error("libxkbcommon development libraries required by Wayland not found. Aborting.")
  305. sys.exit(255)
  306. else:
  307. print_warning(
  308. "libxkbcommon development libraries not found. Disabling dead key composition and key label support."
  309. )
  310. else:
  311. env.Append(CPPDEFINES=["XKB_ENABLED"])
  312. if platform.system() == "Linux":
  313. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  314. if env["udev"]:
  315. if not env["use_sowrap"]:
  316. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  317. env.ParseConfig("pkg-config libudev --cflags --libs")
  318. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  319. else:
  320. print_warning("libudev development libraries not found. Disabling controller hotplugging support.")
  321. env["udev"] = False
  322. else:
  323. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  324. else:
  325. env["udev"] = False # Linux specific
  326. # Linkflags below this line should typically stay the last ones
  327. if not env["builtin_zlib"]:
  328. env.ParseConfig("pkg-config zlib --cflags --libs")
  329. env.Prepend(CPPPATH=["#platform/linuxbsd"])
  330. if env["use_sowrap"]:
  331. env.Prepend(CPPPATH=["#thirdparty/linuxbsd_headers"])
  332. env.Append(
  333. CPPDEFINES=[
  334. "LINUXBSD_ENABLED",
  335. "UNIX_ENABLED",
  336. ("_FILE_OFFSET_BITS", 64),
  337. ]
  338. )
  339. if env["x11"]:
  340. if not env["use_sowrap"]:
  341. if os.system("pkg-config --exists x11"):
  342. print_error("X11 libraries not found. Aborting.")
  343. sys.exit(255)
  344. env.ParseConfig("pkg-config x11 --cflags --libs")
  345. if os.system("pkg-config --exists xcursor"):
  346. print_error("Xcursor library not found. Aborting.")
  347. sys.exit(255)
  348. env.ParseConfig("pkg-config xcursor --cflags --libs")
  349. if os.system("pkg-config --exists xinerama"):
  350. print_error("Xinerama library not found. Aborting.")
  351. sys.exit(255)
  352. env.ParseConfig("pkg-config xinerama --cflags --libs")
  353. if os.system("pkg-config --exists xext"):
  354. print_error("Xext library not found. Aborting.")
  355. sys.exit(255)
  356. env.ParseConfig("pkg-config xext --cflags --libs")
  357. if os.system("pkg-config --exists xrandr"):
  358. print_error("XrandR library not found. Aborting.")
  359. sys.exit(255)
  360. env.ParseConfig("pkg-config xrandr --cflags --libs")
  361. if os.system("pkg-config --exists xrender"):
  362. print_error("XRender library not found. Aborting.")
  363. sys.exit(255)
  364. env.ParseConfig("pkg-config xrender --cflags --libs")
  365. if os.system("pkg-config --exists xi"):
  366. print_error("Xi library not found. Aborting.")
  367. sys.exit(255)
  368. env.ParseConfig("pkg-config xi --cflags --libs")
  369. env.Append(CPPDEFINES=["X11_ENABLED"])
  370. if env["wayland"]:
  371. if not env["use_sowrap"]:
  372. if os.system("pkg-config --exists libdecor-0"):
  373. print_warning("libdecor development libraries not found. Disabling client-side decorations.")
  374. env["libdecor"] = False
  375. else:
  376. env.ParseConfig("pkg-config libdecor-0 --cflags --libs")
  377. if os.system("pkg-config --exists wayland-client"):
  378. print_error("Wayland client library not found. Aborting.")
  379. sys.exit(255)
  380. env.ParseConfig("pkg-config wayland-client --cflags --libs")
  381. if os.system("pkg-config --exists wayland-cursor"):
  382. print_error("Wayland cursor library not found. Aborting.")
  383. sys.exit(255)
  384. env.ParseConfig("pkg-config wayland-cursor --cflags --libs")
  385. if os.system("pkg-config --exists wayland-egl"):
  386. print_error("Wayland EGL library not found. Aborting.")
  387. sys.exit(255)
  388. env.ParseConfig("pkg-config wayland-egl --cflags --libs")
  389. if env["libdecor"]:
  390. env.Append(CPPDEFINES=["LIBDECOR_ENABLED"])
  391. env.Prepend(CPPPATH=["#platform/linuxbsd", "#thirdparty/linuxbsd_headers/wayland/"])
  392. env.Append(CPPDEFINES=["WAYLAND_ENABLED"])
  393. env.Append(LIBS=["rt"]) # Needed by glibc, used by _allocate_shm_file
  394. if env["vulkan"]:
  395. env.Append(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  396. if not env["use_volk"]:
  397. env.ParseConfig("pkg-config vulkan --cflags --libs")
  398. if not env["builtin_glslang"]:
  399. # No pkgconfig file so far, hardcode expected lib name.
  400. env.Append(LIBS=["glslang", "SPIRV"])
  401. if env["opengl3"]:
  402. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  403. env.Append(LIBS=["pthread"])
  404. if platform.system() == "Linux":
  405. env.Append(LIBS=["dl"])
  406. if platform.libc_ver()[0] != "glibc":
  407. if env["execinfo"]:
  408. env.Append(LIBS=["execinfo"])
  409. env.Append(CPPDEFINES=["CRASH_HANDLER_ENABLED"])
  410. else:
  411. # The default crash handler depends on glibc, so if the host uses
  412. # a different libc (BSD libc, musl), libexecinfo is required.
  413. print("Note: Using `execinfo=no` disables the crash handler on platforms where glibc is missing.")
  414. else:
  415. env.Append(CPPDEFINES=["CRASH_HANDLER_ENABLED"])
  416. if platform.system() == "FreeBSD":
  417. env.Append(LINKFLAGS=["-lkvm"])
  418. # Link those statically for portability
  419. if env["use_static_cpp"]:
  420. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  421. if env["use_llvm"] and platform.system() != "FreeBSD":
  422. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  423. else:
  424. if env["use_llvm"] and platform.system() != "FreeBSD":
  425. env.Append(LIBS=["atomic"])