detect.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import os
  2. import platform
  3. import sys
  4. from methods import get_compiler_version, using_gcc, using_clang
  5. def is_active():
  6. return True
  7. def get_name():
  8. return "X11"
  9. def can_build():
  10. if os.name != "posix" or sys.platform == "darwin":
  11. return False
  12. # Check the minimal dependencies
  13. x11_error = os.system("pkg-config --version > /dev/null")
  14. if x11_error:
  15. print("Error: pkg-config not found. Aborting.")
  16. return False
  17. x11_error = os.system("pkg-config x11 --modversion > /dev/null")
  18. if x11_error:
  19. print("Error: X11 libraries not found. Aborting.")
  20. return False
  21. x11_error = os.system("pkg-config xcursor --modversion > /dev/null")
  22. if x11_error:
  23. print("Error: Xcursor library not found. Aborting.")
  24. return False
  25. x11_error = os.system("pkg-config xinerama --modversion > /dev/null")
  26. if x11_error:
  27. print("Error: Xinerama library not found. Aborting.")
  28. return False
  29. x11_error = os.system("pkg-config xext --modversion > /dev/null")
  30. if x11_error:
  31. print("Error: Xext library not found. Aborting.")
  32. return False
  33. x11_error = os.system("pkg-config xrandr --modversion > /dev/null")
  34. if x11_error:
  35. print("Error: XrandR library not found. Aborting.")
  36. return False
  37. x11_error = os.system("pkg-config xrender --modversion > /dev/null")
  38. if x11_error:
  39. print("Error: XRender library not found. Aborting.")
  40. return False
  41. x11_error = os.system("pkg-config xi --modversion > /dev/null")
  42. if x11_error:
  43. print("Error: Xi library not found. Aborting.")
  44. return False
  45. return True
  46. def get_opts():
  47. from SCons.Variables import BoolVariable, EnumVariable
  48. return [
  49. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  50. BoolVariable("use_lld", "Use the LLD linker", False),
  51. BoolVariable("use_thinlto", "Use ThinLTO", False),
  52. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  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/GCC compiler memory sanitizer (MSAN))", False),
  58. BoolVariable("pulseaudio", "Detect and use PulseAudio", True),
  59. BoolVariable("udev", "Use udev for gamepad connection callbacks", True),
  60. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  61. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  62. BoolVariable("touch", "Enable touch events", True),
  63. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  64. ]
  65. def get_flags():
  66. return []
  67. def configure(env):
  68. ## Build type
  69. if env["target"] == "release":
  70. if env["optimize"] == "speed": # optimize for speed (default)
  71. env.Prepend(CCFLAGS=["-O3"])
  72. elif env["optimize"] == "size": # optimize for size
  73. env.Prepend(CCFLAGS=["-Os"])
  74. if env["debug_symbols"]:
  75. env.Prepend(CCFLAGS=["-g2"])
  76. elif env["target"] == "release_debug":
  77. if env["optimize"] == "speed": # optimize for speed (default)
  78. env.Prepend(CCFLAGS=["-O2"])
  79. elif env["optimize"] == "size": # optimize for size
  80. env.Prepend(CCFLAGS=["-Os"])
  81. if env["debug_symbols"]:
  82. env.Prepend(CCFLAGS=["-g2"])
  83. elif env["target"] == "debug":
  84. env.Prepend(CCFLAGS=["-ggdb"])
  85. env.Prepend(CCFLAGS=["-g3"])
  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. if env["arch"] == "" and platform.machine() == "riscv64":
  92. env["arch"] = "rv64"
  93. if env["arch"] == "rv64":
  94. # G = General-purpose extensions, C = Compression extension (very common).
  95. env.Append(CCFLAGS=["-march=rv64gc"])
  96. ## Compiler configuration
  97. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  98. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  99. env["use_llvm"] = True
  100. if env["use_llvm"]:
  101. if "clang++" not in os.path.basename(env["CXX"]):
  102. env["CC"] = "clang"
  103. env["CXX"] = "clang++"
  104. env.extra_suffix = ".llvm" + env.extra_suffix
  105. if env["use_lld"]:
  106. if env["use_llvm"]:
  107. env.Append(LINKFLAGS=["-fuse-ld=lld"])
  108. if env["use_thinlto"]:
  109. # A convenience so you don't need to write use_lto too when using SCons
  110. env["use_lto"] = True
  111. else:
  112. print("Using LLD with GCC is not supported yet. Try compiling with 'use_llvm=yes'.")
  113. sys.exit(255)
  114. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  115. env.extra_suffix += "s"
  116. if env["use_ubsan"]:
  117. env.Append(
  118. CCFLAGS=[
  119. "-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"
  120. ]
  121. )
  122. if env["use_llvm"]:
  123. env.Append(
  124. CCFLAGS=[
  125. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change,implicit-signed-integer-truncation,implicit-unsigned-integer-truncation"
  126. ]
  127. )
  128. else:
  129. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  130. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  131. if env["use_asan"]:
  132. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  133. env.Append(LINKFLAGS=["-fsanitize=address"])
  134. if env["use_lsan"]:
  135. env.Append(CCFLAGS=["-fsanitize=leak"])
  136. env.Append(LINKFLAGS=["-fsanitize=leak"])
  137. if env["use_tsan"]:
  138. env.Append(CCFLAGS=["-fsanitize=thread"])
  139. env.Append(LINKFLAGS=["-fsanitize=thread"])
  140. if env["use_msan"]:
  141. env.Append(CCFLAGS=["-fsanitize=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. # Check for gcc version >= 6 before adding -no-pie
  160. version = get_compiler_version(env) or [-1, -1]
  161. if using_gcc(env):
  162. if version[0] >= 6:
  163. env.Append(CCFLAGS=["-fpie"])
  164. env.Append(LINKFLAGS=["-no-pie"])
  165. # Do the same for clang should be fine with Clang 4 and higher
  166. if using_clang(env):
  167. if version[0] >= 4:
  168. env.Append(CCFLAGS=["-fpie"])
  169. env.Append(LINKFLAGS=["-no-pie"])
  170. ## Dependencies
  171. env.ParseConfig("pkg-config x11 --cflags --libs")
  172. env.ParseConfig("pkg-config xcursor --cflags --libs")
  173. env.ParseConfig("pkg-config xinerama --cflags --libs")
  174. env.ParseConfig("pkg-config xext --cflags --libs")
  175. env.ParseConfig("pkg-config xrandr --cflags --libs")
  176. env.ParseConfig("pkg-config xrender --cflags --libs")
  177. env.ParseConfig("pkg-config xi --cflags --libs")
  178. if env["touch"]:
  179. env.Append(CPPDEFINES=["TOUCH_ENABLED"])
  180. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  181. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  182. # as shared libraries leads to weird issues
  183. if env["builtin_freetype"] or env["builtin_libpng"] or env["builtin_zlib"]:
  184. env["builtin_freetype"] = True
  185. env["builtin_libpng"] = True
  186. env["builtin_zlib"] = True
  187. if not env["builtin_freetype"]:
  188. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  189. if not env["builtin_libpng"]:
  190. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  191. if not env["builtin_bullet"]:
  192. # We need at least version 2.90
  193. min_bullet_version = "2.90"
  194. import subprocess
  195. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  196. if str(bullet_version) < min_bullet_version:
  197. # Abort as system bullet was requested but too old
  198. print(
  199. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  200. bullet_version, min_bullet_version
  201. )
  202. )
  203. sys.exit(255)
  204. env.ParseConfig("pkg-config bullet --cflags --libs")
  205. if False: # not env['builtin_assimp']:
  206. # FIXME: Add min version check
  207. env.ParseConfig("pkg-config assimp --cflags --libs")
  208. if not env["builtin_enet"]:
  209. env.ParseConfig("pkg-config libenet --cflags --libs")
  210. if not env["builtin_squish"]:
  211. env.ParseConfig("pkg-config libsquish --cflags --libs")
  212. if not env["builtin_zstd"]:
  213. env.ParseConfig("pkg-config libzstd --cflags --libs")
  214. # Sound and video libraries
  215. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  216. if not env["builtin_libtheora"]:
  217. env["builtin_libogg"] = False # Needed to link against system libtheora
  218. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  219. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  220. else:
  221. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  222. if any(platform.machine() in s for s in list_of_x86):
  223. env["x86_libtheora_opt_gcc"] = True
  224. if not env["builtin_libvpx"]:
  225. env.ParseConfig("pkg-config vpx --cflags --libs")
  226. if not env["builtin_libvorbis"]:
  227. env["builtin_libogg"] = False # Needed to link against system libvorbis
  228. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  229. if not env["builtin_opus"]:
  230. env["builtin_libogg"] = False # Needed to link against system opus
  231. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  232. if not env["builtin_libogg"]:
  233. env.ParseConfig("pkg-config ogg --cflags --libs")
  234. if not env["builtin_libwebp"]:
  235. env.ParseConfig("pkg-config libwebp --cflags --libs")
  236. if not env["builtin_mbedtls"]:
  237. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  238. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  239. if not env["builtin_wslay"]:
  240. env.ParseConfig("pkg-config libwslay --cflags --libs")
  241. if not env["builtin_miniupnpc"]:
  242. # No pkgconfig file so far, hardcode default paths.
  243. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  244. env.Append(LIBS=["miniupnpc"])
  245. # On Linux wchar_t should be 32-bits
  246. # 16-bit library shouldn't be required due to compiler optimisations
  247. if not env["builtin_pcre2"]:
  248. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  249. # Embree is only used in tools build on x86_64 and aarch64.
  250. if env["tools"] and not env["builtin_embree"] and is64:
  251. # No pkgconfig file so far, hardcode expected lib name.
  252. env.Append(LIBS=["embree3"])
  253. ## Flags
  254. if os.system("pkg-config --exists alsa") == 0: # 0 means found
  255. env["alsa"] = True
  256. env.Append(CPPDEFINES=["ALSA_ENABLED", "ALSAMIDI_ENABLED"])
  257. env.ParseConfig("pkg-config alsa --cflags") # Only cflags, we dlopen the library.
  258. else:
  259. print("Warning: ALSA libraries not found. Disabling the ALSA audio driver.")
  260. if env["pulseaudio"]:
  261. if os.system("pkg-config --exists libpulse") == 0: # 0 means found
  262. env.Append(CPPDEFINES=["PULSEAUDIO_ENABLED"])
  263. env.ParseConfig("pkg-config libpulse --cflags") # Only cflags, we dlopen the library.
  264. else:
  265. print("Warning: PulseAudio development libraries not found. Disabling the PulseAudio audio driver.")
  266. if platform.system() == "Linux":
  267. env.Append(CPPDEFINES=["JOYDEV_ENABLED"])
  268. if env["udev"]:
  269. if os.system("pkg-config --exists libudev") == 0: # 0 means found
  270. env.Append(CPPDEFINES=["UDEV_ENABLED"])
  271. env.ParseConfig("pkg-config libudev --cflags") # Only cflags, we dlopen the library.
  272. else:
  273. print("Warning: libudev development libraries not found. Disabling controller hotplugging support.")
  274. else:
  275. env["udev"] = False # Linux specific
  276. # Linkflags below this line should typically stay the last ones
  277. if not env["builtin_zlib"]:
  278. env.ParseConfig("pkg-config zlib --cflags --libs")
  279. env.Prepend(CPPPATH=["#platform/x11"])
  280. env.Append(CPPDEFINES=["X11_ENABLED", "UNIX_ENABLED", "OPENGL_ENABLED", "GLES_ENABLED", ("_FILE_OFFSET_BITS", 64)])
  281. env.ParseConfig("pkg-config gl --cflags --libs")
  282. env.Append(LIBS=["pthread"])
  283. if platform.system() == "Linux":
  284. env.Append(LIBS=["dl"])
  285. if platform.system().find("BSD") >= 0:
  286. env["execinfo"] = True
  287. if env["execinfo"]:
  288. env.Append(LIBS=["execinfo"])
  289. if not env["tools"]:
  290. import subprocess
  291. import re
  292. linker_version_str = subprocess.check_output([env.subst(env["LINK"]), "-Wl,--version"]).decode("utf-8")
  293. gnu_ld_version = re.search(r"^GNU ld [^$]*(\d+\.\d+)$", linker_version_str, re.MULTILINE)
  294. if not gnu_ld_version:
  295. print(
  296. "Warning: Creating template binaries enabled for PCK embedding is currently only supported with GNU ld, not gold or LLD."
  297. )
  298. else:
  299. if float(gnu_ld_version.group(1)) >= 2.30:
  300. env.Append(LINKFLAGS=["-T", "platform/x11/pck_embed.ld"])
  301. else:
  302. env.Append(LINKFLAGS=["-T", "platform/x11/pck_embed.legacy.ld"])
  303. ## Cross-compilation
  304. if is64 and env["bits"] == "32":
  305. env.Append(CCFLAGS=["-m32"])
  306. env.Append(LINKFLAGS=["-m32", "-L/usr/lib/i386-linux-gnu"])
  307. elif not is64 and env["bits"] == "64":
  308. env.Append(CCFLAGS=["-m64"])
  309. env.Append(LINKFLAGS=["-m64", "-L/usr/lib/i686-linux-gnu"])
  310. # Link those statically for portability
  311. if env["use_static_cpp"]:
  312. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  313. if env["use_llvm"] and platform.system() != "FreeBSD":
  314. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  315. else:
  316. if env["use_llvm"] and platform.system() != "FreeBSD":
  317. env.Append(LIBS=["atomic"])