summaryrefslogtreecommitdiffstats
path: root/osx/lipo
blob: 80e658af3f776e3ac6062d84f1e55e8eb3d098e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3

"""Apply lipo recursively to trees.
"""

import sys
import os
import shutil
import subprocess

# Parse arguments
import argparse

parser = argparse.ArgumentParser(
    description=sys.modules[__name__].__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("output", help="Output tree")
parser.add_argument("input", help="Input trees", nargs="+")
options = parser.parse_args()
output = options.output
inputs = options.input


def ismacho(path):
    """Check if a file is Mach-O"""
    fnull = open(os.devnull, "w")
    try:
        subprocess.check_call(["lipo", "-info", path], stdout=fnull, stderr=fnull)
    except subprocess.CalledProcessError:
        return False
    return True


# Copy
for root, dirs, files in os.walk(inputs[0]):
    # Create root directory in output
    oroot = root[len(inputs[0]) :].lstrip("/")
    oroot = os.path.join(output, oroot)
    if not os.path.isdir(oroot):
        os.makedirs(oroot)
        shutil.copystat(root, oroot)

    # Copy files
    for f in files:
        of = os.path.join(oroot, f)
        f = os.path.join(root, f)
        if os.path.islink(f):
            # Symlink
            linkto = os.readlink(f)
            os.symlink(linkto, of)
        elif ismacho(f):
            sff = [os.path.join(r, f[len(inputs[0]) :].lstrip("/")) for r in inputs]
            args = ["lipo", "-create", "-output", of]
            args.extend(sff)
            subprocess.check_call(args)
        else:
            # Regular file, just copy from the first input directory
            shutil.copyfile(f, of)
            shutil.copystat(f, of)