detect.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import os
  2. import platform
  3. import sys
  4. from methods import get_compiler_version, using_gcc
  5. # This file is mostly based on platform/x11/detect.py.
  6. # If editing this file, make sure to apply relevant changes here too.
  7. def is_active():
  8. return True
  9. def get_name():
  10. return "Server"
  11. def get_program_suffix():
  12. if sys.platform == "darwin":
  13. return "osx"
  14. return "x11"
  15. def can_build():
  16. if os.name != "posix":
  17. return False
  18. return True
  19. def get_opts():
  20. from SCons.Variables import BoolVariable, EnumVariable
  21. return [
  22. EnumVariable("linker", "Linker program", "default", ("default", "bfd", "gold", "lld", "mold")),
  23. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  24. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  25. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  26. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False),
  27. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN))", False),
  28. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False),
  29. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  30. BoolVariable("use_msan", "Use LLVM/GCC compiler memory sanitizer (MSAN))", False),
  31. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  32. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  33. ]
  34. def get_flags():
  35. return []
  36. def configure(env):
  37. ## Build type
  38. if env["target"] == "release":
  39. if env["optimize"] == "speed": # optimize for speed (default)
  40. env.Prepend(CCFLAGS=["-O3"])
  41. elif env["optimize"] == "size": # optimize for size
  42. env.Prepend(CCFLAGS=["-Os"])
  43. if env["debug_symbols"]:
  44. env.Prepend(CCFLAGS=["-g2"])
  45. elif env["target"] == "release_debug":
  46. if env["optimize"] == "speed": # optimize for speed (default)
  47. env.Prepend(CCFLAGS=["-O2"])
  48. elif env["optimize"] == "size": # optimize for size
  49. env.Prepend(CCFLAGS=["-Os"])
  50. if env["debug_symbols"]:
  51. env.Prepend(CCFLAGS=["-g2"])
  52. elif env["target"] == "debug":
  53. env.Prepend(CCFLAGS=["-g3"])
  54. env.Append(LINKFLAGS=["-rdynamic"])
  55. if env["debug_symbols"]:
  56. # Adding dwarf-4 explicitly makes stacktraces work with clang builds,
  57. # otherwise addr2line doesn't understand them
  58. env.Append(CCFLAGS=["-gdwarf-4"])
  59. ## Architecture
  60. # Cross-compilation
  61. # TODO: Support cross-compilation on architectures other than x86.
  62. host_is_64_bit = sys.maxsize > 2**32
  63. if env["bits"] == "default":
  64. env["bits"] = "64" if host_is_64_bit else "32"
  65. if host_is_64_bit and (env["bits"] == "32" or env["arch"] == "x86"):
  66. env.Append(CCFLAGS=["-m32"])
  67. env.Append(LINKFLAGS=["-m32"])
  68. elif not host_is_64_bit and (env["bits"] == "64" or env["arch"] == "x86_64"):
  69. env.Append(CCFLAGS=["-m64"])
  70. env.Append(LINKFLAGS=["-m64"])
  71. if env["arch"] == "" and platform.machine() == "riscv64":
  72. env["arch"] = "rv64"
  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. env.Append(LIBS=["atomic"])
  86. # Linker
  87. if env["linker"] != "default":
  88. print("Using linker program: " + env["linker"])
  89. if env["linker"] == "mold" and using_gcc(env): # GCC < 12.1 doesn't support -fuse-ld=mold.
  90. cc_semver = tuple(get_compiler_version(env))
  91. if cc_semver < (12, 1):
  92. found_wrapper = False
  93. for path in ["/usr/libexec", "/usr/local/libexec", "/usr/lib", "/usr/local/lib"]:
  94. if os.path.isfile(path + "/mold/ld"):
  95. env.Append(LINKFLAGS=["-B" + path + "/mold"])
  96. found_wrapper = True
  97. break
  98. if not found_wrapper:
  99. print("Couldn't locate mold installation path. Make sure it's installed in /usr or /usr/local.")
  100. sys.exit(255)
  101. else:
  102. env.Append(LINKFLAGS=["-fuse-ld=mold"])
  103. else:
  104. env.Append(LINKFLAGS=["-fuse-ld=%s" % env["linker"]])
  105. # Sanitizers
  106. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  107. env.extra_suffix += "s"
  108. if env["use_ubsan"]:
  109. env.Append(CCFLAGS=["-fsanitize=undefined"])
  110. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  111. if env["use_asan"]:
  112. env.Append(CCFLAGS=["-fsanitize=address"])
  113. env.Append(LINKFLAGS=["-fsanitize=address"])
  114. if env["use_lsan"]:
  115. env.Append(CCFLAGS=["-fsanitize=leak"])
  116. env.Append(LINKFLAGS=["-fsanitize=leak"])
  117. if env["use_tsan"]:
  118. env.Append(CCFLAGS=["-fsanitize=thread"])
  119. env.Append(LINKFLAGS=["-fsanitize=thread"])
  120. if env["use_msan"]:
  121. env.Append(CCFLAGS=["-fsanitize=memory"])
  122. env.Append(LINKFLAGS=["-fsanitize=memory"])
  123. # LTO
  124. if env["lto"] != "none":
  125. if env["lto"] == "thin":
  126. if not env["use_llvm"]:
  127. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  128. sys.exit(255)
  129. env.Append(CCFLAGS=["-flto=thin"])
  130. env.Append(LINKFLAGS=["-flto=thin"])
  131. elif 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. env.Append(CCFLAGS=["-flto"])
  136. env.Append(LINKFLAGS=["-flto"])
  137. if not env["use_llvm"]:
  138. env["RANLIB"] = "gcc-ranlib"
  139. env["AR"] = "gcc-ar"
  140. env.Append(CCFLAGS=["-pipe"])
  141. ## Dependencies
  142. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  143. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  144. # as shared libraries leads to weird issues
  145. if env["builtin_freetype"] or env["builtin_libpng"] or env["builtin_zlib"]:
  146. env["builtin_freetype"] = True
  147. env["builtin_libpng"] = True
  148. env["builtin_zlib"] = True
  149. if not env["builtin_freetype"]:
  150. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  151. if not env["builtin_libpng"]:
  152. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  153. if not env["builtin_bullet"]:
  154. # We need at least version 2.90
  155. min_bullet_version = "2.90"
  156. import subprocess
  157. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  158. if str(bullet_version) < min_bullet_version:
  159. # Abort as system bullet was requested but too old
  160. print(
  161. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  162. bullet_version, min_bullet_version
  163. )
  164. )
  165. sys.exit(255)
  166. env.ParseConfig("pkg-config bullet --cflags --libs")
  167. if False: # not env['builtin_assimp']:
  168. # FIXME: Add min version check
  169. env.ParseConfig("pkg-config assimp --cflags --libs")
  170. if not env["builtin_enet"]:
  171. env.ParseConfig("pkg-config libenet --cflags --libs")
  172. if not env["builtin_squish"]:
  173. env.ParseConfig("pkg-config libsquish --cflags --libs")
  174. if not env["builtin_zstd"]:
  175. env.ParseConfig("pkg-config libzstd --cflags --libs")
  176. # Sound and video libraries
  177. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  178. if not env["builtin_libtheora"]:
  179. env["builtin_libogg"] = False # Needed to link against system libtheora
  180. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  181. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  182. else:
  183. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  184. if any(platform.machine() in s for s in list_of_x86):
  185. env["x86_libtheora_opt_gcc"] = True
  186. if not env["builtin_libvpx"]:
  187. env.ParseConfig("pkg-config vpx --cflags --libs")
  188. if not env["builtin_libvorbis"]:
  189. env["builtin_libogg"] = False # Needed to link against system libvorbis
  190. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  191. if not env["builtin_opus"]:
  192. env["builtin_libogg"] = False # Needed to link against system opus
  193. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  194. if not env["builtin_libogg"]:
  195. env.ParseConfig("pkg-config ogg --cflags --libs")
  196. if not env["builtin_libwebp"]:
  197. env.ParseConfig("pkg-config libwebp --cflags --libs")
  198. if not env["builtin_mbedtls"]:
  199. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  200. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  201. if not env["builtin_wslay"]:
  202. env.ParseConfig("pkg-config libwslay --cflags --libs")
  203. if not env["builtin_miniupnpc"]:
  204. # No pkgconfig file so far, hardcode default paths.
  205. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  206. env.Append(LIBS=["miniupnpc"])
  207. # On Linux wchar_t should be 32-bits
  208. # 16-bit library shouldn't be required due to compiler optimisations
  209. if not env["builtin_pcre2"]:
  210. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  211. # Embree is only used in tools build on x86_64 and aarch64.
  212. if env["tools"] and not env["builtin_embree"] and host_is_64_bit:
  213. # No pkgconfig file so far, hardcode expected lib name.
  214. env.Append(LIBS=["embree3"])
  215. ## Flags
  216. # Linkflags below this line should typically stay the last ones
  217. if not env["builtin_zlib"]:
  218. env.ParseConfig("pkg-config zlib --cflags --libs")
  219. env.Prepend(CPPPATH=["#platform/server"])
  220. env.Append(CPPDEFINES=["SERVER_ENABLED", "UNIX_ENABLED"])
  221. if platform.system() == "Darwin":
  222. env.Append(LINKFLAGS=["-framework", "Cocoa", "-framework", "Carbon", "-lz", "-framework", "IOKit"])
  223. env.Append(LIBS=["pthread"])
  224. if platform.system() == "Linux":
  225. env.Append(LIBS=["dl"])
  226. if platform.system().find("BSD") >= 0:
  227. env["execinfo"] = True
  228. if env["execinfo"]:
  229. env.Append(LIBS=["execinfo"])
  230. if platform.system() != "Darwin":
  231. # Link those statically for portability
  232. if env["use_static_cpp"]:
  233. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  234. if env["use_llvm"] and platform.system() != "FreeBSD":
  235. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  236. else:
  237. if env["use_llvm"] and platform.system() != "FreeBSD":
  238. env.Append(LIBS=["atomic"])