import os
import os.path
import subprocess

os.chdir(os.path.dirname(os.path.abspath(__file__)))

def run(cmd):
    print(cmd)
    subprocess.check_call(cmd)

for arch_name, arches in [
        ("i386", ["-arch", "i386"]),
        ("x86_64", ["-arch", "x86_64"]),
        ("fat", ["-arch", "i386", "-arch", "x86_64"]),
        ]:
    CC = ["clang"] + arches

    if not os.path.exists(arch_name):
        os.mkdir(arch_name)

    def cc(args, out):
        run(CC + args + ["-o", os.path.join(arch_name, out)])

    cc(["-shared", "native-lib.c"], "native-lib.dylib")
    cc(["-bundle", "fake-pymodule.c", "./native-lib.dylib"],
       "fake-pymodule.bundle")
    cc(["main-dlopen.c"], "main-dlopen")
    cc(["main-envvar.c", "./native-lib.dylib"], "main-envvar")

@@ -419,16 +419,85 @@
 _name)%0A%0A
+    def outpath(name):%0A        return os.path.join(arch_name, name)%0A%0A
     def 
@@ -543,39 +543,23 @@
 %5B%22-o%22, o
-s.
+ut
 path
-.join(arch_name, 
+(
 out)%5D)%0A%0A
@@ -648,27 +648,33 @@
 ymodule.c%22, 
-%22./
+outpath(%22
 native-lib.d
@@ -678,16 +678,17 @@
 b.dylib%22
+)
 %5D,%0A     
@@ -783,11 +783,17 @@
 c%22, 
-%22./
+outpath(%22
 nati
@@ -805,16 +805,17 @@
 b.dylib%22
+)
 %5D, %22main

Fix paths