blob: a0b34fa7a5dea68f45c7adf7359d357a5338bcd1 [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
Brad Bishop316dfdd2018-06-25 12:45:53 -040011OPKGBUILDCMD ??= "opkg-build -Z xz"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050013OPKG_ARGS += "--force_postinstall --prefer-arch-to-version"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014OPKG_ARGS += "${@['', '--no-install-recommends'][d.getVar("NO_RECOMMENDATIONS") == "1"]}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015OPKG_ARGS += "${@['', '--add-exclude ' + ' --add-exclude '.join((d.getVar('PACKAGE_EXCLUDE') or "").split())][(d.getVar("PACKAGE_EXCLUDE") or "").strip() != ""]}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
17OPKGLIBDIR = "${localstatedir}/lib"
18
19python do_package_ipk () {
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020 import multiprocessing
21 import traceback
22
23 class IPKWritePkgProcess(multiprocessing.Process):
24 def __init__(self, *args, **kwargs):
25 multiprocessing.Process.__init__(self, *args, **kwargs)
26 self._pconn, self._cconn = multiprocessing.Pipe()
27 self._exception = None
28
29 def run(self):
30 try:
31 multiprocessing.Process.run(self)
32 self._cconn.send(None)
33 except Exception as e:
34 tb = traceback.format_exc()
35 self._cconn.send((e, tb))
36
37 @property
38 def exception(self):
39 if self._pconn.poll():
40 self._exception = self._pconn.recv()
41 return self._exception
42
Patrick Williamsc0f7c042017-02-23 20:41:17 -060043
44 oldcwd = os.getcwd()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 workdir = d.getVar('WORKDIR')
47 outdir = d.getVar('PKGWRITEDIRIPK')
48 tmpdir = d.getVar('TMPDIR')
49 pkgdest = d.getVar('PKGDEST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 if not workdir or not outdir or not tmpdir:
51 bb.error("Variables incorrectly set, unable to package")
52 return
53
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054 packages = d.getVar('PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 if not packages or packages == '':
56 bb.debug(1, "No packages; nothing to do")
57 return
58
59 # We're about to add new packages so the index needs to be checked
60 # so remove the appropriate stamp file.
61 if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK):
62 os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"))
63
Brad Bishopd7bf8c12018-02-25 22:55:05 -050064 max_process = int(d.getVar("BB_NUMBER_THREADS") or os.cpu_count() or 1)
65 launched = []
66 error = None
67 pkgs = packages.split()
68 while not error and pkgs:
69 if len(launched) < max_process:
70 p = IPKWritePkgProcess(target=ipk_write_pkg, args=(pkgs.pop(), d))
71 p.start()
72 launched.append(p)
73 for q in launched:
74 # The finished processes are joined when calling is_alive()
75 if not q.is_alive():
76 launched.remove(q)
77 if q.exception:
78 error, traceback = q.exception
79 break
80
81 for p in launched:
82 p.join()
83
84 os.chdir(oldcwd)
85
86 if error:
87 raise error
88}
89do_package_ipk[vardeps] += "ipk_write_pkg"
90do_package_ipk[vardepsexclude] = "BB_NUMBER_THREADS"
91
92def ipk_write_pkg(pkg, d):
93 import re, copy
94 import subprocess
95 import textwrap
96 import collections
97
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 def cleanupcontrol(root):
99 for p in ['CONTROL', 'DEBIAN']:
100 p = os.path.join(root, p)
101 if os.path.exists(p):
102 bb.utils.prunedir(p)
103
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500104 outdir = d.getVar('PKGWRITEDIRIPK')
105 pkgdest = d.getVar('PKGDEST')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 recipesource = os.path.basename(d.getVar('FILE'))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500107
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500108 localdata = bb.data.createCopy(d)
109 root = "%s/%s" % (pkgdest, pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 lf = bb.utils.lockfile(root + ".lock")
112 try:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 localdata.setVar('ROOT', '')
114 localdata.setVar('ROOT_%s' % pkg, root)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 pkgname = localdata.getVar('PKG_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 if not pkgname:
117 pkgname = pkg
118 localdata.setVar('PKG', pkgname)
119
120 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
121
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 basedir = os.path.join(os.path.dirname(root))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123 arch = localdata.getVar('PACKAGE_ARCH')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124
125 if localdata.getVar('IPK_HIERARCHICAL_FEED', False) == "1":
126 # Spread packages across subdirectories so each isn't too crowded
127 if pkgname.startswith('lib'):
128 pkg_prefix = 'lib' + pkgname[3]
129 else:
130 pkg_prefix = pkgname[0]
131
132 # Keep -dbg, -dev, -doc, -staticdev, -locale and -locale-* packages
133 # together. These package suffixes are taken from the definitions of
134 # PACKAGES and PACKAGES_DYNAMIC in meta/conf/bitbake.conf
135 if pkgname[-4:] in ('-dbg', '-dev', '-doc'):
136 pkg_subdir = pkgname[:-4]
137 elif pkgname.endswith('-staticdev'):
138 pkg_subdir = pkgname[:-10]
139 elif pkgname.endswith('-locale'):
140 pkg_subdir = pkgname[:-7]
141 elif '-locale-' in pkgname:
142 pkg_subdir = pkgname[:pkgname.find('-locale-')]
143 else:
144 pkg_subdir = pkgname
145
146 pkgoutdir = "%s/%s/%s/%s" % (outdir, arch, pkg_prefix, pkg_subdir)
147 else:
148 pkgoutdir = "%s/%s" % (outdir, arch)
149
150 bb.utils.mkdirhier(pkgoutdir)
151 os.chdir(root)
152 cleanupcontrol(root)
153 from glob import glob
154 g = glob('*')
155 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV'), localdata.getVar('PKGR')))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158
159 controldir = os.path.join(root, 'CONTROL')
160 bb.utils.mkdirhier(controldir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 ctrlfile = open(os.path.join(controldir, 'control'), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162
163 fields = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 pe = d.getVar('PKGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 if pe and int(pe) > 0:
166 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
167 else:
168 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
169 fields.append(["Description: %s\n", ['DESCRIPTION']])
170 fields.append(["Section: %s\n", ['SECTION']])
171 fields.append(["Priority: %s\n", ['PRIORITY']])
172 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
173 fields.append(["License: %s\n", ['LICENSE']])
174 fields.append(["Architecture: %s\n", ['PACKAGE_ARCH']])
175 fields.append(["OE: %s\n", ['PN']])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500176 if d.getVar('HOMEPAGE'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
178
179 def pullData(l, d):
180 l2 = []
181 for i in l:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 l2.append(d.getVar(i))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 return l2
184
185 ctrlfile.write("Package: %s\n" % pkgname)
186 # check for required fields
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500187 for (c, fs) in fields:
188 for f in fs:
189 if localdata.getVar(f, False) is None:
190 raise KeyError(f)
191 # Special behavior for description...
192 if 'DESCRIPTION' in fs:
193 summary = localdata.getVar('SUMMARY') or localdata.getVar('DESCRIPTION') or "."
194 ctrlfile.write('Description: %s\n' % summary)
195 description = localdata.getVar('DESCRIPTION') or "."
196 description = textwrap.dedent(description).strip()
197 if '\\n' in description:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500198 # Manually indent: multiline description includes a leading space
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199 for t in description.split('\\n'):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500200 ctrlfile.write(' %s\n' % (t.strip() or ' .'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500202 # Auto indent
203 ctrlfile.write('%s\n' % textwrap.fill(description, width=74, initial_indent=' ', subsequent_indent=' '))
204 else:
205 ctrlfile.write(c % tuple(pullData(fs, localdata)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206 # more fields
207
208 custom_fields_chunk = get_package_additional_metadata("ipk", localdata)
209 if custom_fields_chunk is not None:
210 ctrlfile.write(custom_fields_chunk)
211 ctrlfile.write("\n")
212
213 mapping_rename_hook(localdata)
214
215 def debian_cmp_remap(var):
216 # In debian '>' and '<' do not mean what it appears they mean
217 # '<' = less or equal
218 # '>' = greater or equal
219 # adjust these to the '<<' and '>>' equivalents
220 #
221 for dep in var:
222 for i, v in enumerate(var[dep]):
223 if (v or "").startswith("< "):
224 var[dep][i] = var[dep][i].replace("< ", "<< ")
225 elif (v or "").startswith("> "):
226 var[dep][i] = var[dep][i].replace("> ", ">> ")
227
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500228 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229 debian_cmp_remap(rdepends)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500230 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231 debian_cmp_remap(rrecommends)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500232 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 debian_cmp_remap(rsuggests)
234 # Deliberately drop version information here, not wanted/supported by ipk
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500235 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES") or ""), [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600236 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 debian_cmp_remap(rprovides)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500238 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239 debian_cmp_remap(rreplaces)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500240 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 debian_cmp_remap(rconflicts)
242
243 if rdepends:
244 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
245 if rsuggests:
246 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
247 if rrecommends:
248 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
249 if rprovides:
250 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
251 if rreplaces:
252 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
253 if rconflicts:
254 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500255 ctrlfile.write("Source: %s\n" % recipesource)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256 ctrlfile.close()
257
258 for script in ["preinst", "postinst", "prerm", "postrm"]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500259 scriptvar = localdata.getVar('pkg_%s' % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 if not scriptvar:
261 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500262 scriptfile = open(os.path.join(controldir, script), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500263 scriptfile.write(scriptvar)
264 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600265 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266
267 conffiles_str = ' '.join(get_conffiles(pkg, d))
268 if conffiles_str:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500269 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500270 for f in conffiles_str.split():
271 if os.path.exists(oe.path.join(root, f)):
272 conffiles.write('%s\n' % f)
273 conffiles.close()
274
275 os.chdir(basedir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500276 subprocess.check_output("PATH=\"%s\" %s %s %s" % (localdata.getVar("PATH"),
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500277 d.getVar("OPKGBUILDCMD"), pkg, pkgoutdir),
278 stderr=subprocess.STDOUT,
279 shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500281 if d.getVar('IPK_SIGN_PACKAGES') == '1':
282 ipkver = "%s-%s" % (d.getVar('PKGV'), d.getVar('PKGR'))
283 ipk_to_sign = "%s/%s_%s_%s.ipk" % (pkgoutdir, pkgname, ipkver, d.getVar('PACKAGE_ARCH'))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500284 sign_ipk(d, ipk_to_sign)
285
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287 cleanupcontrol(root)
288 bb.utils.unlockfile(lf)
289
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290# Otherwise allarch packages may change depending on override configuration
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500291ipk_write_pkg[vardepsexclude] = "OVERRIDES"
292
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293
294SSTATETASKS += "do_package_write_ipk"
295do_package_write_ipk[sstate-inputdirs] = "${PKGWRITEDIRIPK}"
296do_package_write_ipk[sstate-outputdirs] = "${DEPLOY_DIR_IPK}"
297
298python do_package_write_ipk_setscene () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500299 tmpdir = d.getVar('TMPDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300
301 if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK):
302 os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"))
303
304 sstate_setscene(d)
305}
306addtask do_package_write_ipk_setscene
307
308python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500309 if d.getVar('PACKAGES') != '':
Brad Bishop316dfdd2018-06-25 12:45:53 -0400310 deps = ' opkg-utils-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot xz-native:do_populate_sysroot'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500311 d.appendVarFlag('do_package_write_ipk', 'depends', deps)
312 d.setVarFlag('do_package_write_ipk', 'fakeroot', "1")
313}
314
315python do_package_write_ipk () {
316 bb.build.exec_func("read_subpackage_metadata", d)
317 bb.build.exec_func("do_package_ipk", d)
318}
319do_package_write_ipk[dirs] = "${PKGWRITEDIRIPK}"
320do_package_write_ipk[cleandirs] = "${PKGWRITEDIRIPK}"
321do_package_write_ipk[umask] = "022"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500322do_package_write_ipk[depends] += "${@oe.utils.build_depends_string(d.getVar('PACKAGE_WRITE_DEPS'), 'do_populate_sysroot')}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500323addtask package_write_ipk after do_packagedata do_package
324
325PACKAGEINDEXDEPS += "opkg-utils-native:do_populate_sysroot"
326PACKAGEINDEXDEPS += "opkg-native:do_populate_sysroot"
327
328do_build[recrdeptask] += "do_package_write_ipk"