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