detect.py 5.2 KB

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