detect.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import os
  2. import sys
  3. from methods import detect_darwin_sdk_path, get_darwin_sdk_version
  4. def is_active():
  5. return True
  6. def get_name():
  7. return "iOS"
  8. def can_build():
  9. if sys.platform == "darwin":
  10. if get_darwin_sdk_version("iphone") < 13.0:
  11. print("Detected iOS SDK version older than 13")
  12. return False
  13. return True
  14. elif "OSXCROSS_IOS" in os.environ:
  15. return True
  16. return False
  17. def get_opts():
  18. from SCons.Variables import BoolVariable
  19. return [
  20. (
  21. "IPHONEPATH",
  22. "Path to iPhone toolchain",
  23. "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain",
  24. ),
  25. ("IPHONESDK", "Path to the iPhone SDK", ""),
  26. BoolVariable("ios_simulator", "Build for iOS Simulator", False),
  27. BoolVariable("ios_exceptions", "Enable exceptions", False),
  28. ("ios_triple", "Triple for ios toolchain", ""),
  29. ]
  30. def get_flags():
  31. return [
  32. ("tools", False),
  33. ]
  34. def configure(env):
  35. ## Build type
  36. if env["target"].startswith("release"):
  37. env.Append(CPPDEFINES=["NDEBUG", ("NS_BLOCK_ASSERTIONS", 1)])
  38. if env["optimize"] == "speed": # optimize for speed (default)
  39. env.Append(CCFLAGS=["-O2", "-ftree-vectorize", "-fomit-frame-pointer"])
  40. env.Append(LINKFLAGS=["-O2"])
  41. elif env["optimize"] == "size": # optimize for size
  42. env.Append(CCFLAGS=["-Os", "-ftree-vectorize"])
  43. env.Append(LINKFLAGS=["-Os"])
  44. if env["target"] == "release_debug":
  45. env.Append(CPPDEFINES=["DEBUG_ENABLED"])
  46. elif env["target"] == "debug":
  47. env.Append(CCFLAGS=["-gdwarf-2", "-O0"])
  48. env.Append(CPPDEFINES=["_DEBUG", ("DEBUG", 1), "DEBUG_ENABLED"])
  49. if env["use_lto"]:
  50. env.Append(CCFLAGS=["-flto"])
  51. env.Append(LINKFLAGS=["-flto"])
  52. ## Architecture
  53. if env["arch"] == "x86": # i386
  54. env["bits"] = "32"
  55. elif env["arch"] == "x86_64":
  56. env["bits"] = "64"
  57. elif env["arch"] == "arm" or env["arch"] == "arm32" or env["arch"] == "armv7" or env["bits"] == "32": # arm
  58. env["arch"] = "arm"
  59. env["bits"] = "32"
  60. else: # armv64
  61. env["arch"] = "arm64"
  62. env["bits"] = "64"
  63. ## Compiler configuration
  64. # Save this in environment for use by other modules
  65. if "OSXCROSS_IOS" in os.environ:
  66. env["osxcross"] = True
  67. env["ENV"]["PATH"] = env["IPHONEPATH"] + "/Developer/usr/bin/:" + env["ENV"]["PATH"]
  68. compiler_path = "$IPHONEPATH/usr/bin/${ios_triple}"
  69. s_compiler_path = "$IPHONEPATH/Developer/usr/bin/"
  70. ccache_path = os.environ.get("CCACHE")
  71. if ccache_path is None:
  72. env["CC"] = compiler_path + "clang"
  73. env["CXX"] = compiler_path + "clang++"
  74. env["S_compiler"] = s_compiler_path + "gcc"
  75. else:
  76. # there aren't any ccache wrappers available for iOS,
  77. # to enable caching we need to prepend the path to the ccache binary
  78. env["CC"] = ccache_path + " " + compiler_path + "clang"
  79. env["CXX"] = ccache_path + " " + compiler_path + "clang++"
  80. env["S_compiler"] = ccache_path + " " + s_compiler_path + "gcc"
  81. env["AR"] = compiler_path + "ar"
  82. env["RANLIB"] = compiler_path + "ranlib"
  83. ## Compile flags
  84. if env["ios_simulator"]:
  85. detect_darwin_sdk_path("iphonesimulator", env)
  86. env.Append(CCFLAGS=["-mios-simulator-version-min=10.0"])
  87. env.Append(LINKFLAGS=["-mios-simulator-version-min=10.0"])
  88. env.extra_suffix = ".simulator" + env.extra_suffix
  89. else:
  90. detect_darwin_sdk_path("iphone", env)
  91. env.Append(CCFLAGS=["-miphoneos-version-min=10.0"])
  92. env.Append(LINKFLAGS=["-miphoneos-version-min=10.0"])
  93. if env["arch"] == "x86" or env["arch"] == "x86_64":
  94. env["ENV"]["MACOSX_DEPLOYMENT_TARGET"] = "10.9"
  95. arch_flag = "i386" if env["arch"] == "x86" else env["arch"]
  96. env.Append(
  97. CCFLAGS=(
  98. "-arch "
  99. + arch_flag
  100. + " -fobjc-arc -fobjc-abi-version=2 -fobjc-legacy-dispatch -fmessage-length=0 -fpascal-strings -fblocks -fasm-blocks -isysroot $IPHONESDK"
  101. ).split()
  102. )
  103. elif env["arch"] == "arm":
  104. detect_darwin_sdk_path("iphone", env)
  105. env.Append(
  106. CCFLAGS='-fobjc-arc -arch armv7 -fmessage-length=0 -fno-strict-aliasing -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits -fpascal-strings -fblocks -isysroot $IPHONESDK -fvisibility=hidden -mthumb "-DIBOutlet=__attribute__((iboutlet))" "-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection(ClassName)))" "-DIBAction=void)__attribute__((ibaction)" -MMD -MT dependencies'.split()
  107. )
  108. elif env["arch"] == "arm64":
  109. detect_darwin_sdk_path("iphone", env)
  110. env.Append(
  111. CCFLAGS="-fobjc-arc -arch arm64 -fmessage-length=0 -fno-strict-aliasing -fdiagnostics-print-source-range-info -fdiagnostics-show-category=id -fdiagnostics-parseable-fixits -fpascal-strings -fblocks -fvisibility=hidden -MMD -MT dependencies -isysroot $IPHONESDK".split()
  112. )
  113. env.Append(CPPDEFINES=["NEED_LONG_INT"])
  114. env.Append(CPPDEFINES=["LIBYUV_DISABLE_NEON"])
  115. # Disable exceptions on non-tools (template) builds
  116. if not env["tools"]:
  117. if env["ios_exceptions"]:
  118. env.Append(CCFLAGS=["-fexceptions"])
  119. else:
  120. env.Append(CCFLAGS=["-fno-exceptions"])
  121. # Temp fix for ABS/MAX/MIN macros in iPhone SDK blocking compilation
  122. env.Append(CCFLAGS=["-Wno-ambiguous-macro"])
  123. ## Link flags
  124. if env["arch"] == "x86" or env["arch"] == "x86_64":
  125. arch_flag = "i386" if env["arch"] == "x86" else env["arch"]
  126. env.Append(
  127. LINKFLAGS=[
  128. "-arch",
  129. arch_flag,
  130. "-isysroot",
  131. "$IPHONESDK",
  132. "-Xlinker",
  133. "-objc_abi_version",
  134. "-Xlinker",
  135. "2",
  136. "-F$IPHONESDK",
  137. ]
  138. )
  139. elif env["arch"] == "arm":
  140. env.Append(LINKFLAGS=["-arch", "armv7", "-Wl,-dead_strip"])
  141. if env["arch"] == "arm64":
  142. env.Append(LINKFLAGS=["-arch", "arm64", "-Wl,-dead_strip"])
  143. env.Append(
  144. LINKFLAGS=[
  145. "-isysroot",
  146. "$IPHONESDK",
  147. ]
  148. )
  149. env.Prepend(
  150. CPPPATH=[
  151. "$IPHONESDK/usr/include",
  152. "$IPHONESDK/System/Library/Frameworks/OpenGLES.framework/Headers",
  153. "$IPHONESDK/System/Library/Frameworks/AudioUnit.framework/Headers",
  154. ]
  155. )
  156. env["ENV"]["CODESIGN_ALLOCATE"] = "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate"
  157. env.Prepend(CPPPATH=["#platform/iphone"])
  158. env.Append(CPPDEFINES=["IPHONE_ENABLED", "UNIX_ENABLED", "GLES_ENABLED", "COREAUDIO_ENABLED"])