detect.py 8.1 KB

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