detect.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. elif env["target"] == "debug":
  45. env.Append(CCFLAGS=["-gdwarf-2", "-O0"])
  46. env.Append(CPPDEFINES=["_DEBUG", ("DEBUG", 1)])
  47. if env["use_lto"]:
  48. env.Append(CCFLAGS=["-flto"])
  49. env.Append(LINKFLAGS=["-flto"])
  50. ## Architecture
  51. if env["arch"] == "x86": # i386
  52. env["bits"] = "32"
  53. elif env["arch"] == "x86_64":
  54. env["bits"] = "64"
  55. elif env["arch"] == "arm" or env["arch"] == "arm32" or env["arch"] == "armv7" or env["bits"] == "32": # arm
  56. env["arch"] = "arm"
  57. env["bits"] = "32"
  58. else: # armv64
  59. env["arch"] = "arm64"
  60. env["bits"] = "64"
  61. ## Compiler configuration
  62. # Save this in environment for use by other modules
  63. if "OSXCROSS_IOS" in os.environ:
  64. env["osxcross"] = True
  65. env["ENV"]["PATH"] = env["IPHONEPATH"] + "/Developer/usr/bin/:" + env["ENV"]["PATH"]
  66. compiler_path = "$IPHONEPATH/usr/bin/${ios_triple}"
  67. s_compiler_path = "$IPHONEPATH/Developer/usr/bin/"
  68. ccache_path = os.environ.get("CCACHE")
  69. if ccache_path is None:
  70. env["CC"] = compiler_path + "clang"
  71. env["CXX"] = compiler_path + "clang++"
  72. env["S_compiler"] = s_compiler_path + "gcc"
  73. else:
  74. # there aren't any ccache wrappers available for iOS,
  75. # to enable caching we need to prepend the path to the ccache binary
  76. env["CC"] = ccache_path + " " + compiler_path + "clang"
  77. env["CXX"] = ccache_path + " " + compiler_path + "clang++"
  78. env["S_compiler"] = ccache_path + " " + s_compiler_path + "gcc"
  79. env["AR"] = compiler_path + "ar"
  80. env["RANLIB"] = compiler_path + "ranlib"
  81. ## Compile flags
  82. if env["ios_simulator"]:
  83. detect_darwin_sdk_path("iphonesimulator", env)
  84. env.Append(CCFLAGS=["-mios-simulator-version-min=10.0"])
  85. env.Append(LINKFLAGS=["-mios-simulator-version-min=10.0"])
  86. env.extra_suffix = ".simulator" + env.extra_suffix
  87. else:
  88. detect_darwin_sdk_path("iphone", env)
  89. env.Append(CCFLAGS=["-miphoneos-version-min=10.0"])
  90. env.Append(LINKFLAGS=["-miphoneos-version-min=10.0"])
  91. if env["arch"] == "x86" or env["arch"] == "x86_64":
  92. env["ENV"]["MACOSX_DEPLOYMENT_TARGET"] = "10.9"
  93. arch_flag = "i386" if env["arch"] == "x86" else env["arch"]
  94. env.Append(
  95. CCFLAGS=(
  96. "-arch "
  97. + arch_flag
  98. + " -fobjc-arc -fobjc-abi-version=2 -fobjc-legacy-dispatch -fmessage-length=0 -fpascal-strings -fblocks -fasm-blocks -isysroot $IPHONESDK"
  99. ).split()
  100. )
  101. elif env["arch"] == "arm":
  102. detect_darwin_sdk_path("iphone", env)
  103. env.Append(
  104. 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()
  105. )
  106. elif env["arch"] == "arm64":
  107. detect_darwin_sdk_path("iphone", env)
  108. env.Append(
  109. 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()
  110. )
  111. env.Append(CPPDEFINES=["NEED_LONG_INT"])
  112. env.Append(CPPDEFINES=["LIBYUV_DISABLE_NEON"])
  113. # Disable exceptions on non-tools (template) builds
  114. if not env["tools"]:
  115. if env["ios_exceptions"]:
  116. env.Append(CCFLAGS=["-fexceptions"])
  117. else:
  118. env.Append(CCFLAGS=["-fno-exceptions"])
  119. # Temp fix for ABS/MAX/MIN macros in iPhone SDK blocking compilation
  120. env.Append(CCFLAGS=["-Wno-ambiguous-macro"])
  121. ## Link flags
  122. if env["arch"] == "x86" or env["arch"] == "x86_64":
  123. arch_flag = "i386" if env["arch"] == "x86" else env["arch"]
  124. env.Append(
  125. LINKFLAGS=[
  126. "-arch",
  127. arch_flag,
  128. "-isysroot",
  129. "$IPHONESDK",
  130. "-Xlinker",
  131. "-objc_abi_version",
  132. "-Xlinker",
  133. "2",
  134. "-F$IPHONESDK",
  135. ]
  136. )
  137. elif env["arch"] == "arm":
  138. env.Append(LINKFLAGS=["-arch", "armv7", "-Wl,-dead_strip"])
  139. if env["arch"] == "arm64":
  140. env.Append(LINKFLAGS=["-arch", "arm64", "-Wl,-dead_strip"])
  141. env.Append(
  142. LINKFLAGS=[
  143. "-isysroot",
  144. "$IPHONESDK",
  145. ]
  146. )
  147. env.Prepend(
  148. CPPPATH=[
  149. "$IPHONESDK/usr/include",
  150. "$IPHONESDK/System/Library/Frameworks/OpenGLES.framework/Headers",
  151. "$IPHONESDK/System/Library/Frameworks/AudioUnit.framework/Headers",
  152. ]
  153. )
  154. env["ENV"]["CODESIGN_ALLOCATE"] = "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate"
  155. env.Prepend(CPPPATH=["#platform/iphone"])
  156. env.Append(CPPDEFINES=["IPHONE_ENABLED", "UNIX_ENABLED", "GLES_ENABLED", "COREAUDIO_ENABLED"])