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