detect.py 15 KB

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