Skip to content

Instantly share code, notes, and snippets.

@froody
Last active February 23, 2016 22:38
Show Gist options
  • Save froody/de6846f3455451f81992 to your computer and use it in GitHub Desktop.
Save froody/de6846f3455451f81992 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Install dpkg with `brew install dpkg libelf` before running this script
import argparse
import sys
import bz2
import urllib2
import urllib
import urlparse
import posixpath
import cStringIO
import os
import subprocess
parser = argparse.ArgumentParser(prog='make_sysroot',
description="""Create a sysroot to use when cross-compiling things.
e.g. to make an Ubuntu 14.04 sysroot run:
make_sysroot.py --distro ubuntu --version trusty --arch armhf --install <destination sysroot>"""
)
parser.add_argument('--distro', required=True, help='Distribution')
parser.add_argument('--version', required=True, help='Version')
parser.add_argument('--arch', required=True, help='Architecture')
parser.add_argument('--install', help='Install dir')
args = parser.parse_args(sys.argv[1:])
required_packages = ['libc6', 'libc6-dev', 'linux-libc-dev', 'libicu52',
'libbsd0', 'libbsd-dev', 'libicu-dev', 'libgcc-4.8-dev', 'libstdc++-4.8-dev',
'libstdc++6' , 'libstdc++-4.8-dev', 'libatomic1', 'libgcc1']
def get_packages_list(args):
if args.distro == "ubuntu":
if not os.path.isfile("Packages.bz2"):
url = "http://ports.ubuntu.com/ubuntu-ports/dists/%s/main/binary-%s/Packages.bz2" % (args.version, args.arch)
response = urllib2.urlopen(url)
print response
else:
response = open("Packages.bz2", 'r')
html = bz2.decompress(response.read())
io = cStringIO.StringIO(html)
packages = dict()
curPackage = None
i = 0
for line in io.readlines():
if len(line) == 1 and line[0] == '\n':
curPackage = None
continue
key, value = line.rstrip().split(": ", 1)
if curPackage is None:
assert key == "Package"
curPackage = dict()
packages[value] = curPackage
else:
curPackage[key] = value
return packages
all_packages = get_packages_list(args)
def urlAndNameFromPackage(p):
url = "http://ports.ubuntu.com/ubuntu-ports/" + p['Filename']
name = posixpath.basename(urlparse.urlparse(url).path)
return (url, name)
deps = []
for name in required_packages:
p = all_packages[name]
url, filename = urlAndNameFromPackage(p)
if p.has_key("Depends"):
for d in p['Depends'].split(', '):
deps.append(d)
if not os.path.isfile(filename):
print "Retrieving:", filename
urllib.urlretrieve(url, filename)
if args.install:
print "Installing:", filename
ret = subprocess.call(['dpkg-deb', '-x', filename, args.install])
assert ret == 0
#Fixup broken symlinks
if args.install:
for dirname, dirnames, filenames in os.walk(args.install):
for filename in filenames:
path = os.path.join(dirname, filename)
if os.path.islink(path):
link = os.readlink(path)
if link[0] == os.path.sep:
fulllink = os.path.normpath(os.path.join(args.install, "." + link))
fixed = os.path.relpath(fulllink, dirname)
os.unlink(path)
os.symlink(fixed, path)
@cellularmitosis
Copy link

Hi, I forked your make_sysroot.py script to add Debian support. This means that in addition to armhf, you now have armel available as well.

https://gist.github.com/cellularmitosis/3f61f979d09f875fcada

e.g. ./make_sysroot.py --distro debian --version jessie --arch armel --install sysroot.debian.jessie.armel

Also, I added a "cache" directory to keep the ubuntu / debian files distinct.

Also, Debian doesn't support Packages.bz2 any more, so I switched to Packages.gz.

Cheers,
Jason

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment