detect.py 15 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. return False
  15. x11_error = os.system("pkg-config x11 --modversion > /dev/null ")
  16. if x11_error:
  17. return False
  18. x11_error = os.system("pkg-config xcursor --modversion > /dev/null ")
  19. if x11_error:
  20. print("xcursor not found.. x11 disabled.")
  21. return False
  22. x11_error = os.system("pkg-config xinerama --modversion > /dev/null ")
  23. if x11_error:
  24. print("xinerama not found.. x11 disabled.")
  25. return False
  26. x11_error = os.system("pkg-config xext --modversion > /dev/null ")
  27. if x11_error:
  28. print("xext not found.. x11 disabled.")
  29. return False
  30. x11_error = os.system("pkg-config xrandr --modversion > /dev/null ")
  31. if x11_error:
  32. print("xrandr not found.. x11 disabled.")
  33. return False
  34. x11_error = os.system("pkg-config xrender --modversion > /dev/null ")
  35. if x11_error:
  36. print("xrender not found.. x11 disabled.")
  37. return False
  38. x11_error = os.system("pkg-config xi --modversion > /dev/null ")
  39. if x11_error:
  40. print("xi not found.. Aborting.")
  41. return False
  42. return True
  43. def get_opts():
  44. from SCons.Variables import BoolVariable, EnumVariable
  45. return [
  46. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  47. BoolVariable("use_lld", "Use the LLD linker", False),
  48. BoolVariable("use_thinlto", "Use ThinLTO", False),
  49. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  50. BoolVariable("use_coverage", "Test Godot coverage", False),
  51. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  52. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
  53. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN)", False),
  54. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
  55. BoolVariable("use_msan", "Use LLVM compiler memory sanitizer (MSAN)", False),
  56. BoolVariable("pulseaudio", "Detect and use PulseAudio", True),
  57. BoolVariable("udev", "Use udev for gamepad connection callbacks", True),
  58. BoolVariable("x11", "Enable X11 display", True),
  59. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  60. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  61. BoolVariable("touch", "Enable touch events", True),
  62. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  63. ]
  64. def get_flags():
  65. return []
  66. def configure(env):
  67. ## Build type
  68. if env["target"] == "release":
  69. if env["optimize"] == "speed": # optimize for speed (default)
  70. env.Prepend(CCFLAGS=["-O3"])
  71. elif env["optimize"] == "size": # optimize for size
  72. env.Prepend(CCFLAGS=["-Os"])
  73. if env["debug_symbols"]:
  74. env.Prepend(CCFLAGS=["-g2"])
  75. elif env["target"] == "release_debug":
  76. if env["optimize"] == "speed": # optimize for speed (default)
  77. env.Prepend(CCFLAGS=["-O2"])
  78. elif env["optimize"] == "size": # optimize for size
  79. env.Prepend(CCFLAGS=["-Os"])
  80. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  81. if env["debug_symbols"]:
  82. env.Prepend(CCFLAGS=["-g2"])
  83. elif env["target"] == "debug":
  84. env.Prepend(CCFLAGS=["-g3"])
  85. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  86. env.Append(LINKFLAGS=["-rdynamic"])
  87. ## Architecture
  88. is64 = sys.maxsize > 2 ** 32
  89. if env["bits"] == "default":
  90. env["bits"] = "64" if is64 else "32"
  91. ## Compiler configuration
  92. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  93. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  94. env["use_llvm"] = True
  95. if env["use_llvm"]:
  96. if "clang++" not in os.path.basename(env["CXX"]):
  97. env["CC"] = "clang"
  98. env["CXX"] = "clang++"
  99. env.extra_suffix = ".llvm" + env.extra_suffix
  100. if env["use_lld"]:
  101. if env["use_llvm"]:
  102. env.Append(LINKFLAGS=["-fuse-ld=lld"])
  103. if env["use_thinlto"]:
  104. # A convenience so you don't need to write use_lto too when using SCons
  105. env["use_lto"] = True
  106. else:
  107. print("Using LLD with GCC is not supported yet, try compiling with 'use_llvm=yes'.")
  108. sys.exit(255)
  109. if env["use_coverage"]:
  110. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  111. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  112. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  113. env.extra_suffix += "s"
  114. if env["use_ubsan"]:
  115. env.Append(
  116. CCFLAGS=[
  117. "-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"
  118. ]
  119. )
  120. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  121. if env["use_llvm"]:
  122. env.Append(
  123. CCFLAGS=[
  124. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change"
  125. ]
  126. )
  127. else:
  128. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  129. if env["use_asan"]:
  130. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  131. env.Append(LINKFLAGS=["-fsanitize=address"])
  132. if env["use_lsan"]:
  133. env.Append(CCFLAGS=["-fsanitize=leak"])
  134. env.Append(LINKFLAGS=["-fsanitize=leak"])
  135. if env["use_tsan"]:
  136. env.Append(CCFLAGS=["-fsanitize=thread"])
  137. env.Append(LINKFLAGS=["-fsanitize=thread"])
  138. if env["use_msan"] and env["use_llvm"]:
  139. env.Append(CCFLAGS=["-fsanitize=memory"])
  140. env.Append(CCFLAGS=["-fsanitize-memory-track-origins"])
  141. env.Append(CCFLAGS=["-fsanitize-recover=memory"])
  142. env.Append(LINKFLAGS=["-fsanitize=memory"])
  143. if env["use_lto"]:
  144. if not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  145. env.Append(CCFLAGS=["-flto"])
  146. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  147. else:
  148. if env["use_lld"] and env["use_thinlto"]:
  149. env.Append(CCFLAGS=["-flto=thin"])
  150. env.Append(LINKFLAGS=["-flto=thin"])
  151. else:
  152. env.Append(CCFLAGS=["-flto"])
  153. env.Append(LINKFLAGS=["-flto"])
  154. if not env["use_llvm"]:
  155. env["RANLIB"] = "gcc-ranlib"
  156. env["AR"] = "gcc-ar"
  157. env.Append(CCFLAGS=["-pipe"])
  158. env.Append(LINKFLAGS=["-pipe"])
  159. # -fpie and -no-pie is supported on GCC 6+ and Clang 4+, both below our
  160. # minimal requirements.
  161. env.Append(CCFLAGS=["-fpie"])
  162. env.Append(LINKFLAGS=["-no-pie"])
  163. ## Dependencies
  164. env.ParseConfig("pkg-config x11 --cflags --libs")
  165. env.ParseConfig("pkg-config xcursor --cflags --libs")
  166. env.ParseConfig("pkg-config xinerama --cflags --libs")
  167. env.ParseConfig("pkg-config xext --cflags --libs")
  168. env.ParseConfig("pkg-config xrandr --cflags --libs")
  169. env.ParseConfig("pkg-config xrender --cflags --libs")
  170. env.ParseConfig("pkg-config xi --cflags --libs")
  171. if env["touch"]:
  172. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  173. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  174. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  175. # as shared libraries leads to weird issues
  176. if (
  177. env["builtin_freetype"]
  178. or env["builtin_libpng"]
  179. or env["builtin_zlib"]
  180. or env["builtin_graphite"]
  181. or env["builtin_harfbuzz"]
  182. ):
  183. env["builtin_freetype"] = True
  184. env["builtin_libpng"] = True
  185. env["builtin_zlib"] = True
  186. env["builtin_graphite"] = True
  187. env["builtin_harfbuzz"] = True
  188. if not env["builtin_freetype"]:
  189. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  190. if not env["builtin_graphite"]:
  191. env.ParseConfig("pkg-config graphite2 --cflags --libs")
  192. if not env["builtin_icu"]:
  193. env.ParseConfig("pkg-config icu-uc --cflags --libs")
  194. if not env["builtin_harfbuzz"]:
  195. env.ParseConfig("pkg-config harfbuzz harfbuzz-icu --cflags --libs")
  196. if not env["builtin_libpng"]:
  197. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  198. if not env["builtin_bullet"]:
  199. # We need at least version 2.90
  200. min_bullet_version = "2.90"
  201. import subprocess
  202. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  203. if str(bullet_version) < min_bullet_version:
  204. # Abort as system bullet was requested but too old
  205. print(
  206. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  207. bullet_version, min_bullet_version
  208. )
  209. )
  210. sys.exit(255)
  211. env.ParseConfig("pkg-config bullet --cflags --libs")
  212. if False: # not env['builtin_assimp']:
  213. # FIXME: Add min version check
  214. env.ParseConfig("pkg-config assimp --cflags --libs")
  215. if not env["builtin_enet"]:
  216. env.ParseConfig("pkg-config libenet --cflags --libs")
  217. if not env["builtin_squish"]:
  218. env.ParseConfig("pkg-config libsquish --cflags --libs")
  219. if not env["builtin_zstd"]:
  220. env.ParseConfig("pkg-config libzstd --cflags --libs")
  221. # Sound and video libraries
  222. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  223. if not env["builtin_libtheora"]:
  224. env["builtin_libogg"] = False # Needed to link against system libtheora
  225. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  226. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  227. else:
  228. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  229. if any(platform.machine() in s for s in list_of_x86):
  230. env["x86_libtheora_opt_gcc"] = True
  231. if not env["builtin_libvpx"]:
  232. env.ParseConfig("pkg-config vpx --cflags --libs")
  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_opus"]:
  237. env["builtin_libogg"] = False # Needed to link against system opus
  238. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  239. if not env["builtin_libogg"]:
  240. env.ParseConfig("pkg-config ogg --cflags --libs")
  241. if not env["builtin_libwebp"]:
  242. env.ParseConfig("pkg-config libwebp --cflags --libs")
  243. if not env["builtin_mbedtls"]:
  244. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  245. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  246. if not env["builtin_wslay"]:
  247. env.ParseConfig("pkg-config libwslay --cflags --libs")
  248. if not env["builtin_miniupnpc"]:
  249. # No pkgconfig file so far, hardcode default paths.
  250. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  251. env.Append(LIBS=["miniupnpc"])
  252. # On Linux wchar_t should be 32-bits
  253. # 16-bit library shouldn't be required due to compiler optimisations
  254. if not env["builtin_pcre2"]:
  255. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  256. if not env["builtin_embree"]:
  257. # No pkgconfig file so far, hardcode expected lib name.
  258. env.Append(LIBS=["embree3"])
  259. ## Flags
  260. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  261. print("Enabling ALSA")
  262. env["alsa"] = True
  263. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  264. else:
  265. print("ALSA libraries not found, disabling driver")
  266. if env["pulseaudio"]:
  267. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  268. print("Enabling PulseAudio")
  269. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  270. env.ParseConfig("pkg-config --cflags libpulse")
  271. else:
  272. print("PulseAudio development libraries not found, disabling driver")
  273. if platform.system() == "Linux":
  274. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  275. if env["udev"]:
  276. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  277. print("Enabling udev support")
  278. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  279. else:
  280. print("libudev development libraries not found, disabling udev 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["builtin_vulkan"]:
  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=['OPENGL_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"
  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"])