detect.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. ("VULKAN_SDK_PATH", "Path to the Vulkan SDK", ""),
  18. EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ("no", "5.0", "devel")),
  19. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  20. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  21. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  22. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
  23. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
  24. BoolVariable("use_coverage", "Use instrumentation codes in the binary (e.g. for code coverage)", False),
  25. ]
  26. def get_flags():
  27. return []
  28. def configure(env):
  29. ## Build type
  30. if env["target"] == "release":
  31. if env["optimize"] == "speed": # optimize for speed (default)
  32. env.Prepend(CCFLAGS=["-O3", "-fomit-frame-pointer", "-ftree-vectorize"])
  33. elif env["optimize"] == "size": # optimize for size
  34. env.Prepend(CCFLAGS=["-Os", "-ftree-vectorize"])
  35. if env["arch"] != "arm64":
  36. env.Prepend(CCFLAGS=["-msse2"])
  37. if env["debug_symbols"]:
  38. env.Prepend(CCFLAGS=["-g2"])
  39. elif env["target"] == "release_debug":
  40. if env["optimize"] == "speed": # optimize for speed (default)
  41. env.Prepend(CCFLAGS=["-O2"])
  42. elif env["optimize"] == "size": # optimize for size
  43. env.Prepend(CCFLAGS=["-Os"])
  44. if env["debug_symbols"]:
  45. env.Prepend(CCFLAGS=["-g2"])
  46. elif env["target"] == "debug":
  47. env.Prepend(CCFLAGS=["-g3"])
  48. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  49. ## Architecture
  50. # Mac OS X no longer runs on 32-bit since 10.7 which is unsupported since 2014
  51. # As such, we only support 64-bit
  52. env["bits"] = "64"
  53. ## Compiler configuration
  54. # Save this in environment for use by other modules
  55. if "OSXCROSS_ROOT" in os.environ:
  56. env["osxcross"] = True
  57. if env["arch"] == "arm64":
  58. print("Building for macOS 11.0+, platform arm64.")
  59. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  60. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  61. else:
  62. print("Building for macOS 10.12+, platform x86_64.")
  63. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  64. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  65. env.Append(CCFLAGS=["-fobjc-arc"])
  66. if not "osxcross" in env: # regular native build
  67. if env["macports_clang"] != "no":
  68. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  69. mpclangver = env["macports_clang"]
  70. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  71. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  72. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  73. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  74. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  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. if env["arch"] == "arm64":
  84. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  85. else:
  86. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  87. ccache_path = os.environ.get("CCACHE")
  88. if ccache_path is None:
  89. env["CC"] = basecmd + "cc"
  90. env["CXX"] = basecmd + "c++"
  91. else:
  92. # there aren't any ccache wrappers available for OS X cross-compile,
  93. # to enable caching we need to prepend the path to the ccache binary
  94. env["CC"] = ccache_path + " " + basecmd + "cc"
  95. env["CXX"] = ccache_path + " " + basecmd + "c++"
  96. env["AR"] = basecmd + "ar"
  97. env["RANLIB"] = basecmd + "ranlib"
  98. env["AS"] = basecmd + "as"
  99. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  100. env.extra_suffix += ".san"
  101. env.Append(CCFLAGS=["-DSANITIZERS_ENABLED"])
  102. if env["use_ubsan"]:
  103. env.Append(
  104. CCFLAGS=[
  105. "-fsanitize=undefined,shift,shift-exponent,integer-divide-by-zero,unreachable,vla-bound,null,return,signed-integer-overflow,bounds,float-divide-by-zero,float-cast-overflow,nonnull-attribute,returns-nonnull-attribute,bool,enum,vptr,pointer-overflow,builtin"
  106. ]
  107. )
  108. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  109. env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
  110. if env["use_asan"]:
  111. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  112. env.Append(LINKFLAGS=["-fsanitize=address"])
  113. if env["use_tsan"]:
  114. env.Append(CCFLAGS=["-fsanitize=thread"])
  115. env.Append(LINKFLAGS=["-fsanitize=thread"])
  116. if env["use_coverage"]:
  117. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  118. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  119. ## Dependencies
  120. if env["builtin_libtheora"]:
  121. if env["arch"] != "arm64":
  122. env["x86_libtheora_opt_gcc"] = True
  123. ## Flags
  124. env.Prepend(CPPPATH=["#platform/osx"])
  125. env.Append(CPPDEFINES=["OSX_ENABLED", "UNIX_ENABLED", "APPLE_STYLE_KEYS", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
  126. env.Append(
  127. LINKFLAGS=[
  128. "-framework",
  129. "Cocoa",
  130. "-framework",
  131. "Carbon",
  132. "-framework",
  133. "AudioUnit",
  134. "-framework",
  135. "CoreAudio",
  136. "-framework",
  137. "CoreMIDI",
  138. "-framework",
  139. "IOKit",
  140. "-framework",
  141. "ForceFeedback",
  142. "-framework",
  143. "CoreVideo",
  144. "-framework",
  145. "AVFoundation",
  146. "-framework",
  147. "CoreMedia",
  148. ]
  149. )
  150. env.Append(LIBS=["pthread", "z"])
  151. if env["opengl3"]:
  152. env.Append(CPPDEFINES=["GLES_ENABLED", "GLES3_ENABLED"])
  153. env.Append(CCFLAGS=["-Wno-deprecated-declarations"]) # Disable deprecation warnings
  154. env.Append(LINKFLAGS=["-framework", "OpenGL"])
  155. env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
  156. if env["vulkan"]:
  157. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  158. env.Append(LINKFLAGS=["-framework", "Metal", "-framework", "QuartzCore", "-framework", "IOSurface"])
  159. if not env["use_volk"]:
  160. env.Append(LINKFLAGS=["-L$VULKAN_SDK_PATH/MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/", "-lMoltenVK"])