9 class Platform(object):
12 sdk_re = re.compile(r'.*-sdk ([a-zA-Z0-9.]*)')
16 for line in subprocess.Popen(['xcodebuild', '-sdk', sdkname, '-version'], stdout=subprocess.PIPE).stdout:
17 kv = line.strip().split(': ', 1)
23 desktop_sdk_info = sdkinfo('macosx')
27 for line in subprocess.Popen(['xcodebuild', '-showsdks'], stdout=subprocess.PIPE).stdout:
28 match = sdk_re.match(line)
31 latest_desktop = match.group(1)
35 desktop_sdk = latest_sdks()
37 class desktop_platform_32(Platform):
41 triple = 'i386-apple-darwin10'
42 sdkroot = desktop_sdk_info['Path']
44 prefix = "#if defined(__i386__) && !defined(__x86_64__)\n\n"
47 class desktop_platform_64(Platform):
51 triple = 'x86_64-apple-darwin10'
52 sdkroot = desktop_sdk_info['Path']
54 prefix = "#if !defined(__i386__) && defined(__x86_64__)\n\n"
57 def move_file(src_dir, dst_dir, filename, file_suffix=None, prefix='', suffix=''):
58 if not os.path.exists(dst_dir):
61 out_filename = filename
64 split_name = os.path.splitext(filename)
65 out_filename = "%s_%s%s" % (split_name[0], file_suffix, split_name[1])
67 with open(os.path.join(src_dir, filename)) as in_file:
68 with open(os.path.join(dst_dir, out_filename), 'w') as out_file:
70 out_file.write(prefix)
72 out_file.write(in_file.read())
75 out_file.write(suffix)
77 headers_seen = collections.defaultdict(set)
79 def move_source_tree(src_dir, dest_dir, dest_include_dir, arch=None, prefix=None, suffix=None):
80 for root, dirs, files in os.walk(src_dir, followlinks=True):
81 relroot = os.path.relpath(root,src_dir)
83 def move_dir(arch, prefix='', suffix='', files=[]):
86 if file.endswith('.h'):
90 headers_seen[file].add(arch)
91 move_file(root, dest_include_dir, file, arch, prefix=prefix, suffix=suffix)
94 outroot = os.path.join(dest_dir, relroot)
95 move_file(root, outroot, file, prefix=prefix, suffix=suffix)
102 elif relroot == 'x86':
103 move_dir(arch='i386',
104 prefix="#if defined(__i386__) && !defined(__x86_64__)\n\n",
107 move_dir(arch='x86_64',
108 prefix="#if !defined(__i386__) && defined(__x86_64__)\n\n",
112 def build_target(platform):
114 return subprocess.check_output(['xcrun', '-sdk', platform.sdkroot, '-find', cmd]).strip()
116 build_dir = 'build_' + platform.name
117 if not os.path.exists(build_dir):
118 os.makedirs(build_dir)
119 env = dict(CC=xcrun_cmd('clang'),
121 CFLAGS='-arch %s -isysroot %s -mmacosx-version-min=10.6' % (platform.arch, platform.sdkroot))
122 working_dir=os.getcwd()
125 subprocess.check_call(['../configure', '-host', platform.triple], env=env)
126 move_source_tree('.', None, '../osx/include',
128 prefix=platform.prefix,
129 suffix=platform.suffix)
130 move_source_tree('./include', None, '../osx/include',
132 prefix=platform.prefix,
133 suffix=platform.suffix)
135 os.chdir(working_dir)
137 for header_name, archs in headers_seen.iteritems():
138 basename, suffix = os.path.splitext(header_name)
141 move_source_tree('src', 'osx/src', 'osx/include')
142 move_source_tree('include', None, 'osx/include')
143 build_target(desktop_platform_32)
144 build_target(desktop_platform_64)
146 for header_name, archs in headers_seen.iteritems():
147 basename, suffix = os.path.splitext(header_name)
148 with open(os.path.join('osx/include', header_name), 'w') as header:
150 header.write('#include <%s_%s%s>\n' % (basename, arch, suffix))
152 if __name__ == '__main__':