detect.py 6.6 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("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")),
  18. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  19. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  20. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False),
  21. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False),
  22. ]
  23. def get_flags():
  24. return []
  25. def configure(env):
  26. ## Build type
  27. if env["target"] == "release":
  28. if env["debug_symbols"] != "full":
  29. if env["optimize"] == "speed": # optimize for speed (default)
  30. env.Prepend(CCFLAGS=["-O3", "-fomit-frame-pointer", "-ftree-vectorize", "-msse2"])
  31. else: # optimize for size
  32. env.Prepend(CCFLAGS=["-Os", "-ftree-vectorize", "-msse2"])
  33. if env["debug_symbols"] == "yes":
  34. env.Prepend(CCFLAGS=["-g2"])
  35. elif env["target"] == "release_debug":
  36. if env["debug_symbols"] != "full":
  37. if env["optimize"] == "speed": # optimize for speed (default)
  38. env.Prepend(CCFLAGS=["-O2"])
  39. else: # optimize for size
  40. env.Prepend(CCFLAGS=["-Os"])
  41. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  42. if env["debug_symbols"] == "yes":
  43. env.Prepend(CCFLAGS=["-g2"])
  44. elif env["target"] == "debug":
  45. env.Prepend(CCFLAGS=["-g3"])
  46. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  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 not "osxcross" in env: # regular native build
  57. if env["arch"] == "arm64":
  58. print("Building for macOS 10.15+, platform arm64.")
  59. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15", "-target", "arm64-apple-macos10.15"])
  60. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15", "-target", "arm64-apple-macos10.15"])
  61. else:
  62. print("Building for macOS 10.9+, platform x86-64.")
  63. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.9"])
  64. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.9"])
  65. if env["macports_clang"] != "no":
  66. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  67. mpclangver = env["macports_clang"]
  68. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  69. env["LINK"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  70. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  71. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  72. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  73. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  74. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  75. else:
  76. env["CC"] = "clang"
  77. env["CXX"] = "clang++"
  78. detect_darwin_sdk_path("osx", env)
  79. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  80. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  81. else: # osxcross build
  82. root = os.environ.get("OSXCROSS_ROOT", 0)
  83. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  84. ccache_path = os.environ.get("CCACHE")
  85. if ccache_path is None:
  86. env["CC"] = basecmd + "cc"
  87. env["CXX"] = basecmd + "c++"
  88. else:
  89. # there aren't any ccache wrappers available for OS X cross-compile,
  90. # to enable caching we need to prepend the path to the ccache binary
  91. env["CC"] = ccache_path + " " + basecmd + "cc"
  92. env["CXX"] = ccache_path + " " + basecmd + "c++"
  93. env["AR"] = basecmd + "ar"
  94. env["RANLIB"] = basecmd + "ranlib"
  95. env["AS"] = basecmd + "as"
  96. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  97. if env["CXX"] == "clang++":
  98. env.Append(CPPDEFINES=["TYPED_METHOD_BIND"])
  99. env["CC"] = "clang"
  100. env["LINK"] = "clang++"
  101. if env["use_ubsan"] or env["use_asan"] 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_tsan"]:
  110. env.Append(CCFLAGS=["-fsanitize=thread"])
  111. env.Append(LINKFLAGS=["-fsanitize=thread"])
  112. ## Dependencies
  113. if env["builtin_libtheora"]:
  114. if env["arch"] != "arm64":
  115. env["x86_libtheora_opt_gcc"] = True
  116. ## Flags
  117. env.Prepend(CPPPATH=["#platform/osx"])
  118. env.Append(
  119. CPPDEFINES=[
  120. "OSX_ENABLED",
  121. "UNIX_ENABLED",
  122. "GLES_ENABLED",
  123. "APPLE_STYLE_KEYS",
  124. "COREAUDIO_ENABLED",
  125. "COREMIDI_ENABLED",
  126. "GL_SILENCE_DEPRECATION",
  127. ]
  128. )
  129. env.Append(
  130. LINKFLAGS=[
  131. "-framework",
  132. "Cocoa",
  133. "-framework",
  134. "Carbon",
  135. "-framework",
  136. "OpenGL",
  137. "-framework",
  138. "AGL",
  139. "-framework",
  140. "AudioUnit",
  141. "-framework",
  142. "CoreAudio",
  143. "-framework",
  144. "CoreMIDI",
  145. "-lz",
  146. "-framework",
  147. "IOKit",
  148. "-framework",
  149. "ForceFeedback",
  150. "-framework",
  151. "AVFoundation",
  152. "-framework",
  153. "CoreMedia",
  154. "-framework",
  155. "CoreVideo",
  156. ]
  157. )
  158. env.Append(LIBS=["pthread"])