detect.py 7.2 KB

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