summaryrefslogtreecommitdiff
path: root/classes/package.oeclass
blob: f0d5f5a83287473e4a1dcb65ad4eb72731e1caff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
def legitimize_package_name(s):
	return s.lower().replace('_', '-').replace('@', '+').replace(',', '+')

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):
	import os, os.path, oe

	dvar = oe.data.getVar('D', d, 1)
	if not dvar:
		oe.error("D not defined")
		return

	packages = oe.data.getVar('PACKAGES', d, 1).split()
	if not packages:
		# nothing to do
		return

	if postinst:
		postinst = '#!/bin/sh\n' + postinst
	if postrm:
		postrm = '#!/bin/sh\n' + postrm
	if not recursive:
		objs = os.listdir(dvar + root)
	else:
		objs = []
		for walkroot, dirs, files in os.walk(dvar + root):
			for file in files:
				relpath = os.path.join(walkroot, file).replace(dvar + root + '/', '', 1)
				if relpath:
					objs.append(relpath)

	if extra_depends == None:
		extra_depends = oe.data.getVar('PKG_' + packages[0], d, 1) or packages[0]

	for o in objs:
		import re, stat
		m = re.match(file_regex, os.path.basename(o))
		if not m:
			continue
		f = os.path.join(dvar + root, o)
		mode = os.lstat(f).st_mode
		if not (stat.S_ISREG(mode) or (allow_dirs and stat.S_ISDIR(mode))):
			continue
		on = legitimize_package_name(m.group(1))
		pkg = output_pattern % on
		if not pkg in packages:
			packages.append(pkg)
			if aux_files_pattern:
				oe.data.setVar('FILES_' + pkg, "%s %s" % (os.path.join(root, o), aux_files_pattern % on), d)
			else:
				oe.data.setVar('FILES_' + pkg, os.path.join(root, o), d)
			if extra_depends != '':
				oe.data.setVar('RDEPENDS_' + pkg, extra_depends, d)
			oe.data.setVar('DESCRIPTION_' + pkg, description % on, d)
			if postinst:
				oe.data.setVar('pkg_postinst_' + pkg, postinst, d)
			if postrm:
				oe.data.setVar('pkg_postrm_' + pkg, postrm, d)
		else:
			oldfiles = oe.data.getVar('FILES_' + pkg, d, 1)
			if not oldfiles:
				oe.fatal("Package '%s' exists but has no files" % pkg)
			oe.data.setVar('FILES_' + pkg, oldfiles + " " + os.path.join(root, o), d)
		if callable(hook):
			hook(f, pkg, file_regex, output_pattern, m.group(1))

	oe.data.setVar('PACKAGES', ' '.join(packages), d)

def package_frob_arch(d):
	import oe, os
	machine = oe.data.getVar('MACHINE', d, 1)
	if not machine:
		# can't specialise if MACHINE not defined
		return
	old_arch = oe.data.getVar('PACKAGE_ARCH', d, 1)
	if (old_arch == machine):
		# Nothing to do
		return
	paths = []
	for p in [ "${FILE_DIRNAME}/${PF}", "${FILE_DIRNAME}/${P}", "${FILE_DIRNAME}/${PN}", "${FILE_DIRNAME}/files", "${FILE_DIRNAME}" ]:
		paths.append(oe.data.expand(os.path.join(p, machine), d))
	for s in oe.data.getVar('SRC_URI', d, 1).split():
		local = oe.data.expand(oe.fetch.localpath(s, d), d)
		for mp in paths:
			if local.startswith(mp):
				oe.note("overriding PACKAGE_ARCH from %s to %s" % (old_arch, machine))
				oe.data.setVar('PACKAGE_ARCH', machine, d)
				return

