Add root.json to the bundle
[bitmask_bundler.git] / bundler / darwin_dyliber.py
1 import os
2
3 from sh import otool, install_name_tool, find
4
5 def parse_otool_output(output):
6     lines = output.splitlines()[1:]
7     libs = []
8     for line in lines:
9         line = line.strip()
10         if len(line) == 0:
11             continue
12         line = line.split("(")[0].strip()
13         lib = os.path.split(line)[-1]
14         libs.append((lib, line))
15
16     return libs
17
18 def locate_lib(executable_path, lib):
19     return find(executable_path, "-name", lib, "-type", "f").strip()
20
21 def install_name_tooler(executable_path, lib_path):
22     out = otool("-L", lib_path)
23     _, lib_name = os.path.split(lib_path)
24     libs = parse_otool_output(out)
25     updated_any = False
26     for lib, original in libs:
27         do_id = lib == lib_name
28
29         if original.find("Carbon") > 0:
30             continue
31         location = locate_lib(executable_path, lib)
32         if location is None or len(location) == 0:
33             continue
34         try:
35             if do_id:
36                 install_name_tool("-id",
37                                   os.path.join("@executable_path",
38                                                os.path.relpath(location,
39                                                                executable_path)),
40                                   lib_path)
41             else:
42                 install_name_tool("-change", original,
43                                   os.path.join("@executable_path",
44                                                os.path.relpath(location,
45                                                                executable_path)),
46                                   lib_path)
47             updated_any = True
48         except Exception as e:
49             print "ERROR Fixing", lib
50             print e
51     if updated_any:
52         print "Fixed", lib_path
53
54 def fix_all_dylibs(executable_path):
55     print "Fixing all dylibs, this might take a while..."
56     files = find(executable_path, "-type", "f").strip().splitlines()
57     for f in files:
58         install_name_tooler(executable_path, f)
59     print "Done"