detect.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import os
  2. import sys
  3. from typing import TYPE_CHECKING
  4. from methods import detect_darwin_sdk_path, print_error
  5. if TYPE_CHECKING:
  6. from SCons.Script.SConscript import SConsEnvironment
  7. def get_name():
  8. return "iOS"
  9. def can_build():
  10. if sys.platform == "darwin" or ("OSXCROSS_IOS" in os.environ):
  11. return True
  12. return False
  13. def get_opts():
  14. from SCons.Variables import BoolVariable
  15. return [
  16. ("vulkan_sdk_path", "Path to the Vulkan SDK", ""),
  17. (
  18. "IOS_TOOLCHAIN_PATH",
  19. "Path to iOS toolchain",
  20. "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain",
  21. ),
  22. ("IOS_SDK_PATH", "Path to the iOS SDK", ""),
  23. BoolVariable("ios_simulator", "Build for iOS Simulator", False),
  24. ("ios_triple", "Triple for ios toolchain", ""),
  25. BoolVariable("generate_bundle", "Generate an APP bundle after building iOS/macOS binaries", False),
  26. ]
  27. def get_doc_classes():
  28. return [
  29. "EditorExportPlatformIOS",
  30. ]
  31. def get_doc_path():
  32. return "doc_classes"
  33. def get_flags():
  34. return {
  35. "arch": "arm64",
  36. "target": "template_debug",
  37. "use_volk": False,
  38. "metal": True,
  39. "supported": ["mono"],
  40. "builtin_pcre2_with_jit": False,
  41. }
  42. def configure(env: "SConsEnvironment"):
  43. # Validate arch.
  44. supported_arches = ["x86_64", "arm64"]
  45. if env["arch"] not in supported_arches:
  46. print_error(
  47. 'Unsupported CPU architecture "%s" for iOS. Supported architectures are: %s.'
  48. % (env["arch"], ", ".join(supported_arches))
  49. )
  50. sys.exit(255)
  51. ## LTO
  52. if env["lto"] == "auto": # Disable by default as it makes linking in Xcode very slow.
  53. env["lto"] = "none"
  54. if env["lto"] != "none":
  55. if env["lto"] == "thin":
  56. env.Append(CCFLAGS=["-flto=thin"])
  57. env.Append(LINKFLAGS=["-flto=thin"])
  58. else:
  59. env.Append(CCFLAGS=["-flto"])
  60. env.Append(LINKFLAGS=["-flto"])
  61. ## Compiler configuration
  62. # Save this in environment for use by other modules
  63. if "OSXCROSS_IOS" in os.environ:
  64. env["osxcross"] = True
  65. env["ENV"]["PATH"] = env["IOS_TOOLCHAIN_PATH"] + "/Developer/usr/bin/:" + env["ENV"]["PATH"]
  66. compiler_path = "$IOS_TOOLCHAIN_PATH/usr/bin/${ios_triple}"
  67. ccache_path = os.environ.get("CCACHE")
  68. if ccache_path is None:
  69. env["CC"] = compiler_path + "clang"
  70. env["CXX"] = compiler_path + "clang++"
  71. env["S_compiler"] = compiler_path + "clang"
  72. else:
  73. # there aren't any ccache wrappers available for iOS,
  74. # to enable caching we need to prepend the path to the ccache binary
  75. env["CC"] = ccache_path + " " + compiler_path + "clang"
  76. env["CXX"] = ccache_path + " " + compiler_path + "clang++"
  77. env["S_compiler"] = ccache_path + " " + compiler_path + "clang"
  78. env["AR"] = compiler_path + "ar"
  79. env["RANLIB"] = compiler_path + "ranlib"
  80. ## Compile flags
  81. if env["ios_simulator"]:
  82. detect_darwin_sdk_path("iossimulator", env)
  83. env.Append(ASFLAGS=["-mios-simulator-version-min=12.0"])
  84. env.Append(CCFLAGS=["-mios-simulator-version-min=12.0"])
  85. env.Append(CPPDEFINES=["IOS_SIMULATOR"])
  86. env.extra_suffix = ".simulator" + env.extra_suffix
  87. else:
  88. detect_darwin_sdk_path("ios", env)
  89. env.Append(ASFLAGS=["-miphoneos-version-min=12.0"])
  90. env.Append(CCFLAGS=["-miphoneos-version-min=12.0"])
  91. if env["arch"] == "x86_64":
  92. if not env["ios_simulator"]:
  93. print_error("Building for iOS with 'arch=x86_64' requires 'ios_simulator=yes'.")
  94. sys.exit(255)
  95. env["ENV"]["MACOSX_DEPLOYMENT_TARGET"] = "10.9"
  96. env.Append(
  97. CCFLAGS=(
  98. "-fobjc-arc -arch x86_64"
  99. " -fobjc-abi-version=2 -fobjc-legacy-dispatch -fmessage-length=0 -fpascal-strings -fblocks"
  100. " -fasm-blocks -isysroot $IOS_SDK_PATH"
  101. ).split()
  102. )
  103. env.Append(ASFLAGS=["-arch", "x86_64"])
  104. elif env["arch"] == "arm64":
  105. env.Append(
  106. CCFLAGS=(
  107. "-fobjc-arc -arch arm64 -fmessage-length=0 -fno-strict-aliasing"
  108. " -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits"
  109. " -fpascal-strings -fblocks -fvisibility=hidden -MMD -MT dependencies"
  110. " -isysroot $IOS_SDK_PATH".split()
  111. )
  112. )
  113. env.Append(ASFLAGS=["-arch", "arm64"])
  114. # Temp fix for ABS/MAX/MIN macros in iOS SDK blocking compilation
  115. env.Append(CCFLAGS=["-Wno-ambiguous-macro"])
  116. env.Prepend(
  117. CPPPATH=[
  118. "$IOS_SDK_PATH/usr/include",
  119. "$IOS_SDK_PATH/System/Library/Frameworks/AudioUnit.framework/Headers",
  120. ]
  121. )
  122. env.Prepend(CPPPATH=["#platform/ios"])
  123. env.Append(CPPDEFINES=["IOS_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED"])
  124. if env["metal"] and env["arch"] != "arm64":
  125. # Only supported on arm64, so skip it for x86_64 builds.
  126. env["metal"] = False
  127. if env["metal"]:
  128. env.AppendUnique(CPPDEFINES=["METAL_ENABLED", "RD_ENABLED"])
  129. env.Prepend(
  130. CPPPATH=[
  131. "$IOS_SDK_PATH/System/Library/Frameworks/Metal.framework/Headers",
  132. "$IOS_SDK_PATH/System/Library/Frameworks/QuartzCore.framework/Headers",
  133. ]
  134. )
  135. env.Prepend(CPPPATH=["#thirdparty/spirv-cross"])
  136. if env["vulkan"]:
  137. env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  138. if env["opengl3"]:
  139. env.Append(CPPDEFINES=["GLES3_ENABLED", "GLES_SILENCE_DEPRECATION"])
  140. env.Prepend(
  141. CPPPATH=[
  142. "$IOS_SDK_PATH/System/Library/Frameworks/OpenGLES.framework/Headers",
  143. ]
  144. )