9 class Platform(object):
12 class simulator_platform(Platform
):
13 directory
= 'darwin_ios'
14 sdk
= 'iphonesimulator'
16 triple
= 'i386-apple-darwin11'
17 version_min
= '-miphoneos-version-min=7.0'
19 prefix
= "#ifdef __i386__\n\n"
22 src_files
= ['darwin.S', 'win32.S', 'ffi.c']
25 class simulator64_platform(Platform
):
26 directory
= 'darwin_ios'
27 sdk
= 'iphonesimulator'
29 triple
= 'x86_64-apple-darwin13'
30 version_min
= '-miphoneos-version-min=7.0'
32 prefix
= "#ifdef __x86_64__\n\n"
35 src_files
= ['darwin64.S', 'ffi64.c']
38 class device_platform(Platform
):
39 directory
= 'darwin_ios'
42 triple
= 'arm-apple-darwin11'
43 version_min
= '-miphoneos-version-min=7.0'
45 prefix
= "#ifdef __arm__\n\n"
48 src_files
= ['sysv.S', 'trampoline.S', 'ffi.c']
51 class device64_platform(Platform
):
52 directory
= 'darwin_ios'
55 triple
= 'aarch64-apple-darwin13'
56 version_min
= '-miphoneos-version-min=7.0'
58 prefix
= "#ifdef __arm64__\n\n"
61 src_files
= ['sysv.S', 'ffi.c']
64 class desktop32_platform(Platform
):
65 directory
= 'darwin_osx'
68 triple
= 'i386-apple-darwin10'
69 version_min
= '-mmacosx-version-min=10.6'
71 src_files
= ['darwin.S', 'win32.S', 'ffi.c']
73 prefix
= "#ifdef __i386__\n\n"
77 class desktop64_platform(Platform
):
78 directory
= 'darwin_osx'
81 triple
= 'x86_64-apple-darwin10'
82 version_min
= '-mmacosx-version-min=10.6'
84 prefix
= "#ifdef __x86_64__\n\n"
87 src_files
= ['darwin64.S', 'ffi64.c']
93 except OSError as exc
: # Python >2.5
94 if exc
.errno
== errno
.EEXIST
:
100 def move_file(src_dir
, dst_dir
, filename
, file_suffix
=None, prefix
='', suffix
=''):
102 out_filename
= filename
105 split_name
= os
.path
.splitext(filename
)
106 out_filename
= "%s_%s%s" % (split_name
[0], file_suffix
, split_name
[1])
108 with
open(os
.path
.join(src_dir
, filename
)) as in_file
:
109 with
open(os
.path
.join(dst_dir
, out_filename
), 'w') as out_file
:
111 out_file
.write(prefix
)
113 out_file
.write(in_file
.read())
116 out_file
.write(suffix
)
119 def list_files(src_dir
, pattern
=None, filelist
=None):
120 if pattern
: filelist
= glob
.iglob(os
.path
.join(src_dir
, pattern
))
121 for file in filelist
:
122 yield os
.path
.basename(file)
125 def copy_files(src_dir
, dst_dir
, pattern
=None, filelist
=None, file_suffix
=None, prefix
=None, suffix
=None):
126 for filename
in list_files(src_dir
, pattern
=pattern
, filelist
=filelist
):
127 move_file(src_dir
, dst_dir
, filename
, file_suffix
=file_suffix
, prefix
=prefix
, suffix
=suffix
)
130 def copy_src_platform_files(platform
):
131 src_dir
= os
.path
.join('src', platform
.src_dir
)
132 dst_dir
= os
.path
.join(platform
.directory
, 'src', platform
.src_dir
)
133 copy_files(src_dir
, dst_dir
, filelist
=platform
.src_files
, file_suffix
=platform
.arch
, prefix
=platform
.prefix
, suffix
=platform
.suffix
)
136 def build_target(platform
, platform_headers
):
138 return 'xcrun -sdk %s %s -arch %s' % (platform
.sdk
, cmd
, platform
.arch
)
140 tag
='%s-%s' % (platform
.sdk
, platform
.arch
)
141 build_dir
= 'build_%s' % tag
143 env
= dict(CC
=xcrun_cmd('clang'),
145 CFLAGS
='%s' % (platform
.version_min
))
146 working_dir
= os
.getcwd()
149 subprocess
.check_call(['../configure', '-host', platform
.triple
], env
=env
)
151 os
.chdir(working_dir
)
153 for src_dir
in [build_dir
, os
.path
.join(build_dir
, 'include')]:
155 os
.path
.join(platform
.directory
, 'include'),
157 file_suffix
=platform
.arch
,
158 prefix
=platform
.prefix
,
159 suffix
=platform
.suffix
)
161 for filename
in list_files(src_dir
, pattern
='*.h'):
162 platform_headers
[filename
].add((platform
.prefix
, platform
.arch
, platform
.suffix
))
166 with
open('src/arm/trampoline.S', 'w') as tramp_out
:
167 p
= subprocess
.Popen(['bash', 'src/arm/gentramp.sh'], stdout
=tramp_out
)
171 def generate_source_and_headers(generate_osx
=True, generate_ios
=True):
172 copy_files('src', 'darwin_common/src', pattern
='*.c')
173 copy_files('include', 'darwin_common/include', pattern
='*.h')
177 copy_src_platform_files(simulator_platform
)
178 copy_src_platform_files(simulator64_platform
)
179 copy_src_platform_files(device_platform
)
180 copy_src_platform_files(device64_platform
)
182 copy_src_platform_files(desktop32_platform
)
183 copy_src_platform_files(desktop64_platform
)
185 platform_headers
= collections
.defaultdict(set)
188 build_target(simulator_platform
, platform_headers
)
189 build_target(simulator64_platform
, platform_headers
)
190 build_target(device_platform
, platform_headers
)
191 build_target(device64_platform
, platform_headers
)
193 build_target(desktop32_platform
, platform_headers
)
194 build_target(desktop64_platform
, platform_headers
)
196 mkdir_p('darwin_common/include')
197 for header_name
, tag_tuples
in platform_headers
.iteritems():
198 basename
, suffix
= os
.path
.splitext(header_name
)
199 with
open(os
.path
.join('darwin_common/include', header_name
), 'w') as header
:
200 for tag_tuple
in tag_tuples
:
201 header
.write('%s#include <%s_%s%s>\n%s\n' % (tag_tuple
[0], basename
, tag_tuple
[1], suffix
, tag_tuple
[2]))
203 if __name__
== '__main__':
204 parser
= argparse
.ArgumentParser()
205 parser
.add_argument('--only-ios', action
='store_true', default
=False)
206 parser
.add_argument('--only-osx', action
='store_true', default
=False)
207 args
= parser
.parse_args()
209 generate_source_and_headers(generate_osx
=not args
.only_ios
, generate_ios
=not args
.only_osx
)