detect.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import os
  2. import platform
  3. import sys
  4. # This file is mostly based on platform/x11/detect.py.
  5. # If editing this file, make sure to apply relevant changes here too.
  6. def is_active():
  7. return True
  8. def get_name():
  9. return "Server"
  10. def get_program_suffix():
  11. if sys.platform == "darwin":
  12. return "osx"
  13. return "linuxbsd"
  14. def can_build():
  15. if os.name != "posix":
  16. return False
  17. return True
  18. def get_opts():
  19. from SCons.Variables import BoolVariable, EnumVariable
  20. return [
  21. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  22. BoolVariable("use_static_cpp", "Link libgcc and libstdc++ statically for better portability", True),
  23. BoolVariable("use_coverage", "Test Godot coverage", False),
  24. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  25. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
  26. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN)", False),
  27. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
  28. BoolVariable("use_msan", "Use LLVM compiler memory sanitizer (MSAN)", False),
  29. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  30. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  31. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  32. ]
  33. def get_flags():
  34. return []
  35. def configure(env):
  36. ## Build type
  37. if env["target"] == "release":
  38. if env["optimize"] == "speed": # optimize for speed (default)
  39. env.Prepend(CCFLAGS=["-O3"])
  40. elif env["optimize"] == "size": # optimize for size
  41. env.Prepend(CCFLAGS=["-Os"])
  42. if env["debug_symbols"]:
  43. env.Prepend(CCFLAGS=["-g2"])
  44. elif env["target"] == "release_debug":
  45. if env["optimize"] == "speed": # optimize for speed (default)
  46. env.Prepend(CCFLAGS=["-O2"])
  47. elif env["optimize"] == "size": # optimize for size
  48. env.Prepend(CCFLAGS=["-Os"])
  49. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  50. if env["debug_symbols"]:
  51. env.Prepend(CCFLAGS=["-g2"])
  52. elif env["target"] == "debug":
  53. env.Prepend(CCFLAGS=["-g3"])
  54. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  55. env.Append(LINKFLAGS=["-rdynamic"])
  56. ## Architecture
  57. is64 = sys.maxsize > 2 ** 32
  58. if env["bits"] == "default":
  59. env["bits"] = "64" if is64 else "32"
  60. ## Compiler configuration
  61. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  62. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  63. env["use_llvm"] = True
  64. if env["use_llvm"]:
  65. if "clang++" not in os.path.basename(env["CXX"]):
  66. env["CC"] = "clang"
  67. env["CXX"] = "clang++"
  68. env.extra_suffix = ".llvm" + env.extra_suffix
  69. env.Append(LIBS=["atomic"])
  70. if env["use_coverage"]:
  71. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  72. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  73. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  74. env.extra_suffix += "s"
  75. if env["use_ubsan"]:
  76. env.Append(
  77. CCFLAGS=[
  78. "-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"
  79. ]
  80. )
  81. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  82. if env["use_llvm"]:
  83. env.Append(
  84. CCFLAGS=[
  85. "-fsanitize=nullability-return,nullability-arg,function,nullability-assign,implicit-integer-sign-change"
  86. ]
  87. )
  88. else:
  89. env.Append(CCFLAGS=["-fsanitize=bounds-strict"])
  90. if env["use_asan"]:
  91. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  92. env.Append(LINKFLAGS=["-fsanitize=address"])
  93. if env["use_lsan"]:
  94. env.Append(CCFLAGS=["-fsanitize=leak"])
  95. env.Append(LINKFLAGS=["-fsanitize=leak"])
  96. if env["use_tsan"]:
  97. env.Append(CCFLAGS=["-fsanitize=thread"])
  98. env.Append(LINKFLAGS=["-fsanitize=thread"])
  99. if env["use_msan"] and env["use_llvm"]:
  100. env.Append(CCFLAGS=["-fsanitize=memory"])
  101. env.Append(CCFLAGS=["-fsanitize-memory-track-origins"])
  102. env.Append(CCFLAGS=["-fsanitize-recover=memory"])
  103. env.Append(LINKFLAGS=["-fsanitize=memory"])
  104. if env["use_lto"]:
  105. env.Append(CCFLAGS=["-flto"])
  106. if not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  107. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  108. else:
  109. env.Append(LINKFLAGS=["-flto"])
  110. if not env["use_llvm"]:
  111. env["RANLIB"] = "gcc-ranlib"
  112. env["AR"] = "gcc-ar"
  113. env.Append(CCFLAGS=["-pipe"])
  114. env.Append(LINKFLAGS=["-pipe"])
  115. ## Dependencies
  116. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  117. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  118. # as shared libraries leads to weird issues
  119. if (
  120. env["builtin_freetype"]
  121. or env["builtin_libpng"]
  122. or env["builtin_zlib"]
  123. or env["builtin_graphite"]
  124. or env["builtin_harfbuzz"]
  125. ):
  126. env["builtin_freetype"] = True
  127. env["builtin_libpng"] = True
  128. env["builtin_zlib"] = True
  129. env["builtin_graphite"] = True
  130. env["builtin_harfbuzz"] = True
  131. if not env["builtin_freetype"]:
  132. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  133. if not env["builtin_graphite"]:
  134. env.ParseConfig("pkg-config graphite2 --cflags --libs")
  135. if not env["builtin_icu"]:
  136. env.ParseConfig("pkg-config icu-uc --cflags --libs")
  137. if not env["builtin_harfbuzz"]:
  138. env.ParseConfig("pkg-config harfbuzz harfbuzz-icu --cflags --libs")
  139. if not env["builtin_libpng"]:
  140. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  141. if not env["builtin_bullet"]:
  142. # We need at least version 2.89
  143. import subprocess
  144. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  145. if str(bullet_version) < "2.89":
  146. # Abort as system bullet was requested but too old
  147. print(
  148. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  149. bullet_version, "2.89"
  150. )
  151. )
  152. sys.exit(255)
  153. env.ParseConfig("pkg-config bullet --cflags --libs")
  154. if False: # not env['builtin_assimp']:
  155. # FIXME: Add min version check
  156. env.ParseConfig("pkg-config assimp --cflags --libs")
  157. if not env["builtin_enet"]:
  158. env.ParseConfig("pkg-config libenet --cflags --libs")
  159. if not env["builtin_squish"]:
  160. env.ParseConfig("pkg-config libsquish --cflags --libs")
  161. if not env["builtin_zstd"]:
  162. env.ParseConfig("pkg-config libzstd --cflags --libs")
  163. # Sound and video libraries
  164. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  165. if not env["builtin_libtheora"]:
  166. env["builtin_libogg"] = False # Needed to link against system libtheora
  167. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  168. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  169. else:
  170. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  171. if any(platform.machine() in s for s in list_of_x86):
  172. env["x86_libtheora_opt_gcc"] = True
  173. if not env["builtin_libvpx"]:
  174. env.ParseConfig("pkg-config vpx --cflags --libs")
  175. if not env["builtin_libvorbis"]:
  176. env["builtin_libogg"] = False # Needed to link against system libvorbis
  177. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  178. if not env["builtin_opus"]:
  179. env["builtin_libogg"] = False # Needed to link against system opus
  180. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  181. if not env["builtin_libogg"]:
  182. env.ParseConfig("pkg-config ogg --cflags --libs")
  183. if not env["builtin_libwebp"]:
  184. env.ParseConfig("pkg-config libwebp --cflags --libs")
  185. if not env["builtin_mbedtls"]:
  186. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  187. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  188. if not env["builtin_wslay"]:
  189. env.ParseConfig("pkg-config libwslay --cflags --libs")
  190. if not env["builtin_miniupnpc"]:
  191. # No pkgconfig file so far, hardcode default paths.
  192. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  193. env.Append(LIBS=["miniupnpc"])
  194. # On Linux wchar_t should be 32-bits
  195. # 16-bit library shouldn't be required due to compiler optimisations
  196. if not env["builtin_pcre2"]:
  197. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  198. ## Flags
  199. # Linkflags below this line should typically stay the last ones
  200. if not env["builtin_zlib"]:
  201. env.ParseConfig("pkg-config zlib --cflags --libs")
  202. env.Prepend(CPPPATH=["#platform/server"])
  203. env.Append(CPPDEFINES=["SERVER_ENABLED", "UNIX_ENABLED"])
  204. if platform.system() == "Darwin":
  205. env.Append(
  206. LINKFLAGS=[
  207. "-framework",
  208. "Cocoa",
  209. "-framework",
  210. "Carbon",
  211. "-lz",
  212. "-framework",
  213. "IOKit",
  214. ]
  215. )
  216. env.Append(LIBS=["pthread"])
  217. if platform.system() == "Linux":
  218. env.Append(LIBS=["dl"])
  219. if platform.system().find("BSD") >= 0:
  220. env["execinfo"] = True
  221. if env["execinfo"]:
  222. env.Append(LIBS=["execinfo"])
  223. # Link those statically for portability
  224. if env["use_static_cpp"]:
  225. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])