···1+#!/usr/bin/env python2
2+#
3+# Script to build and install packages into the Steam runtime
4+# Patched version of https://github.com/ValveSoftware/steam-runtime/blob/master/build-runtime.py
5+6+import os
7+import re
8+import sys
9+import subprocess
10+import argparse
11+import json
12+13+# The top level directory
14+top = sys.path[0]
15+16+def parse_args():
17+ parser = argparse.ArgumentParser()
18+ parser.add_argument("-r", "--runtime", help="specify runtime path", default=os.path.join(top,"runtime"))
19+ parser.add_argument("-i", "--input", help="packages JSON", required=True)
20+ return parser.parse_args()
21+22+23+def install_deb (basename, deb, md5, dest_dir):
24+ installtag_dir=os.path.join(dest_dir, "installed")
25+ if not os.access(installtag_dir, os.W_OK):
26+ os.makedirs(installtag_dir)
27+28+ #
29+ # Write the tag file and checksum to the 'installed' subdirectory
30+ #
31+ with open(os.path.join(installtag_dir,basename),"w") as f:
32+ subprocess.check_call(['dpkg-deb', '-c', deb], stdout=f)
33+ with open(os.path.join(installtag_dir,basename+".md5"),"w") as f:
34+ f.write("%s %s.deb\n" % (md5, basename))
35+36+ #
37+ # Unpack the package into the dest_dir
38+ #
39+ os.chdir(top)
40+ subprocess.check_call(['dpkg-deb', '-x', deb, dest_dir])
41+42+43+#
44+# Walks through the files in the runtime directory and converts any absolute symlinks
45+# to their relative equivalent
46+#
47+def fix_symlinks ():
48+ for dir, subdirs, files in os.walk(args.runtime):
49+ for name in files:
50+ filepath=os.path.join(dir,name)
51+ if os.path.islink(filepath):
52+ target = os.readlink(filepath)
53+ if os.path.isabs(target):
54+ #
55+ # compute the target of the symlink based on the 'root' of the architecture's runtime
56+ #
57+ target2 = os.path.join(args.runtime,target[1:])
58+59+ #
60+ # Set the new relative target path
61+ #
62+ os.unlink(filepath)
63+ os.symlink(os.path.relpath(target2,dir), filepath)
64+65+#
66+# Creates the usr/lib/debug/.build-id/xx/xxxxxxxxx.debug symlink tree for all the debug
67+# symbols
68+#
69+def fix_debuglinks ():
70+ for dir, subdirs, files in os.walk(os.path.join(args.runtime,"usr/lib/debug")):
71+ if ".build-id" in subdirs:
72+ subdirs.remove(".build-id") # don't recurse into .build-id directory we are creating
73+74+ for file in files:
75+76+ #
77+ # scrape the output of readelf to find the buildid for this binary
78+ #
79+ p = subprocess.Popen(["readelf", '-n', os.path.join(dir,file)], stdout=subprocess.PIPE)
80+ for line in iter(p.stdout.readline, ""):
81+ m = re.search('Build ID: (\w{2})(\w+)',line)
82+ if m:
83+ linkdir = os.path.join(args.runtime,"usr/lib/debug/.build-id",m.group(1))
84+ if not os.access(linkdir, os.W_OK):
85+ os.makedirs(linkdir)
86+ link = os.path.join(linkdir,m.group(2))
87+ print "SYMLINKING symbol file %s to %s" % (link, os.path.relpath(os.path.join(dir,file),linkdir))
88+ if os.path.lexists(link):
89+ os.unlink(link)
90+ os.symlink(os.path.relpath(os.path.join(dir,file), linkdir),link)
91+92+93+args = parse_args()
94+95+96+print ("Creating Steam Runtime in %s" % args.runtime)
97+98+with open(args.input) as pkgfile:
99+ pkgs = json.load(pkgfile)
100+ for pkg in pkgs:
101+ install_deb(pkg["name"], pkg["source"], pkg["md5"], args.runtime)
102+103+fix_debuglinks()
104+fix_symlinks()
105+106+# vi: set noexpandtab: