Commit 21bde92c9abb378f9c456a9d95e6f9b99ef8c920

Zachary Waldowski 2013-11-30T03:43:42

Darwin: Clean up, modernize generator scripts

diff --git a/generate-ios-source-and-headers.py b/generate-ios-source-and-headers.py
index c2bca73..68d31d0 100644
--- a/generate-ios-source-and-headers.py
+++ b/generate-ios-source-and-headers.py
@@ -1,29 +1,42 @@
 #!/usr/bin/env python
-
 import subprocess
 import re
 import os
 import errno
 import collections
-import sys
+#developer_path =
+
 
 class Platform(object):
     pass
 
 sdk_re = re.compile(r'.*-sdk ([a-zA-Z0-9.]*)')
 
+
 def sdkinfo(sdkname):
     ret = {}
     for line in subprocess.Popen(['xcodebuild', '-sdk', sdkname, '-version'], stdout=subprocess.PIPE).stdout:
         kv = line.strip().split(': ', 1)
         if len(kv) == 2:
-            k,v = kv
+            k, v = kv
             ret[k] = v
     return ret
 
+
+def mkdir_p(path):
+    try:
+        os.makedirs(path)
+    except OSError as exc:  # Python >2.5
+        if exc.errno == errno.EEXIST:
+            pass
+        else:
+            raise
+
+
 sim_sdk_info = sdkinfo('iphonesimulator')
 device_sdk_info = sdkinfo('iphoneos')
 
+
 def latest_sdks():
     latest_sim = None
     latest_device = None
@@ -39,20 +52,22 @@ def latest_sdks():
 
 sim_sdk, device_sdk = latest_sdks()
 
+
 class simulator_platform(Platform):
-    sdk='iphonesimulator'
+    sdk = 'iphonesimulator'
     arch = 'i386'
-    name = 'simulator'
+    short_arch = arch
     triple = 'i386-apple-darwin10'
     sdkroot = sim_sdk_info['Path']
 
     prefix = "#if !defined(__arm__) && defined(__i386__)\n\n"
     suffix = "\n\n#endif"
 
+
 class device_platform(Platform):
-    sdk='iphoneos'
-    name = 'ios'
+    sdk = 'iphoneos'
     arch = 'armv7'
+    short_arch = 'arm'
     triple = 'arm-apple-darwin10'
     sdkroot = device_sdk_info['Path']
 
@@ -61,14 +76,12 @@ class device_platform(Platform):
 
 
 def move_file(src_dir, dst_dir, filename, file_suffix=None, prefix='', suffix=''):
-    if not os.path.exists(dst_dir):
-        os.makedirs(dst_dir)
-
+    mkdir_p(dst_dir)
     out_filename = filename
 
     if file_suffix:
         split_name = os.path.splitext(filename)
-        out_filename =  "%s_%s%s" % (split_name[0], file_suffix, split_name[1])
+        out_filename = "%s_%s%s" % (split_name[0], file_suffix, split_name[1])
 
     with open(os.path.join(src_dir, filename)) as in_file:
         with open(os.path.join(dst_dir, out_filename), 'w') as out_file:
@@ -82,16 +95,15 @@ def move_file(src_dir, dst_dir, filename, file_suffix=None, prefix='', suffix=''
 
 headers_seen = collections.defaultdict(set)
 
+
 def move_source_tree(src_dir, dest_dir, dest_include_dir, arch=None, prefix=None, suffix=None):
     for root, dirs, files in os.walk(src_dir, followlinks=True):
-        relroot = os.path.relpath(root,src_dir)
+        relroot = os.path.relpath(root, src_dir)
 
         def move_dir(arch, prefix='', suffix='', files=[]):
             for file in files:
-                file_suffix = None
                 if file.endswith('.h'):
                     if dest_include_dir:
-                        file_suffix = arch
                         if arch:
                             headers_seen[file].add(arch)
                         move_file(root, dest_include_dir, file, arch, prefix=prefix, suffix=suffix)
@@ -109,42 +121,51 @@ def move_source_tree(src_dir, dest_dir, dest_include_dir, arch=None, prefix=None
             move_dir(arch='arm',
                      prefix="#ifdef __arm__\n\n",
                      suffix="\n\n#endif",
-                     files=files)
+                     files=['sysv.S', 'trampoline.S', 'ffi.c'])
         elif relroot == 'x86':
             move_dir(arch='i386',
                      prefix="#if !defined(__arm__) && defined(__i386__)\n\n",
                      suffix="\n\n#endif",
-                     files=files)
+                     files=['darwin.S', 'ffi.c'])
+
 
 def build_target(platform):
     def xcrun_cmd(cmd):
         return subprocess.check_output(['xcrun', '-sdk', platform.sdkroot, '-find', cmd]).strip()
 
