1 def legitimize_package_name(s):
7 return ('\u%s' % cp).decode('unicode_escape').encode('utf-8')
9 # Handle unicode codepoints encoded as <U0123>, as in glibc locale files.
10 s = re.sub('<U([0-9A-Fa-f]{1,4})>', fixutf, s)
12 # Remaining package name validity fixes
13 return s.lower().replace('_', '-').replace('@', '+').replace(',', '+').replace('/', '-')
15 STAGING_PKGMAPS_DIR ?= "${STAGING_DIR}/pkgmaps"
17 def add_package_mapping (pkg, new_name, d):
22 c = codecs.getencoder("string_escape")
25 pmap_dir = bb.data.getVar('STAGING_PKGMAPS_DIR', d, 1)
27 bb.mkdirhier(pmap_dir)
29 data_file = os.path.join(pmap_dir, pkg)
31 f = open(data_file, 'w')
32 f.write("%s\n" % encode(new_name))
35 def get_package_mapping (pkg, d):
40 c = codecs.getdecoder("string_escape")
43 data_file = bb.data.expand("${STAGING_PKGMAPS_DIR}/%s" % pkg, d)
45 if os.access(data_file, os.R_OK):
46 f = file(data_file, 'r')
50 return decode(l).strip()
53 def runtime_mapping_rename (varname, d):
56 #bb.note("%s before: %s" % (varname, bb.data.getVar(varname, d, 1)))
59 for depend in explode_deps(bb.data.getVar(varname, d, 1) or ""):
60 # Have to be careful with any version component of the depend
61 split_depend = depend.split(' (')
62 new_depend = get_package_mapping(split_depend[0].strip(), d)
63 if len(split_depend) > 1:
64 new_depends.append("%s (%s" % (new_depend, split_depend[1]))
66 new_depends.append(new_depend)
68 bb.data.setVar(varname, " ".join(new_depends) or None, d)
70 #bb.note("%s after: %s" % (varname, bb.data.getVar(varname, d, 1)))
72 python package_mapping_rename_hook () {
73 runtime_mapping_rename("RDEPENDS", d)
74 runtime_mapping_rename("RRECOMMENDS", d)
75 runtime_mapping_rename("RSUGGESTS", d)
76 runtime_mapping_rename("RPROVIDES", d)
77 runtime_mapping_rename("RREPLACES", d)
78 runtime_mapping_rename("RCONFLICTS", d)
82 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):
83 import os, os.path, bb
85 dvar = bb.data.getVar('D', d, 1)
87 bb.error("D not defined")
90 packages = bb.data.getVar('PACKAGES', d, 1).split()
96 postinst = '#!/bin/sh\n' + postinst + '\n'
98 postrm = '#!/bin/sh\n' + postrm + '\n'
100 objs = os.listdir(dvar + root)
103 for walkroot, dirs, files in os.walk(dvar + root):
105 relpath = os.path.join(walkroot, file).replace(dvar + root + '/', '', 1)
109 if extra_depends == None:
110 extra_depends = bb.data.getVar('PKG_' + packages[1], d, 1) or packages[1]
115 m = re.match(file_regex, o)
117 m = re.match(file_regex, os.path.basename(o))
121 f = os.path.join(dvar + root, o)
122 mode = os.lstat(f).st_mode
123 if not (stat.S_ISREG(mode) or stat.S_ISLNK(mode) or (allow_dirs and stat.S_ISDIR(mode))):
125 on = legitimize_package_name(m.group(1))
126 pkg = output_pattern % on
127 if not pkg in packages:
129 packages = [pkg] + packages
132 the_files = [os.path.join(root, o)]
133 if aux_files_pattern:
134 if type(aux_files_pattern) is list:
135 for fp in aux_files_pattern:
136 the_files.append(fp % on)
138 the_files.append(aux_files_pattern % on)
139 if aux_files_pattern_verbatim:
140 if type(aux_files_pattern_verbatim) is list:
141 for fp in aux_files_pattern_verbatim:
142 the_files.append(fp % m.group(1))
144 the_files.append(aux_files_pattern_verbatim % m.group(1))
145 bb.data.setVar('FILES_' + pkg, " ".join(the_files), d)
146 if extra_depends != '':
147 the_depends = bb.data.getVar('RDEPENDS_' + pkg, d, 1)
149 the_depends = '%s %s' % (the_depends, extra_depends)
151 the_depends = extra_depends
152 bb.data.setVar('RDEPENDS_' + pkg, the_depends, d)
153 bb.data.setVar('DESCRIPTION_' + pkg, description % on, d)
155 bb.data.setVar('pkg_postinst_' + pkg, postinst, d)
157 bb.data.setVar('pkg_postrm_' + pkg, postrm, d)
159 oldfiles = bb.data.getVar('FILES_' + pkg, d, 1)
161 bb.fatal("Package '%s' exists but has no files" % pkg)
162 bb.data.setVar('FILES_' + pkg, oldfiles + " " + os.path.join(root, o), d)
164 hook(f, pkg, file_regex, output_pattern, m.group(1))
166 bb.data.setVar('PACKAGES', ' '.join(packages), d)
168 # Function to strip a single file, called from RUNSTRIP below
169 # A working 'file' (one which works on the target architecture)
170 # is necessary for this stuff to work.
171 PACKAGE_DEPENDS ?= "file-native"
172 DEPENDS_prepend =+ "${PACKAGE_DEPENDS} "
173 # file(1) output to match to consider a file an unstripped executable
174 FILE_UNSTRIPPED_MATCH ?= "not stripped"
175 #FIXME: this should be "" when any errors are gone!
176 IGNORE_STRIP_ERRORS ?= "1"
182 oewarn "file $1: failed (forced strip)" >&2
183 echo '${FILE_UNSTRIPPED_MATCH}'
185 } | grep -q '${FILE_UNSTRIPPED_MATCH}'
193 mkdir -p $(dirname "$1")/.debug
194 debugfile="$(dirname "$1")/.debug/$(basename "$1")"
195 '${OBJCOPY}' --only-keep-debug "$1" "$debugfile"
198 '${OBJCOPY}' --add-gnu-debuglink="$debugfile" "$1"
199 test -n "$ro" && chmod -w "$1"
202 oewarn "runstrip: ${STRIP} $1: strip failed" >&2
203 if [ x${IGNORE_STRIP_ERRORS} == x1 ]
205 #FIXME: remove this, it's for error detection
206 if file "$1" 2>/dev/null >&2
208 (oefatal "${STRIP} $1: command failed" >/dev/tty)
210 (oefatal "file $1: command failed" >/dev/tty)
216 oenote "runstrip: skip $1"
221 python populate_packages () {
222 import glob, stat, errno, re
224 workdir = bb.data.getVar('WORKDIR', d, 1)
226 bb.error("WORKDIR not defined, unable to package")
229 import os # path manipulations
230 outdir = bb.data.getVar('DEPLOY_DIR', d, 1)
232 bb.error("DEPLOY_DIR not defined, unable to package")
236 dvar = bb.data.getVar('D', d, 1)
238 bb.error("D not defined, unable to package")
242 packages = bb.data.getVar('PACKAGES', d, 1)
244 bb.debug(1, "PACKAGES not defined, nothing to package")
247 pn = bb.data.getVar('PN', d, 1)
249 bb.error("PN not defined")
257 except (os.error, AttributeError):
259 return (s[stat.ST_MODE] & stat.S_IEXEC)
261 # Sanity check PACKAGES for duplicates - should be moved to
262 # sanity.bbclass once we have the infrastucture
264 for pkg in packages.split():
265 if pkg in package_list:
266 bb.error("-------------------")
267 bb.error("%s is listed in PACKAGES mutliple times, this leads to packaging errors." % pkg)
268 bb.error("Please fix the metadata/report this as bug to OE bugtracker.")
269 bb.error("-------------------")
271 package_list.append(pkg)
273 if (bb.data.getVar('INHIBIT_PACKAGE_STRIP', d, 1) != '1'):
275 for root, dirs, files in os.walk(dvar):
277 file = os.path.join(root, f)
278 if not os.path.islink(file) and isexec(file):
279 stripfunc += "\trunstrip %s || st=1\n" % (file)
280 if not stripfunc == "":
282 localdata = bb.data.createCopy(d)
284 bb.data.setVar('RUNSTRIP', '\tlocal st\n\tst=0\n%s\treturn $st' % stripfunc, localdata)
285 bb.data.setVarFlag('RUNSTRIP', 'func', 1, localdata)
286 bb.build.exec_func('RUNSTRIP', localdata)
288 for pkg in package_list:
289 localdata = bb.data.createCopy(d)
290 root = os.path.join(workdir, "install", pkg)
292 os.system('rm -rf %s' % root)
294 bb.data.setVar('ROOT', '', localdata)
295 bb.data.setVar('ROOT_%s' % pkg, root, localdata)
296 pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
299 bb.data.setVar('PKG', pkgname, localdata)
301 overrides = bb.data.getVar('OVERRIDES', localdata, 1)
303 raise bb.build.FuncFailed('OVERRIDES not defined')
304 bb.data.setVar('OVERRIDES', overrides+':'+pkg, localdata)
306 bb.data.update_data(localdata)
308 root = bb.data.getVar('ROOT', localdata, 1)
310 filesvar = bb.data.getVar('FILES', localdata, 1) or ""
311 files = filesvar.split()
313 if os.path.isabs(file):
315 if not os.path.islink(file):
316 if os.path.isdir(file):
317 newfiles = [ os.path.join(file,x) for x in os.listdir(file) ]
321 globbed = glob.glob(file)
323 if [ file ] != globbed:
326 if (not os.path.islink(file)) and (not os.path.exists(file)):
328 if file.find('.pyo') != -1:
330 fpath = os.path.join(root,file)
331 dpath = os.path.dirname(fpath)
333 ret = bb.movefile(file,fpath)
334 if ret is None or ret == 0:
335 raise bb.build.FuncFailed("File population failed")
340 for root, dirs, files in os.walk(dvar):
342 path = os.path.join(root[len(dvar):], f)
343 unshipped.append(path)
346 bb.note("the following files were installed but not shipped in any package:")
350 bb.build.exec_func("package_name_hook", d)
352 for pkg in package_list:
353 pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
355 bb.data.setVar('PKG_%s' % pkg, pkg, d)
357 add_package_mapping(pkg, pkgname, d)
361 for pkg in package_list:
362 dangling_links[pkg] = []
364 inst_root = os.path.join(workdir, "install", pkg)
365 for root, dirs, files in os.walk(inst_root):
367 path = os.path.join(root, f)
368 rpath = path[len(inst_root):]
369 pkg_files[pkg].append(rpath)
372 except OSError, (err, strerror):
373 if err != errno.ENOENT:
375 target = os.readlink(path)
377 target = os.path.join(root[len(inst_root):], target)
378 dangling_links[pkg].append(os.path.normpath(target))
380 for pkg in package_list:
381 rdepends = explode_deps(bb.data.getVar('RDEPENDS_' + pkg, d, 1) or bb.data.getVar('RDEPENDS', d, 1) or "")
382 for l in dangling_links[pkg]:
384 bb.debug(1, "%s contains dangling link %s" % (pkg, l))
385 for p in package_list:
386 for f in pkg_files[p]:
389 bb.debug(1, "target found in %s" % p)
392 dp = bb.data.getVar('PKG_' + p, d, 1) or p
393 if not dp in rdepends:
397 bb.note("%s contains dangling symlink to %s" % (pkg, l))
398 bb.data.setVar('RDEPENDS_' + pkg, " " + " ".join(rdepends), d)
400 def write_if_exists(f, pkg, var):
403 c = codecs.getencoder("string_escape")
406 val = bb.data.getVar('%s_%s' % (var, pkg), d, 1)
408 f.write('%s_%s: %s\n' % (var, pkg, encode(val)))
410 data_file = os.path.join(workdir, "install", pn + ".package")
411 f = open(data_file, 'w')
412 f.write("PACKAGES: %s\n" % packages)
413 for pkg in package_list:
414 write_if_exists(f, pkg, 'DESCRIPTION')
415 write_if_exists(f, pkg, 'RDEPENDS')
416 write_if_exists(f, pkg, 'RPROVIDES')
417 write_if_exists(f, pkg, 'PKG')
418 write_if_exists(f, pkg, 'ALLOW_EMPTY')
419 write_if_exists(f, pkg, 'FILES')
420 write_if_exists(f, pkg, 'pkg_postinst')
421 write_if_exists(f, pkg, 'pkg_postrm')
422 write_if_exists(f, pkg, 'pkg_preinst')
423 write_if_exists(f, pkg, 'pkg_prerm')
425 bb.build.exec_func("read_subpackage_metadata", d)
428 ldconfig_postinst_fragment() {
429 if [ x"$D" = "x" ]; then
434 python package_do_shlibs() {
435 import os, re, os.path
437 exclude_shlibs = bb.data.getVar('EXCLUDE_FROM_SHLIBS', d, 0)
439 bb.note("not generating shlibs")
442 lib_re = re.compile("^lib.*\.so")
443 libdir_re = re.compile(".*/lib$")
445 packages = bb.data.getVar('PACKAGES', d, 1)
447 bb.debug(1, "no packages to build; not calculating shlibs")
450 workdir = bb.data.getVar('WORKDIR', d, 1)
452 bb.error("WORKDIR not defined")
455 staging = bb.data.getVar('STAGING_DIR', d, 1)
457 bb.error("STAGING_DIR not defined")
460 ver = bb.data.getVar('PV', d, 1)
462 bb.error("PV not defined")
465 target_sys = bb.data.getVar('TARGET_SYS', d, 1)
467 bb.error("TARGET_SYS not defined")
470 shlibs_dir = os.path.join(staging, target_sys, "shlibs")
471 old_shlibs_dir = os.path.join(staging, "shlibs")
472 bb.mkdirhier(shlibs_dir)
475 for pkg in packages.split():
476 needs_ldconfig = False
477 bb.debug(2, "calculating shlib provides for %s" % pkg)
479 pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
485 top = os.path.join(workdir, "install", pkg)
486 for root, dirs, files in os.walk(top):
489 path = os.path.join(root, file)
490 if os.access(path, os.X_OK) or lib_re.match(file):
491 cmd = (bb.data.getVar('BUILD_PREFIX', d, 1) or "") + "objdump -p " + path + " 2>/dev/null"
493 lines = fd.readlines()
496 m = re.match("\s+NEEDED\s+([^\s]*)", l)
498 needed[pkg].append(m.group(1))
499 m = re.match("\s+SONAME\s+([^\s]*)", l)
500 if m and not m.group(1) in sonames:
501 sonames.append(m.group(1))
502 if m and libdir_re.match(root):
503 needs_ldconfig = True
504 shlibs_file = os.path.join(shlibs_dir, pkgname + ".list")
505 if os.path.exists(shlibs_file):
506 os.remove(shlibs_file)
507 shver_file = os.path.join(shlibs_dir, pkgname + ".ver")
508 if os.path.exists(shver_file):
509 os.remove(shver_file)
511 fd = open(shlibs_file, 'w')
515 fd = open(shver_file, 'w')
519 bb.debug(1, 'adding ldconfig call to postinst for %s' % pkg)
520 postinst = bb.data.getVar('pkg_postinst_%s' % pkg, d, 1) or bb.data.getVar('pkg_postinst', d, 1)
522 postinst = '#!/bin/sh\n'
523 postinst += bb.data.getVar('ldconfig_postinst_fragment', d, 1)
524 bb.data.setVar('pkg_postinst_%s' % pkg, postinst, d)
527 list_re = re.compile('^(.*)\.list$')
528 for dir in [old_shlibs_dir, shlibs_dir]:
529 if not os.path.exists(dir):
531 for file in os.listdir(dir):
532 m = list_re.match(file)
535 fd = open(os.path.join(dir, file))
536 lines = fd.readlines()
538 ver_file = os.path.join(dir, dep_pkg + '.ver')
540 if os.path.exists(ver_file):
542 lib_ver = fd.readline().rstrip()
545 shlib_provider[l.rstrip()] = (dep_pkg, lib_ver)
548 for pkg in packages.split():
549 bb.debug(2, "calculating shlib requirements for %s" % pkg)
551 p_pkg = bb.data.getVar("PKG_%s" % pkg, d, 1) or pkg
554 for n in needed[pkg]:
555 if n in shlib_provider.keys():
556 (dep_pkg, ver_needed) = shlib_provider[n]
562 dep = "%s (>= %s)" % (dep_pkg, ver_needed)
568 bb.note("Couldn't find shared library provider for %s" % n)
571 deps_file = os.path.join(workdir, "install", pkg + ".shlibdeps")
572 if os.path.exists(deps_file):
575 fd = open(deps_file, 'w')
581 python package_do_pkgconfig () {
584 packages = bb.data.getVar('PACKAGES', d, 1)
586 bb.debug(1, "no packages to build; not calculating pkgconfig dependencies")
589 workdir = bb.data.getVar('WORKDIR', d, 1)
591 bb.error("WORKDIR not defined")
594 staging = bb.data.getVar('STAGING_DIR', d, 1)
596 bb.error("STAGING_DIR not defined")
599 target_sys = bb.data.getVar('TARGET_SYS', d, 1)
601 bb.error("TARGET_SYS not defined")
604 shlibs_dir = os.path.join(staging, target_sys, "shlibs")
605 old_shlibs_dir = os.path.join(staging, "shlibs")
606 bb.mkdirhier(shlibs_dir)
608 pc_re = re.compile('(.*)\.pc$')
609 var_re = re.compile('(.*)=(.*)')
610 field_re = re.compile('(.*): (.*)')
612 pkgconfig_provided = {}
613 pkgconfig_needed = {}
614 for pkg in packages.split():
615 pkgconfig_provided[pkg] = []
616 pkgconfig_needed[pkg] = []
617 top = os.path.join(workdir, "install", pkg)
618 for root, dirs, files in os.walk(top):
620 m = pc_re.match(file)
624 pkgconfig_provided[pkg].append(name)
625 path = os.path.join(root, file)
626 if not os.access(path, os.R_OK):
629 lines = f.readlines()
636 bb.data.setVar(name, bb.data.expand(val, pd), pd)
638 m = field_re.match(l)
641 exp = bb.data.expand(m.group(2), pd)
642 if hdr == 'Requires':
643 pkgconfig_needed[pkg] += exp.replace(',', ' ').split()
645 for pkg in packages.split():
646 ppkg = bb.data.getVar("PKG_" + pkg, d, 1) or pkg
647 pkgs_file = os.path.join(shlibs_dir, ppkg + ".pclist")
648 if os.path.exists(pkgs_file):
650 if pkgconfig_provided[pkg] != []:
651 f = open(pkgs_file, 'w')
652 for p in pkgconfig_provided[pkg]:
656 for dir in [old_shlibs_dir, shlibs_dir]:
657 if not os.path.exists(dir):
659 for file in os.listdir(dir):
660 m = re.match('^(.*)\.pclist$', file)
663 fd = open(os.path.join(dir, file))
664 lines = fd.readlines()
666 pkgconfig_provided[pkg] = []
668 pkgconfig_provided[pkg].append(l.rstrip())
670 for pkg in packages.split():
672 for n in pkgconfig_needed[pkg]:
674 for k in pkgconfig_provided.keys():
675 if n in pkgconfig_provided[k]:
676 if k != pkg and not (k in deps):
680 bb.note("couldn't find pkgconfig module '%s' in any package" % n)
681 deps_file = os.path.join(workdir, "install", pkg + ".pcdeps")
682 if os.path.exists(deps_file):
685 fd = open(deps_file, 'w')
691 python package_do_split_locales() {
694 if (bb.data.getVar('PACKAGE_NO_LOCALE', d, 1) == '1'):
695 bb.debug(1, "package requested not splitting locales")
698 packages = (bb.data.getVar('PACKAGES', d, 1) or "").split()
700 bb.debug(1, "no packages to build; not splitting locales")
703 datadir = bb.data.getVar('datadir', d, 1)
705 bb.note("datadir not defined")
708 dvar = bb.data.getVar('D', d, 1)
710 bb.error("D not defined")
713 pn = bb.data.getVar('PN', d, 1)
715 bb.error("PN not defined")
718 if pn + '-locale' in packages:
719 packages.remove(pn + '-locale')
721 localedir = os.path.join(dvar + datadir, 'locale')
723 if not os.path.isdir(localedir):
724 bb.debug(1, "No locale files in this package")
727 locales = os.listdir(localedir)
729 mainpkg = packages[0]
732 ln = legitimize_package_name(l)
733 pkg = pn + '-locale-' + ln
735 bb.data.setVar('FILES_' + pkg, os.path.join(datadir, 'locale', l), d)
736 bb.data.setVar('RDEPENDS_' + pkg, '${PKG_%s} virtual-locale-%s' % (mainpkg, ln), d)
737 bb.data.setVar('RPROVIDES_' + pkg, '%s-locale %s-translation' % (pn, ln), d)
738 bb.data.setVar('DESCRIPTION_' + pkg, '%s translation for %s' % (l, pn), d)
740 bb.data.setVar('PACKAGES', ' '.join(packages), d)
742 # we don't want to RDEPEND any package on created locales.
743 # rdep = (bb.data.getVar('RDEPENDS_%s' % mainpkg, d, 1) or bb.data.getVar('RDEPENDS', d, 1) or "").split()
744 # rdep.append('%s-locale*' % pn)
745 # bb.data.setVar('RDEPENDS_%s' % mainpkg, ' '.join(rdep), d)
748 PACKAGEFUNCS ?= " package_do_split_locales \
749 populate_packages package_do_shlibs \
750 package_do_pkgconfig read_shlibdeps"
751 python package_do_package () {
752 for f in (bb.data.getVar('PACKAGEFUNCS', d, 1) or '').split():
753 bb.build.exec_func(f, d)
756 do_package[dirs] = "${D}"
757 # shlibs requires any DEPENDS to have already packaged for the *.list files
758 do_package[deptask] = "do_package"
759 populate_packages[dirs] = "${D}"
760 EXPORT_FUNCTIONS do_package do_shlibs do_split_locales mapping_rename_hook
761 addtask package before do_build after do_install