merge of '63238ec5d594052d5284fab3a6fc891dff49f06e'
[vuplus_openembedded] / classes / package_ipk.bbclass
1 inherit package
2
3 BOOTSTRAP_EXTRA_RDEPENDS += "ipkg-collateral ipkg"
4 IMAGE_PKGTYPE ?= "ipk"
5
6 IPKGCONF_TARGET = "${STAGING_ETCDIR_NATIVE}/ipkg.conf"
7 IPKGCONF_SDK =  "${STAGING_ETCDIR_NATIVE}/ipkg-sdk.conf"
8
9 python package_ipk_fn () {
10         from bb import data
11         bb.data.setVar('PKGFN', bb.data.getVar('PKG',d), d)
12 }
13
14 python package_ipk_install () {
15         #
16         # Warning - this function is not multimachine safe (see stagingdir reference)!
17         #
18
19         import os, sys
20         pkg = bb.data.getVar('PKG', d, 1)
21         pkgfn = bb.data.getVar('PKGFN', d, 1)
22         rootfs = bb.data.getVar('IMAGE_ROOTFS', d, 1)
23         ipkdir = bb.data.getVar('DEPLOY_DIR_IPK', d, 1)
24         stagingdir = bb.data.getVar('STAGING_DIR', d, 1)
25         tmpdir = bb.data.getVar('TMPDIR', d, 1)
26
27         if None in (pkg,pkgfn,rootfs):
28                 raise bb.build.FuncFailed("missing variables (one or more of PKG, PKGFN, IMAGEROOTFS)")
29         try:
30                 bb.mkdirhier(rootfs)
31                 os.chdir(rootfs)
32         except OSError:
33                 (type, value, traceback) = sys.exc_info()
34                 print value
35                 raise bb.build.FuncFailed
36
37         # Generate ipk.conf if it or the stamp doesnt exist
38         conffile = os.path.join(stagingdir,"ipkg.conf")
39         if not os.access(conffile, os.R_OK):
40                 ipkg_archs = bb.data.getVar('PACKAGE_ARCHS',d)
41                 if ipkg_archs is None:
42                         bb.error("PACKAGE_ARCHS missing")
43                         raise FuncFailed
44                 ipkg_archs = ipkg_archs.split()
45                 arch_priority = 1
46
47                 f = open(conffile,"w")
48                 for arch in ipkg_archs:
49                         f.write("arch %s %s\n" % ( arch, arch_priority ))
50                         arch_priority += 1
51                 f.write("src local file:%s" % ipkdir)
52                 f.close()
53
54
55         if (not os.access(os.path.join(ipkdir,"Packages"), os.R_OK) or
56                 not os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"),os.R_OK):
57                 ret = os.system('ipkg-make-index -p %s %s ' % (os.path.join(ipkdir, "Packages"), ipkdir))
58                 if (ret != 0 ):
59                         raise bb.build.FuncFailed
60                 f = open(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"),"w")
61                 f.close()
62
63         ret = os.system('ipkg-cl  -o %s -f %s update' % (rootfs, conffile))
64         ret = os.system('ipkg-cl  -o %s -f %s install %s' % (rootfs, conffile, pkgfn))
65         if (ret != 0 ):
66                 raise bb.build.FuncFailed
67 }
68
69 #
70 # Update the Packages index files in ${DEPLOY_DIR_IPK}
71 #
72 package_update_index_ipk () {
73         set -x
74
75         ipkgarchs="${PACKAGE_ARCHS}"
76
77         if [ ! -z "${DEPLOY_KEEP_PACKAGES}" ]; then
78                 return
79         fi
80
81         touch ${DEPLOY_DIR_IPK}/Packages
82         ipkg-make-index -r ${DEPLOY_DIR_IPK}/Packages -p ${DEPLOY_DIR_IPK}/Packages -l ${DEPLOY_DIR_IPK}/Packages.filelist -m ${DEPLOY_DIR_IPK}
83
84         for arch in $ipkgarchs; do
85                 if [ -e ${DEPLOY_DIR_IPK}/$arch/ ] ; then 
86                         touch ${DEPLOY_DIR_IPK}/$arch/Packages
87                         ipkg-make-index -r ${DEPLOY_DIR_IPK}/$arch/Packages -p ${DEPLOY_DIR_IPK}/$arch/Packages -l ${DEPLOY_DIR_IPK}/$arch/Packages.filelist -m ${DEPLOY_DIR_IPK}/$arch/
88                 fi
89                 if [ -e ${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk/ ] ; then 
90                         touch ${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk/Packages
91                         ipkg-make-index -r ${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk/Packages -p ${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk/Packages -l ${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk/Packages.filelist -m ${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk/
92                 fi
93         done
94 }
95
96 #
97 # Generate an ipkg conf file ${IPKGCONF_TARGET} suitable for use against 
98 # the target system and an ipkg conf file ${IPKGCONF_SDK} suitable for 
99 # use against the host system in sdk builds
100 #
101 package_generate_ipkg_conf () {
102         mkdir -p ${STAGING_ETCDIR_NATIVE}/
103         echo "src oe file:${DEPLOY_DIR_IPK}" > ${IPKGCONF_TARGET}
104         echo "src oe file:${DEPLOY_DIR_IPK}" > ${IPKGCONF_SDK}
105         ipkgarchs="${PACKAGE_ARCHS}"
106         priority=1
107         for arch in $ipkgarchs; do
108                 echo "arch $arch $priority" >> ${IPKGCONF_TARGET}
109                 echo "arch ${BUILD_ARCH}-$arch-sdk $priority" >> ${IPKGCONF_SDK}
110                 priority=$(expr $priority + 5)
111                 if [ -e ${DEPLOY_DIR_IPK}/$arch/Packages ] ; then
112                         echo "src oe-$arch file:${DEPLOY_DIR_IPK}/$arch" >> ${IPKGCONF_TARGET}
113                 fi
114                 if [ -e ${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk/Packages ] ; then
115                         echo "src oe-${BUILD_ARCH}-$arch-sdk file:${DEPLOY_DIR_IPK}/${BUILD_ARCH}-$arch-sdk" >> ${IPKGCONF_SDK}
116                 fi
117         done
118 }
119
120 python do_package_ipk () {
121         import sys, re, copy
122
123         workdir = bb.data.getVar('WORKDIR', d, 1)
124         if not workdir:
125                 bb.error("WORKDIR not defined, unable to package")
126                 return
127
128         import os # path manipulations
129         outdir = bb.data.getVar('DEPLOY_DIR_IPK', d, 1)
130         if not outdir:
131                 bb.error("DEPLOY_DIR_IPK not defined, unable to package")
132                 return
133
134         dvar = bb.data.getVar('D', d, 1)
135         if not dvar:
136                 bb.error("D not defined, unable to package")
137                 return
138         bb.mkdirhier(dvar)
139
140         packages = bb.data.getVar('PACKAGES', d, 1)
141         if not packages:
142                 bb.debug(1, "PACKAGES not defined, nothing to package")
143                 return
144
145         tmpdir = bb.data.getVar('TMPDIR', d, 1)
146
147         if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK):
148                 os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"))
149
150         if packages == []:
151                 bb.debug(1, "No packages; nothing to do")
152                 return
153
154         for pkg in packages.split():
155                 localdata = bb.data.createCopy(d)
156                 pkgdest = bb.data.getVar('PKGDEST', d, 1)
157                 root = "%s/%s" % (pkgdest, pkg)
158
159                 lf = bb.utils.lockfile(root + ".lock")
160
161                 bb.data.setVar('ROOT', '', localdata)
162                 bb.data.setVar('ROOT_%s' % pkg, root, localdata)
163                 pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
164                 if not pkgname:
165                         pkgname = pkg
166                 bb.data.setVar('PKG', pkgname, localdata)
167
168                 overrides = bb.data.getVar('OVERRIDES', localdata)
169                 if not overrides:
170                         raise bb.build.FuncFailed('OVERRIDES not defined')
171                 overrides = bb.data.expand(overrides, localdata)
172                 bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
173
174                 bb.data.update_data(localdata)
175                 basedir = os.path.join(os.path.dirname(root))
176                 arch = bb.data.getVar('PACKAGE_ARCH', localdata, 1)
177                 pkgoutdir = "%s/%s" % (outdir, arch)
178                 bb.mkdirhier(pkgoutdir)
179                 os.chdir(root)
180                 from glob import glob
181                 g = glob('*')
182                 try:
183                         del g[g.index('CONTROL')]
184                         del g[g.index('./CONTROL')]
185                 except ValueError:
186                         pass
187                 if not g and bb.data.getVar('ALLOW_EMPTY', localdata) != "1":
188                         from bb import note
189                         note("Not creating empty archive for %s-%s-%s" % (pkg, bb.data.getVar('PV', localdata, 1), bb.data.getVar('PR', localdata, 1)))
190                         bb.utils.unlockfile(lf)
191                         continue
192
193                 controldir = os.path.join(root, 'CONTROL')
194                 bb.mkdirhier(controldir)
195                 try:
196                         ctrlfile = file(os.path.join(controldir, 'control'), 'w')
197                 except OSError:
198                         bb.utils.unlockfile(lf)
199                         raise bb.build.FuncFailed("unable to open control file for writing.")
200
201                 fields = []
202                 pe = bb.data.getVar('PE', d, 1)
203                 if pe and int(pe) > 0:
204                         fields.append(["Version: %s:%s-%s\n", ['PE', 'PV', 'PR']])
205                 else:
206                         fields.append(["Version: %s-%s\n", ['PV', 'PR']])
207                 fields.append(["Description: %s\n", ['DESCRIPTION']])
208                 fields.append(["Section: %s\n", ['SECTION']])
209                 fields.append(["Priority: %s\n", ['PRIORITY']])
210                 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
211                 fields.append(["Architecture: %s\n", ['PACKAGE_ARCH']])
212                 fields.append(["OE: %s\n", ['PN']])
213                 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
214
215                 def pullData(l, d):
216                         l2 = []
217                         for i in l:
218                                 l2.append(bb.data.getVar(i, d, 1))
219                         return l2
220
221                 ctrlfile.write("Package: %s\n" % pkgname)
222                 # check for required fields
223                 try:
224                         for (c, fs) in fields:
225                                 for f in fs:
226                                         if bb.data.getVar(f, localdata) is None:
227                                                 raise KeyError(f)
228                                 ctrlfile.write(c % tuple(pullData(fs, localdata)))
229                 except KeyError:
230                         (type, value, traceback) = sys.exc_info()
231                         ctrlfile.close()
232                         bb.utils.unlockfile(lf)
233                         raise bb.build.FuncFailed("Missing field for ipk generation: %s" % value)
234                 # more fields
235
236                 bb.build.exec_func("mapping_rename_hook", localdata)
237
238                 rdepends = explode_deps(bb.data.getVar("RDEPENDS", localdata, 1) or "")
239                 rrecommends = explode_deps(bb.data.getVar("RRECOMMENDS", localdata, 1) or "")
240                 rsuggests = (bb.data.getVar("RSUGGESTS", localdata, 1) or "").split()
241                 rprovides = (bb.data.getVar("RPROVIDES", localdata, 1) or "").split()
242                 rreplaces = (bb.data.getVar("RREPLACES", localdata, 1) or "").split()
243                 rconflicts = (bb.data.getVar("RCONFLICTS", localdata, 1) or "").split()
244                 if rdepends:
245                         ctrlfile.write("Depends: %s\n" % ", ".join(rdepends))
246                 if rsuggests:
247                         ctrlfile.write("Suggests: %s\n" % ", ".join(rsuggests))
248                 if rrecommends:
249                         ctrlfile.write("Recommends: %s\n" % ", ".join(rrecommends))
250                 if rprovides:
251                         ctrlfile.write("Provides: %s\n" % ", ".join(rprovides))
252                 if rreplaces:
253                         ctrlfile.write("Replaces: %s\n" % ", ".join(rreplaces))
254                 if rconflicts:
255                         ctrlfile.write("Conflicts: %s\n" % ", ".join(rconflicts))
256                 src_uri = bb.data.getVar("SRC_URI", localdata, 1)
257                 if src_uri:
258                         src_uri = re.sub("\s+", " ", src_uri)
259                         ctrlfile.write("Source: %s\n" % " ".join(src_uri.split()))
260                 ctrlfile.close()
261
262                 for script in ["preinst", "postinst", "prerm", "postrm"]:
263                         scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
264                         if not scriptvar:
265                                 continue
266                         try:
267                                 scriptfile = file(os.path.join(controldir, script), 'w')
268                         except OSError:
269                                 bb.utils.unlockfile(lf)
270                                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
271                         scriptfile.write(scriptvar)
272                         scriptfile.close()
273                         os.chmod(os.path.join(controldir, script), 0755)
274
275                 conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
276                 if conffiles_str:
277                         try:
278                                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
279                         except OSError:
280                                 bb.utils.unlockfile(lf)
281                                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
282                         for f in conffiles_str.split():
283                                 conffiles.write('%s\n' % f)
284                         conffiles.close()
285
286                 os.chdir(basedir)
287                 ret = os.system("PATH=\"%s\" %s %s %s" % (bb.data.getVar("PATH", localdata, 1), 
288                                                           bb.data.getVar("IPKGBUILDCMD",d,1), pkg, pkgoutdir))
289                 if ret != 0:
290                         bb.utils.unlockfile(lf)
291                         raise bb.build.FuncFailed("ipkg-build execution failed")
292
293                 for script in ["preinst", "postinst", "prerm", "postrm", "control" ]:
294                         scriptfile = os.path.join(controldir, script)
295                         try:
296                                 os.remove(scriptfile)
297                         except OSError:
298                                 pass
299                 try:
300                         os.rmdir(controldir)
301                 except OSError:
302                         pass
303                 bb.utils.unlockfile(lf)
304 }
305
306 python () {
307     import bb
308     if bb.data.getVar('PACKAGES', d, True) != '':
309         deps = (bb.data.getVarFlag('do_package_write_ipk', 'depends', d) or "").split()
310         deps.append('ipkg-utils-native:do_populate_staging')
311         deps.append('fakeroot-native:do_populate_staging')
312         bb.data.setVarFlag('do_package_write_ipk', 'depends', " ".join(deps), d)
313 }
314
315 python do_package_write_ipk () {
316         bb.build.exec_func("read_subpackage_metadata", d)
317         bb.build.exec_func("do_package_ipk", d)
318 }
319 do_package_write_ipk[dirs] = "${D}"
320 addtask package_write_ipk before do_package_write after do_package