detect.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "x11"
  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_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  24. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False),
  25. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN))", False),
  26. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False),
  27. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  28. BoolVariable("use_msan", "Use LLVM/GCC compiler memory sanitizer (MSAN))", False),
  29. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  30. BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False),
  31. ]
  32. def get_flags():
  33. return []
  34. def configure(env):
  35. ## Build type
  36. if env["target"] == "release":
  37. if env["optimize"] == "speed": # optimize for speed (default)
  38. env.Prepend(CCFLAGS=["-O3"])
  39. elif env["optimize"] == "size": # optimize for size
  40. env.Prepend(CCFLAGS=["-Os"])
  41. if env["debug_symbols"]:
  42. env.Prepend(CCFLAGS=["-g2"])
  43. elif env["target"] == "release_debug":
  44. if env["optimize"] == "speed": # optimize for speed (default)
  45. env.Prepend(CCFLAGS=["-O2"])
  46. elif env["optimize"] == "size": # optimize for size
  47. env.Prepend(CCFLAGS=["-Os"])
  48. if env["debug_symbols"]:
  49. env.Prepend(CCFLAGS=["-g2"])
  50. elif env["target"] == "debug":
  51. env.Prepend(CCFLAGS=["-g3"])
  52. env.Append(LINKFLAGS=["-rdynamic"])
  53. ## Architecture
  54. is64 = sys.maxsize > 2**32
  55. if env["bits"] == "default":
  56. env["bits"] = "64" if is64 else "32"
  57. if env["arch"] == "" and platform.machine() == "riscv64":
  58. env["arch"] = "rv64"
  59. if env["arch"] == "rv64":
  60. # G = General-purpose extensions, C = Compression extension (very common).
  61. env.Append(CCFLAGS=["-march=rv64gc"])
  62. ## Compiler configuration
  63. if "CXX" in env and "clang" in os.path.basename(env["CXX"]):
  64. # Convenience check to enforce the use_llvm overrides when CXX is clang(++)
  65. env["use_llvm"] = True
  66. if env["use_llvm"]:
  67. if "clang++" not in os.path.basename(env["CXX"]):
  68. env["CC"] = "clang"
  69. env["CXX"] = "clang++"
  70. env.extra_suffix = ".llvm" + env.extra_suffix
  71. env.Append(LIBS=["atomic"])
  72. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"] or env["use_msan"]:
  73. env.extra_suffix += "s"
  74. if env["use_ubsan"]:
  75. env.Append(CCFLAGS=["-fsanitize=undefined"])
  76. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  77. if env["use_asan"]:
  78. env.Append(CCFLAGS=["-fsanitize=address"])
  79. env.Append(LINKFLAGS=["-fsanitize=address"])
  80. if env["use_lsan"]:
  81. env.Append(CCFLAGS=["-fsanitize=leak"])
  82. env.Append(LINKFLAGS=["-fsanitize=leak"])
  83. if env["use_tsan"]:
  84. env.Append(CCFLAGS=["-fsanitize=thread"])
  85. env.Append(LINKFLAGS=["-fsanitize=thread"])
  86. if env["use_msan"]:
  87. env.Append(CCFLAGS=["-fsanitize=memory"])
  88. env.Append(LINKFLAGS=["-fsanitize=memory"])
  89. if env["use_lto"]:
  90. env.Append(CCFLAGS=["-flto"])
  91. if not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  92. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  93. else:
  94. env.Append(LINKFLAGS=["-flto"])
  95. if not env["use_llvm"]:
  96. env["RANLIB"] = "gcc-ranlib"
  97. env["AR"] = "gcc-ar"
  98. env.Append(CCFLAGS=["-pipe"])
  99. env.Append(LINKFLAGS=["-pipe"])
  100. ## Dependencies
  101. # FIXME: Check for existence of the libs before parsing their flags with pkg-config
  102. # freetype depends on libpng and zlib, so bundling one of them while keeping others
  103. # as shared libraries leads to weird issues
  104. if env["builtin_freetype"] or env["builtin_libpng"] or env["builtin_zlib"]:
  105. env["builtin_freetype"] = True
  106. env["builtin_libpng"] = True
  107. env["builtin_zlib"] = True
  108. if not env["builtin_freetype"]:
  109. env.ParseConfig("pkg-config freetype2 --cflags --libs")
  110. if not env["builtin_libpng"]:
  111. env.ParseConfig("pkg-config libpng16 --cflags --libs")
  112. if not env["builtin_bullet"]:
  113. # We need at least version 2.90
  114. min_bullet_version = "2.90"
  115. import subprocess
  116. bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
  117. if str(bullet_version) < min_bullet_version:
  118. # Abort as system bullet was requested but too old
  119. print(
  120. "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
  121. bullet_version, min_bullet_version
  122. )
  123. )
  124. sys.exit(255)
  125. env.ParseConfig("pkg-config bullet --cflags --libs")
  126. if False: # not env['builtin_assimp']:
  127. # FIXME: Add min version check
  128. env.ParseConfig("pkg-config assimp --cflags --libs")
  129. if not env["builtin_enet"]:
  130. env.ParseConfig("pkg-config libenet --cflags --libs")
  131. if not env["builtin_squish"]:
  132. env.ParseConfig("pkg-config libsquish --cflags --libs")
  133. if not env["builtin_zstd"]:
  134. env.ParseConfig("pkg-config libzstd --cflags --libs")
  135. # Sound and video libraries
  136. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  137. if not env["builtin_libtheora"]:
  138. env["builtin_libogg"] = False # Needed to link against system libtheora
  139. env["builtin_libvorbis"] = False # Needed to link against system libtheora
  140. env.ParseConfig("pkg-config theora theoradec --cflags --libs")
  141. else:
  142. list_of_x86 = ["x86_64", "x86", "i386", "i586"]
  143. if any(platform.machine() in s for s in list_of_x86):
  144. env["x86_libtheora_opt_gcc"] = True
  145. if not env["builtin_libvpx"]:
  146. env.ParseConfig("pkg-config vpx --cflags --libs")
  147. if not env["builtin_libvorbis"]:
  148. env["builtin_libogg"] = False # Needed to link against system libvorbis
  149. env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
  150. if not env["builtin_opus"]:
  151. env["builtin_libogg"] = False # Needed to link against system opus
  152. env.ParseConfig("pkg-config opus opusfile --cflags --libs")
  153. if not env["builtin_libogg"]:
  154. env.ParseConfig("pkg-config ogg --cflags --libs")
  155. if not env["builtin_libwebp"]:
  156. env.ParseConfig("pkg-config libwebp --cflags --libs")
  157. if not env["builtin_mbedtls"]:
  158. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  159. env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
  160. if not env["builtin_wslay"]:
  161. env.ParseConfig("pkg-config libwslay --cflags --libs")
  162. if not env["builtin_miniupnpc"]:
  163. # No pkgconfig file so far, hardcode default paths.
  164. env.Prepend(CPPPATH=["/usr/include/miniupnpc"])
  165. env.Append(LIBS=["miniupnpc"])
  166. # On Linux wchar_t should be 32-bits
  167. # 16-bit library shouldn't be required due to compiler optimisations
  168. if not env["builtin_pcre2"]:
  169. env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
  170. # Embree is only compatible with x86_64. Yet another unreliable hack that will break
  171. # cross-compilation, this will really need to be handle better. Thankfully only affects
  172. # people who disable builtin_embree (likely distro packagers).
  173. if env["tools"] and not env["builtin_embree"] and (is64 and platform.machine() == "x86_64"):
  174. # No pkgconfig file so far, hardcode expected lib name.
  175. env.Append(LIBS=["embree3"])
  176. ## Flags
  177. # Linkflags below this line should typically stay the last ones
  178. if not env["builtin_zlib"]:
  179. env.ParseConfig("pkg-config zlib --cflags --libs")
  180. env.Prepend(CPPPATH=["#platform/server"])
  181. env.Append(CPPDEFINES=["SERVER_ENABLED", "UNIX_ENABLED"])
  182. if platform.system() == "Darwin":
  183. env.Append(LINKFLAGS=["-framework", "Cocoa", "-framework", "Carbon", "-lz", "-framework", "IOKit"])
  184. env.Append(LIBS=["pthread"])
  185. if platform.system() == "Linux":
  186. env.Append(LIBS=["dl"])
  187. if platform.system().find("BSD") >= 0:
  188. env["execinfo"] = True
  189. if env["execinfo"]:
  190. env.Append(LIBS=["execinfo"])
  191. if platform.system() != "Darwin":
  192. # Link those statically for portability
  193. if env["use_static_cpp"]:
  194. env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"])
  195. if env["use_llvm"] and platform.system() != "FreeBSD":
  196. env["LINKCOM"] = env["LINKCOM"] + " -l:libatomic.a"
  197. else:
  198. if env["use_llvm"] and platform.system() != "FreeBSD":
  199. env.Append(LIBS=["atomic"])