detect.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. def is_active():
  7. return True
  8. def get_name():
  9. return "LinuxBSD"
  10. def can_build():
  11. if os.name != "posix" or sys.platform == "darwin":
  12. return False
  13. pkgconf_error = os.system("pkg-config --version > /dev/null")
  14. if pkgconf_error:
  15. print("Error: pkg-config not found. Aborting.")
  16. return False
  17. return True
  18. def get_opts():
  19. from SCons.Variables import BoolVariable, EnumVariable
  20. return [
  21. EnumVariable("linker", "Linker program", "default", ("default", "bfd", "gold", "lld", "mold")),
  22. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  23. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  24. BoolVariable("use_coverage", "Test Godot coverage", False),
  25. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  26. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
  27. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN)", False),
  28. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
  29. BoolVariable("use_msan", "Use LLVM compiler memory sanitizer (MSAN)", False),
  30. BoolVariable("pulseaudio", "Detect and use PulseAudio", True),
  31. BoolVariable("dbus", "Detect and use D-Bus to handle screensaver and portal desktop settings", True),
  32. BoolVariable("speechd", "Detect and use Speech Dispatcher for Text-to-Speech support", True),
  33. BoolVariable("fontconfig", "Detect and use fontconfig for system fonts support", True),
  34. BoolVariable("udev", "Use udev for gamepad connection callbacks", True),
  35. BoolVariable("x11", "Enable X11 display", True),
  36. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  37. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  38. BoolVariable("touch", "Enable touch events", True),
  39. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  40. ]
  41. def get_flags():
  42. return [
  43. ("arch", detect_arch()),
  44. ]
  45. def configure(env):
  46. # Validate arch.
  47. supported_arches = ["x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc32", "ppc64"]
  48. if env["arch"] not in supported_arches:
  49. print(
  50. 'Unsupported CPU architecture "%s" for Linux / *BSD. Supported architectures are: %s.'
  51. % (env["arch"], ", ".join(supported_arches))
  52. )
  53. sys.exit()
  54. ## Build type
  55. if env["target"] == "release":
  56. if env["optimize"] == "speed": # optimize for speed (default)
  57. env.Prepend(CCFLAGS=["-O3"])
  58. elif env["optimize"] == "size": # optimize for size
  59. env.Prepend(CCFLAGS=["-Os"])
  60. if env["debug_symbols"]:
  61. env.Prepend(CCFLAGS=["-g2"])
  62. elif env["target"] == "release_debug":
  63. if env["optimize"] == "speed": # optimize for speed (default)
  64. env.Prepend(CCFLAGS=["-O2"])
  65. elif env["optimize"] == "size": # optimize for size
  66. env.Prepend(CCFLAGS=["-Os"])
  67. if env["debug_symbols"]:
  68. env.Prepend(CCFLAGS=["-g2"])
  69. elif env["target"] == "debug":
  70. env.Prepend(CCFLAGS=["-g3"])
  71. env.Append(LINKFLAGS=["-rdynamic"])
  72. # CPU architecture flags.
  73. if env["arch"] == "rv64":
  74. # G = General-purpose extensions, C = Compression extension (very common).
  75. env.Append(CCFLAGS=["-march=rv64gc"])
  76. ## Compiler configuration
  77. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  78. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  79. env["use_llvm"] = True
  80. if env["use_llvm"]:
  81. if "clang++" not in os.path.basename(env["CXX"]):
  82. env["CC"] = "clang"
  83. env["CXX"] = "clang++"
  84. env.extra_suffix = ".llvm" + env.extra_suffix
  85. if env["linker"] != "default":
  86. print("Using linker program: " + env["linker"])
  87. if env["linker"] == "mold" and using_gcc(env): # GCC < 12.1 doesn't support -fuse-ld=mold.
  88. cc_version = get_compiler_version(env)
  89. cc_semver = (int(cc_version["major"]), int(cc_version["minor"]))
  90. if cc_semver < (12, 1):
  91. found_wrapper = False
  92. for path in ["/usr/libexec", "/usr/local/libexec", "/usr/lib", "/usr/local/lib"]:
  93. if os.path.isfile(path + "/mold/ld"):
  94. env.Append(LINKFLAGS=["-B" + path + "/mold"])
  95. found_wrapper = True
  96. break
  97. if not found_wrapper:
  98. print("Couldn't locate mold installation path. Make sure it's installed in /usr or /usr/local.")
  99. sys.exit(255)
  100. else:
  101. env.Append(LINKFLAGS=["-fuse-ld=mold"])
  102. else:
  103. env.Append(LINKFLAGS=["-fuse-ld=%s" % env["linker"]])
  104. if env["use_coverage"]:
  105. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  106. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  107. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  108. env.extra_suffix += ".san"
  109. env.Append(CCFLAGS=["-DSANITIZERS_ENABLED"])
  110. if env["use_ubsan"]:
  111. env.Append(
  112. CCFLAGS=[
  113. "-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"
  114. ]
  115. )
  116. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  117. if env["use_llvm"]:
  118. env.Append(
  119. CCFLAGS=[
  120. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change"
  121. ]
  122. )
  123. else:
  124. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  125. if env["use_asan"]:
  126. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  127. env.Append(LINKFLAGS=["-fsanitize=address"])
  128. if env["use_lsan"]:
  129. env.Append(CCFLAGS=["-fsanitize=leak"])
  130. env.Append(LINKFLAGS=["-fsanitize=leak"])
  131. if env["use_tsan"]:
  132. env.Append(CCFLAGS=["-fsanitize=thread"])
  133. env.Append(LINKFLAGS=["-fsanitize=thread"])
  134. if env["use_msan"] and env["use_llvm"]:
  135. env.Append(CCFLAGS=["-fsanitize=memory"])
  136. env.Append(CCFLAGS=["-fsanitize-memory-track-origins"])
  137. env.Append(CCFLAGS=["-fsanitize-recover=memory"])
  138. env.Append(LINKFLAGS=["-fsanitize=memory"])
  139. # LTO
  140. if env["lto"] != "none":
  141. if env["lto"] == "thin":
  142. if not env["use_llvm"]:
  143. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  144. sys.exit(255)
  145. env.Append(CCFLAGS=["-flto=thin"])
  146. env.Append(LINKFLAGS=["-flto=thin"])
  147. elif not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  148. env.Append(CCFLAGS=["-flto"])
  149. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  150. else:
  151. env.Append(CCFLAGS=["-flto"])
  152. env.Append(LINKFLAGS=["-flto"])
  153. if not env["use_llvm"]:
  154. env["RANLIB"] = "gcc-ranlib"
  155. env["AR"] = "gcc-ar"
  156. env.Append(CCFLAGS=["-pipe"])
  157. ## Dependencies
  158. if env["x11"]:
  159. env.ParseConfig("pkg-config x11 --cflags --libs")
  160. env.ParseConfig("pkg-config xcursor --cflags --libs")
  161. env.ParseConfig("pkg-config xinerama --cflags --libs")
  162. env.ParseConfig("pkg-config xext --cflags --libs")
  163. env.ParseConfig("pkg-config xrandr --cflags --libs")
  164. env.ParseConfig("pkg-config xrender --cflags --libs")
  165. env.ParseConfig("pkg-config xi --cflags --libs")
  166. if env["touch"]:
  167. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  168. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  169. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  170. # as shared libraries leads to weird issues
  171. if (
  172. env["builtin_freetype"]
  173. or env["builtin_libpng"]
  174. or env["builtin_zlib"]
  175. or env["builtin_graphite"]
  176. or env["builtin_harfbuzz"]
  177. ):
  178. env["builtin_freetype"] = True
  179. env["builtin_libpng"] = True
  180. env["builtin_zlib"] = True
  181. env["builtin_graphite"] = True
  182. env["builtin_harfbuzz"] = True
  183. if not env["builtin_freetype"]:
  184. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  185. if not env["builtin_graphite"]:
  186. env.ParseConfig("pkg-config graphite2 --cflags --libs")
  187. if not env["builtin_icu"]:
  188. env.ParseConfig("pkg-config icu-uc --cflags --libs")
  189. if not env["builtin_harfbuzz"]:
  190. env.ParseConfig("pkg-config harfbuzz harfbuzz-icu --cflags --libs")
  191. if not env["builtin_libpng"]:
  192. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  193. if not env["builtin_enet"]:
  194. env.ParseConfig("pkg-config libenet --cflags --libs")
  195. if not env["builtin_squish"]:
  196. env.ParseConfig("pkg-config libsquish --cflags --libs")
  197. if not env["builtin_zstd"]:
  198. env.ParseConfig("pkg-config libzstd --cflags --libs")
  199. # Sound and video libraries
  200. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  201. if not env["builtin_libtheora"]:
  202. env["builtin_libogg"] = False # Needed to link against system libtheora
  203. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  204. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  205. else:
  206. if env["arch"] in ["x86_64", "x86_32"]:
  207. env["x86_libtheora_opt_gcc"] = True
  208. if not env["builtin_libvorbis"]:
  209. env["builtin_libogg"] = False # Needed to link against system libvorbis
  210. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  211. if not env["builtin_libogg"]:
  212. env.ParseConfig("pkg-config ogg --cflags --libs")
  213. if not env["builtin_libwebp"]:
  214. env.ParseConfig("pkg-config libwebp --cflags --libs")
  215. if not env["builtin_mbedtls"]:
  216. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  217. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  218. if not env["builtin_wslay"]:
  219. env.ParseConfig("pkg-config libwslay --cflags --libs")
  220. if not env["builtin_miniupnpc"]:
  221. # No pkgconfig file so far, hardcode default paths.
  222. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  223. env.Append(LIBS=["miniupnpc"])
  224. # On Linux wchar_t should be 32-bits
  225. # 16-bit library shouldn't be required due to compiler optimisations
  226. if not env["builtin_pcre2"]:
  227. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  228. if not env["builtin_embree"]:
  229. # No pkgconfig file so far, hardcode expected lib name.
  230. env.Append(LIBS=["embree3"])
  231. ## Flags
  232. if env["fontconfig"]:
  233. if os.system("pkg-config --exists fontconfig") == 0: # 0 means found
  234. env.Append(CPPDEFINES=["FONTCONFIG_ENABLED"])
  235. env.ParseConfig("pkg-config fontconfig --cflags") # Only cflags, we dlopen the library.
  236. else:
  237. env["fontconfig"] = False
  238. print("Warning: fontconfig libraries not found. Disabling the system fonts support.")
  239. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  240. env["alsa"] = True
  241. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  242. env.ParseConfig("pkg-config alsa --cflags") # Only cflags, we dlopen the library.
  243. else:
  244. print("Warning: ALSA libraries not found. Disabling the ALSA audio driver.")
  245. if env["pulseaudio"]:
  246. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  247. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  248. env.ParseConfig("pkg-config libpulse --cflags") # Only cflags, we dlopen the library.
  249. else:
  250. env["pulseaudio"] = False
  251. print("Warning: PulseAudio development libraries not found. Disabling the PulseAudio audio driver.")
  252. if env["dbus"]:
  253. if os.system("pkg-config --exists dbus-1") == 0: # 0 means found
  254. env.Append(CPPDEFINES=["DBUS_ENABLED"])
  255. env.ParseConfig("pkg-config dbus-1 --cflags") # Only cflags, we dlopen the library.
  256. else:
  257. env["dbus"] = False
  258. print("Warning: D-Bus development libraries not found. Disabling screensaver prevention.")
  259. if env["speechd"]:
  260. if os.system("pkg-config --exists speech-dispatcher") == 0: # 0 means found
  261. env.Append(CPPDEFINES=["SPEECHD_ENABLED"])
  262. env.ParseConfig("pkg-config speech-dispatcher --cflags") # Only cflags, we dlopen the library.
  263. else:
  264. env["speechd"] = False
  265. print("Warning: Speech Dispatcher development libraries not found. Disabling Text-to-Speech support.")
  266. if platform.system() == "Linux":
  267. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  268. if env["udev"]:
  269. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  270. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  271. env.ParseConfig("pkg-config libudev --cflags") # Only cflags, we dlopen the library.
  272. else:
  273. env["udev"] = False
  274. print("Warning: libudev development libraries not found. Disabling controller hotplugging support.")
  275. else:
  276. env["udev"] = False # Linux specific
  277. # Linkflags below this line should typically stay the last ones
  278. if not env["builtin_zlib"]:
  279. env.ParseConfig("pkg-config zlib --cflags --libs")
  280. env.Prepend(CPPPATH=["#platform/linuxbsd"])
  281. if env["x11"]:
  282. if not env["vulkan"]:
  283. print("Error: X11 support requires vulkan=yes")
  284. env.Exit(255)
  285. env.Append(CPPDEFINES=["X11_ENABLED"])
  286. env.Append(CPPDEFINES=["UNIX_ENABLED"])
  287. env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
  288. if env["vulkan"]:
  289. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  290. if not env["use_volk"]:
  291. env.ParseConfig("pkg-config vulkan --cflags --libs")
  292. if not env["builtin_glslang"]:
  293. # No pkgconfig file so far, hardcode expected lib name.
  294. env.Append(LIBS=["glslang", "SPIRV"])
  295. if env["opengl3"]:
  296. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  297. env.ParseConfig("pkg-config gl --cflags --libs")
  298. env.Append(LIBS=["pthread"])
  299. if platform.system() == "Linux":
  300. env.Append(LIBS=["dl"])
  301. if platform.system().find("BSD") >= 0:
  302. env["execinfo"] = True
  303. if env["execinfo"]:
  304. env.Append(LIBS=["execinfo"])
  305. if not env["tools"]:
  306. import subprocess
  307. import re
  308. linker_version_str = subprocess.check_output(
  309. [env.subst(env["LINK"]), "-Wl,--version"] + env.subst(env["LINKFLAGS"])
  310. ).decode("utf-8")
  311. gnu_ld_version = re.search("^GNU ld [^$]*(\d+\.\d+)$", linker_version_str, re.MULTILINE)
  312. if not gnu_ld_version:
  313. print(
  314. "Warning: Creating template binaries enabled for PCK embedding is currently only supported with GNU ld, not gold or LLD."
  315. )
  316. else:
  317. if float(gnu_ld_version.group(1)) >= 2.30:
  318. env.Append(LINKFLAGS=["-T", "platform/linuxbsd/pck_embed.ld"])
  319. else:
  320. env.Append(LINKFLAGS=["-T", "platform/linuxbsd/pck_embed.legacy.ld"])
  321. ## Cross-compilation
  322. # TODO: Support cross-compilation on architectures other than x86.
  323. host_is_64_bit = sys.maxsize > 2**32
  324. if host_is_64_bit and env["arch"] == "x86_32":
  325. env.Append(CCFLAGS=["-m32"])
  326. env.Append(LINKFLAGS=["-m32", "-L/usr/lib/i386-linux-gnu"])
  327. elif not host_is_64_bit and env["arch"] == "x86_64":
  328. env.Append(CCFLAGS=["-m64"])
  329. env.Append(LINKFLAGS=["-m64", "-L/usr/lib/i686-linux-gnu"])
  330. # Link those statically for portability
  331. if env["use_static_cpp"]:
  332. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  333. if env["use_llvm"]:
  334. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  335. else:
  336. if env["use_llvm"]:
  337. env.Append(LIBS=["atomic"])