detect.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import os
  2. import platform
  3. import sys
  4. from methods import get_compiler_version, using_gcc
  5. def is_active():
  6. return True
  7. def get_name():
  8. return "LinuxBSD"
  9. def can_build():
  10. if os.name != "posix" or sys.platform == "darwin":
  11. return False
  12. pkgconf_error = os.system("pkg-config --version > /dev/null")
  13. if pkgconf_error:
  14. print("Error: pkg-config not found. Aborting.")
  15. return False
  16. return True
  17. def get_opts():
  18. from SCons.Variables import BoolVariable, EnumVariable
  19. return [
  20. EnumVariable("linker", "Linker program", "default", ("default", "bfd", "gold", "lld", "mold")),
  21. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  22. BoolVariable("use_thinlto", "Use ThinLTO (LLVM only, requires linker=lld, implies use_lto=yes)", 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", 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. def configure(env):
  44. ## Build type
  45. if env["target"] == "release":
  46. if env["optimize"] == "speed": # optimize for speed (default)
  47. env.Prepend(CCFLAGS=["-O3"])
  48. elif env["optimize"] == "size": # optimize for size
  49. env.Prepend(CCFLAGS=["-Os"])
  50. if env["debug_symbols"]:
  51. env.Prepend(CCFLAGS=["-g2"])
  52. elif env["target"] == "release_debug":
  53. if env["optimize"] == "speed": # optimize for speed (default)
  54. env.Prepend(CCFLAGS=["-O2"])
  55. elif env["optimize"] == "size": # optimize for size
  56. env.Prepend(CCFLAGS=["-Os"])
  57. if env["debug_symbols"]:
  58. env.Prepend(CCFLAGS=["-g2"])
  59. elif env["target"] == "debug":
  60. env.Prepend(CCFLAGS=["-g3"])
  61. env.Append(LINKFLAGS=["-rdynamic"])
  62. ## Architecture
  63. is64 = sys.maxsize > 2**32
  64. if env["bits"] == "default":
  65. env["bits"] = "64" if is64 else "32"
  66. machines = {
  67. "riscv64": "rv64",
  68. "ppc64le": "ppc64",
  69. "ppc64": "ppc64",
  70. "ppcle": "ppc",
  71. "ppc": "ppc",
  72. }
  73. if env["arch"] == "" and platform.machine() in machines:
  74. env["arch"] = machines[platform.machine()]
  75. if env["arch"] == "rv64":
  76. # G = General-purpose extensions, C = Compression extension (very common).
  77. env.Append(CCFLAGS=["-march=rv64gc"])
  78. ## Compiler configuration
  79. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  80. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  81. env["use_llvm"] = True
  82. if env["use_llvm"]:
  83. if "clang++" not in os.path.basename(env["CXX"]):
  84. env["CC"] = "clang"
  85. env["CXX"] = "clang++"
  86. env.extra_suffix = ".llvm" + env.extra_suffix
  87. if env["linker"] != "default":
  88. print("Using linker program: " + env["linker"])
  89. if env["linker"] == "mold" and using_gcc(env): # GCC < 12.1 doesn't support -fuse-ld=mold.
  90. cc_version = get_compiler_version(env)
  91. cc_semver = (int(cc_version["major"]), int(cc_version["minor"]))
  92. if cc_semver < (12, 1):
  93. found_wrapper = False
  94. for path in ["/usr/libexec", "/usr/local/libexec", "/usr/lib", "/usr/local/lib"]:
  95. if os.path.isfile(path + "/mold/ld"):
  96. env.Append(LINKFLAGS=["-B" + path + "/mold"])
  97. found_wrapper = True
  98. break
  99. if not found_wrapper:
  100. print("Couldn't locate mold installation path. Make sure it's installed in /usr or /usr/local.")
  101. sys.exit(255)
  102. else:
  103. env.Append(LINKFLAGS=["-fuse-ld=mold"])
  104. else:
  105. env.Append(LINKFLAGS=["-fuse-ld=%s" % env["linker"]])
  106. if env["use_thinlto"]:
  107. if not env["use_llvm"] or env["linker"] != "lld":
  108. print("ThinLTO is only compatible with LLVM and the LLD linker, use `use_llvm=yes linker=lld`.")
  109. sys.exit(255)
  110. else:
  111. env["use_lto"] = True # ThinLTO implies LTO
  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. if env["use_lto"]:
  148. if env["use_thinlto"]:
  149. env.Append(CCFLAGS=["-flto=thin"])
  150. env.Append(LINKFLAGS=["-flto=thin"])
  151. elif not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  152. env.Append(CCFLAGS=["-flto"])
  153. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  154. else:
  155. env.Append(CCFLAGS=["-flto"])
  156. env.Append(LINKFLAGS=["-flto"])
  157. if not env["use_llvm"]:
  158. env["RANLIB"] = "gcc-ranlib"
  159. env["AR"] = "gcc-ar"
  160. env.Append(CCFLAGS=["-pipe"])
  161. ## Dependencies
  162. if env["x11"]:
  163. env.ParseConfig("pkg-config x11 --cflags --libs")
  164. env.ParseConfig("pkg-config xcursor --cflags --libs")
  165. env.ParseConfig("pkg-config xinerama --cflags --libs")
  166. env.ParseConfig("pkg-config xext --cflags --libs")
  167. env.ParseConfig("pkg-config xrandr --cflags --libs")
  168. env.ParseConfig("pkg-config xrender --cflags --libs")
  169. env.ParseConfig("pkg-config xi --cflags --libs")
  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
  175. if (
  176. env["builtin_freetype"]
  177. or env["builtin_libpng"]
  178. or env["builtin_zlib"]
  179. or env["builtin_graphite"]
  180. or env["builtin_harfbuzz"]
  181. ):
  182. env["builtin_freetype"] = True
  183. env["builtin_libpng"] = True
  184. env["builtin_zlib"] = True
  185. env["builtin_graphite"] = True
  186. env["builtin_harfbuzz"] = True
  187. if not env["builtin_freetype"]:
  188. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  189. if not env["builtin_graphite"]:
  190. env.ParseConfig("pkg-config graphite2 --cflags --libs")
  191. if not env["builtin_icu"]:
  192. env.ParseConfig("pkg-config icu-uc --cflags --libs")
  193. if not env["builtin_harfbuzz"]:
  194. env.ParseConfig("pkg-config harfbuzz harfbuzz-icu --cflags --libs")
  195. if not env["builtin_libpng"]:
  196. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  197. if not env["builtin_enet"]:
  198. env.ParseConfig("pkg-config libenet --cflags --libs")
  199. if not env["builtin_squish"]:
  200. env.ParseConfig("pkg-config libsquish --cflags --libs")
  201. if not env["builtin_zstd"]:
  202. env.ParseConfig("pkg-config libzstd --cflags --libs")
  203. # Sound and video libraries
  204. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  205. if not env["builtin_libtheora"]:
  206. env["builtin_libogg"] = False # Needed to link against system libtheora
  207. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  208. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  209. else:
  210. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  211. if any(platform.machine() in s for s in list_of_x86):
  212. env["x86_libtheora_opt_gcc"] = True
  213. if not env["builtin_libvorbis"]:
  214. env["builtin_libogg"] = False # Needed to link against system libvorbis
  215. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  216. if not env["builtin_libogg"]:
  217. env.ParseConfig("pkg-config ogg --cflags --libs")
  218. if not env["builtin_libwebp"]:
  219. env.ParseConfig("pkg-config libwebp --cflags --libs")
  220. if not env["builtin_mbedtls"]:
  221. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  222. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  223. if not env["builtin_wslay"]:
  224. env.ParseConfig("pkg-config libwslay --cflags --libs")
  225. if not env["builtin_miniupnpc"]:
  226. # No pkgconfig file so far, hardcode default paths.
  227. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  228. env.Append(LIBS=["miniupnpc"])
  229. # On Linux wchar_t should be 32-bits
  230. # 16-bit library shouldn't be required due to compiler optimisations
  231. if not env["builtin_pcre2"]:
  232. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  233. if not env["builtin_embree"]:
  234. # No pkgconfig file so far, hardcode expected lib name.
  235. env.Append(LIBS=["embree3"])
  236. ## Flags
  237. if env["fontconfig"]:
  238. if os.system("pkg-config --exists fontconfig") == 0: # 0 means found
  239. env.Append(CPPDEFINES=["FONTCONFIG_ENABLED"])
  240. env.ParseConfig("pkg-config fontconfig --cflags") # Only cflags, we dlopen the library.
  241. else:
  242. env["fontconfig"] = False
  243. print("Warning: fontconfig libraries not found. Disabling the system fonts support.")
  244. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  245. env["alsa"] = True
  246. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  247. env.ParseConfig("pkg-config alsa --cflags") # Only cflags, we dlopen the library.
  248. else:
  249. print("Warning: ALSA libraries not found. Disabling the ALSA audio driver.")
  250. if env["pulseaudio"]:
  251. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  252. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  253. env.ParseConfig("pkg-config libpulse --cflags") # Only cflags, we dlopen the library.
  254. else:
  255. env["pulseaudio"] = False
  256. print("Warning: PulseAudio development libraries not found. Disabling the PulseAudio audio driver.")
  257. if env["dbus"]:
  258. if os.system("pkg-config --exists dbus-1") == 0: # 0 means found
  259. env.Append(CPPDEFINES=["DBUS_ENABLED"])
  260. env.ParseConfig("pkg-config dbus-1 --cflags") # Only cflags, we dlopen the library.
  261. else:
  262. env["dbus"] = False
  263. print("Warning: D-Bus development libraries not found. Disabling screensaver prevention.")
  264. if env["speechd"]:
  265. if os.system("pkg-config --exists speech-dispatcher") == 0: # 0 means found
  266. env.Append(CPPDEFINES=["SPEECHD_ENABLED"])
  267. env.ParseConfig("pkg-config speech-dispatcher --cflags") # Only cflags, we dlopen the library.
  268. else:
  269. env["speechd"] = False
  270. print("Warning: Speech Dispatcher development libraries not found. Disabling Text-to-Speech support.")
  271. if platform.system() == "Linux":
  272. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  273. if env["udev"]:
  274. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  275. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  276. env.ParseConfig("pkg-config libudev --cflags") # Only cflags, we dlopen the library.
  277. else:
  278. env["udev"] = False
  279. print("Warning: libudev development libraries not found. Disabling controller hotplugging support.")
  280. else:
  281. env["udev"] = False # Linux specific
  282. # Linkflags below this line should typically stay the last ones
  283. if not env["builtin_zlib"]:
  284. env.ParseConfig("pkg-config zlib --cflags --libs")
  285. env.Prepend(CPPPATH=["#platform/linuxbsd"])
  286. if env["x11"]:
  287. if not env["vulkan"]:
  288. print("Error: X11 support requires vulkan=yes")
  289. env.Exit(255)
  290. env.Append(CPPDEFINES=["X11_ENABLED"])
  291. env.Append(CPPDEFINES=["UNIX_ENABLED"])
  292. env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
  293. if env["vulkan"]:
  294. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  295. if not env["use_volk"]:
  296. env.ParseConfig("pkg-config vulkan --cflags --libs")
  297. if not env["builtin_glslang"]:
  298. # No pkgconfig file so far, hardcode expected lib name.
  299. env.Append(LIBS=["glslang", "SPIRV"])
  300. if env["opengl3"]:
  301. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  302. env.ParseConfig("pkg-config gl --cflags --libs")
  303. env.Append(LIBS=["pthread"])
  304. if platform.system() == "Linux":
  305. env.Append(LIBS=["dl"])
  306. if platform.system().find("BSD") >= 0:
  307. env["execinfo"] = True
  308. if env["execinfo"]:
  309. env.Append(LIBS=["execinfo"])
  310. if not env["tools"]:
  311. import subprocess
  312. import re
  313. linker_version_str = subprocess.check_output([env.subst(env["LINK"]), "-Wl,--version"]).decode("utf-8")
  314. gnu_ld_version = re.search("^GNU ld [^$]*(\d+\.\d+)$", linker_version_str, re.MULTILINE)
  315. if not gnu_ld_version:
  316. print(
  317. "Warning: Creating template binaries enabled for PCK embedding is currently only supported with GNU ld, not gold or LLD."
  318. )
  319. else:
  320. if float(gnu_ld_version.group(1)) >= 2.30:
  321. env.Append(LINKFLAGS=["-T", "platform/linuxbsd/pck_embed.ld"])
  322. else:
  323. env.Append(LINKFLAGS=["-T", "platform/linuxbsd/pck_embed.legacy.ld"])
  324. ## Cross-compilation
  325. if is64 and env["bits"] == "32":
  326. env.Append(CCFLAGS=["-m32"])
  327. env.Append(LINKFLAGS=["-m32", "-L/usr/lib/i386-linux-gnu"])
  328. elif not is64 and env["bits"] == "64":
  329. env.Append(CCFLAGS=["-m64"])
  330. env.Append(LINKFLAGS=["-m64", "-L/usr/lib/i686-linux-gnu"])
  331. # Link those statically for portability
  332. if env["use_static_cpp"]:
  333. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  334. if env["use_llvm"]:
  335. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  336. else:
  337. if env["use_llvm"]:
  338. env.Append(LIBS=["atomic"])