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