detect.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import os
  2. import sys
  3. from methods import detect_darwin_sdk_path
  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", "-fomit-frame-pointer", "-ftree-vectorize"])
  32. elif env["optimize"] == "size": # optimize for size
  33. env.Prepend(CCFLAGS=["-Os", "-ftree-vectorize"])
  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. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  44. if env["debug_symbols"]:
  45. env.Prepend(CCFLAGS=["-g2"])
  46. elif env["target"] == "debug":
  47. env.Prepend(CCFLAGS=["-g3"])
  48. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  49. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  50. ## Architecture
  51. # Mac OS X no longer runs on 32-bit since 10.7 which is unsupported since 2014
  52. # As such, we only support 64-bit
  53. env["bits"] = "64"
  54. ## Compiler configuration
  55. # Save this in environment for use by other modules
  56. if "OSXCROSS_ROOT" in os.environ:
  57. env["osxcross"] = True
  58. if env["arch"] == "arm64":
  59. print("Building for macOS 10.15+, platform arm64.")
  60. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15"])
  61. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15"])
  62. else:
  63. print("Building for macOS 10.9+, platform x86-64.")
  64. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  65. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  66. if not "osxcross" in env: # regular native build
  67. if env["macports_clang"] != "no":
  68. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  69. mpclangver = env["macports_clang"]
  70. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  71. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  72. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  73. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  74. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  75. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  76. else:
  77. env["CC"] = "clang"
  78. env["CXX"] = "clang++"
  79. detect_darwin_sdk_path("osx", env)
  80. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  81. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  82. else: # osxcross build
  83. root = os.environ.get("OSXCROSS_ROOT", 0)
  84. if env["arch"] == "arm64":
  85. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  86. else:
  87. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  88. ccache_path = os.environ.get("CCACHE")
  89. if ccache_path is None:
  90. env["CC"] = basecmd + "cc"
  91. env["CXX"] = basecmd + "c++"
  92. else:
  93. # there aren't any ccache wrappers available for OS X cross-compile,
  94. # to enable caching we need to prepend the path to the ccache binary
  95. env["CC"] = ccache_path + " " + basecmd + "cc"
  96. env["CXX"] = ccache_path + " " + basecmd + "c++"
  97. env["AR"] = basecmd + "ar"
  98. env["RANLIB"] = basecmd + "ranlib"
  99. env["AS"] = basecmd + "as"
  100. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  101. if env["use_ubsan"] or env["use_asan"] or env["use_lsan"] or env["use_tsan"]:
  102. env.extra_suffix += "s"
  103. if env["use_ubsan"]:
  104. env.Append(CCFLAGS=["-fsanitize=undefined"])
  105. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  106. if env["use_asan"]:
  107. env.Append(CCFLAGS=["-fsanitize=address"])
  108. env.Append(LINKFLAGS=["-fsanitize=address"])
  109. if env["use_lsan"]:
  110. env.Append(CCFLAGS=["-fsanitize=leak"])
  111. env.Append(LINKFLAGS=["-fsanitize=leak"])
  112. if env["use_tsan"]:
  113. env.Append(CCFLAGS=["-fsanitize=thread"])
  114. env.Append(LINKFLAGS=["-fsanitize=thread"])
  115. ## Dependencies
  116. if env["builtin_libtheora"]:
  117. if env["arch"] != "arm64":
  118. env["x86_libtheora_opt_gcc"] = True
  119. ## Flags
  120. env.Prepend(CPPPATH=["#platform/osx"])
  121. env.Append(
  122. CPPDEFINES=[
  123. "OSX_ENABLED",
  124. "UNIX_ENABLED",
  125. "GLES_ENABLED",
  126. "APPLE_STYLE_KEYS",
  127. "COREAUDIO_ENABLED",
  128. "COREMIDI_ENABLED",
  129. "GL_SILENCE_DEPRECATION",
  130. ]
  131. )
  132. env.Append(
  133. LINKFLAGS=[
  134. "-framework",
  135. "Cocoa",
  136. "-framework",
  137. "Carbon",
  138. "-framework",
  139. "OpenGL",
  140. "-framework",
  141. "AGL",
  142. "-framework",
  143. "AudioUnit",
  144. "-framework",
  145. "CoreAudio",
  146. "-framework",
  147. "CoreMIDI",
  148. "-lz",
  149. "-framework",
  150. "IOKit",
  151. "-framework",
  152. "ForceFeedback",
  153. "-framework",
  154. "AVFoundation",
  155. "-framework",
  156. "CoreMedia",
  157. "-framework",
  158. "CoreVideo",
  159. ]
  160. )
  161. env.Append(LIBS=["pthread"])