1 def legitimize_package_name(s):
2 return s.lower().replace('_', '-').replace('@', '+').replace(',', '+').replace('/', '-')
4 def do_split_packages(d, root, file_regex, output_pattern, description, postinst=None, recursive=False, hook=None, extra_depends=None, aux_files_pattern=None, postrm=None, allow_dirs=False, prepend=False, match_path=False, aux_files_pattern_verbatim=None):
7 dvar = bb.data.getVar('D', d, 1)
9 bb.error("D not defined")
12 packages = bb.data.getVar('PACKAGES', d, 1).split()
18 postinst = '#!/bin/sh\n' + postinst + '\n'
20 postrm = '#!/bin/sh\n' + postrm + '\n'
22 objs = os.listdir(dvar + root)
25 for walkroot, dirs, files in os.walk(dvar + root):
27 relpath = os.path.join(walkroot, file).replace(dvar + root + '/', '', 1)
31 if extra_depends == None:
32 extra_depends = bb.data.getVar('PKG_' + packages[0], d, 1) or packages[0]
37 m = re.match(file_regex, o)
39 m = re.match(file_regex, os.path.basename(o))
43 f = os.path.join(dvar + root, o)
44 mode = os.lstat(f).st_mode
45 if not (stat.S_ISREG(mode) or (allow_dirs and stat.S_ISDIR(mode))):
47 on = legitimize_package_name(m.group(1))
48 pkg = output_pattern % on
49 if not pkg in packages:
51 packages = [pkg] + packages
54 the_files = [os.path.join(root, o)]
56 if type(aux_files_pattern) is list:
57 for fp in aux_files_pattern:
58 the_files.append(fp % on)
60 the_files.append(aux_files_pattern % on)
61 if aux_files_pattern_verbatim:
62 if type(aux_files_pattern_verbatim) is list:
63 for fp in aux_files_pattern_verbatim:
64 the_files.append(fp % m.group(1))
66 the_files.append(aux_files_pattern_verbatim % m.group(1))
67 bb.data.setVar('FILES_' + pkg, " ".join(the_files), d)
68 if extra_depends != '':
69 the_depends = bb.data.getVar('RDEPENDS_' + pkg, d, 1)
71 the_depends = '%s %s' % (the_depends, extra_depends)
73 the_depends = extra_depends
74 bb.data.setVar('RDEPENDS_' + pkg, the_depends, d)
75 bb.data.setVar('DESCRIPTION_' + pkg, description % on, d)
77 bb.data.setVar('pkg_postinst_' + pkg, postinst, d)
79 bb.data.setVar('pkg_postrm_' + pkg, postrm, d)
81 oldfiles = bb.data.getVar('FILES_' + pkg, d, 1)
83 bb.fatal("Package '%s' exists but has no files" % pkg)
84 bb.data.setVar('FILES_' + pkg, oldfiles + " " + os.path.join(root, o), d)
86 hook(f, pkg, file_regex, output_pattern, m.group(1))
88 bb.data.setVar('PACKAGES', ' '.join(packages), d)
90 # Function to strip a single file, called from RUNSTRIP below
91 # A working 'file' (one which works on the target architecture)
92 # is necessary for this stuff to work.
93 #FIXME: this should be "" when any errors are gone!
94 IGNORE_STRIP_ERRORS ?= "1"
100 oewarn "file $1: failed (forced strip)" >&2
103 } | grep -q 'not stripped'
113 test -n "$ro" && chmod -w "$1"
116 oewarn "runstrip: ${STRIP} $1: strip failed" >&2
117 if [ x${IGNORE_STRIP_ERRORS} == x1 ]
119 #FIXME: remove this, it's for error detection
120 if file "$1" 2>/dev/null >&2
122 (oefatal "${STRIP} $1: command failed" >/dev/tty)
124 (oefatal "file $1: command failed" >/dev/tty)
130 oenote "runstrip: skip $1"
135 python populate_packages () {
136 import glob, stat, errno, re
138 workdir = bb.data.getVar('WORKDIR', d, 1)
140 bb.error("WORKDIR not defined, unable to package")
143 import os # path manipulations
144 outdir = bb.data.getVar('DEPLOY_DIR', d, 1)
146 bb.error("DEPLOY_DIR not defined, unable to package")
150 dvar = bb.data.getVar('D', d, 1)
152 bb.error("D not defined, unable to package")
156 packages = bb.data.getVar('PACKAGES', d, 1)
158 bb.debug(1, "PACKAGES not defined, nothing to package")
161 pn = bb.data.getVar('PN', d, 1)
163 bb.error("PN not defined")
171 except (os.error, AttributeError):
173 return (s[stat.ST_MODE] & stat.S_IEXEC)
175 for pkg in packages.split():
176 localdata = bb.data.createCopy(d)
177 root = os.path.join(workdir, "install", pkg)
179 os.system('rm -rf %s' % root)
181 bb.data.setVar('ROOT', '', localdata)
182 bb.data.setVar('ROOT_%s' % pkg, root, localdata)
183 pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
186 bb.data.setVar('PKG', pkgname, localdata)
188 overrides = bb.data.getVar('OVERRIDES', localdata, 1)
190 raise bb.build.FuncFailed('OVERRIDES not defined')
191 bb.data.setVar('OVERRIDES', overrides+':'+pkg, localdata)
193 bb.data.update_data(localdata)
195 root = bb.data.getVar('ROOT', localdata, 1)
197 filesvar = bb.data.getVar('FILES', localdata, 1) or ""
198 files = filesvar.split()
201 if os.path.isabs(file):
203 if not os.path.islink(file):
204 if os.path.isdir(file):
205 newfiles = [ os.path.join(file,x) for x in os.listdir(file) ]
209 globbed = glob.glob(file)
211 if [ file ] != globbed:
214 if (not os.path.islink(file)) and (not os.path.exists(file)):
216 fpath = os.path.join(root,file)
217 dpath = os.path.dirname(fpath)
219 if (bb.data.getVar('INHIBIT_PACKAGE_STRIP', d, 1) != '1') and not os.path.islink(file) and isexec(file):
220 stripfunc += "\trunstrip %s || st=1\n" % fpath
221 ret = bb.movefile(file,fpath)
222 if ret is None or ret == 0:
223 raise bb.build.FuncFailed("File population failed")
224 if not stripfunc == "":
227 bb.data.setVar('RUNSTRIP', '\tlocal st\n\tst=0\n%s\treturn $st' % stripfunc, localdata)
228 bb.data.setVarFlag('RUNSTRIP', 'func', 1, localdata)
229 bb.build.exec_func('RUNSTRIP', localdata)
234 for root, dirs, files in os.walk(dvar):
236 path = os.path.join(root[len(dvar):], f)
237 unshipped.append(path)
240 bb.note("the following files were installed but not shipped in any package:")
244 bb.build.exec_func("package_name_hook", d)
246 for pkg in packages.split():
247 if bb.data.getVar('PKG_%s' % pkg, d, 1) is None:
248 bb.data.setVar('PKG_%s' % pkg, pkg, d)
252 for pkg in packages.split():
253 dangling_links[pkg] = []
255 inst_root = os.path.join(workdir, "install", pkg)
256 for root, dirs, files in os.walk(inst_root):
258 path = os.path.join(root, f)
259 rpath = path[len(inst_root):]
260 pkg_files[pkg].append(rpath)
263 except OSError, (err, strerror):
264 if err != errno.ENOENT:
266 target = os.readlink(path)
268 target = os.path.join(root[len(inst_root):], target)
269 dangling_links[pkg].append(os.path.normpath(target))
271 for pkg in packages.split():
272 rdepends = explode_deps(bb.data.getVar('RDEPENDS_' + pkg, d, 1) or bb.data.getVar('RDEPENDS', d, 1) or "")
273 for l in dangling_links[pkg]:
275 bb.debug(1, "%s contains dangling link %s" % (pkg, l))
276 for p in packages.split():
277 for f in pkg_files[p]:
280 bb.debug(1, "target found in %s" % p)
283 dp = bb.data.getVar('PKG_' + p, d, 1) or p
284 if not dp in rdepends:
288 bb.note("%s contains dangling symlink to %s" % (pkg, l))
289 bb.data.setVar('RDEPENDS_' + pkg, " " + " ".join(rdepends), d)
291 def write_if_exists(f, pkg, var):
294 c = codecs.getencoder("string_escape")
297 val = bb.data.getVar('%s_%s' % (var, pkg), d, 1)
299 f.write('%s_%s: %s\n' % (var, pkg, encode(val)))
301 data_file = os.path.join(workdir, "install", pn + ".package")
302 f = open(data_file, 'w')
303 f.write("PACKAGES: %s\n" % packages)
304 for pkg in packages.split():
305 write_if_exists(f, pkg, 'DESCRIPTION')
306 write_if_exists(f, pkg, 'RDEPENDS')
307 write_if_exists(f, pkg, 'RPROVIDES')
308 write_if_exists(f, pkg, 'PKG')
309 write_if_exists(f, pkg, 'ALLOW_EMPTY')
310 write_if_exists(f, pkg, 'FILES')
311 write_if_exists(f, pkg, 'pkg_postinst')
312 write_if_exists(f, pkg, 'pkg_postrm')
313 write_if_exists(f, pkg, 'pkg_preinst')
314 write_if_exists(f, pkg, 'pkg_prerm')
316 bb.build.exec_func("read_subpackage_metadata", d)
319 ldconfig_postinst_fragment() {
320 if [ x"$D" = "x" ]; then
325 python package_do_shlibs() {
326 import os, re, os.path
328 exclude_shlibs = bb.data.getVar('EXCLUDE_FROM_SHLIBS', d, 0)
330 bb.note("not generating shlibs")
333 lib_re = re.compile("^lib.*\.so")
334 libdir_re = re.compile(".*/lib$")
336 packages = bb.data.getVar('PACKAGES', d, 1)
338 bb.debug(1, "no packages to build; not calculating shlibs")
341 workdir = bb.data.getVar('WORKDIR', d, 1)
343 bb.error("WORKDIR not defined")
346 staging = bb.data.getVar('STAGING_DIR', d, 1)
348 bb.error("STAGING_DIR not defined")
351 ver = bb.data.getVar('PV', d, 1)
353 bb.error("PV not defined")
356 target_sys = bb.data.getVar('TARGET_SYS', d, 1)
358 bb.error("TARGET_SYS not defined")
361 shlibs_dir = os.path.join(staging, target_sys, "shlibs")
362 old_shlibs_dir = os.path.join(staging, "shlibs")
363 bb.mkdirhier(shlibs_dir)
366 for pkg in packages.split():
367 needs_ldconfig = False
368 bb.debug(2, "calculating shlib provides for %s" % pkg)
370 pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
376 top = os.path.join(workdir, "install", pkg)
377 for root, dirs, files in os.walk(top):
380 path = os.path.join(root, file)
381 if os.access(path, os.X_OK) or lib_re.match(file):
382 cmd = (bb.data.getVar('BUILD_PREFIX', d, 1) or "") + "objdump -p " + path + " 2>/dev/null"
384 lines = fd.readlines()
387 m = re.match("\s+NEEDED\s+([^\s]*)", l)
389 needed[pkg].append(m.group(1))
390 m = re.match("\s+SONAME\s+([^\s]*)", l)
391 if m and not m.group(1) in sonames:
392 sonames.append(m.group(1))
393 if m and libdir_re.match(root):
394 needs_ldconfig = True
395 shlibs_file = os.path.join(shlibs_dir, pkgname + ".list")
396 if os.path.exists(shlibs_file):
397 os.remove(shlibs_file)
398 shver_file = os.path.join(shlibs_dir, pkgname + ".ver")
399 if os.path.exists(shver_file):
400 os.remove(shver_file)
402 fd = open(shlibs_file, 'w')
406 fd = open(shver_file, 'w')
410 bb.debug(1, 'adding ldconfig call to postinst for %s' % pkg)
411 postinst = bb.data.getVar('pkg_postinst_%s' % pkg, d, 1) or bb.data.getVar('pkg_postinst', d, 1)
413 postinst = '#!/bin/sh\n'
414 postinst += bb.data.getVar('ldconfig_postinst_fragment', d, 1)
415 bb.data.setVar('pkg_postinst_%s' % pkg, postinst, d)
418 list_re = re.compile('^(.*)\.list$')
419 for dir in [old_shlibs_dir, shlibs_dir]:
420 if not os.path.exists(dir):
422 for file in os.listdir(dir):
423 m = list_re.match(file)
426 fd = open(os.path.join(dir, file))
427 lines = fd.readlines()
429 ver_file = os.path.join(dir, dep_pkg + '.ver')
431 if os.path.exists(ver_file):
433 lib_ver = fd.readline().rstrip()
436 shlib_provider[l.rstrip()] = (dep_pkg, lib_ver)
439 for pkg in packages.split():
440 bb.debug(2, "calculating shlib requirements for %s" % pkg)
442 p_pkg = bb.data.getVar("PKG_%s" % pkg, d, 1) or pkg
445 for n in needed[pkg]:
446 if n in shlib_provider.keys():
447 (dep_pkg, ver_needed) = shlib_provider[n]
453 dep = "%s (>= %s)" % (dep_pkg, ver_needed)
459 bb.note("Couldn't find shared library provider for %s" % n)
462 deps_file = os.path.join(workdir, "install", pkg + ".shlibdeps")
463 if os.path.exists(deps_file):
466 fd = open(deps_file, 'w')
472 python package_do_pkgconfig () {
475 packages = bb.data.getVar('PACKAGES', d, 1)
477 bb.debug(1, "no packages to build; not calculating pkgconfig dependencies")
480 workdir = bb.data.getVar('WORKDIR', d, 1)
482 bb.error("WORKDIR not defined")
485 staging = bb.data.getVar('STAGING_DIR', d, 1)
487 bb.error("STAGING_DIR not defined")
490 target_sys = bb.data.getVar('TARGET_SYS', d, 1)
492 bb.error("TARGET_SYS not defined")
495 shlibs_dir = os.path.join(staging, target_sys, "shlibs")
496 old_shlibs_dir = os.path.join(staging, "shlibs")
497 bb.mkdirhier(shlibs_dir)
499 pc_re = re.compile('(.*)\.pc$')
500 var_re = re.compile('(.*)=(.*)')
501 field_re = re.compile('(.*): (.*)')
503 pkgconfig_provided = {}
504 pkgconfig_needed = {}
505 for pkg in packages.split():
506 pkgconfig_provided[pkg] = []
507 pkgconfig_needed[pkg] = []
508 top = os.path.join(workdir, "install", pkg)
509 for root, dirs, files in os.walk(top):
511 m = pc_re.match(file)
515 pkgconfig_provided[pkg].append(name)
516 path = os.path.join(root, file)
517 if not os.access(path, os.R_OK):
520 lines = f.readlines()
527 bb.data.setVar(name, bb.data.expand(val, pd), pd)
529 m = field_re.match(l)
532 exp = bb.data.expand(m.group(2), pd)
533 if hdr == 'Requires':
534 pkgconfig_needed[pkg] += exp.replace(',', ' ').split()
536 for pkg in packages.split():
537 ppkg = bb.data.getVar("PKG_" + pkg, d, 1) or pkg
538 pkgs_file = os.path.join(shlibs_dir, ppkg + ".pclist")
539 if os.path.exists(pkgs_file):
541 if pkgconfig_provided[pkg] != []:
542 f = open(pkgs_file, 'w')
543 for p in pkgconfig_provided[pkg]:
547 for dir in [old_shlibs_dir, shlibs_dir]:
548 if not os.path.exists(dir):
550 for file in os.listdir(dir):
551 m = re.match('^(.*)\.pclist$', file)
554 fd = open(os.path.join(dir, file))
555 lines = fd.readlines()
557 pkgconfig_provided[pkg] = []
559 pkgconfig_provided[pkg].append(l.rstrip())
561 for pkg in packages.split():
563 for n in pkgconfig_needed[pkg]:
565 for k in pkgconfig_provided.keys():
566 if n in pkgconfig_provided[k]:
567 if k != pkg and not (k in deps):
571 bb.note("couldn't find pkgconfig module '%s' in any package" % n)
572 deps_file = os.path.join(workdir, "install", pkg + ".pcdeps")
573 if os.path.exists(deps_file):
576 fd = open(deps_file, 'w')
582 python package_do_split_locales() {
585 if (bb.data.getVar('PACKAGE_NO_LOCALE', d, 1) == '1'):
586 bb.debug(1, "package requested not splitting locales")
589 packages = (bb.data.getVar('PACKAGES', d, 1) or "").split()
591 bb.debug(1, "no packages to build; not splitting locales")
594 datadir = bb.data.getVar('datadir', d, 1)
596 bb.note("datadir not defined")
599 dvar = bb.data.getVar('D', d, 1)
601 bb.error("D not defined")
604 pn = bb.data.getVar('PN', d, 1)
606 bb.error("PN not defined")
609 if pn + '-locale' in packages:
610 packages.remove(pn + '-locale')
612 localedir = os.path.join(dvar + datadir, 'locale')
614 if not os.path.isdir(localedir):
615 bb.debug(1, "No locale files in this package")
618 locales = os.listdir(localedir)
620 mainpkg = packages[0]
623 ln = legitimize_package_name(l)
624 pkg = pn + '-locale-' + ln
626 bb.data.setVar('FILES_' + pkg, os.path.join(datadir, 'locale', l), d)
627 bb.data.setVar('RDEPENDS_' + pkg, '${PKG_%s} virtual-locale-%s' % (mainpkg, ln), d)
628 bb.data.setVar('RPROVIDES_' + pkg, '%s-locale %s-translation' % (pn, ln), d)
629 bb.data.setVar('DESCRIPTION_' + pkg, '%s translation for %s' % (l, pn), d)
631 bb.data.setVar('PACKAGES', ' '.join(packages), d)
633 rdep = (bb.data.getVar('RDEPENDS_%s' % mainpkg, d, 1) or bb.data.getVar('RDEPENDS', d, 1) or "").split()
634 rdep.append('%s-locale*' % pn)
635 bb.data.setVar('RDEPENDS_%s' % mainpkg, ' '.join(rdep), d)
638 PACKAGEFUNCS = "do_install package_do_split_locales \
639 populate_packages package_do_shlibs \
640 package_do_pkgconfig read_shlibdeps"
641 python package_do_package () {
642 for f in (bb.data.getVar('PACKAGEFUNCS', d, 1) or '').split():
643 bb.build.exec_func(f, d)
646 do_package[dirs] = "${D}"
647 populate_packages[dirs] = "${D}"
648 EXPORT_FUNCTIONS do_package do_shlibs do_split_locales
649 addtask package before do_build after do_populate_staging