-    build_dir = 'build_' + platform.name
-    if not os.path.exists(build_dir):
-        os.makedirs(build_dir)
-        env = dict(CC=xcrun_cmd('clang'),
-                   LD=xcrun_cmd('ld'),
-                   CFLAGS='-arch %s -isysroot %s -miphoneos-version-min=4.0' % (platform.arch, platform.sdkroot))
-        working_dir=os.getcwd()
-        try:
-            os.chdir(build_dir)
-            subprocess.check_call(['../configure', '-host', platform.triple], env=env)
-            move_source_tree('.', None, '../ios/include',
-                             arch=platform.arch,
-                             prefix=platform.prefix,
-                             suffix=platform.suffix)
-            move_source_tree('./include', None, '../ios/include',
-                            arch=platform.arch,
-                            prefix=platform.prefix,
-                            suffix=platform.suffix)
-        finally:
-            os.chdir(working_dir)
-
-        for header_name, archs in headers_seen.iteritems():
-            basename, suffix = os.path.splitext(header_name)
+    build_dir = 'build_' + platform.short_arch
+    mkdir_p(build_dir)
+    env = dict(CC=xcrun_cmd('clang'),
+               LD=xcrun_cmd('ld'),
+               CFLAGS='-arch %s -isysroot %s -miphoneos-version-min=4.3' % (platform.arch, platform.sdkroot))
+    working_dir = os.getcwd()
+    try:
+        os.chdir(build_dir)
+        subprocess.check_call(['../configure', '-host', platform.triple], env=env)
+        move_source_tree('.', None, '../ios/include',
+                         arch=platform.short_arch,
+                         prefix=platform.prefix,
+                         suffix=platform.suffix)
+        move_source_tree('./include', None, '../ios/include',
+                         arch=platform.short_arch,
+                         prefix=platform.prefix,
+                         suffix=platform.suffix)
+    finally:
+        os.chdir(working_dir)
+
+    for header_name, archs in headers_seen.iteritems():
+        basename, suffix = os.path.splitext(header_name)
+
+
+def make_tramp():
+    with open('src/arm/trampoline.S', 'w') as tramp_out:
+        p = subprocess.Popen(['bash', 'src/arm/gentramp.sh'], stdout=tramp_out)
+        p.wait()
+
 
 def main():
+    make_tramp()
+
     move_source_tree('src', 'ios/src', 'ios/include')
     move_source_tree('include', None, 'ios/include')
     build_target(simulator_platform)
diff --git a/generate-osx-source-and-headers.py b/generate-osx-source-and-headers.py
index 64313c1..c664ec7 100644
--- a/generate-osx-source-and-headers.py
+++ b/generate-osx-source-and-headers.py
@@ -2,26 +2,27 @@
 import subprocess
 import re
 import os
-import errno
 import collections
-import sys
+
 
 class Platform(object):
     pass
 
 sdk_re = re.compile(r'.*-sdk ([a-zA-Z0-9.]*)')
 
+
 def sdkinfo(sdkname):
     ret = {}
     for line in subprocess.Popen(['xcodebuild', '-sdk', sdkname, '-version'], stdout=subprocess.PIPE).stdout:
         kv = line.strip().split(': ', 1)
         if len(kv) == 2:
-            k,v = kv
+            k, v = kv
             ret[k] = v
     return ret
 
 desktop_sdk_info = sdkinfo('macosx')
 