python populate_packages () {
	import glob, copy, stat, errno, re

	workdir = oe.data.getVar('WORKDIR', d, 1)
	if not workdir:
		oe.error("WORKDIR not defined, unable to package")
		return

	import os # path manipulations
	outdir = oe.data.getVar('DEPLOY_DIR', d, 1)
	if not outdir:
		oe.error("DEPLOY_DIR not defined, unable to package")
		return
	oe.mkdirhier(outdir)

	dvar = oe.data.getVar('D', d, 1)
	if not dvar:
		oe.error("D not defined, unable to package")
		return
	oe.mkdirhier(dvar)

	packages = oe.data.getVar('PACKAGES', d, 1)
	if not packages:
		oe.debug(1, "PACKAGES not defined, nothing to package")
		return

	pn = oe.data.getVar('PN', d, 1)
	if not pn:
		oe.error("PN not defined")
		return

	package_frob_arch(d)

	os.chdir(dvar)

	def isexec(path):
		try:
			s = os.stat(path)
		except (os.error, AttributeError):
			return 0
		return (s[stat.ST_MODE] & stat.S_IEXEC)

	for pkg in packages.split():
		localdata = copy.deepcopy(d)
		root = os.path.join(workdir, "install", pkg)

		os.system('rm -rf %s' % root)

		oe.data.setVar('ROOT', '', localdata)
		oe.data.setVar('ROOT_%s' % pkg, root, localdata)
		pkgname = oe.data.getVar('PKG_%s' % pkg, localdata, 1)
		if not pkgname:
			pkgname = pkg
		oe.data.setVar('PKG', pkgname, localdata)

		overrides = oe.data.getVar('OVERRIDES', localdata, 1)
		if not overrides:
			raise oe.build.FuncFailed('OVERRIDES not defined')
		oe.data.setVar('OVERRIDES', overrides+':'+pkg, localdata)

		oe.data.update_data(localdata)

		root = oe.data.getVar('ROOT', localdata, 1)
		oe.mkdirhier(root)
		filesvar = oe.data.getVar('FILES', localdata, 1) or ""
		files = filesvar.split()
		stripfunc = ""
		for file in files:
			if os.path.isabs(file):
				file = '.' + file
			if not os.path.islink(file):
				if os.path.isdir(file):
					newfiles =  [ os.path.join(file,x) for x in os.listdir(file) ]
					if newfiles:
						files += newfiles
						continue
			globbed = glob.glob(file)
			if globbed:
				if [ file ] != globbed:
					files += globbed
					continue
			if (not os.path.islink(file)) and (not os.path.exists(file)):
				continue
			fpath = os.path.join(root,file)
			dpath = os.path.dirname(fpath)
			oe.mkdirhier(dpath)
			if (oe.data.getVar('INHIBIT_PACKAGE_STRIP', d, 1) != '1') and not os.path.islink(file) and isexec(file):
				stripfunc += "${STRIP} %s || : ;\n" % fpath
			ret = oe.movefile(file,fpath)
			if ret is None or ret == 0:
				raise oe.build.FuncFailed("File population failed")
		if not stripfunc == "":
			from oe import build
			# strip
			oe.data.setVar('RUNSTRIP', '%s\nreturn 0' % stripfunc, localdata)
			oe.data.setVarFlag('RUNSTRIP', 'func', 1, localdata)
			oe.build.exec_func('RUNSTRIP', localdata)
		del localdata
	os.chdir(workdir)

	unshipped = []
	for root, dirs, files in os.walk(dvar):
		for f in files:
			path = os.path.join(root[len(dvar):], f)
			unshipped.append(path)

	if unshipped != []:
		oe.note("the following files were installed but not shipped in any package:")
		for f in unshipped:
			oe.note("  " + f)

	oe.build.exec_func("package_name_hook", d)

	dangling_links = {}
	pkg_files = {}
	for pkg in packages.split():
		dangling_links[pkg] = []
		pkg_files[pkg] = []
		inst_root = os.path.join(workdir, "install", pkg)
		for root, dirs, files in os.walk(inst_root):
			for f in files:
				path = os.path.join(root, f)
				rpath = path[len(inst_root):]
				pkg_files[pkg].append(rpath)
				try:
					s = os.stat(path)
				except OSError, (err, strerror):
					if err != errno.ENOENT:
						raise
					target = os.readlink(path)
					if target[0] != '/':
						target = os.path.join(root[len(inst_root):], target)
					dangling_links[pkg].append(os.path.normpath(target))

	for pkg in packages.split():
		rdepends = explode_deps(oe.data.getVar('RDEPENDS_' + pkg, d, 1) or oe.data.getVar('RDEPENDS', d, 1) or "")
		for l in dangling_links[pkg]:
			found = False
			oe.debug(1, "%s contains dangling link %s" % (pkg, l))
			for p in packages.split():
				for f in pkg_files[p]:
					if f == l:
						found = True
						oe.debug(1, "target found in %s" % p)
						dp = oe.data.getVar('PKG_' + p, d, 1) or p
						if not dp in rdepends:
							rdepends.append(dp)
						break
			if found == False:
				oe.note("%s contains dangling symlink to %s" % (pkg, l))
		oe.data.setVar('RDEPENDS_' + pkg, " " + " ".join(rdepends), d)

	def write_if_exists(f, pkg, var):
		def encode(str):
			import codecs
			c = codecs.getencoder("string_escape")
			return c(str)[0]

		val = oe.data.getVar('%s_%s' % (var, pkg), d, 1)
		if val:
			f.write('%s_%s: %s\n' % (var, pkg, encode(val)))

	data_file = os.path.join(workdir, "install", pn + ".package")
	f = open(data_file, 'w')
	f.write("PACKAGES: %s\n" % packages)
	for pkg in packages.split():
		write_if_exists(f, pkg, 'DESCRIPTION')
		write_if_exists(f, pkg, 'RDEPENDS')
		write_if_exists(f, pkg, 'RPROVIDES')
		write_if_exists(f, pkg, 'PKG')
		write_if_exists(f, pkg, 'ALLOW_EMPTY')
		write_if_exists(f, pkg, 'FILES')
		write_if_exists(f, pkg, 'pkg_postinst')
		write_if_exists(f, pkg, 'pkg_postrm')
	f.close()
	oe.build.exec_func("read_subpackage_metadata", d)
}

