detect.py 6.2 KB

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