blob: e7e7d4929b36b3a306d8788b12a27d157acdb90e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001inherit package
2
3IMAGE_PKGTYPE ?= "ipk"
4
5IPKGCONF_TARGET = "${WORKDIR}/opkg.conf"
6IPKGCONF_SDK = "${WORKDIR}/opkg-sdk.conf"
7
8PKGWRITEDIRIPK = "${WORKDIR}/deploy-ipks"
9
10# Program to be used to build opkg packages
11OPKGBUILDCMD ??= "opkg-build"
12
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050013OPKG_ARGS += "--force_postinstall --prefer-arch-to-version"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014OPKG_ARGS += "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS", True) == "1"]}"
15OPKG_ARGS += "${@['', '--add-exclude ' + ' --add-exclude '.join((d.getVar('PACKAGE_EXCLUDE', True) or "").split())][(d.getVar("PACKAGE_EXCLUDE", True) or "") != ""]}"
16
17OPKGLIBDIR = "${localstatedir}/lib"
18
19python do_package_ipk () {
20 import re, copy
21 import textwrap
22 import subprocess
Patrick Williamsc0f7c042017-02-23 20:41:17 -060023 import collections
24
25 oldcwd = os.getcwd()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026
27 workdir = d.getVar('WORKDIR', True)
28 outdir = d.getVar('PKGWRITEDIRIPK', True)
29 tmpdir = d.getVar('TMPDIR', True)
30 pkgdest = d.getVar('PKGDEST', True)
31 if not workdir or not outdir or not tmpdir:
32 bb.error("Variables incorrectly set, unable to package")
33 return
34
35 packages = d.getVar('PACKAGES', True)
36 if not packages or packages == '':
37 bb.debug(1, "No packages; nothing to do")
38 return
39
40 # We're about to add new packages so the index needs to be checked
41 # so remove the appropriate stamp file.
42 if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK):
43 os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"))
44
45 def cleanupcontrol(root):
46 for p in ['CONTROL', 'DEBIAN']:
47 p = os.path.join(root, p)
48 if os.path.exists(p):
49 bb.utils.prunedir(p)
50
Brad Bishop37a0e4d2017-12-04 01:01:44 -050051 recipesource = os.path.basename(d.getVar('FILE', True))
52
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 for pkg in packages.split():
54 localdata = bb.data.createCopy(d)
55 root = "%s/%s" % (pkgdest, pkg)
56
57 lf = bb.utils.lockfile(root + ".lock")
58
59 localdata.setVar('ROOT', '')
60 localdata.setVar('ROOT_%s' % pkg, root)
61 pkgname = localdata.getVar('PKG_%s' % pkg, True)
62 if not pkgname:
63 pkgname = pkg
64 localdata.setVar('PKG', pkgname)
65
66 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
67
68 bb.data.update_data(localdata)
69 basedir = os.path.join(os.path.dirname(root))
70 arch = localdata.getVar('PACKAGE_ARCH', True)
71
72 if localdata.getVar('IPK_HIERARCHICAL_FEED', False) == "1":
73 # Spread packages across subdirectories so each isn't too crowded
74 if pkgname.startswith('lib'):
75 pkg_prefix = 'lib' + pkgname[3]
76 else:
77 pkg_prefix = pkgname[0]
78
79 # Keep -dbg, -dev, -doc, -staticdev, -locale and -locale-* packages
80 # together. These package suffixes are taken from the definitions of
81 # PACKAGES and PACKAGES_DYNAMIC in meta/conf/bitbake.conf
82 if pkgname[-4:] in ('-dbg', '-dev', '-doc'):
83 pkg_subdir = pkgname[:-4]
84 elif pkgname.endswith('-staticdev'):
85 pkg_subdir = pkgname[:-10]
86 elif pkgname.endswith('-locale'):
87 pkg_subdir = pkgname[:-7]
88 elif '-locale-' in pkgname:
89 pkg_subdir = pkgname[:pkgname.find('-locale-')]
90 else:
91 pkg_subdir = pkgname
92
93 pkgoutdir = "%s/%s/%s/%s" % (outdir, arch, pkg_prefix, pkg_subdir)
94 else:
95 pkgoutdir = "%s/%s" % (outdir, arch)
96
97 bb.utils.mkdirhier(pkgoutdir)
98 os.chdir(root)
99 cleanupcontrol(root)
100 from glob import glob
101 g = glob('*')
102 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
103 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV', True), localdata.getVar('PKGR', True)))
104 bb.utils.unlockfile(lf)
105 continue
106
107 controldir = os.path.join(root, 'CONTROL')
108 bb.utils.mkdirhier(controldir)
109 try:
110 ctrlfile = open(os.path.join(controldir, 'control'), 'w')
111 except OSError:
112 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600113 bb.fatal("unable to open control file for writing")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
115 fields = []
116 pe = d.getVar('PKGE', True)
117 if pe and int(pe) > 0:
118 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
119 else:
120 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
121 fields.append(["Description: %s\n", ['DESCRIPTION']])
122 fields.append(["Section: %s\n", ['SECTION']])
123 fields.append(["Priority: %s\n", ['PRIORITY']])
124 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
125 fields.append(["License: %s\n", ['LICENSE']])
126 fields.append(["Architecture: %s\n", ['PACKAGE_ARCH']])
127 fields.append(["OE: %s\n", ['PN']])
128 if d.getVar('HOMEPAGE', True):
129 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
130
131 def pullData(l, d):
132 l2 = []
133 for i in l:
134 l2.append(d.getVar(i, True))
135 return l2
136
137 ctrlfile.write("Package: %s\n" % pkgname)
138 # check for required fields
139 try:
140 for (c, fs) in fields:
141 for f in fs:
142 if localdata.getVar(f, False) is None:
143 raise KeyError(f)
144 # Special behavior for description...
145 if 'DESCRIPTION' in fs:
146 summary = localdata.getVar('SUMMARY', True) or localdata.getVar('DESCRIPTION', True) or "."
147 ctrlfile.write('Description: %s\n' % summary)
148 description = localdata.getVar('DESCRIPTION', True) or "."
149 description = textwrap.dedent(description).strip()
150 if '\\n' in description:
151 # Manually indent
152 for t in description.split('\\n'):
153 # We don't limit the width when manually indent, but we do
154 # need the textwrap.fill() to set the initial_indent and
155 # subsequent_indent, so set a large width
156 ctrlfile.write('%s\n' % textwrap.fill(t.strip(), width=100000, initial_indent=' ', subsequent_indent=' '))
157 else:
158 # Auto indent
159 ctrlfile.write('%s\n' % textwrap.fill(description, width=74, initial_indent=' ', subsequent_indent=' '))
160 else:
161 ctrlfile.write(c % tuple(pullData(fs, localdata)))
162 except KeyError:
163 import sys
164 (type, value, traceback) = sys.exc_info()
165 ctrlfile.close()
166 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167 bb.fatal("Missing field for ipk generation: %s" % value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 # more fields
169
170 custom_fields_chunk = get_package_additional_metadata("ipk", localdata)
171 if custom_fields_chunk is not None:
172 ctrlfile.write(custom_fields_chunk)
173 ctrlfile.write("\n")
174
175 mapping_rename_hook(localdata)
176
177 def debian_cmp_remap(var):
178 # In debian '>' and '<' do not mean what it appears they mean
179 # '<' = less or equal
180 # '>' = greater or equal
181 # adjust these to the '<<' and '>>' equivalents
182 #
183 for dep in var:
184 for i, v in enumerate(var[dep]):
185 if (v or "").startswith("< "):
186 var[dep][i] = var[dep][i].replace("< ", "<< ")
187 elif (v or "").startswith("> "):
188 var[dep][i] = var[dep][i].replace("> ", ">> ")
189
190 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS", True) or "")
191 debian_cmp_remap(rdepends)
192 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS", True) or "")
193 debian_cmp_remap(rrecommends)
194 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS", True) or "")
195 debian_cmp_remap(rsuggests)
196 # Deliberately drop version information here, not wanted/supported by ipk
197 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES", True) or ""), [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600198 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 debian_cmp_remap(rprovides)
200 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES", True) or "")
201 debian_cmp_remap(rreplaces)
202 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS", True) or "")
203 debian_cmp_remap(rconflicts)
204
205 if rdepends:
206 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
207 if rsuggests:
208 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
209 if rrecommends:
210 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
211 if rprovides:
212 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
213 if rreplaces:
214 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
215 if rconflicts:
216 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500217 ctrlfile.write("Source: %s\n" % recipesource)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218 ctrlfile.close()
219
220 for script in ["preinst", "postinst", "prerm", "postrm"]:
221 scriptvar = localdata.getVar('pkg_%s' % script, True)
222 if not scriptvar:
223 continue
224 try:
225 scriptfile = open(os.path.join(controldir, script), 'w')
226 except OSError:
227 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600228 bb.fatal("unable to open %s script file for writing" % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229 scriptfile.write(scriptvar)
230 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600231 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232
233 conffiles_str = ' '.join(get_conffiles(pkg, d))
234 if conffiles_str:
235 try:
236 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
237 except OSError:
238 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600239 bb.fatal("unable to open conffiles for writing")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240 for f in conffiles_str.split():
241 if os.path.exists(oe.path.join(root, f)):
242 conffiles.write('%s\n' % f)
243 conffiles.close()
244
245 os.chdir(basedir)
246 ret = subprocess.call("PATH=\"%s\" %s %s %s" % (localdata.getVar("PATH", True),
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600247 d.getVar("OPKGBUILDCMD", True), pkg, pkgoutdir), shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 if ret != 0:
249 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600250 bb.fatal("opkg-build execution failed")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500252 if d.getVar('IPK_SIGN_PACKAGES', True) == '1':
253 ipkver = "%s-%s" % (d.getVar('PKGV', True), d.getVar('PKGR', True))
254 ipk_to_sign = "%s/%s_%s_%s.ipk" % (pkgoutdir, pkgname, ipkver, d.getVar('PACKAGE_ARCH', True))
255 sign_ipk(d, ipk_to_sign)
256
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 cleanupcontrol(root)
258 bb.utils.unlockfile(lf)
259
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260 os.chdir(oldcwd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500261}
262# Otherwise allarch packages may change depending on override configuration
263do_package_ipk[vardepsexclude] = "OVERRIDES"
264
265SSTATETASKS += "do_package_write_ipk"
266do_package_write_ipk[sstate-inputdirs] = "${PKGWRITEDIRIPK}"
267do_package_write_ipk[sstate-outputdirs] = "${DEPLOY_DIR_IPK}"
268
269python do_package_write_ipk_setscene () {
270 tmpdir = d.getVar('TMPDIR', True)
271
272 if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK):
273 os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"))
274
275 sstate_setscene(d)
276}
277addtask do_package_write_ipk_setscene
278
279python () {
280 if d.getVar('PACKAGES', True) != '':
281 deps = ' opkg-utils-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
282 d.appendVarFlag('do_package_write_ipk', 'depends', deps)
283 d.setVarFlag('do_package_write_ipk', 'fakeroot', "1")
284}
285
286python do_package_write_ipk () {
287 bb.build.exec_func("read_subpackage_metadata", d)
288 bb.build.exec_func("do_package_ipk", d)
289}
290do_package_write_ipk[dirs] = "${PKGWRITEDIRIPK}"
291do_package_write_ipk[cleandirs] = "${PKGWRITEDIRIPK}"
292do_package_write_ipk[umask] = "022"
293addtask package_write_ipk after do_packagedata do_package
294
295PACKAGEINDEXDEPS += "opkg-utils-native:do_populate_sysroot"
296PACKAGEINDEXDEPS += "opkg-native:do_populate_sysroot"
297
298do_build[recrdeptask] += "do_package_write_ipk"