detect.py 21 KB

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