detect.py 19 KB

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