detect.py 5.7 KB

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