detect.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import os
  2. import sys
  3. import subprocess
  4. from methods import detect_darwin_sdk_path
  5. def is_active():
  6. return True
  7. def get_name():
  8. return "OSX"
  9. def can_build():
  10. if sys.platform == "darwin" or ("OSXCROSS_ROOT" in os.environ):
  11. return True
  12. return False
  13. def get_opts():
  14. from SCons.Variables import BoolVariable, EnumVariable
  15. return [
  16. ("osxcross_sdk", "OSXCross SDK version", "darwin16"),
  17. ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
  18. BoolVariable(
  19. "use_static_mvk",
  20. "Link MoltenVK statically as Level-0 driver (better portability) or use Vulkan ICD loader (enables"
  21. " validation layers)",
  22. False,
  23. ),
  24. EnumVariable("debug_symbols", "Add debugging symbols to release builds", "yes", ("yes", "no", "full")),
  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"] == "yes":
  42. env.Prepend(CCFLAGS=["-g1"])
  43. if env["debug_symbols"] == "full":
  44. env.Prepend(CCFLAGS=["-g2"])
  45. elif env["target"] == "release_debug":
  46. if env["optimize"] == "speed": # optimize for speed (default)
  47. env.Prepend(CCFLAGS=["-O2"])
  48. else: # optimize for size
  49. env.Prepend(CCFLAGS=["-Os"])
  50. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  51. if env["debug_symbols"] == "yes":
  52. env.Prepend(CCFLAGS=["-g1"])
  53. if env["debug_symbols"] == "full":
  54. env.Prepend(CCFLAGS=["-g2"])
  55. elif env["target"] == "debug":
  56. env.Prepend(CCFLAGS=["-g3"])
  57. env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
  58. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  59. ## Architecture
  60. # Mac OS X no longer runs on 32-bit since 10.7 which is unsupported since 2014
  61. # As such, we only support 64-bit
  62. env["bits"] = "64"
  63. ## Compiler configuration
  64. # Save this in environment for use by other modules
  65. if "OSXCROSS_ROOT" in os.environ:
  66. env["osxcross"] = True
  67. if env["arch"] == "arm64":
  68. print("Building for macOS 10.15+, platform arm64.")
  69. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15", "-target", "arm64-apple-macos10.15"])
  70. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=10.15", "-target", "arm64-apple-macos10.15"])
  71. else:
  72. print("Building for macOS 10.12+, platform x86-64.")
  73. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  74. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  75. if not "osxcross" in env: # regular native build
  76. if env["macports_clang"] != "no":
  77. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  78. mpclangver = env["macports_clang"]
  79. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  80. env["LINK"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  81. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  82. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  83. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  84. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  85. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  86. else:
  87. env["CC"] = "clang"
  88. env["CXX"] = "clang++"
  89. detect_darwin_sdk_path("osx", env)
  90. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  91. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  92. else: # osxcross build
  93. root = os.environ.get("OSXCROSS_ROOT", 0)
  94. if env["arch"] == "arm64":
  95. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  96. else:
  97. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  98. ccache_path = os.environ.get("CCACHE")
  99. if ccache_path is None:
  100. env["CC"] = basecmd + "cc"
  101. env["CXX"] = basecmd + "c++"
  102. else:
  103. # there aren't any ccache wrappers available for OS X cross-compile,
  104. # to enable caching we need to prepend the path to the ccache binary
  105. env["CC"] = ccache_path + " " + basecmd + "cc"
  106. env["CXX"] = ccache_path + " " + basecmd + "c++"
  107. env["AR"] = basecmd + "ar"
  108. env["RANLIB"] = basecmd + "ranlib"
  109. env["AS"] = basecmd + "as"
  110. env.Append(CPPDEFINES=["__MACPORTS__"]) # hack to fix libvpx MM256_BROADCASTSI128_SI256 define
  111. if env["CXX"] == "clang++":
  112. env.Append(CPPDEFINES=["TYPED_METHOD_BIND"])
  113. env["CC"] = "clang"
  114. env["LINK"] = "clang++"
  115. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  116. env.extra_suffix += "s"
  117. if env["use_ubsan"]:
  118. env.Append(CCFLAGS=["-fsanitize=undefined"])
  119. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  120. if env["use_asan"]:
  121. env.Append(CCFLAGS=["-fsanitize=address"])
  122. env.Append(LINKFLAGS=["-fsanitize=address"])
  123. if env["use_tsan"]:
  124. env.Append(CCFLAGS=["-fsanitize=thread"])
  125. env.Append(LINKFLAGS=["-fsanitize=thread"])
  126. ## Dependencies
  127. if env["builtin_libtheora"]:
  128. if env["arch"] != "arm64":
  129. env["x86_libtheora_opt_gcc"] = True
  130. ## Flags
  131. env.Prepend(CPPPATH=["#platform/osx"])
  132. env.Append(CPPDEFINES=["OSX_ENABLED", "UNIX_ENABLED", "APPLE_STYLE_KEYS", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
  133. env.Append(
  134. LINKFLAGS=[
  135. "-framework",
  136. "Cocoa",
  137. "-framework",
  138. "Carbon",
  139. "-framework",
  140. "AudioUnit",
  141. "-framework",
  142. "CoreAudio",
  143. "-framework",
  144. "CoreMIDI",
  145. "-framework",
  146. "IOKit",
  147. "-framework",
  148. "ForceFeedback",
  149. "-framework",
  150. "CoreVideo",
  151. "-framework",
  152. "AVFoundation",
  153. "-framework",
  154. "CoreMedia",
  155. ]
  156. )
  157. env.Append(LIBS=["pthread", "z"])
  158. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  159. env.Append(LINKFLAGS=["-framework", "Metal", "-framework", "QuartzCore", "-framework", "IOSurface"])
  160. if env["use_static_mvk"]:
  161. env.Append(LINKFLAGS=["-framework", "MoltenVK"])
  162. env["builtin_vulkan"] = False
  163. elif not env["builtin_vulkan"]:
  164. env.Append(LIBS=["vulkan"])
  165. # env.Append(CPPDEFINES=['GLES_ENABLED', 'OPENGL_ENABLED'])