ldconfig_postinst_fragment() {
if [ x"$D" = "x" ]; then
	ldconfig
fi
}

python package_do_shlibs() {
	import os, re, os.path

	lib_re = re.compile("^lib.*\.so")
	libdir_re = re.compile(".*/lib$")

	packages = oe.data.getVar('PACKAGES', d, 1)
	if not packages:
		oe.debug(1, "no packages to build; not calculating shlibs")
		return

	workdir = oe.data.getVar('WORKDIR', d, 1)
	if not workdir:
		oe.error("WORKDIR not defined")
		return

	staging = oe.data.getVar('STAGING_DIR', d, 1)
	if not staging:
		oe.error("STAGING_DIR not defined")
		return

	ver = oe.data.getVar('PV', d, 1)
	if not ver:
		oe.error("PV not defined")
		return

	target_sys = oe.data.getVar('TARGET_SYS', d, 1)
	if not target_sys:
		oe.error("TARGET_SYS not defined")
		return

	shlibs_dir = os.path.join(staging, target_sys, "shlibs")
	old_shlibs_dir = os.path.join(staging, "shlibs")
	oe.mkdirhier(shlibs_dir)

	needed = {}
	for pkg in packages.split():
		needs_ldconfig = False
		oe.debug(2, "calculating shlib provides for %s" % pkg)

		pkgname = oe.data.getVar('PKG_%s' % pkg, d, 1)
		if not pkgname:
			pkgname = pkg

		needed[pkg] = []
		sonames = list()
		top = os.path.join(workdir, "install", pkg)
		for root, dirs, files in os.walk(top):
			for file in files:
				soname = None
				path = os.path.join(root, file)
				if os.access(path, os.X_OK) or lib_re.match(file):
					cmd = (oe.data.getVar('BUILD_PREFIX', d, 1) or "") + "objdump -p " + path + " 2>/dev/null"
					fd = os.popen(cmd)
					lines = fd.readlines()
					fd.close()
					for l in lines:
						m = re.match("\s+NEEDED\s+([^\s]*)", l)
						if m:
							needed[pkg].append(m.group(1))
						m = re.match("\s+SONAME\s+([^\s]*)", l)
						if m and not m.group(1) in sonames:
							sonames.append(m.group(1))
						if m and libdir_re.match(root):
							needs_ldconfig = True
		shlibs_file = os.path.join(shlibs_dir, pkgname + ".list")
		if os.path.exists(shlibs_file):
			os.remove(shlibs_file)
		shver_file = os.path.join(shlibs_dir, pkgname + ".ver")
		if os.path.exists(shver_file):
			os.remove(shver_file)
		if len(sonames):
			fd = open(shlibs_file, 'w')
			for s in sonames:
				fd.write(s + '\n')
			fd.close()
			fd = open(shver_file, 'w')
			fd.write(ver + '\n')
			fd.close()
		if needs_ldconfig:
			oe.note('adding ldconfig call to postinst for %s' % pkg)
			postinst = oe.data.getVar('pkg_postinst_%s' % pkg, d, 1) or oe.data.getVar('pkg_postinst', d, 1)
			if not postinst:
				postinst = '#!/bin/sh\n'
			postinst += oe.data.getVar('ldconfig_postinst_fragment', d, 1)
			oe.data.setVar('pkg_postinst_%s' % pkg, postinst, d)

	shlib_provider = {}
	list_re = re.compile('^(.*)\.list$')
	for dir in [shlibs_dir, old_shlibs_dir]: 
		if not os.path.exists(dir):
			continue
		for file in os.listdir(dir):
			m = list_re.match(file)
			if m:
				dep_pkg = m.group(1)
				fd = open(os.path.join(dir, file))
				lines = fd.readlines()
				fd.close()
				ver_file = os.path.join(dir, dep_pkg + '.ver')
				lib_ver = None
				if os.path.exists(ver_file):
					fd = open(ver_file)
					lib_ver = fd.readline().rstrip()
					fd.close()
				for l in lines:
					shlib_provider[l.rstrip()] = (dep_pkg, lib_ver)


	for pkg in packages.split():
		oe.debug(2, "calculating shlib requirements for %s" % pkg)

		deps = list()
		for n in needed[pkg]:
			if n in shlib_provider.keys():
				(dep_pkg, ver_needed) = shlib_provider[n]

				if ver_needed:
					dep = "%s (>= %s)" % (dep_pkg, ver_needed)
				else:
					dep = dep_pkg
				if not dep in deps:
					deps.append(dep)
			else:
				oe.note("Couldn't find shared library provider for %s" % n)


		deps_file = os.path.join(workdir, "install", pkg + ".shlibdeps")
		if os.path.exists(deps_file):
			os.remove(deps_file)
		if len(deps):
			fd = open(deps_file, 'w')
			for dep in deps:
				fd.write(dep + '\n')
			fd.close()
}

