DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Log | Files | Refs | README

bundle32.py (2228B)


      1 #!/usr/bin/env python3
      2 """Collect the 32-bit runtime libraries a.out needs into a self-contained dir.
      3 
      4 usage: bundle32.py SYSROOT BINARY OUTDIR
      5 Follows DT_NEEDED transitively (readelf), starting from the binary plus the
      6 Mesa/GLX pieces that libGL loads at runtime (dlopen), and copies real files
      7 (no symlinks, so it works on shared folders).  Also writes OUTDIR/dri/ with the
      8 software driver.
      9 """
     10 import os, re, shutil, subprocess, sys
     11 
     12 sysroot, binary, out = sys.argv[1:4]
     13 SEARCH = [f"{sysroot}/usr/lib/i386-linux-gnu", f"{sysroot}/lib/i386-linux-gnu",
     14           f"{sysroot}/usr/lib/i386-linux-gnu/pulseaudio", f"{sysroot}/lib", f"{sysroot}/usr/lib"]
     15 # dlopen'd at runtime, so not visible as DT_NEEDED
     16 EXTRA = ["libGL.so.1", "libGLX_mesa.so.0", "libGLX.so.0", "libGLdispatch.so.0", "libglapi.so.0",
     17          "libX11.so.6", "libXext.so.6", "libXrandr.so.2", "libXinerama.so.1", "libXcursor.so.1",
     18          "libXi.so.6", "libXxf86vm.so.1", "libdl.so.2", "libm.so.6", "libpthread.so.0", "libc.so.6",
     19          "libnss_files.so.2", "libresolv.so.2"]
     20 DRIVERS = ["swrast_dri.so", "kms_swrast_dri.so"]
     21 
     22 def find(name):
     23     for d in SEARCH:
     24         p = os.path.join(d, name)
     25         if os.path.exists(p): return p
     26 
     27 def needed(path):
     28     o = subprocess.run(["readelf", "-d", path], capture_output=True, text=True).stdout
     29     return re.findall(r"NEEDED.*\[(.*?)\]", o)
     30 
     31 os.makedirs(out + "/dri", exist_ok=True)
     32 todo = needed(binary) + EXTRA + ["ld-linux.so.2"]
     33 seen = set()
     34 while todo:
     35     n = todo.pop()
     36     if n in seen: continue
     37     seen.add(n)
     38     p = find(n)
     39     if not p:
     40         print("warning: not found:", n); continue
     41     shutil.copy2(p, os.path.join(out, n))
     42     todo += needed(p)
     43 for d in DRIVERS:
     44     p = find("dri/" + d)
     45     if p:
     46         shutil.copy2(p, os.path.join(out, "dri", d))
     47         for n in needed(p): todo.append(n)
     48 while todo:
     49     n = todo.pop()
     50     if n in seen: continue
     51     seen.add(n)
     52     p = find(n)
     53     if p: shutil.copy2(p, os.path.join(out, n)); todo += needed(p)
     54 open(os.path.join(out, ".done"), "w").write("ok\n")
     55 size = sum(os.path.getsize(os.path.join(out, f)) for f in os.listdir(out) if os.path.isfile(os.path.join(out, f)))
     56 print(f"{len(seen)} libs bundled in {out} (~{size >> 20} MB + drivers)")