+
 def latest_sdks():
     latest_desktop = None
     for line in subprocess.Popen(['xcodebuild', '-showsdks'], stdout=subprocess.PIPE).stdout:
@@ -34,8 +35,9 @@ def latest_sdks():
 
 desktop_sdk = latest_sdks()
 
+
 class desktop_platform_32(Platform):
-    sdk='macosx'
+    sdk = 'macosx'
     arch = 'i386'
     name = 'mac32'
     triple = 'i386-apple-darwin10'
@@ -44,8 +46,9 @@ class desktop_platform_32(Platform):
     prefix = "#if defined(__i386__) && !defined(__x86_64__)\n\n"
     suffix = "\n\n#endif"
 
+
 class desktop_platform_64(Platform):
-    sdk='macosx'
+    sdk = 'macosx'
     arch = 'x86_64'
     name = 'mac'
     triple = 'x86_64-apple-darwin10'
@@ -54,6 +57,7 @@ class desktop_platform_64(Platform):
     prefix = "#if !defined(__i386__) && defined(__x86_64__)\n\n"
     suffix = "\n\n#endif"
 
+
 def move_file(src_dir, dst_dir, filename, file_suffix=None, prefix='', suffix=''):
     if not os.path.exists(dst_dir):
         os.makedirs(dst_dir)
@@ -62,7 +66,7 @@ def move_file(src_dir, dst_dir, filename, file_suffix=None, prefix='', suffix=''
 
     if file_suffix:
         split_name = os.path.splitext(filename)
-        out_filename =  "%s_%s%s" % (split_name[0], file_suffix, split_name[1])
+        out_filename = "%s_%s%s" % (split_name[0], file_suffix, split_name[1])
 
     with open(os.path.join(src_dir, filename)) as in_file:
         with open(os.path.join(dst_dir, out_filename), 'w') as out_file:
@@ -76,16 +80,15 @@ def move_file(src_dir, dst_dir, filename, file_suffix=None, prefix='', suffix=''
 
 headers_seen = collections.defaultdict(set)
 
+
 def move_source_tree(src_dir, dest_dir, dest_include_dir, arch=None, prefix=None, suffix=None):
     for root, dirs, files in os.walk(src_dir, followlinks=True):
-        relroot = os.path.relpath(root,src_dir)
+        relroot = os.path.relpath(root, src_dir)
 
         def move_dir(arch, prefix='', suffix='', files=[]):
             for file in files:
-                file_suffix = None
                 if file.endswith('.h'):
                     if dest_include_dir:
-                        file_suffix = arch
                         if arch:
                             headers_seen[file].add(arch)
                         move_file(root, dest_include_dir, file, arch, prefix=prefix, suffix=suffix)
@@ -103,11 +106,12 @@ def move_source_tree(src_dir, dest_dir, dest_include_dir, arch=None, prefix=None
             move_dir(arch='i386',
                      prefix="#if defined(__i386__) && !defined(__x86_64__)\n\n",
                      suffix="\n\n#endif",
-                     files=files)
+                     files=['darwin.S', 'ffi.c'])
             move_dir(arch='x86_64',
                      prefix="#if !defined(__i386__) && defined(__x86_64__)\n\n",
                      suffix="\n\n#endif",
-                     files=files)
+                     files=['darwin64.S', 'ffi64.c'])
+
 
 def build_target(platform):
     def xcrun_cmd(cmd):
@@ -119,7 +123,7 @@ def build_target(platform):
         env = dict(CC=xcrun_cmd('clang'),
                    LD=xcrun_cmd('ld'),
                    CFLAGS='-arch %s -isysroot %s -mmacosx-version-min=10.6' % (platform.arch, platform.sdkroot))
-        working_dir=os.getcwd()
+        working_dir = os.getcwd()
         try:
             os.chdir(build_dir)
             subprocess.check_call(['../configure', '-host', platform.triple], env=env)
@@ -137,6 +141,7 @@ def build_target(platform):
         for header_name, archs in headers_seen.iteritems():
             basename, suffix = os.path.splitext(header_name)
 
+
 def main():
     move_source_tree('src', 'osx/src', 'osx/include')
     move_source_tree('include', None, 'osx/include')