···11+#!/usr/bin/env python2
22+#
33+# Script to build and install packages into the Steam runtime
44+# Patched version of https://github.com/ValveSoftware/steam-runtime/blob/master/build-runtime.py
55+66+import os
77+import re
88+import sys
99+import subprocess
1010+import argparse
1111+import json
1212+1313+# The top level directory
1414+top = sys.path[0]
1515+1616+def parse_args():
1717+ parser = argparse.ArgumentParser()
1818+ parser.add_argument("-r", "--runtime", help="specify runtime path", default=os.path.join(top,"runtime"))
1919+ parser.add_argument("-i", "--input", help="packages JSON", required=True)
2020+ return parser.parse_args()
2121+2222+2323+def install_deb (basename, deb, md5, dest_dir):
2424+ installtag_dir=os.path.join(dest_dir, "installed")
2525+ if not os.access(installtag_dir, os.W_OK):
2626+ os.makedirs(installtag_dir)
2727+2828+ #
2929+ # Write the tag file and checksum to the 'installed' subdirectory
3030+ #
3131+ with open(os.path.join(installtag_dir,basename),"w") as f:
3232+ subprocess.check_call(['dpkg-deb', '-c', deb], stdout=f)
3333+ with open(os.path.join(installtag_dir,basename+".md5"),"w") as f:
3434+ f.write("%s %s.deb\n" % (md5, basename))
3535+3636+ #
3737+ # Unpack the package into the dest_dir
3838+ #
3939+ os.chdir(top)
4040+ subprocess.check_call(['dpkg-deb', '-x', deb, dest_dir])
4141+4242+4343+#
4444+# Walks through the files in the runtime directory and converts any absolute symlinks
4545+# to their relative equivalent
4646+#
4747+def fix_symlinks ():
4848+ for dir, subdirs, files in os.walk(args.runtime):
4949+ for name in files:
5050+ filepath=os.path.join(dir,name)
5151+ if os.path.islink(filepath):
5252+ target = os.readlink(filepath)
5353+ if os.path.isabs(target):
5454+ #
5555+ # compute the target of the symlink based on the 'root' of the architecture's runtime
5656+ #
5757+ target2 = os.path.join(args.runtime,target[1:])
5858+5959+ #
6060+ # Set the new relative target path
6161+ #
6262+ os.unlink(filepath)
6363+ os.symlink(os.path.relpath(target2,dir), filepath)
6464+6565+#
6666+# Creates the usr/lib/debug/.build-id/xx/xxxxxxxxx.debug symlink tree for all the debug
6767+# symbols
6868+#
6969+def fix_debuglinks ():
7070+ for dir, subdirs, files in os.walk(os.path.join(args.runtime,"usr/lib/debug")):
7171+ if ".build-id" in subdirs:
7272+ subdirs.remove(".build-id") # don't recurse into .build-id directory we are creating
7373+7474+ for file in files:
7575+7676+ #
7777+ # scrape the output of readelf to find the buildid for this binary
7878+ #
7979+ p = subprocess.Popen(["readelf", '-n', os.path.join(dir,file)], stdout=subprocess.PIPE)
8080+ for line in iter(p.stdout.readline, ""):
8181+ m = re.search('Build ID: (\w{2})(\w+)',line)
8282+ if m:
8383+ linkdir = os.path.join(args.runtime,"usr/lib/debug/.build-id",m.group(1))
8484+ if not os.access(linkdir, os.W_OK):
8585+ os.makedirs(linkdir)
8686+ link = os.path.join(linkdir,m.group(2))
8787+ print "SYMLINKING symbol file %s to %s" % (link, os.path.relpath(os.path.join(dir,file),linkdir))
8888+ if os.path.lexists(link):
8989+ os.unlink(link)
9090+ os.symlink(os.path.relpath(os.path.join(dir,file), linkdir),link)
9191+9292+9393+args = parse_args()
9494+9595+9696+print ("Creating Steam Runtime in %s" % args.runtime)
9797+9898+with open(args.input) as pkgfile:
9999+ pkgs = json.load(pkgfile)
100100+ for pkg in pkgs:
101101+ install_deb(pkg["name"], pkg["source"], pkg["md5"], args.runtime)
102102+103103+fix_debuglinks()
104104+fix_symlinks()
105105+106106+# vi: set noexpandtab: