detect.py 8.2 KB

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