detect.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import os
  2. import sys
  3. from methods import detect_darwin_sdk_path, get_compiler_version, is_vanilla_clang
  4. def is_active():
  5. return True
  6. def get_name():
  7. return "OSX"
  8. def can_build():
  9. if sys.platform == "darwin" or ("OSXCROSS_ROOT" in os.environ):
  10. return True
  11. return False
  12. def get_opts():
  13. from SCons.Variables import BoolVariable, EnumVariable
  14. return [
  15. ("osxcross_sdk", "OSXCross SDK version", "darwin14"),
  16. ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
  17. EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ("no", "5.0", "devel")),
  18. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  19. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  20. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  21. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False),
  22. BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN))", False),
  23. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False),
  24. ]
  25. def get_flags():
  26. return []
  27. def configure(env):
  28. ## Build type
  29. if env["target"] == "release":
  30. if env["optimize"] == "speed": # optimize for speed (default)
  31. env.Prepend(CCFLAGS=["-O3"])
  32. elif env["optimize"] == "size": # optimize for size
  33. env.Prepend(CCFLAGS=["-Os"])
  34. if env["arch"] != "arm64":
  35. env.Prepend(CCFLAGS=["-msse2"])
  36. if env["debug_symbols"]:
  37. env.Prepend(CCFLAGS=["-g2"])
  38. elif env["target"] == "release_debug":
  39. if env["optimize"] == "speed": # optimize for speed (default)
  40. env.Prepend(CCFLAGS=["-O2"])
  41. elif env["optimize"] == "size": # optimize for size
  42. env.Prepend(CCFLAGS=["-Os"])
  43. if env["debug_symbols"]:
  44. env.Prepend(CCFLAGS=["-g2"])
  45. elif env["target"] == "debug":
  46. env.Prepend(CCFLAGS=["-g3"])
  47. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  48. ## Architecture
  49. # Mac OS X no longer runs on 32-bit since 10.7 which is unsupported since 2014
  50. # As such, we only support 64-bit
  51. env["bits"] = "64"
  52. ## Compiler configuration
  53. # Save this in environment for use by other modules
  54. if "OSXCROSS_ROOT" in os.environ:
  55. env["osxcross"] = True
  56. if env["arch"] == "arm64":
  57. print("Building for macOS 11.0+, platform arm64.")
  58. env.Append(ASFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  59. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  60. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  61. else:
  62. print("Building for macOS 10.13+, platform x86-64.")
  63. env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  64. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  65. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  66. cc_version = get_compiler_version(env) or [-1, -1]
  67. vanilla = is_vanilla_clang(env)
  68. # Workaround for Xcode 15 linker bug.
  69. if not vanilla and cc_version[0] == 15 and cc_version[1] == 0:
  70. env.Prepend(LINKFLAGS=["-ld_classic"])
  71. if not "osxcross" in env: # regular native build
  72. if env["macports_clang"] != "no":
  73. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  74. mpclangver = env["macports_clang"]
  75. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  76. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  77. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  78. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  79. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  80. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  81. else:
  82. env["CC"] = "clang"
  83. env["CXX"] = "clang++"
  84. detect_darwin_sdk_path("osx", env)
  85. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  86. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  87. else: # osxcross build
  88. root = os.environ.get("OSXCROSS_ROOT", 0)
  89. if env["arch"] == "arm64":
  90. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  91. else:
  92. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  93. ccache_path = os.environ.get("CCACHE")
  94. if ccache_path is None:
  95. env["CC"] = basecmd + "cc"
  96. env["CXX"] = basecmd + "c++"
  97. else:
  98. # there aren't any ccache wrappers available for OS X cross-compile,
  99. # to enable caching we need to prepend the path to the ccache binary
  100. env["CC"] = ccache_path + " " + basecmd + "cc"
  101. env["CXX"] = ccache_path + " " + basecmd + "c++"
  102. env["AR"] = basecmd + "ar"
  103. env["RANLIB"] = basecmd + "ranlib"
  104. env["AS"] = basecmd + "as"
  105. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  106. # LTO
  107. if env["lto"] == "auto": # LTO benefits for macOS (size, performance) haven't been clearly established yet.
  108. env["lto"] = "none"
  109. if env["lto"] != "none":
  110. if env["lto"] == "thin":
  111. env.Append(CCFLAGS=["-flto=thin"])
  112. env.Append(LINKFLAGS=["-flto=thin"])
  113. else:
  114. env.Append(CCFLAGS=["-flto"])
  115. env.Append(LINKFLAGS=["-flto"])
  116. # Sanitizers
  117. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"]:
  118. env.extra_suffix += "s"
  119. if env["use_ubsan"]:
  120. env.Append(CCFLAGS=["-fsanitize=undefined"])
  121. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  122. if env["use_asan"]:
  123. env.Append(CCFLAGS=["-fsanitize=address"])
  124. env.Append(LINKFLAGS=["-fsanitize=address"])
  125. if env["use_lsan"]:
  126. env.Append(CCFLAGS=["-fsanitize=leak"])
  127. env.Append(LINKFLAGS=["-fsanitize=leak"])
  128. if env["use_tsan"]:
  129. env.Append(CCFLAGS=["-fsanitize=thread"])
  130. env.Append(LINKFLAGS=["-fsanitize=thread"])
  131. ## Dependencies
  132. if env["builtin_libtheora"]:
  133. if env["arch"] != "arm64":
  134. env["x86_libtheora_opt_gcc"] = True
  135. ## Flags
  136. env.Prepend(CPPPATH=["#platform/osx"])
  137. env.Append(
  138. CPPDEFINES=[
  139. "OSX_ENABLED",
  140. "UNIX_ENABLED",
  141. "GLES_ENABLED",
  142. "APPLE_STYLE_KEYS",
  143. "COREAUDIO_ENABLED",
  144. "COREMIDI_ENABLED",
  145. "GL_SILENCE_DEPRECATION",
  146. ]
  147. )
  148. env.Append(
  149. LINKFLAGS=[
  150. "-framework",
  151. "Cocoa",
  152. "-framework",
  153. "Carbon",
  154. "-framework",
  155. "OpenGL",
  156. "-framework",
  157. "AGL",
  158. "-framework",
  159. "AudioUnit",
  160. "-framework",
  161. "CoreAudio",
  162. "-framework",
  163. "CoreMIDI",
  164. "-lz",
  165. "-framework",
  166. "IOKit",
  167. "-framework",
  168. "ForceFeedback",
  169. "-framework",
  170. "AVFoundation",
  171. "-framework",
  172. "CoreMedia",
  173. "-framework",
  174. "CoreVideo",
  175. ]
  176. )
  177. env.Append(LIBS=["pthread"])