detect.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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", False),
  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("pulseaudio", "Detect and use PulseAudio", True),
  56. BoolVariable("udev", "Use udev for gamepad connection callbacks", False),
  57. EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")),
  58. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  59. BoolVariable("touch", "Enable touch events", True),
  60. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  61. ]
  62. def get_flags():
  63. return []
  64. def configure(env):
  65. ## Build type
  66. if env["target"] == "release":
  67. if env["optimize"] == "speed": # optimize for speed (default)
  68. env.Prepend(CCFLAGS=["-O3"])
  69. else: # optimize for size
  70. env.Prepend(CCFLAGS=["-Os"])
  71. if env["debug_symbols"] == "yes":
  72. env.Prepend(CCFLAGS=["-g2"])
  73. elif env["target"] == "release_debug":
  74. if env["optimize"] == "speed": # optimize for speed (default)
  75. env.Prepend(CCFLAGS=["-O2"])
  76. else: # optimize for size
  77. env.Prepend(CCFLAGS=["-Os"])
  78. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  79. if env["debug_symbols"] == "yes":
  80. env.Prepend(CCFLAGS=["-g2"])
  81. elif env["target"] == "debug":
  82. env.Prepend(CCFLAGS=["-g3"])
  83. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  84. env.Append(LINKFLAGS=["-rdynamic"])
  85. ## Architecture
  86. is64 = sys.maxsize > 2 ** 32
  87. if env["bits"] == "default":
  88. env["bits"] = "64" if is64 else "32"
  89. ## Compiler configuration
  90. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  91. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  92. env["use_llvm"] = True
  93. if env["use_llvm"]:
  94. if "clang++" not in os.path.basename(env["CXX"]):
  95. env["CC"] = "clang"
  96. env["CXX"] = "clang++"
  97. env.extra_suffix = ".llvm" + env.extra_suffix
  98. if env["use_lld"]:
  99. if env["use_llvm"]:
  100. env.Append(LINKFLAGS=["-fuse-ld=lld"])
  101. if env["use_thinlto"]:
  102. # A convenience so you don't need to write use_lto too when using SCons
  103. env["use_lto"] = True
  104. else:
  105. print("Using LLD with GCC is not supported yet, try compiling with 'use_llvm=yes'.")
  106. sys.exit(255)
  107. if env["use_coverage"]:
  108. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  109. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  110. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"]:
  111. env.extra_suffix += "s"
  112. if env["use_ubsan"]:
  113. env.Append(CCFLAGS=["-fsanitize=undefined"])
  114. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  115. if env["use_asan"]:
  116. env.Append(CCFLAGS=["-fsanitize=address"])
  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_lto"]:
  125. if not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  126. env.Append(CCFLAGS=["-flto"])
  127. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  128. else:
  129. if env["use_lld"] and env["use_thinlto"]:
  130. env.Append(CCFLAGS=["-flto=thin"])
  131. env.Append(LINKFLAGS=["-flto=thin"])
  132. else:
  133. env.Append(CCFLAGS=["-flto"])
  134. env.Append(LINKFLAGS=["-flto"])
  135. if not env["use_llvm"]:
  136. env["RANLIB"] = "gcc-ranlib"
  137. env["AR"] = "gcc-ar"
  138. env.Append(CCFLAGS=["-pipe"])
  139. env.Append(LINKFLAGS=["-pipe"])
  140. # -fpie and -no-pie is supported on GCC 6+ and Clang 4+, both below our
  141. # minimal requirements.
  142. env.Append(CCFLAGS=["-fpie"])
  143. env.Append(LINKFLAGS=["-no-pie"])
  144. ## Dependencies
  145. env.ParseConfig("pkg-config x11 --cflags --libs")
  146. env.ParseConfig("pkg-config xcursor --cflags --libs")
  147. env.ParseConfig("pkg-config xinerama --cflags --libs")
  148. env.ParseConfig("pkg-config xext --cflags --libs")
  149. env.ParseConfig("pkg-config xrandr --cflags --libs")
  150. env.ParseConfig("pkg-config xrender --cflags --libs")
  151. env.ParseConfig("pkg-config xi --cflags --libs")
  152. if env["touch"]:
  153. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  154. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  155. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  156. # as shared libraries leads to weird issues
  157. if (
  158. env["builtin_freetype"]
  159. or env["builtin_libpng"]
  160. or env["builtin_zlib"]
  161. or env["builtin_graphite"]
  162. or env["builtin_harfbuzz"]
  163. ):
  164. env["builtin_freetype"] = True
  165. env["builtin_libpng"] = True
  166. env["builtin_zlib"] = True
  167. env["builtin_graphite"] = True
  168. env["builtin_harfbuzz"] = True
  169. if not env["builtin_freetype"]:
  170. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  171. if not env["builtin_graphite"]:
  172. env.ParseConfig("pkg-config graphite2 --cflags --libs")
  173. if not env["builtin_icu"]:
  174. env.ParseConfig("pkg-config icu-uc --cflags --libs")
  175. if not env["builtin_harfbuzz"]:
  176. env.ParseConfig("pkg-config harfbuzz harfbuzz-icu --cflags --libs")
  177. if not env["builtin_libpng"]:
  178. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  179. if not env["builtin_bullet"]:
  180. # We need at least version 2.90
  181. min_bullet_version = "2.90"
  182. import subprocess
  183. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  184. if str(bullet_version) < min_bullet_version:
  185. # Abort as system bullet was requested but too old
  186. print(
  187. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  188. bullet_version, min_bullet_version
  189. )
  190. )
  191. sys.exit(255)
  192. env.ParseConfig("pkg-config bullet --cflags --libs")
  193. if False: # not env['builtin_assimp']:
  194. # FIXME: Add min version check
  195. env.ParseConfig("pkg-config assimp --cflags --libs")
  196. if not env["builtin_enet"]:
  197. env.ParseConfig("pkg-config libenet --cflags --libs")
  198. if not env["builtin_squish"]:
  199. env.ParseConfig("pkg-config libsquish --cflags --libs")
  200. if not env["builtin_zstd"]:
  201. env.ParseConfig("pkg-config libzstd --cflags --libs")
  202. # Sound and video libraries
  203. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  204. if not env["builtin_libtheora"]:
  205. env["builtin_libogg"] = False # Needed to link against system libtheora
  206. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  207. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  208. else:
  209. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  210. if any(platform.machine() in s for s in list_of_x86):
  211. env["x86_libtheora_opt_gcc"] = True
  212. if not env["builtin_libvpx"]:
  213. env.ParseConfig("pkg-config vpx --cflags --libs")
  214. if not env["builtin_libvorbis"]:
  215. env["builtin_libogg"] = False # Needed to link against system libvorbis
  216. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  217. if not env["builtin_opus"]:
  218. env["builtin_libogg"] = False # Needed to link against system opus
  219. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  220. if not env["builtin_libogg"]:
  221. env.ParseConfig("pkg-config ogg --cflags --libs")
  222. if not env["builtin_libwebp"]:
  223. env.ParseConfig("pkg-config libwebp --cflags --libs")
  224. if not env["builtin_mbedtls"]:
  225. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  226. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  227. if not env["builtin_wslay"]:
  228. env.ParseConfig("pkg-config libwslay --cflags --libs")
  229. if not env["builtin_miniupnpc"]:
  230. # No pkgconfig file so far, hardcode default paths.
  231. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  232. env.Append(LIBS=["miniupnpc"])
  233. # On Linux wchar_t should be 32-bits
  234. # 16-bit library shouldn't be required due to compiler optimisations
  235. if not env["builtin_pcre2"]:
  236. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  237. ## Flags
  238. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  239. print("Enabling ALSA")
  240. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  241. # Don't parse --cflags, we don't need to add /usr/include/alsa to include path
  242. env.ParseConfig("pkg-config alsa --libs")
  243. else:
  244. print("ALSA libraries not found, disabling driver")
  245. if env["pulseaudio"]:
  246. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  247. print("Enabling PulseAudio")
  248. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  249. env.ParseConfig("pkg-config --cflags --libs libpulse")
  250. else:
  251. print("PulseAudio development libraries not found, disabling driver")
  252. if platform.system() == "Linux":
  253. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  254. if env["udev"]:
  255. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  256. print("Enabling udev support")
  257. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  258. env.ParseConfig("pkg-config libudev --cflags --libs")
  259. else:
  260. print("libudev development libraries not found, disabling udev support")
  261. # Linkflags below this line should typically stay the last ones
  262. if not env["builtin_zlib"]:
  263. env.ParseConfig("pkg-config zlib --cflags --libs")
  264. env.Prepend(CPPPATH=["#platform/linuxbsd"])
  265. env.Append(CPPDEFINES=["X11_ENABLED", "UNIX_ENABLED"])
  266. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  267. if not env["builtin_vulkan"]:
  268. env.ParseConfig("pkg-config vulkan --cflags --libs")
  269. if not env["builtin_glslang"]:
  270. # No pkgconfig file for glslang so far
  271. env.Append(LIBS=["glslang", "SPIRV"])
  272. # env.Append(CPPDEFINES=['OPENGL_ENABLED'])
  273. env.Append(LIBS=["GL"])
  274. env.Append(LIBS=["pthread"])
  275. if platform.system() == "Linux":
  276. env.Append(LIBS=["dl"])
  277. if platform.system().find("BSD") >= 0:
  278. env["execinfo"] = True
  279. if env["execinfo"]:
  280. env.Append(LIBS=["execinfo"])
  281. if not env["tools"]:
  282. import subprocess
  283. import re
  284. linker_version_str = subprocess.check_output([env.subst(env["LINK"]), "-Wl,--version"]).decode("utf-8")
  285. gnu_ld_version = re.search("^GNU ld [^$]*(\d+\.\d+)$", linker_version_str, re.MULTILINE)
  286. if not gnu_ld_version:
  287. print(
  288. "Warning: Creating template binaries enabled for PCK embedding is currently only supported with GNU ld"
  289. )
  290. else:
  291. if float(gnu_ld_version.group(1)) >= 2.30:
  292. env.Append(LINKFLAGS=["-T", "platform/linuxbsd/pck_embed.ld"])
  293. else:
  294. env.Append(LINKFLAGS=["-T", "platform/linuxbsd/pck_embed.legacy.ld"])
  295. ## Cross-compilation
  296. if is64 and env["bits"] == "32":
  297. env.Append(CCFLAGS=["-m32"])
  298. env.Append(LINKFLAGS=["-m32", "-L/usr/lib/i386-linux-gnu"])
  299. elif not is64 and env["bits"] == "64":
  300. env.Append(CCFLAGS=["-m64"])
  301. env.Append(LINKFLAGS=["-m64", "-L/usr/lib/i686-linux-gnu"])
  302. # Link those statically for portability
  303. if env["use_static_cpp"]:
  304. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])