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("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. if env["arch"] == "" and platform.machine() == "riscv64":
  93. env["arch"] = "rv64"
  94. if env["arch"] == "rv64":
  95. # G = General-purpose extensions, C = Compression extension (very common).
  96. env.Append(CCFLAGS=["-march=rv64gc"])
  97. ## Compiler configuration
  98. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  99. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  100. env["use_llvm"] = True
  101. if env["use_llvm"]:
  102. if "clang++" not in os.path.basename(env["CXX"]):
  103. env["CC"] = "clang"
  104. env["CXX"] = "clang++"
  105. env.extra_suffix = ".llvm" + env.extra_suffix
  106. if env["use_lld"]:
  107. if env["use_llvm"]:
  108. env.Append(LINKFLAGS=["-fuse-ld=lld"])
  109. if env["use_thinlto"]:
  110. # A convenience so you don't need to write use_lto too when using SCons
  111. env["use_lto"] = True
  112. else:
  113. print("Using LLD with GCC is not supported yet. Try compiling with 'use_llvm=yes'.")
  114. sys.exit(255)
  115. if env["use_coverage"]:
  116. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  117. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  118. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  119. env.extra_suffix += "s"
  120. if env["use_ubsan"]:
  121. env.Append(
  122. CCFLAGS=[
  123. "-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"
  124. ]
  125. )
  126. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  127. if env["use_llvm"]:
  128. env.Append(
  129. CCFLAGS=[
  130. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change"
  131. ]
  132. )
  133. else:
  134. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  135. if env["use_asan"]:
  136. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  137. env.Append(LINKFLAGS=["-fsanitize=address"])
  138. if env["use_lsan"]:
  139. env.Append(CCFLAGS=["-fsanitize=leak"])
  140. env.Append(LINKFLAGS=["-fsanitize=leak"])
  141. if env["use_tsan"]:
  142. env.Append(CCFLAGS=["-fsanitize=thread"])
  143. env.Append(LINKFLAGS=["-fsanitize=thread"])
  144. if env["use_msan"] and env["use_llvm"]:
  145. env.Append(CCFLAGS=["-fsanitize=memory"])
  146. env.Append(CCFLAGS=["-fsanitize-memory-track-origins"])
  147. env.Append(CCFLAGS=["-fsanitize-recover=memory"])
  148. env.Append(LINKFLAGS=["-fsanitize=memory"])
  149. if env["use_lto"]:
  150. if 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. if env["use_lld"] and env["use_thinlto"]:
  155. env.Append(CCFLAGS=["-flto=thin"])
  156. env.Append(LINKFLAGS=["-flto=thin"])
  157. else:
  158. env.Append(CCFLAGS=["-flto"])
  159. env.Append(LINKFLAGS=["-flto"])
  160. if not env["use_llvm"]:
  161. env["RANLIB"] = "gcc-ranlib"
  162. env["AR"] = "gcc-ar"
  163. env.Append(CCFLAGS=["-pipe"])
  164. env.Append(LINKFLAGS=["-pipe"])
  165. ## Dependencies
  166. env.ParseConfig("pkg-config x11 --cflags --libs")
  167. env.ParseConfig("pkg-config xcursor --cflags --libs")
  168. env.ParseConfig("pkg-config xinerama --cflags --libs")
  169. env.ParseConfig("pkg-config xext --cflags --libs")
  170. env.ParseConfig("pkg-config xrandr --cflags --libs")
  171. env.ParseConfig("pkg-config xrender --cflags --libs")
  172. env.ParseConfig("pkg-config xi --cflags --libs")
  173. if env["touch"]:
  174. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  175. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  176. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  177. # as shared libraries leads to weird issues
  178. if (
  179. env["builtin_freetype"]
  180. or env["builtin_libpng"]
  181. or env["builtin_zlib"]
  182. or env["builtin_graphite"]
  183. or env["builtin_harfbuzz"]
  184. ):
  185. env["builtin_freetype"] = True
  186. env["builtin_libpng"] = True
  187. env["builtin_zlib"] = True
  188. env["builtin_graphite"] = True
  189. env["builtin_harfbuzz"] = True
  190. if not env["builtin_freetype"]:
  191. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  192. if not env["builtin_graphite"]:
  193. env.ParseConfig("pkg-config graphite2 --cflags --libs")
  194. if not env["builtin_icu"]:
  195. env.ParseConfig("pkg-config icu-uc --cflags --libs")
  196. if not env["builtin_harfbuzz"]:
  197. env.ParseConfig("pkg-config harfbuzz harfbuzz-icu --cflags --libs")
  198. if not env["builtin_libpng"]:
  199. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  200. if not env["builtin_bullet"]:
  201. # We need at least version 2.90
  202. min_bullet_version = "2.90"
  203. import subprocess
  204. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  205. if str(bullet_version) < min_bullet_version:
  206. # Abort as system bullet was requested but too old
  207. print(
  208. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  209. bullet_version, min_bullet_version
  210. )
  211. )
  212. sys.exit(255)
  213. env.ParseConfig("pkg-config bullet --cflags --libs")
  214. if False: # not env['builtin_assimp']:
  215. # FIXME: Add min version check
  216. env.ParseConfig("pkg-config assimp --cflags --libs")
  217. if not env["builtin_enet"]:
  218. env.ParseConfig("pkg-config libenet --cflags --libs")
  219. if not env["builtin_squish"]:
  220. env.ParseConfig("pkg-config libsquish --cflags --libs")
  221. if not env["builtin_zstd"]:
  222. env.ParseConfig("pkg-config libzstd --cflags --libs")
  223. # Sound and video libraries
  224. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  225. if not env["builtin_libtheora"]:
  226. env["builtin_libogg"] = False # Needed to link against system libtheora
  227. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  228. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  229. else:
  230. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  231. if any(platform.machine() in s for s in list_of_x86):
  232. env["x86_libtheora_opt_gcc"] = True
  233. if not env["builtin_libvorbis"]:
  234. env["builtin_libogg"] = False # Needed to link against system libvorbis
  235. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  236. if not env["builtin_libogg"]:
  237. env.ParseConfig("pkg-config ogg --cflags --libs")
  238. if not env["builtin_libwebp"]:
  239. env.ParseConfig("pkg-config libwebp --cflags --libs")
  240. if not env["builtin_mbedtls"]:
  241. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  242. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  243. if not env["builtin_wslay"]:
  244. env.ParseConfig("pkg-config libwslay --cflags --libs")
  245. if not env["builtin_miniupnpc"]:
  246. # No pkgconfig file so far, hardcode default paths.
  247. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  248. env.Append(LIBS=["miniupnpc"])
  249. # On Linux wchar_t should be 32-bits
  250. # 16-bit library shouldn't be required due to compiler optimisations
  251. if not env["builtin_pcre2"]:
  252. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  253. if not env["builtin_embree"]:
  254. # No pkgconfig file so far, hardcode expected lib name.
  255. env.Append(LIBS=["embree3"])
  256. ## Flags
  257. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  258. env["alsa"] = True
  259. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  260. else:
  261. print("Warning: ALSA libraries not found. Disabling the ALSA audio driver.")
  262. if env["pulseaudio"]:
  263. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  264. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  265. env.ParseConfig("pkg-config --cflags libpulse")
  266. else:
  267. print("Warning: PulseAudio development libraries not found. Disabling the PulseAudio audio driver.")
  268. if env["dbus"]:
  269. if os.system("pkg-config --exists dbus-1") == 0: # 0 means found
  270. env.Append(CPPDEFINES=["DBUS_ENABLED"])
  271. env.ParseConfig("pkg-config --cflags --libs dbus-1")
  272. else:
  273. print("Warning: D-Bus development libraries not found. Disabling screensaver prevention.")
  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. else:
  280. print("Warning: libudev development libraries not found. Disabling controller hotplugging support.")
  281. else:
  282. env["udev"] = False # Linux specific
  283. # Linkflags below this line should typically stay the last ones
  284. if not env["builtin_zlib"]:
  285. env.ParseConfig("pkg-config zlib --cflags --libs")
  286. env.Prepend(CPPPATH=["#platform/linuxbsd"])
  287. if env["x11"]:
  288. if not env["vulkan"]:
  289. print("Error: X11 support requires vulkan=yes")
  290. env.Exit(255)
  291. env.Append(CPPDEFINES=["X11_ENABLED"])
  292. env.Append(CPPDEFINES=["UNIX_ENABLED"])
  293. env.Append(CPPDEFINES=[("_FILE_OFFSET_BITS", 64)])
  294. if env["vulkan"]:
  295. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  296. if not env["use_volk"]:
  297. env.ParseConfig("pkg-config vulkan --cflags --libs")
  298. if not env["builtin_glslang"]:
  299. # No pkgconfig file for glslang so far
  300. env.Append(LIBS=["glslang", "SPIRV"])
  301. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  302. env.Append(LIBS=["GL"])
  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"])