detect.py 7.1 KB

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