squashfs-img.bbclass: fixed handling for FILES entries like /usr/lib/*, fixed FILES...
[opendreambox.git] / meta-opendreambox / classes / squashfs-img.bbclass
1 SQUASHFS_IMG_NAMES ?= ""
2 SQUASHFS_IMG_REPLACES ?= ""
3 SQUASHFS_IMG_BLOCKSIZES ?= ""
4 SQUASHFS_IMG_FILESREGEX ?= ""
5 SQUASHFS_IMG_COMPRESSOR ?= ""
6 SQUASHFS_IMG_SUPPORT_ENABLED ?= "${@base_less_or_equal('DREAMBOX_IMAGE_SIZE', 64, 1, 0, d)}"
7 SQUASHFS_IMG_PR_INC ?= "-sqsh0"
8
9 DEPENDS += '${@base_contains('SQUASHFS_IMG_SUPPORT_ENABLED', '1', 'squashfs-tools-native dreambox-squashfs-support', '', d)}'
10
11 PACKAGE_ARCH := "${@base_contains('SQUASHFS_IMG_SUPPORT_ENABLED', '1', '${MACHINE_ARCH}', '${PACKAGE_ARCH}', d)}"
12
13 PR .= "${@base_contains('SQUASHFS_IMG_SUPPORT_ENABLED', '1', '${SQUASHFS_IMG_PR_INC}', '', d)}"
14
15 python emit_pkgdata_prepend() {
16         import os, subprocess, glob, re, shutil
17         enabled = d.getVar('SQUASHFS_IMG_SUPPORT_ENABLED', True)
18
19         if enabled != '1':
20                 bb.note('squashfs-image-support is disabled..')
21         else:
22                 def makedirs(dir):
23                         try:
24                                 os.makedirs(dir)
25                         except:
26                                 pass
27
28                 def rename(src, dest):
29                         try:
30                                 os.rename(src, dest)
31                         except:
32                                 pass
33
34                 def symlink(source, link_name):
35                         try:
36                                 os.symlink(source, link_name)
37                         except:
38                                 pass
39
40                 def rmtree(path):
41                         try:
42                                 shutil.rmtree(path)
43                         except:
44                                 pass
45
46                 packages = d.getVar('PACKAGES', True).split(' ')
47                 packagesdir = d.getVar('PKGDEST', True)
48
49                 package_names = d.getVar('SQUASHFS_IMG_PACKAGES', True).split(':')
50                 package_replaces = d.getVar('SQUASHFS_IMG_REPLACES', True).split(':')
51                 package_blocksizes = d.getVar('SQUASHFS_IMG_BLOCKSIZES', True).split(':')
52                 package_filesregex = d.getVar('SQUASHFS_IMG_FILESREGEX', True).split(':')
53                 package_compressor = d.getVar('SQUASHFS_IMG_COMPRESSOR', True)
54
55                 if not package_compressor:
56                         kernel_version = d.getVar('DREAMBOX_KERNEL_VERSION', True)
57                         package_compressor = kernel_version <= '2.6.18' and 'lzma' or 'xz'
58
59                 num_replaces = len(package_replaces)
60                 num_blocksizes = len(package_blocksizes)
61                 num_filesregex = len(package_filesregex)
62                 idx = 0
63
64                 for package in package_names:
65                         bb.note('building squashfs img %s' %package)
66
67                         package_files = [ ]
68                         squashfs_files = [ ]
69                         packagedir = packagesdir+'/'+package
70
71                         if num_replaces <= idx:
72                                 bb.fatal('no replaces for %s' %package)
73
74                         replaces = package_replaces[idx].split(' ')
75                         replaces = [ repl.strip('\t ') for repl in replaces if repl.strip('\t ') ]
76
77                         blocksize = ''
78                         if num_blocksizes > idx:
79                                 blocksize = package_blocksizes[idx]
80                         if blocksize == '':
81                                 blocksize = str(128*1024)
82
83                         if num_filesregex > idx:
84                                 filesregex = package_filesregex[idx]
85
86                         if filesregex != '':
87                                 filesre = re.compile(filesregex)
88                         else:
89                                 filesre = None
90
91                         bb.note('replaces %s, blocksize %s, file_regex %s' %(replaces, blocksize, filesregex))
92
93                         for repl in replaces:
94                                 bb.note('process replacement %s' %repl)
95                                 try:
96                                         files_pathes = d.getVar('FILES_%s'%repl, True).split(' ')
97                                         files_pathes = [ p.strip('\t ') for p in files_pathes ]
98                                 except:
99                                         bb.fatal('couldnt retrieve FILES_%s!' %repl)
100                                 repldir = packagesdir+'/'+repl
101                                 bb.note('FILES_%s %s' %(repl, files_pathes))
102
103                                 l = len(repldir)
104                                 for path in files_pathes:
105                                         if path.startswith('/etc'):
106                                                 continue
107                                         realp = repldir+path
108                                         for realpath in glob.glob(realp):
109                                                 if os.path.isdir(realpath):
110                                                         files = []
111                                                         for root, dirs, names in os.walk(realpath):
112                                                                 files += [ root+'/'+name for name in names ]
113                                                 else:
114                                                         if os.path.islink(realpath):
115                                                                 continue
116                                                         files = glob.glob(realpath)
117                                                 files = [ (f, l) for f in files if not os.path.islink(f) and (not filesre or filesre.search(f) is not None) ]
118                                                 squashfs_files += files
119
120                         if not squashfs_files:
121                                 bb.fatal("no squashfs files ... maybe regex wrong?!?")
122
123                         bb.note('squashfs_files %s' %[ f for f, l in squashfs_files ])
124
125                         makedirs(packagedir+'/media/squashfs-images/'+package)
126                         makedirs(packagedir+'/squashfs-images')
127
128                         for f, l in squashfs_files:
129                                 makedirs(packagedir+'/tmp'+os.path.dirname(f)[l:])
130                                 rename(f, packagedir+'/tmp'+f[l:])
131                                 symlink('/media/squashfs-images/'+package+f[l:], f)
132
133                         squashfs_cmd = ['mksquashfs']
134                         squashfs_cmd.append(packagedir+'/tmp/')
135                         squashfs_cmd.append(packagedir+'/squashfs-images/'+package)
136                         squashfs_cmd.append('-b')
137                         squashfs_cmd.append('%s' %blocksize)
138                         squashfs_cmd.append('-comp')
139                         squashfs_cmd.append('%s' %package_compressor)
140
141                         bb.note('squashfs_cmd %s' %squashfs_cmd)
142
143                         if subprocess.call(squashfs_cmd):
144                                 bb.fatal('mksquashfs failed!')
145
146                         rmtree(packagedir+'/tmp')
147
148                         for repl in replaces:
149                                 rdepends = d.getVar('RDEPENDS_'+repl, True).split(' ')
150                                 rdepends.append(package)
151                                 d.setVar('RDEPENDS_'+repl, ' '.join(rdepends))
152                                 d.setVar('INSANE_SKIP_'+repl, 'dev-so')
153                                 d.setVar('ALLOW_EMPTY_'+repl, '1')
154
155                         d.setVar('RDEPENDS_'+package, 'dreambox-squashfs-support')
156                         d.setVar('FILES_'+package, '/')
157                         d.setVar('pkg_postinst_'+package, 'if [ -z "$D" ]; then if ! mountpoint -q /media/squashfs-images/'+package+'; then mount -t squashfs -o ro,loop /squashfs-images/'+package+' /media/squashfs-images/'+package+' || touch /var/tmp/.mount_'+package+'_needed; else touch /var/tmp/.mount_'+package+'_needed; fi; fi')
158                         d.setVar('pkg_prerm_'+package, 'if [ -z "$D" ]; then if mountpoint -q /media/squashfs-images/'+package+'; then umount /media/squashfs-images/'+package+' && losetup -d `losetup | grep '+package+' | cut -d: -f1` || touch /var/tmp/.umount_'+package+'_needed; else touch /var/tmp/.umount_'+package+'_needed; fi; fi')
159                         packages.append(package)
160
161                         idx += 1
162
163                 d.setVar('PACKAGES', ' '.join(packages))
164 }