summaryrefslogtreecommitdiff
path: root/classes/package_rpm.bbclass
blob: ab09bb24d3805fd1825a96096426d9782961621b (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
inherit package

RPMBUILD="rpmbuild --short-circuit ${RPMOPTS}"
IMAGE_PKGTYPE ?= "rpm"

RPMBUILDPATH="${WORKDIR}/rpm"

RPMOPTS="--rcfile=${WORKDIR}/rpmrc"
RPMOPTS="--rcfile=${WORKDIR}/rpmrc --target ${TARGET_SYS}"
RPM="rpm ${RPMOPTS}"

python write_specfile() {
	version = bb.data.getVar('PV', d, 1)
	version = version.replace('-', '+')
	bb.data.setVar('RPMPV', version, d)

	out_vartranslate = {
		"PKG": "Name",
		"RPMPV": "Version",
		"DESCRIPTION": "%description",
		"ROOT": "BuildRoot",
		"LICENSE": "License",
		"SECTION": "Group",
		"pkg_postinst": "%post",
		"pkg_preinst": "%pre",
	}

	root = bb.data.getVar('ROOT', d)

	# get %files
	filesvar = bb.data.expand(bb.data.getVar('FILES', d), d) or ""
	from glob import glob
	files = filesvar.split()
	todelete = []
	for file in files:
		if file[0] == '.':
			newfile = file[1:]
			files[files.index(file)] = newfile
			file = newfile
		else:
			newfile = file
		realfile = os.path.join(root, './'+file)
		if not glob(realfile):
			todelete.append(files[files.index(newfile)])
	for r in todelete:
		try:
			del files[files.index(r)]
		except ValueError:
			pass

	if not files and bb.data.getVar('ALLOW_EMPTY', d) != "1":
		bb.note("Not creating empty archive for %s" % (bb.data.expand('${PKG}-${PV}-${PR}${DISTRO_PR}', d, True)))
		return

	# output .spec using this metadata store
	try:
		from __builtin__ import file
		if not bb.data.getVar('OUTSPECFILE', d):
			raise OSError('eek!')
		specfile = file(bb.data.getVar('OUTSPECFILE', d), 'w')
	except OSError:
		raise bb.build.FuncFailed("unable to open spec file for writing.")

	fd = specfile
	for var in out_vartranslate.keys():
		if out_vartranslate[var][0] == "%":
			continue
		val = bb.data.getVar(var, d, 1)
		if val:
			fd.write("%s\t: %s\n" % (out_vartranslate[var], val))

	fd.write("AutoReqProv: no\n")

	def fix_dep_versions(varname):
		depends = bb.utils.explode_dep_versions(bb.data.getVar(varname, d, True) or "")
		newdeps = []
		for dep in depends:
			ver = depends[dep]
			if dep and ver:
				if '-' in ver:
					subd = read_subpkgdata_dict(dep, d)
					pv = subd['PV']
					reppv = pv.replace('-', '+')
					ver = ver.replace(pv, reppv)
				newdeps.append("%s (%s)" % (dep, ver))
			elif dep:
				newdeps.append(dep)
		bb.data.setVar(varname, " ".join(newdeps), d)

	fix_dep_versions('RDEPENDS')
	fix_dep_versions('RRECOMMENDS')

	bb.build.exec_func("mapping_rename_hook", d)

	def write_dep_field(varname, outstring):
		depends = bb.utils.explode_dep_versions(bb.data.getVar(varname, d, True) or "")
		for dep in depends:
			ver = depends[dep]
			if dep and ver:
				fd.write("%s: %s %s\n" % (outstring, dep, ver))
			elif dep:
				fd.write("%s: %s\n" % (outstring, dep))

	write_dep_field('RDEPENDS', 'Requires')
	write_dep_field('RRECOMMENDS', 'Recommends')
	write_dep_field('RPROVIDES', 'Provides')

	fd.write("Release\t: %s\n" % bb.data.expand('${PR}${DISTRO_PR}', d, True))
	fd.write("Summary\t: .\n")

	for var in out_vartranslate.keys():
		if out_vartranslate[var][0] != "%":
			continue
		val = bb.data.getVar(var, d)
		if val:
			fd.write(out_vartranslate[var] + "\n")
			fd.write(val + "\n\n")

	fd.write("%files\n")
	for file in files:
		if file[0] != '/':
			fd.write('/')
		fd.write("%s\n" % file)

	fd.close()

	# call out rpm -bb on the .spec, thereby creating an rpm

	bb.data.setVar('BUILDSPEC', "${RPMBUILD} -bb ${OUTSPECFILE}\n", d)
	bb.data.setVarFlag('BUILDSPEC', 'func', '1', d)
	bb.build.exec_func('BUILDSPEC', d)

	# move the rpm into the pkgoutdir
	rpm = bb.data.expand('${RPMBUILDPATH}/RPMS/${TARGET_ARCH}/${PKG}-${RPMPV}-${PR}${DISTRO_PR}.${TARGET_ARCH}.rpm', d)
	outrpm = bb.data.expand('${DEPLOY_DIR_RPM}/${PACKAGE_ARCH}/${PKG}-${RPMPV}-${PR}.${TARGET_ARCH}.rpm', d)
	bb.movefile(rpm, outrpm)
}

rpm_prep() {
	if [ ! -e ${WORKDIR}/rpmrc ]; then
		mkdir -p ${RPMBUILDPATH}/{SPECS,RPMS/{i386,i586,i686,noarch,ppc,mips,mipsel,arm},SRPMS,SOURCES,BUILD}
		echo 'macrofiles:${STAGING_DIR_NATIVE}/usr/lib/rpm/macros:${WORKDIR}/macros' > ${WORKDIR}/rpmrc
		echo '%_topdir ${RPMBUILDPATH}' > ${WORKDIR}/macros
		echo '%_repackage_dir ${WORKDIR}' >> ${WORKDIR}/macros
	fi
}

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

	outdir = bb.data.getVar('DEPLOY_DIR_RPM', d, 1)
	if not outdir:
		bb.error("DEPLOY_DIR_RPM not defined, unable to package")
		return
	bb.mkdirhier(outdir)

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

	if packages == []:
		bb.debug(1, "No packages; nothing to do")
		return

	# If "rpm" comes into overrides the presence of this function causes problems.
	# Since we don't need it, remove it for now - hacky.
	bb.data.delVar("do_package_write_rpm", d)

	for pkg in packages.split():
		localdata = bb.data.createCopy(d)
		pkgdest = bb.data.getVar('PKGDEST', d, 1)
		root = "%s/%s" % (pkgdest, pkg)

		lf = bb.utils.lockfile(root + ".lock")

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

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

		bb.data.update_data(localdata)

		basedir = os.path.join(os.path.dirname(root))
		pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
		bb.mkdirhier(pkgoutdir)
		bb.data.setVar('OUTSPECFILE', os.path.join(workdir, "%s.spec" % pkg), localdata)
		# Save the value of RPMBUILD expanded into the new dictonary so any 
		# changes in the compoents that make up workdir don't break packaging
		bb.data.setVar('RPMBUILD', bb.data.getVar("RPMBUILD", d, True), localdata)
		bb.data.setVar('RPMBUILDPATH', bb.data.getVar("RPMBUILDPATH", d, True), localdata)
		bb.build.exec_func('write_specfile', localdata)
		bb.utils.unlockfile(lf)
}

python () {
    if bb.data.getVar('PACKAGES', d, True) != '' and \
       not bb.data.inherits_class('native', d) and \
       not bb.data.inherits_class('cross', d):
        deps = (bb.data.getVarFlag('do_package_write_rpm', 'depends', d) or "").split()
        deps.append('rpm-native:do_populate_staging')
        deps.append('fakeroot-native:do_populate_staging')
        bb.data.setVarFlag('do_package_write_rpm', 'depends', " ".join(deps), d)
}


python do_package_write_rpm () {
	bb.build.exec_func("read_subpackage_metadata", d)
	bb.build.exec_func("rpm_prep", d)
	bb.build.exec_func("do_package_rpm", d)
}

do_package_write_rpm[dirs] = "${D}"
addtask package_write_rpm before do_package_write after do_package