python package_do_pkgconfig () {
	import re, os

	packages = oe.data.getVar('PACKAGES', d, 1)
	if not packages:
		oe.debug(1, "no packages to build; not calculating pkgconfig dependencies")
		return

	workdir = oe.data.getVar('WORKDIR', d, 1)
	if not workdir:
		oe.error("WORKDIR not defined")
		return

	staging = oe.data.getVar('STAGING_DIR', d, 1)
	if not staging:
		oe.error("STAGING_DIR not defined")
		return

	pc_re = re.compile('(.*)\.pc$')
	var_re = re.compile('(.*)=(.*)')
	field_re = re.compile('(.*): (.*)')

	pkgconfig_provided = {}
	pkgconfig_needed = {}
	for pkg in packages.split():
		pkgconfig_provided[pkg] = []
		pkgconfig_needed[pkg] = []
		top = os.path.join(workdir, "install", pkg)
		for root, dirs, files in os.walk(top):
			for file in files:
				m = pc_re.match(file)
				if m:
					pd = {}
					name = m.group(1)
					pkgconfig_provided[pkg].append(name)
					path = os.path.join(root, file)
					if not os.access(path, os.R_OK):
						continue
					f = open(path, 'r')
					lines = f.readlines()
					f.close()
					for l in lines:
						m = var_re.match(l)
						if m:
							name = m.group(1)
							val = m.group(2)
							oe.data.setVar(name, oe.data.expand(val, pd), pd)
							continue
						m = field_re.match(l)
						if m:
							hdr = m.group(1)
							exp = oe.data.expand(m.group(2), pd)
							if hdr == 'Requires':
								pkgconfig_needed[pkg] += exp.replace(',', ' ').split()

	shlibs_dir = os.path.join(staging, "shlibs")
	oe.mkdirhier(shlibs_dir)

	for pkg in packages.split():
		pkgs_file = os.path.join(shlibs_dir, pkg + ".pclist")
		if os.path.exists(pkgs_file):
			os.remove(pkgs_file)
		if pkgconfig_provided[pkg] != []:
			f = open(pkgs_file, 'w')
			for p in pkgconfig_provided[pkg]:
				f.write('%s\n' % p)
			f.close()

	for file in os.listdir(shlibs_dir):
		m = re.match('^(.*)\.pclist$', file)
		if m:
			pkg = m.group(1)
			fd = open(os.path.join(shlibs_dir, file))
			lines = fd.readlines()
			fd.close()
			pkgconfig_provided[pkg] = []
			for l in lines:
				pkgconfig_provided[pkg].append(l.rstrip())

	for pkg in packages.split():
		deps = []
		for n in pkgconfig_needed[pkg]:
			found = False
			for k in pkgconfig_provided.keys():
				if n in pkgconfig_provided[k]:
					if k != pkg and not (k in deps):
						deps.append(k)
					found = True
			if found == False:
				oe.note("couldn't find pkgconfig module '%s' in any package" % n)
		deps_file = os.path.join(workdir, "install", pkg + ".pcdeps")
		if os.path.exists(deps_file):
			os.remove(deps_file)
		if len(deps):
			fd = open(deps_file, 'w')
			for dep in deps:
				fd.write(dep + '\n')
			fd.close()
}

