detect.py 14 KB

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