1+ #!/usr/bin/env python
2+
3+ import argparse
4+ import sys
5+ import gzip
6+ import urllib
7+ import urlparse
8+ import posixpath
9+ import cStringIO
10+ import os
11+ import subprocess
12+
13+
14+ def get_packages_list (args ):
15+ filename = "Packages.gz"
16+ filepath = cachedir + "/" + filename
17+ if not os .path .isfile (filepath ):
18+ url = mirror + \
19+ "/dists/%s/main/binary-%s/%s" % (args .version , args .arch , filename )
20+ print "Retrieving:" , url
21+ urllib .urlretrieve (url , filepath )
22+ io = gzip .open (filepath , 'r' )
23+
24+ packages = dict ()
25+ curPackage = None
26+ i = 0
27+
28+ print "Processing:" , filepath
29+ for line in io .readlines ():
30+ if len (line ) == 1 and line [0 ] == '\n ' :
31+ curPackage = None
32+ continue
33+
34+ try :
35+ key , value = line .rstrip ().split (": " , 1 )
36+ except :
37+ continue
38+ if curPackage is None :
39+ assert key == "Package"
40+ curPackage = dict ()
41+ packages [value ] = curPackage
42+ else :
43+ curPackage [key ] = value
44+ io .close ()
45+
46+ return packages
47+
48+
49+ def urlAndNameFromPackage (p ):
50+ url = mirror + "/" + p ['Filename' ]
51+ name = posixpath .basename (urlparse .urlparse (url ).path )
52+ return (url , name )
53+
54+
55+ parser = argparse .ArgumentParser (prog = 'make_sysroot' ,
56+ description = """Create a sysroot to use when cross-compiling things.
57+ e.g. to make an Ubuntu 14.04 armhf sysroot run:
58+ make_sysroot.py --distro ubuntu --version trusty --arch armhf --install <destination sysroot>
59+ e.g. to make a Debian Jessie armel sysroot run:
60+ make_sysroot.py --distro debian --version jessie --arch armel --install <destination sysroot>"""
61+ )
62+ parser .add_argument ('--distro' , required = True , help = 'Distribution' )
63+ parser .add_argument ('--version' , required = True , help = 'Version' )
64+ parser .add_argument ('--arch' , required = True , help = 'Architecture' )
65+ parser .add_argument ('--install' , help = 'Install dir' )
66+
67+
68+ args = parser .parse_args (sys .argv [1 :])
69+
70+
71+ required_packages = [
72+ 'libc6' ,
73+ 'libc6-dev' ,
74+ 'linux-libc-dev' ,
75+ 'libicu52' ,
76+ 'libicu-dev' ,
77+ 'libbsd0' ,
78+ 'libbsd-dev' ,
79+ 'libgcc-4.8-dev' ,
80+ 'libstdc++-4.8-dev' ,
81+ 'libstdc++6' ,
82+ 'libstdc++-4.8-dev' ,
83+ 'libatomic1' ,
84+ 'libgcc1' ,
85+ 'libxml2' ,
86+ 'libxml2-dev' ,
87+ 'libuuid1' ,
88+ 'uuid-dev' ,
89+ 'libncurses5' ,
90+ 'libncurses5-dev' ,
91+ 'libtinfo5' ,
92+ 'libtinfo-dev' ,
93+ 'zlib1g' ,
94+ 'zlib1g-dev' ,
95+ 'libedit2' ,
96+ 'libedit-dev' ,
97+ 'python-dev' , # for LLDB
98+ 'libsqlite3-0' , # for LLBuild
99+ 'libsqlite3-dev' # for LLBuild
100+ ]
101+
102+ mirror = ""
103+ if args .distro == "ubuntu" :
104+ mirror = "http://ports.ubuntu.com/ubuntu-ports"
105+ elif args .distro == "debian" :
106+ mirror = "http://ftp.debian.org/debian"
107+ elif args .distro == "raspbian" :
108+ mirror = "http://archive.raspbian.com/raspbian"
109+ else :
110+ sys .exit (1 )
111+
112+ cachedir = "cache/%s/%s" % (args .distro , args .version )
113+ if not os .path .exists (cachedir ):
114+ os .makedirs (cachedir )
115+
116+ all_packages = get_packages_list (args )
117+
118+ deps = []
119+
120+ for name in required_packages :
121+ p = all_packages [name ]
122+ url , filename = urlAndNameFromPackage (p )
123+ if p .has_key ("Depends" ):
124+ for d in p ['Depends' ].split (', ' ):
125+ deps .append (d )
126+ filepath = cachedir + "/" + filename
127+ if not os .path .isfile (filepath ):
128+ print "Retrieving:" , url
129+ urllib .urlretrieve (url , filepath )
130+
131+ if args .install :
132+ print "Installing:" , filepath
133+ ret = subprocess .call (['dpkg-deb' , '-x' , filepath , args .install ])
134+ assert ret == 0
135+
136+
137+ # Fixup broken symlinks
138+ if args .install :
139+ for dirname , dirnames , filenames in os .walk (args .install ):
140+ for filename in filenames :
141+ path = os .path .join (dirname , filename )
142+ if os .path .islink (path ):
143+ link = os .readlink (path )
144+ if link [0 ] == os .path .sep :
145+ fulllink = os .path .normpath (
146+ os .path .join (args .install , "." + link ))
147+ fixed = os .path .relpath (fulllink , dirname )
148+ os .unlink (path )
149+ os .symlink (fixed , path )
0 commit comments