1import os
2import sys
3
4
5def is_active():
6    return True
7
8
9def get_name():
10    return "iOS"
11
12
13def can_build():
14
15    if sys.platform == 'darwin' or ("OSXCROSS_IOS" in os.environ):
16        return True
17
18    return False
19
20
21def get_opts():
22
23    return [
24        ('IPHONEPLATFORM', 'name of the iphone platform', 'iPhoneOS'),
25        ('IPHONEPATH', 'the path to iphone toolchain', '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain'),
26        ('IPHONESDK', 'path to the iphone SDK', '/Applications/Xcode.app/Contents/Developer/Platforms/${IPHONEPLATFORM}.platform/Developer/SDKs/${IPHONEPLATFORM}.sdk/'),
27        ('SDKVERSION', 'SDK version to link against', '12.1'),
28        ('game_center', 'Support for game center', 'yes'),
29        ('store_kit', 'Support for in-app store', 'yes'),
30        ('icloud', 'Support for iCloud', 'yes'),
31        ('ios_gles22_override', 'Force GLES2.0 on iOS', 'yes'),
32        ('ios_exceptions', 'Enable exceptions', 'no'),
33        ('ios_triple', 'Triple for ios toolchain', ''),
34        ('ios_sim', 'Build simulator binary', 'no'),
35        ('use_lto', 'Use link time optimization', 'no')
36    ]
37
38
39def get_flags():
40
41    return [
42        ('tools', 'no'),
43    ]
44
45
46def configure(env):
47
48    env.Append(CPPPATH=['#platform/iphone'])
49
50    env['ENV']['PATH'] = env['IPHONEPATH'] + "/Developer/usr/bin/:" + env['ENV']['PATH']
51
52    compiler_path = '$IPHONEPATH/usr/bin/${ios_triple}'
53
54    ccache_path = os.environ.get("CCACHE")
55    if ccache_path == None:
56        env['CC'] = compiler_path + 'clang'
57        env['CXX'] = compiler_path + 'clang++'
58    else:
59        # there aren't any ccache wrappers available for iOS,
60        # to enable caching we need to prepend the path to the ccache binary
61        env['CC'] = ccache_path + ' ' + compiler_path + 'clang'
62        env['CXX'] = ccache_path + ' ' + compiler_path + 'clang++'
63    env['AR'] = compiler_path + 'ar'
64    env['RANLIB'] = compiler_path + 'ranlib'
65
66    import string
67    if (env["ios_sim"] == "yes" or env["arch"] == "x86"):  # i386, simulator
68        env["arch"] = "x86"
69        env["bits"] = "32"
70        env.Append(CCFLAGS='-arch i386 -fobjc-abi-version=2 -fobjc-legacy-dispatch -fmessage-length=0 -fpascal-strings -fblocks -fasm-blocks -D__IPHONE_OS_VERSION_MIN_REQUIRED=40100 -isysroot $IPHONESDK -mios-simulator-version-min=4.3 -DCUSTOM_MATRIX_TRANSFORM_H=\\\"build/iphone/matrix4_iphone.h\\\" -DCUSTOM_VECTOR3_TRANSFORM_H=\\\"build/iphone/vector3_iphone.h\\\"'.split())
71    elif (env["arch"] == "arm64"):  # arm64
72        env["bits"] = "64"
73        env.Append(CCFLAGS='-fno-objc-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 -miphoneos-version-min=9.0 -isysroot $IPHONESDK'.split())
74        env.Append(CPPFLAGS=['-DNEED_LONG_INT'])
75        env.Append(CPPFLAGS=['-DLIBYUV_DISABLE_NEON'])
76    else:  # armv7
77        env["arch"] = "arm"
78        env["bits"] = "32"
79        env.Append(CCFLAGS='-fno-objc-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)" -miphoneos-version-min=9.0 -MMD -MT dependencies'.split())
80
81    if (env["arch"] == "x86"):
82        env['IPHONEPLATFORM'] = 'iPhoneSimulator'
83        env.Append(LINKFLAGS=['-arch', 'i386', '-mios-simulator-version-min=9.0',
84                              '-isysroot', '$IPHONESDK',
85                              '-Xlinker', '-sdk_version', '-Xlinker', '$SDKVERSION',
86                              '-Xlinker', '-objc_abi_version', '-Xlinker', '2',
87                              '-framework', 'AudioToolbox',
88                              '-framework', 'AVFoundation',
89                              '-framework', 'CoreAudio',
90                              '-framework', 'CoreGraphics',
91                              '-framework', 'CoreMedia',
92                              '-framework', 'CoreMotion',
93                              '-framework', 'Foundation',
94                              '-framework', 'Security',
95                              '-framework', 'UIKit',
96                              '-framework', 'MediaPlayer',
97                              '-framework', 'OpenGLES',
98                              '-framework', 'QuartzCore',
99                              '-framework', 'SystemConfiguration',
100                              '-framework', 'GameController',
101                              '-F$IPHONESDK',
102                              ])
103    elif (env["arch"] == "arm64"):
104        env.Append(LINKFLAGS=['-arch', 'arm64', '-Wl,-dead_strip', '-miphoneos-version-min=9.0',
105                                                '-isysroot', '$IPHONESDK',
106                                                '-Xlinker', '-sdk_version', '-Xlinker', '$SDKVERSION',
107                                                '-framework', 'Foundation',
108                                                '-framework', 'UIKit',
109                                                '-framework', 'CoreGraphics',
110                                                '-framework', 'OpenGLES',
111                                                '-framework', 'QuartzCore',
112                                                '-framework', 'CoreAudio',
113                                                '-framework', 'AudioToolbox',
114                                                '-framework', 'SystemConfiguration',
115                                                '-framework', 'Security',
116                                                #'-framework', 'AdSupport',
117                                                '-framework', 'MediaPlayer',
118                                                '-framework', 'AVFoundation',
119                                                '-framework', 'CoreMedia',
120                                                '-framework', 'CoreMotion',
121                                                '-framework', 'GameController',
122                              ])
123    else:
124        env.Append(LINKFLAGS=['-arch', 'armv7', '-Wl,-dead_strip', '-miphoneos-version-min=9.0',
125                                                '-isysroot', '$IPHONESDK',
126                                                '-Xlinker', '-sdk_version', '-Xlinker', '$SDKVERSION',
127                                                '-framework', 'Foundation',
128                                                '-framework', 'UIKit',
129                                                '-framework', 'CoreGraphics',
130                                                '-framework', 'OpenGLES',
131                                                '-framework', 'QuartzCore',
132                                                '-framework', 'CoreAudio',
133                                                '-framework', 'AudioToolbox',
134                                                '-framework', 'SystemConfiguration',
135                                                '-framework', 'Security',
136                                                #'-framework', 'AdSupport',
137                                                '-framework', 'MediaPlayer',
138                                                '-framework', 'AVFoundation',
139                                                '-framework', 'CoreMedia',
140                                                '-framework', 'CoreMotion',
141                                                '-framework', 'GameController',
142                              ])
143
144    if env['game_center'] == 'yes':
145        env.Append(CPPFLAGS=['-DGAME_CENTER_ENABLED'])
146        env.Append(LINKFLAGS=['-framework', 'GameKit'])
147
148    if env['store_kit'] == 'yes':
149        env.Append(CPPFLAGS=['-DSTOREKIT_ENABLED'])
150        env.Append(LINKFLAGS=['-framework', 'StoreKit'])
151
152    if env['icloud'] == 'yes':
153        env.Append(CPPFLAGS=['-DICLOUD_ENABLED'])
154
155    env.Append(CPPPATH=['$IPHONESDK/usr/include', '$IPHONESDK/System/Library/Frameworks/OpenGLES.framework/Headers', '$IPHONESDK/System/Library/Frameworks/AudioUnit.framework/Headers'])
156
157    if (env["target"].startswith("release")):
158
159        env.Append(CPPFLAGS=['-DNDEBUG', '-DNS_BLOCK_ASSERTIONS=1'])
160        env.Append(CPPFLAGS=['-O2', '-ftree-vectorize', '-fomit-frame-pointer', '-ffast-math', '-funsafe-math-optimizations'])
161        env.Append(LINKFLAGS=['-O2'])
162        if env['use_lto'] == 'yes':
163            env.Append(CPPFLAGS=['-flto'])
164            env.Append(LINKFLAGS=['-flto'])
165
166        if env["target"] == "release_debug":
167            env.Append(CPPFLAGS=['-DDEBUG_ENABLED'])
168
169    elif (env["target"] == "debug"):
170
171        env.Append(CPPFLAGS=['-D_DEBUG', '-DDEBUG=1', '-gdwarf-2', '-O0', '-DDEBUG_ENABLED'])
172        env.Append(CPPFLAGS=['-DDEBUG_MEMORY_ENABLED'])
173
174    if (env["ios_sim"] == "yes"):  # TODO: Check if needed?
175        env['ENV']['MACOSX_DEPLOYMENT_TARGET'] = '10.6'
176    env['ENV']['CODESIGN_ALLOCATE'] = '/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate'
177    env.Append(CPPFLAGS=['-DIPHONE_ENABLED', '-DUNIX_ENABLED', '-DGLES2_ENABLED', '-DMPC_FIXED_POINT'])
178
179    # TODO: Move that to opus module's config
180    if("module_opus_enabled" in env and env["module_opus_enabled"] != "no"):
181        env.opus_fixed_point = "yes"
182        if env["arch"] == "x86":
183            pass
184        elif(env["arch"] == "arm64"):
185            env.Append(CFLAGS=["-DOPUS_ARM64_OPT"])
186        else:
187            env.Append(CFLAGS=["-DOPUS_ARM_OPT"])
188
189    if env['ios_exceptions'] == 'yes':
190        env.Append(CPPFLAGS=['-fexceptions'])
191    else:
192        env.Append(CPPFLAGS=['-fno-exceptions'])
193    # env['neon_enabled']=True
194    env['S_compiler'] = '$IPHONEPATH/Developer/usr/bin/gcc'
195
196    import methods
197    env.Append(BUILDERS={'GLSL120': env.Builder(action=methods.build_legacygl_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
198    env.Append(BUILDERS={'GLSL': env.Builder(action=methods.build_glsl_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
199    env.Append(BUILDERS={'GLSL120GLES': env.Builder(action=methods.build_gles2_headers, suffix='glsl.gen.h', src_suffix='.glsl')})
200