detect.py 16 KB

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