detect.py 17 KB

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