blob: eb00932336f26c8c8ae27e163329514b4f3d2c33 [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
51 for pkg in packages.split():
52 localdata = bb.data.createCopy(d)
53 root = "%s/%s" % (pkgdest, pkg)
54
55 lf = bb.utils.lockfile(root + ".lock")
56
57 localdata.setVar('ROOT', '')
58 localdata.setVar('ROOT_%s' % pkg, root)
59 pkgname = localdata.getVar('PKG_%s' % pkg, True)
60 if not pkgname:
61 pkgname = pkg
62 localdata.setVar('PKG', pkgname)
63
64 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + pkg)
65
66 bb.data.update_data(localdata)
67 basedir = os.path.join(os.path.dirname(root))
68 arch = localdata.getVar('PACKAGE_ARCH', True)
69
70 if localdata.getVar('IPK_HIERARCHICAL_FEED', False) == "1":
71 # Spread packages across subdirectories so each isn't too crowded
72 if pkgname.startswith('lib'):
73 pkg_prefix = 'lib' + pkgname[3]
74 else:
75 pkg_prefix = pkgname[0]
76
77 # Keep -dbg, -dev, -doc, -staticdev, -locale and -locale-* packages
78 # together. These package suffixes are taken from the definitions of
79 # PACKAGES and PACKAGES_DYNAMIC in meta/conf/bitbake.conf
80 if pkgname[-4:] in ('-dbg', '-dev', '-doc'):
81 pkg_subdir = pkgname[:-4]
82 elif pkgname.endswith('-staticdev'):
83 pkg_subdir = pkgname[:-10]
84 elif pkgname.endswith('-locale'):
85 pkg_subdir = pkgname[:-7]
86 elif '-locale-' in pkgname:
87 pkg_subdir = pkgname[:pkgname.find('-locale-')]
88 else:
89 pkg_subdir = pkgname
90
91 pkgoutdir = "%s/%s/%s/%s" % (outdir, arch, pkg_prefix, pkg_subdir)
92 else:
93 pkgoutdir = "%s/%s" % (outdir, arch)
94
95 bb.utils.mkdirhier(pkgoutdir)
96 os.chdir(root)
97 cleanupcontrol(root)
98 from glob import glob
99 g = glob('*')
100 if not g and localdata.getVar('ALLOW_EMPTY', False) != "1":
101 bb.note("Not creating empty archive for %s-%s-%s" % (pkg, localdata.getVar('PKGV', True), localdata.getVar('PKGR', True)))
102 bb.utils.unlockfile(lf)
103 continue
104
105 controldir = os.path.join(root, 'CONTROL')
106 bb.utils.mkdirhier(controldir)
107 try:
108 ctrlfile = open(os.path.join(controldir, 'control'), 'w')
109 except OSError:
110 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600111 bb.fatal("unable to open control file for writing")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112
113 fields = []
114 pe = d.getVar('PKGE', True)
115 if pe and int(pe) > 0:
116 fields.append(["Version: %s:%s-%s\n", ['PKGE', 'PKGV', 'PKGR']])
117 else:
118 fields.append(["Version: %s-%s\n", ['PKGV', 'PKGR']])
119 fields.append(["Description: %s\n", ['DESCRIPTION']])
120 fields.append(["Section: %s\n", ['SECTION']])
121 fields.append(["Priority: %s\n", ['PRIORITY']])
122 fields.append(["Maintainer: %s\n", ['MAINTAINER']])
123 fields.append(["License: %s\n", ['LICENSE']])
124 fields.append(["Architecture: %s\n", ['PACKAGE_ARCH']])
125 fields.append(["OE: %s\n", ['PN']])
126 if d.getVar('HOMEPAGE', True):
127 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
128
129 def pullData(l, d):
130 l2 = []
131 for i in l:
132 l2.append(d.getVar(i, True))
133 return l2
134
135 ctrlfile.write("Package: %s\n" % pkgname)
136 # check for required fields
137 try:
138 for (c, fs) in fields:
139 for f in fs:
140 if localdata.getVar(f, False) is None:
141 raise KeyError(f)
142 # Special behavior for description...
143 if 'DESCRIPTION' in fs:
144 summary = localdata.getVar('SUMMARY', True) or localdata.getVar('DESCRIPTION', True) or "."
145 ctrlfile.write('Description: %s\n' % summary)
146 description = localdata.getVar('DESCRIPTION', True) or "."
147 description = textwrap.dedent(description).strip()
148 if '\\n' in description:
149 # Manually indent
150 for t in description.split('\\n'):
151 # We don't limit the width when manually indent, but we do
152 # need the textwrap.fill() to set the initial_indent and
153 # subsequent_indent, so set a large width
154 ctrlfile.write('%s\n' % textwrap.fill(t.strip(), width=100000, initial_indent=' ', subsequent_indent=' '))
155 else:
156 # Auto indent
157 ctrlfile.write('%s\n' % textwrap.fill(description, width=74, initial_indent=' ', subsequent_indent=' '))
158 else:
159 ctrlfile.write(c % tuple(pullData(fs, localdata)))
160 except KeyError:
161 import sys
162 (type, value, traceback) = sys.exc_info()
163 ctrlfile.close()
164 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600165 bb.fatal("Missing field for ipk generation: %s" % value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 # more fields
167
168 custom_fields_chunk = get_package_additional_metadata("ipk", localdata)
169 if custom_fields_chunk is not None:
170 ctrlfile.write(custom_fields_chunk)
171 ctrlfile.write("\n")
172
173 mapping_rename_hook(localdata)
174
175 def debian_cmp_remap(var):
176 # In debian '>' and '<' do not mean what it appears they mean
177 # '<' = less or equal
178 # '>' = greater or equal
179 # adjust these to the '<<' and '>>' equivalents
180 #
181 for dep in var:
182 for i, v in enumerate(var[dep]):
183 if (v or "").startswith("< "):
184 var[dep][i] = var[dep][i].replace("< ", "<< ")
185 elif (v or "").startswith("> "):
186 var[dep][i] = var[dep][i].replace("> ", ">> ")
187
188 rdepends = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS", True) or "")
189 debian_cmp_remap(rdepends)
190 rrecommends = bb.utils.explode_dep_versions2(localdata.getVar("RRECOMMENDS", True) or "")
191 debian_cmp_remap(rrecommends)
192 rsuggests = bb.utils.explode_dep_versions2(localdata.getVar("RSUGGESTS", True) or "")
193 debian_cmp_remap(rsuggests)
194 # Deliberately drop version information here, not wanted/supported by ipk
195 rprovides = dict.fromkeys(bb.utils.explode_dep_versions2(localdata.getVar("RPROVIDES", True) or ""), [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600196 rprovides = collections.OrderedDict(sorted(rprovides.items(), key=lambda x: x[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 debian_cmp_remap(rprovides)
198 rreplaces = bb.utils.explode_dep_versions2(localdata.getVar("RREPLACES", True) or "")
199 debian_cmp_remap(rreplaces)
200 rconflicts = bb.utils.explode_dep_versions2(localdata.getVar("RCONFLICTS", True) or "")
201 debian_cmp_remap(rconflicts)
202
203 if rdepends:
204 ctrlfile.write("Depends: %s\n" % bb.utils.join_deps(rdepends))
205 if rsuggests:
206 ctrlfile.write("Suggests: %s\n" % bb.utils.join_deps(rsuggests))
207 if rrecommends:
208 ctrlfile.write("Recommends: %s\n" % bb.utils.join_deps(rrecommends))
209 if rprovides:
210 ctrlfile.write("Provides: %s\n" % bb.utils.join_deps(rprovides))
211 if rreplaces:
212 ctrlfile.write("Replaces: %s\n" % bb.utils.join_deps(rreplaces))
213 if rconflicts:
214 ctrlfile.write("Conflicts: %s\n" % bb.utils.join_deps(rconflicts))
215 src_uri = localdata.getVar("SRC_URI", True).strip() or "None"
216 if src_uri:
217 src_uri = re.sub("\s+", " ", src_uri)
218 ctrlfile.write("Source: %s\n" % " ".join(src_uri.split()))
219 ctrlfile.close()
220
221 for script in ["preinst", "postinst", "prerm", "postrm"]:
222 scriptvar = localdata.getVar('pkg_%s' % script, True)
223 if not scriptvar:
224 continue
225 try:
226 scriptfile = open(os.path.join(controldir, script), 'w')
227 except OSError:
228 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600229 bb.fatal("unable to open %s script file for writing" % script)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 scriptfile.write(scriptvar)
231 scriptfile.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600232 os.chmod(os.path.join(controldir, script), 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233
234 conffiles_str = ' '.join(get_conffiles(pkg, d))
235 if conffiles_str:
236 try:
237 conffiles = open(os.path.join(controldir, 'conffiles'), 'w')
238 except OSError:
239 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600240 bb.fatal("unable to open conffiles for writing")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 for f in conffiles_str.split():
242 if os.path.exists(oe.path.join(root, f)):
243 conffiles.write('%s\n' % f)
244 conffiles.close()
245
246 os.chdir(basedir)
247 ret = subprocess.call("PATH=\"%s\" %s %s %s" % (localdata.getVar("PATH", True),
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600248 d.getVar("OPKGBUILDCMD", True), pkg, pkgoutdir), shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 if ret != 0:
250 bb.utils.unlockfile(lf)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600251 bb.fatal("opkg-build execution failed")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500253 if d.getVar('IPK_SIGN_PACKAGES', True) == '1':
254 ipkver = "%s-%s" % (d.getVar('PKGV', True), d.getVar('PKGR', True))
255 ipk_to_sign = "%s/%s_%s_%s.ipk" % (pkgoutdir, pkgname, ipkver, d.getVar('PACKAGE_ARCH', True))
256 sign_ipk(d, ipk_to_sign)
257
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258 cleanupcontrol(root)
259 bb.utils.unlockfile(lf)
260
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600261 os.chdir(oldcwd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262}
263# Otherwise allarch packages may change depending on override configuration
264do_package_ipk[vardepsexclude] = "OVERRIDES"
265
266SSTATETASKS += "do_package_write_ipk"
267do_package_write_ipk[sstate-inputdirs] = "${PKGWRITEDIRIPK}"
268do_package_write_ipk[sstate-outputdirs] = "${DEPLOY_DIR_IPK}"
269
270python do_package_write_ipk_setscene () {
271 tmpdir = d.getVar('TMPDIR', True)
272
273 if os.access(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"), os.R_OK):
274 os.unlink(os.path.join(tmpdir, "stamps", "IPK_PACKAGE_INDEX_CLEAN"))
275
276 sstate_setscene(d)
277}
278addtask do_package_write_ipk_setscene
279
280python () {
281 if d.getVar('PACKAGES', True) != '':
282 deps = ' opkg-utils-native:do_populate_sysroot virtual/fakeroot-native:do_populate_sysroot'
283 d.appendVarFlag('do_package_write_ipk', 'depends', deps)
284 d.setVarFlag('do_package_write_ipk', 'fakeroot', "1")
285}
286
287python do_package_write_ipk () {
288 bb.build.exec_func("read_subpackage_metadata", d)
289 bb.build.exec_func("do_package_ipk", d)
290}
291do_package_write_ipk[dirs] = "${PKGWRITEDIRIPK}"
292do_package_write_ipk[cleandirs] = "${PKGWRITEDIRIPK}"
293do_package_write_ipk[umask] = "022"
294addtask package_write_ipk after do_packagedata do_package
295
296PACKAGEINDEXDEPS += "opkg-utils-native:do_populate_sysroot"
297PACKAGEINDEXDEPS += "opkg-native:do_populate_sysroot"
298
299do_build[recrdeptask] += "do_package_write_ipk"