• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2import subprocess
3import os
4import errno
5import collections
6import glob
7import argparse
8
9class Platform(object):
10    pass
11
12class simulator_platform(Platform):
13    directory = 'darwin_ios'
14    sdk = 'iphonesimulator'
15    arch = 'i386'
16    triple = 'i386-apple-darwin11'
17    version_min = '-miphoneos-version-min=7.0'
18
19    prefix = "#ifdef __i386__\n\n"
20    suffix = "\n\n#endif"
21    src_dir = 'x86'
22    src_files = ['sysv.S', 'ffi.c', 'internal.h']
23
24
25class simulator64_platform(Platform):
26    directory = 'darwin_ios'
27    sdk = 'iphonesimulator'
28    arch = 'x86_64'
29    triple = 'x86_64-apple-darwin13'
30    version_min = '-miphoneos-version-min=7.0'
31
32    prefix = "#ifdef __x86_64__\n\n"
33    suffix = "\n\n#endif"
34    src_dir = 'x86'
35    src_files = ['unix64.S', 'ffi64.c', 'ffiw64.c', 'win64.S', 'internal64.h', 'asmnames.h']
36
37
38class device_platform(Platform):
39    directory = 'darwin_ios'
40    sdk = 'iphoneos'
41    arch = 'armv7'
42    triple = 'arm-apple-darwin11'
43    version_min = '-miphoneos-version-min=7.0'
44
45    prefix = "#ifdef __arm__\n\n"
46    suffix = "\n\n#endif"
47    src_dir = 'arm'
48    src_files = ['sysv.S', 'ffi.c', 'internal.h']
49
50
51class device64_platform(Platform):
52    directory = 'darwin_ios'
53    sdk = 'iphoneos'
54    arch = 'arm64'
55    triple = 'aarch64-apple-darwin13'
56    version_min = '-miphoneos-version-min=7.0'
57
58    prefix = "#ifdef __arm64__\n\n"
59    suffix = "\n\n#endif"
60    src_dir = 'aarch64'
61    src_files = ['sysv.S', 'ffi.c', 'internal.h']
62
63
64class desktop32_platform(Platform):
65    directory = 'darwin_osx'
66    sdk = 'macosx'
67    arch = 'i386'
68    triple = 'i386-apple-darwin10'
69    version_min = '-mmacosx-version-min=10.6'
70    src_dir = 'x86'
71    src_files = ['sysv.S', 'ffi.c', 'internal.h']
72
73    prefix = "#ifdef __i386__\n\n"
74    suffix = "\n\n#endif"
75
76
77class desktop64_platform(Platform):
78    directory = 'darwin_osx'
79    sdk = 'macosx'
80    arch = 'x86_64'
81    triple = 'x86_64-apple-darwin10'
82    version_min = '-mmacosx-version-min=10.6'
83
84    prefix = "#ifdef __x86_64__\n\n"
85    suffix = "\n\n#endif"
86    src_dir = 'x86'
87    src_files = ['unix64.S', 'ffi64.c', 'ffiw64.c', 'win64.S', 'internal64.h', 'asmnames.h']
88
89
90def mkdir_p(path):
91    try:
92        os.makedirs(path)
93    except OSError as exc:  # Python >2.5
94        if exc.errno != errno.EEXIST:
95            raise
96
97
98def move_file(src_dir, dst_dir, filename, file_suffix=None, prefix='', suffix=''):
99    mkdir_p(dst_dir)
100    out_filename = filename
101
102    if file_suffix:
103        if filename in ['internal64.h', 'asmnames.h', 'internal.h']:
104            out_filename = filename
105        else:
106            split_name = os.path.splitext(filename)
107            out_filename = "%s_%s%s" % (split_name[0], file_suffix, split_name[1])
108
109    with open(os.path.join(src_dir, filename)) as in_file:
110        with open(os.path.join(dst_dir, out_filename), 'w') as out_file:
111            if prefix:
112                out_file.write(prefix)
113
114            out_file.write(in_file.read())
115
116            if suffix:
117                out_file.write(suffix)
118
119
120def list_files(src_dir, pattern=None, filelist=None):
121    if pattern: filelist = glob.iglob(os.path.join(src_dir, pattern))
122    for file in filelist:
123        yield os.path.basename(file)
124
125
126def copy_files(src_dir, dst_dir, pattern=None, filelist=None, file_suffix=None, prefix=None, suffix=None):
127    for filename in list_files(src_dir, pattern=pattern, filelist=filelist):
128        move_file(src_dir, dst_dir, filename, file_suffix=file_suffix, prefix=prefix, suffix=suffix)
129
130
131def copy_src_platform_files(platform):
132    src_dir = os.path.join('src', platform.src_dir)
133    dst_dir = os.path.join(platform.directory, 'src', platform.src_dir)
134    copy_files(src_dir, dst_dir, filelist=platform.src_files, file_suffix=platform.arch, prefix=platform.prefix, suffix=platform.suffix)
135
136
137def build_target(platform, platform_headers):
138    def xcrun_cmd(cmd):
139        return 'xcrun -sdk %s %s -arch %s' % (platform.sdk, cmd, platform.arch)
140
141    tag='%s-%s' % (platform.sdk, platform.arch)
142    build_dir = 'build_%s' % tag
143    mkdir_p(build_dir)
144    env = dict(CC=xcrun_cmd('clang'),
145               LD=xcrun_cmd('ld'),
146               CFLAGS='%s' % (platform.version_min))
147    working_dir = os.getcwd()
148    try:
149        os.chdir(build_dir)
150        subprocess.check_call(['../configure', '-host', platform.triple], env=env)
151    finally:
152        os.chdir(working_dir)
153
154    for src_dir in [build_dir, os.path.join(build_dir, 'include')]:
155        copy_files(src_dir,
156                   os.path.join(platform.directory, 'include'),
157                   pattern='*.h',
158                   file_suffix=platform.arch,
159                   prefix=platform.prefix,
160                   suffix=platform.suffix)
161
162        for filename in list_files(src_dir, pattern='*.h'):
163            platform_headers[filename].add((platform.prefix, platform.arch, platform.suffix))
164
165
166def generate_source_and_headers(generate_osx=True, generate_ios=True):
167    copy_files('src', 'darwin_common/src', pattern='*.c')
168    copy_files('include', 'darwin_common/include', pattern='*.h')
169
170    if generate_ios:
171        copy_src_platform_files(simulator_platform)
172        copy_src_platform_files(simulator64_platform)
173        copy_src_platform_files(device_platform)
174        copy_src_platform_files(device64_platform)
175    if generate_osx:
176        copy_src_platform_files(desktop32_platform)
177        copy_src_platform_files(desktop64_platform)
178
179    platform_headers = collections.defaultdict(set)
180
181    if generate_ios:
182        build_target(simulator_platform, platform_headers)
183        build_target(simulator64_platform, platform_headers)
184        build_target(device_platform, platform_headers)
185        build_target(device64_platform, platform_headers)
186    if generate_osx:
187        build_target(desktop32_platform, platform_headers)
188        build_target(desktop64_platform, platform_headers)
189
190    mkdir_p('darwin_common/include')
191    for header_name, tag_tuples in platform_headers.iteritems():
192        basename, suffix = os.path.splitext(header_name)
193        with open(os.path.join('darwin_common/include', header_name), 'w') as header:
194            for tag_tuple in tag_tuples:
195                header.write('%s#include <%s_%s%s>\n%s\n' % (tag_tuple[0], basename, tag_tuple[1], suffix, tag_tuple[2]))
196
197if __name__ == '__main__':
198    parser = argparse.ArgumentParser()
199    parser.add_argument('--only-ios', action='store_true', default=False)
200    parser.add_argument('--only-osx', action='store_true', default=False)
201    args = parser.parse_args()
202
203    generate_source_and_headers(generate_osx=not args.only_ios, generate_ios=not args.only_osx)
204