detect.py 15 KB

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