python package_do_split_locales() {
	import os

	if (oe.data.getVar('PACKAGE_NO_LOCALE', d, 1) == '1'):
		oe.debug(1, "package requested not splitting locales")
		return

	packages = (oe.data.getVar('PACKAGES', d, 1) or "").split()
	if not packages:
		oe.debug(1, "no packages to build; not splitting locales")
		return

	datadir = oe.data.getVar('datadir', d, 1)
	if not datadir:
		oe.note("datadir not defined")
		return

	dvar = oe.data.getVar('D', d, 1)
	if not dvar:
		oe.error("D not defined")
		return

	pn = oe.data.getVar('PN', d, 1)
	if not pn:
		oe.error("PN not defined")
		return

	if pn + '-locale' in packages:
		packages.remove(pn + '-locale')

	localedir = os.path.join(dvar + datadir, 'locale')

	if not os.path.isdir(localedir):
		oe.note("No locale files in this package")
		return

	locales = os.listdir(localedir)

	mainpkg = packages[0]
	mainpkg_n = oe.data.getVar('PKG_%s' % mainpkg, d, 1) or mainpkg

	for l in locales:
		ln = legitimize_package_name(l)
		pkg = pn + '-locale-' + ln
		packages.append(pkg)
		oe.data.setVar('FILES_' + pkg, os.path.join(datadir, 'locale', l), d)
		oe.data.setVar('RDEPENDS_' + pkg, '%s virtual-locale-%s' % (mainpkg_n, ln), d)
		oe.data.setVar('RPROVIDES_' + pkg, '%s-locale %s-translation' % (pn, ln), d)
		oe.data.setVar('DESCRIPTION_' + pkg, '%s translation for %s' % (l, pn), d)

	oe.data.setVar('PACKAGES', ' '.join(packages), d)

	rrec = (oe.data.getVar('RRECOMMENDS_%s' % mainpkg, d, 1) or oe.data.getVar('RRECOMMENDS', d, 1) or "").split()
	rrec.append('%s-locale*' % pn)
	oe.data.setVar('RRECOMMENDS_%s' % mainpkg, ' '.join(rrec), d)
}

python package_do_package () {
	oe.build.exec_func('do_install', d)
	oe.build.exec_func('package_do_split_locales', d)
	oe.build.exec_func('populate_packages', d)
	oe.build.exec_func('package_do_shlibs', d)
	oe.build.exec_func('package_do_pkgconfig', d)
	oe.build.exec_func('read_shlibdeps', d)
}

do_package[dirs] = "${D}"
populate_packages[dirs] = "${D}"
EXPORT_FUNCTIONS do_package do_shlibs do_split_locales
addtask package before do_build after do_populate_staging