detect.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import os
  2. import sys
  3. from methods import detect_darwin_sdk_path
  4. from typing import TYPE_CHECKING
  5. if TYPE_CHECKING:
  6. from SCons import Environment
  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. (
  17. "IOS_TOOLCHAIN_PATH",
  18. "Path to iOS toolchain",
  19. "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain",
  20. ),
  21. ("IOS_SDK_PATH", "Path to the iOS SDK", ""),
  22. BoolVariable("ios_simulator", "Build for iOS Simulator", False),
  23. ("ios_triple", "Triple for ios toolchain", ""),
  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"), # Default for convenience.
  34. ("target", "template_debug"),
  35. ("use_volk", False),
  36. ]
  37. def configure(env: "Environment"):
  38. # Validate arch.
  39. supported_arches = ["x86_64", "arm64"]
  40. if env["arch"] not in supported_arches:
  41. print(
  42. 'Unsupported CPU architecture "%s" for iOS. Supported architectures are: %s.'
  43. % (env["arch"], ", ".join(supported_arches))
  44. )
  45. sys.exit()
  46. ## LTO
  47. if env["lto"] == "auto": # Disable by default as it makes linking in Xcode very slow.
  48. env["lto"] = "none"
  49. if env["lto"] != "none":
  50. if env["lto"] == "thin":
  51. env.Append(CCFLAGS=["-flto=thin"])
  52. env.Append(LINKFLAGS=["-flto=thin"])
  53. else:
  54. env.Append(CCFLAGS=["-flto"])
  55. env.Append(LINKFLAGS=["-flto"])
  56. ## Compiler configuration
  57. # Save this in environment for use by other modules
  58. if "OSXCROSS_IOS" in os.environ:
  59. env["osxcross"] = True
  60. env["ENV"]["PATH"] = env["IOS_TOOLCHAIN_PATH"] + "/Developer/usr/bin/:" + env["ENV"]["PATH"]
  61. compiler_path = "$IOS_TOOLCHAIN_PATH/usr/bin/${ios_triple}"
  62. ccache_path = os.environ.get("CCACHE")
  63. if ccache_path is None:
  64. env["CC"] = compiler_path + "clang"
  65. env["CXX"] = compiler_path + "clang++"
  66. env["S_compiler"] = compiler_path + "clang"
  67. else:
  68. # there aren't any ccache wrappers available for iOS,
  69. # to enable caching we need to prepend the path to the ccache binary
  70. env["CC"] = ccache_path + " " + compiler_path + "clang"
  71. env["CXX"] = ccache_path + " " + compiler_path + "clang++"
  72. env["S_compiler"] = ccache_path + " " + compiler_path + "clang"
  73. env["AR"] = compiler_path + "ar"
  74. env["RANLIB"] = compiler_path + "ranlib"
  75. ## Compile flags
  76. if env["ios_simulator"]:
  77. detect_darwin_sdk_path("iossimulator", env)
  78. env.Append(ASFLAGS=["-mios-simulator-version-min=12.0"])
  79. env.Append(CCFLAGS=["-mios-simulator-version-min=12.0"])
  80. env.extra_suffix = ".simulator" + env.extra_suffix
  81. else:
  82. detect_darwin_sdk_path("ios", env)
  83. env.Append(ASFLAGS=["-miphoneos-version-min=12.0"])
  84. env.Append(CCFLAGS=["-miphoneos-version-min=12.0"])
  85. if env["arch"] == "x86_64":
  86. if not env["ios_simulator"]:
  87. print("ERROR: Building for iOS with 'arch=x86_64' requires 'ios_simulator=yes'.")
  88. sys.exit(255)
  89. env["ENV"]["MACOSX_DEPLOYMENT_TARGET"] = "10.9"
  90. env.Append(
  91. CCFLAGS=(
  92. "-fobjc-arc -arch x86_64"
  93. " -fobjc-abi-version=2 -fobjc-legacy-dispatch -fmessage-length=0 -fpascal-strings -fblocks"
  94. " -fasm-blocks -isysroot $IOS_SDK_PATH"
  95. ).split()
  96. )
  97. env.Append(ASFLAGS=["-arch", "x86_64"])
  98. elif env["arch"] == "arm64":
  99. env.Append(
  100. CCFLAGS=(
  101. "-fobjc-arc -arch arm64 -fmessage-length=0 -fno-strict-aliasing"
  102. " -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits"
  103. " -fpascal-strings -fblocks -fvisibility=hidden -MMD -MT dependencies"
  104. " -isysroot $IOS_SDK_PATH".split()
  105. )
  106. )
  107. env.Append(ASFLAGS=["-arch", "arm64"])
  108. env.Append(CPPDEFINES=["NEED_LONG_INT"])
  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. "$IOS_SDK_PATH/usr/include",
  114. "$IOS_SDK_PATH/System/Library/Frameworks/AudioUnit.framework/Headers",
  115. ]
  116. )
  117. env.Prepend(CPPPATH=["#platform/ios"])
  118. env.Append(CPPDEFINES=["IOS_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED"])
  119. if env["vulkan"]:
  120. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  121. if env["opengl3"]:
  122. env.Append(CPPDEFINES=["GLES3_ENABLED", "GLES_SILENCE_DEPRECATION"])
  123. env.Prepend(
  124. CPPPATH=[
  125. "$IOS_SDK_PATH/System/Library/Frameworks/OpenGLES.framework/Headers",
  126. ]
  127. )