detect.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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", "darwin16"),
  16. ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
  17. BoolVariable(
  18. "use_static_mvk",
  19. "Link MoltenVK statically as Level-0 driver (better portability) or use Vulkan ICD loader (enables"
  20. " validation layers)",
  21. False,
  22. ),
  23. EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ("no", "5.0", "devel")),
  24. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  25. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  26. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  27. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False),
  28. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", 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", "-fomit-frame-pointer", "-ftree-vectorize"])
  37. else: # optimize for size
  38. env.Prepend(CCFLAGS=["-Os", "-ftree-vectorize"])
  39. if env["arch"] != "arm64":
  40. env.Prepend(CCFLAGS=["-msse2"])
  41. if env["debug_symbols"]:
  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(CPPDEFINES=["DEBUG_ENABLED"])
  49. if env["debug_symbols"]:
  50. env.Prepend(CCFLAGS=["-g2"])
  51. elif env["target"] == "debug":
  52. env.Prepend(CCFLAGS=["-g3"])
  53. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  54. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  55. ## Architecture
  56. # Mac OS X no longer runs on 32-bit since 10.7 which is unsupported since 2014
  57. # As such, we only support 64-bit
  58. env["bits"] = "64"
  59. ## Compiler configuration
  60. # Save this in environment for use by other modules
  61. if "OSXCROSS_ROOT" in os.environ:
  62. env["osxcross"] = True
  63. if env["arch"] == "arm64":
  64. print("Building for macOS 10.15+, platform arm64.")
  65. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15"])
  66. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15"])
  67. else:
  68. print("Building for macOS 10.12+, platform x86-64.")
  69. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  70. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  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. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  107. env.extra_suffix += "s"
  108. if env["use_ubsan"]:
  109. env.Append(CCFLAGS=["-fsanitize=undefined"])
  110. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  111. if env["use_asan"]:
  112. env.Append(CCFLAGS=["-fsanitize=address"])
  113. env.Append(LINKFLAGS=["-fsanitize=address"])
  114. if env["use_tsan"]:
  115. env.Append(CCFLAGS=["-fsanitize=thread"])
  116. env.Append(LINKFLAGS=["-fsanitize=thread"])
  117. ## Dependencies
  118. if env["builtin_libtheora"]:
  119. if env["arch"] != "arm64":
  120. env["x86_libtheora_opt_gcc"] = True
  121. ## Flags
  122. env.Prepend(CPPPATH=["#platform/osx"])
  123. env.Append(CPPDEFINES=["OSX_ENABLED", "UNIX_ENABLED", "APPLE_STYLE_KEYS", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
  124. env.Append(
  125. LINKFLAGS=[
  126. "-framework",
  127. "Cocoa",
  128. "-framework",
  129. "Carbon",
  130. "-framework",
  131. "AudioUnit",
  132. "-framework",
  133. "CoreAudio",
  134. "-framework",
  135. "CoreMIDI",
  136. "-framework",
  137. "IOKit",
  138. "-framework",
  139. "ForceFeedback",
  140. "-framework",
  141. "CoreVideo",
  142. "-framework",
  143. "AVFoundation",
  144. "-framework",
  145. "CoreMedia",
  146. ]
  147. )
  148. env.Append(LIBS=["pthread", "z"])
  149. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  150. env.Append(LINKFLAGS=["-framework", "Metal", "-framework", "QuartzCore", "-framework", "IOSurface"])
  151. if env["use_static_mvk"]:
  152. env.Append(LINKFLAGS=["-framework", "MoltenVK"])
  153. env["builtin_vulkan"] = False
  154. elif not env["builtin_vulkan"]:
  155. env.Append(LIBS=["vulkan"])
  156. # env.Append(CPPDEFINES=['GLES_ENABLED', 'OPENGL_ENABLED'])