detect.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import os
  2. import platform
  3. import sys
  4. from methods import get_compiler_version, use_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. BoolVariable('use_llvm', 'Use the LLVM compiler', False),
  23. BoolVariable('use_static_cpp', 'Link libgcc and libstdc++ statically for better portability', 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. EnumVariable('debug_symbols', 'Add debugging symbols to release builds', 'yes', ('yes', 'no', 'full')),
  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"] == "yes"):
  41. env.Prepend(CCFLAGS=['-g1'])
  42. if (env["debug_symbols"] == "full"):
  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', '-DDEBUG_ENABLED'])
  47. else: #optimize for size
  48. env.Prepend(CCFLAGS=['-Os', '-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', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_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["LINK"] = "clang++"
  69. env.Append(CPPFLAGS=['-DTYPED_METHOD_BIND'])
  70. env.extra_suffix = ".llvm" + env.extra_suffix
  71. if env['use_ubsan'] or env['use_asan'] or env['use_lsan']:
  72. env.extra_suffix += "s"
  73. if env['use_ubsan']:
  74. env.Append(CCFLAGS=['-fsanitize=undefined'])
  75. env.Append(LINKFLAGS=['-fsanitize=undefined'])
  76. if env['use_asan']:
  77. env.Append(CCFLAGS=['-fsanitize=address'])
  78. env.Append(LINKFLAGS=['-fsanitize=address'])
  79. if env['use_lsan']:
  80. env.Append(CCFLAGS=['-fsanitize=leak'])
  81. env.Append(LINKFLAGS=['-fsanitize=leak'])
  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 libpng --cflags --libs')
  105. if not env['builtin_bullet']:
  106. # We need at least version 2.88
  107. import subprocess
  108. bullet_version = subprocess.check_output(['pkg-config', 'bullet', '--modversion']).strip()
  109. if str(bullet_version) < "2.88":
  110. # Abort as system bullet was requested but too old
  111. print("Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(bullet_version, "2.88"))
  112. sys.exit(255)
  113. env.ParseConfig('pkg-config bullet --cflags --libs')
  114. if not env['builtin_enet']:
  115. env.ParseConfig('pkg-config libenet --cflags --libs')
  116. if not env['builtin_squish'] and env['tools']:
  117. env.ParseConfig('pkg-config libsquish --cflags --libs')
  118. if not env['builtin_zstd']:
  119. env.ParseConfig('pkg-config libzstd --cflags --libs')
  120. # Sound and video libraries
  121. # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
  122. if not env['builtin_libtheora']:
  123. env['builtin_libogg'] = False # Needed to link against system libtheora
  124. env['builtin_libvorbis'] = False # Needed to link against system libtheora
  125. env.ParseConfig('pkg-config theora theoradec --cflags --libs')
  126. else:
  127. list_of_x86 = ['x86_64', 'x86', 'i386', 'i586']
  128. if any(platform.machine() in s for s in list_of_x86):
  129. env["x86_libtheora_opt_gcc"] = True
  130. if not env['builtin_libvpx']:
  131. env.ParseConfig('pkg-config vpx --cflags --libs')
  132. if not env['builtin_libvorbis']:
  133. env['builtin_libogg'] = False # Needed to link against system libvorbis
  134. env.ParseConfig('pkg-config vorbis vorbisfile --cflags --libs')
  135. if not env['builtin_opus']:
  136. env['builtin_libogg'] = False # Needed to link against system opus
  137. env.ParseConfig('pkg-config opus opusfile --cflags --libs')
  138. if not env['builtin_libogg']:
  139. env.ParseConfig('pkg-config ogg --cflags --libs')
  140. if not env['builtin_libwebp']:
  141. env.ParseConfig('pkg-config libwebp --cflags --libs')
  142. if not env['builtin_mbedtls']:
  143. # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
  144. env.Append(LIBS=['mbedtls', 'mbedcrypto', 'mbedx509'])
  145. if not env['builtin_libwebsockets']:
  146. env.ParseConfig('pkg-config libwebsockets --cflags --libs')
  147. if not env['builtin_miniupnpc']:
  148. # No pkgconfig file so far, hardcode default paths.
  149. env.Append(CPPPATH=["/usr/include/miniupnpc"])
  150. env.Append(LIBS=["miniupnpc"])
  151. # On Linux wchar_t should be 32-bits
  152. # 16-bit library shouldn't be required due to compiler optimisations
  153. if not env['builtin_pcre2']:
  154. env.ParseConfig('pkg-config libpcre2-32 --cflags --libs')
  155. ## Flags
  156. # Linkflags below this line should typically stay the last ones
  157. if not env['builtin_zlib']:
  158. env.ParseConfig('pkg-config zlib --cflags --libs')
  159. env.Append(CPPPATH=['#platform/server'])
  160. env.Append(CPPFLAGS=['-DSERVER_ENABLED', '-DUNIX_ENABLED'])
  161. if (platform.system() == "Darwin"):
  162. env.Append(LINKFLAGS=['-framework', 'Cocoa', '-framework', 'Carbon', '-lz', '-framework', 'IOKit'])
  163. env.Append(LIBS=['pthread'])
  164. if (platform.system() == "Linux"):
  165. env.Append(LIBS=['dl'])
  166. if (platform.system().find("BSD") >= 0):
  167. env["execinfo"] = True
  168. if env["execinfo"]:
  169. env.Append(LIBS=['execinfo'])
  170. # Link those statically for portability
  171. if env['use_static_cpp']:
  172. env.Append(LINKFLAGS=['-static-libgcc', '-static-libstdc++'])