summaryrefslogtreecommitdiffstats
path: root/src/opkbuild.sh
blob: 39b51039d6a3d6498b4460705720bb90afe9cc73 (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
#!@@SH@@
#
# opkbuild
# src/opkbuild
# Build opkg packages.
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. '@@LIBOPKBUILD@@/load.sm'

ob_use locale
ob_use output
ob_use package
ob_use metadata

export OPK_SOURCE=
export OPK_SOURCE_VERSION=
export OPK_SOURCE_VERSION_UPSTREAM=
export OPK_BINARY_VERSION=
export OPK_PACKAGES=
export OPK_BUILD_ARCH=
export OPK_BUILD_ARCH_CPU=
export OPK_BUILD_ARCH_KERNEL=
export OPK_BUILD_ARCH_LIBS=
export OPK_BUILD_PLAT=
export OPK_BUILD_ARCH_GNU=
export OPK_HOST_ARCH=
export OPK_HOST_ARCH_CPU=
export OPK_HOST_ARCH_KERNEL=
export OPK_HOST_ARCH_LIBS=
export OPK_HOST_PLAT=
export OPK_HOST_ARCH_GNU=

opt_build=
opt_target=
opt_host_arch=
opt_host_plat=
opt_check_build_deps=
opt_clean=
opt_uid0_cmd=

main()
{
	ob_set_text_domain 'opkbuild'

	get_options "${@}"
	shift $(($OPTIND - 1))
	if [ ${#} -ne 0 ]; then
		usage
		exit 1
	fi

	test_uid0_cmd

	setup_arch_plat

	make_work_area

	setup_package

	if [ "${opt_build}" = 'source' -o "${opt_build}" = 'full' ]; then
		build_source
	fi

	if [ "${opt_build}" != 'source' ]; then
		print_arch_stats
		"${opt_check_build_deps}" && '@@BINDIR@@/ob-checkbuilddeps'
		setup_build
		build
		clean
	fi
}

usage()
{
	printf "$(ob_get_msg 'usage')\n" 'opkbuild'
}

help()
{
	usage
	printf '%s\n' "$(ob_get_msg 'help')"
}

version()
{
	printf "$(ob_get_msg 'version')\n" \
		'opkbuild' '@@PACKAGE_NAME@@' '@@PACKAGE_VERSION@@'
}

get_options()
{
	# Parse and handle command-line options.
	while getopts 'bBASFT:a:P:DdCcr:hV' opt; do
		case "${opt}" in
			b)
				if [ -n "${opt_build}" ]; then
					ob_error "$(ob_get_msg 'bbasf_mutex')"
				fi
				opt_build='binary'
				;;
			B)
				if [ -n "${opt_build}" ]; then
					ob_error "$(ob_get_msg 'bbasf_mutex')"
				fi
				opt_build='binary-arch'
				;;
			A)
				if [ -n "${opt_build}" ]; then
					ob_error "$(ob_get_msg 'bbasf_mutex')"
				fi
				opt_build='binary-indep'
				;;
			S)
				if [ -n "${opt_build}" ]; then
					ob_error "$(ob_get_msg 'bbasf_mutex')"
				fi
				opt_build='source'
				;;
			F)
				if [ -n "${opt_build}" ]; then
					ob_error "$(ob_get_msg 'bbasf_mutex')"
				fi
				opt_build='full'
				;;
			T)
				opt_target="${OPTARG}"
				;;
			a)
				opt_host_arch="${OPTARG}"
				;;
			P)
				opt_host_plat="${OPTARG}"
				;;
			D)
				opt_check_build_deps='true'
				;;
			d)
				opt_check_build_deps='false'
				;;
			C)
				opt_clean='true'
				;;
			c)
				opt_clean='false'
				;;
			r)
				opt_uid0_cmd="${OPTARG}"
				;;
			h)
				help
				exit 0
				;;
			V)
				version
				exit 0
				;;
			?)
				ob_error "$(ob_get_msg 'bad_opt')" "${opt}"
				exit 1
				;;
		esac
	done

	# Set default option values.
	[ -z "${opt_build}" ] && opt_build='full'
	[ -z "${opt_check_build_deps}" ] && opt_check_build_deps='true'
	[ -z "${opt_uid0_cmd}" ] && opt_uid0_cmd='fakeroot'

	# Set cleaning behavior.
	if [ -n "${opt_target}" ]; then
		[ -z "${opt_clean}" ] && opt_clean='false'
	else
		[ -z "${opt_clean}" ] && opt_clean='true'
	fi
}

test_uid0_cmd()
{
	# Verify that the UID 0 command works.
	test_uid=$("${opt_uid0_cmd}" id -u)

	if [ ${?} -ne 0 ]; then
		ob_error "$(ob_get_msg 'uid0_cmd_not_found')" "${opt_uid0_cmd}"
	fi

	if [ ${test_uid} -ne 0 ]; then
		ob_error "$(ob_get_msg 'uid0_cmd_bad_uid')" "${opt_uid0_cmd}"
	fi
}

setup_arch_plat()
{
	ob_info "$(ob_get_msg 'setup_arch_plat')"

	OPK_BUILD_ARCH="$(ob_get_system_arch)"
	OPK_BUILD_PLAT="$(ob_get_system_plat)"

	if [ -n "${opt_host_arch}" ]; then
		OPK_HOST_ARCH="${opt_host_arch}"
	else
		OPK_HOST_ARCH="${OPK_BUILD_ARCH}"
	fi

	if [ -n "${opt_host_plat}" ]; then
		OPK_HOST_PLAT="${opt_host_plat}"
	else
		OPK_HOST_PLAT="${OPK_BUILD_PLAT}"
	fi

	IFS='-' read OPK_BUILD_ARCH_CPU OPK_BUILD_ARCH_KERNEL OPK_BUILD_ARCH_LIBS \
		<<EOF
${OPK_BUILD_ARCH}
EOF
	IFS='-' read OPK_HOST_ARCH_CPU OPK_HOST_ARCH_KERNEL OPK_HOST_ARCH_LIBS \
		<<EOF
${OPK_HOST_ARCH}
EOF

	{
		read OPK_BUILD_ARCH_GNU
		read OPK_BUILD_ARCH_KBUILD
	} <"$(ob_get_system_path 'archtab' "${OPK_BUILD_ARCH}")"
	{
		read OPK_HOST_ARCH_GNU
		read OPK_HOST_ARCH_KBUILD
	} <"$(ob_get_system_path 'archtab' "${OPK_HOST_ARCH}")"
}

make_work_area()
{
	ob_info "$(ob_get_msg 'make_work_area')"

	mkdir -p 'tmp'
	cd 'tmp'
}

setup_package()
{
	ob_info "$(ob_get_msg 'parse_package_metadata')"

	ob_init_package '..'
	ob_parse_package_metadata -c '.opkbuild.cache'
	
	OPK_SOURCE="$(ob_get_source_parameter 'Source')"
	OPK_SOURCE_VERSION="$(ob_get_source_parameter 'Version')"
	ob_parse_version -u 'OPK_SOURCE_VERSION_UPSTREAM' "${OPK_SOURCE_VERSION}"
	OPK_BINARY_VERSION="$(ob_get_source_parameter 'Version')"
}

build_source()
{
	ob_info "$(ob_get_msg 'build_source')"

	src="$(ob_get_source_parameter 'Source')"
	ver="$(ob_get_source_parameter 'Version')"
	src_pkg_data_base="src-${src}.data$(ob_get_system_path 'package-source' \
		"${src}" "${ver}")"

	"${opt_uid0_cmd}" mkdir -p \
		"${src_pkg_data_base}" || \
		ob_error "$(ob_get_msg 'cant_make_src_pkg_dir')"

	for file in ../*; do
		case "${file}" in
			../tmp)
				;;
			../*)
				"${opt_uid0_cmd}" cp -R "${file}" "${src_pkg_data_base}" || \
					ob_error "$(ob_get_msg 'cant_install_src_pkg_file')"
				;;
		esac
	done

	OB_DO_SOURCE='true' '@@BINDIR@@/ob-gencontrol'
	OB_DO_SOURCE='true' '@@BINDIR@@/ob-buildopk'

	rm -Rf "src-${src}.data" || ob_error "$(ob_get_msg 'cant_rm_src_pkg_data')"
}

print_arch_stats()
{
	ob_info "$(ob_get_msg 'build_arch_stat_header')"
	ob_info "$(ob_get_msg 'arch_stat_arch')" \
		"${OPK_BUILD_ARCH}" "${OPK_BUILD_ARCH_GNU}"
	ob_info "$(ob_get_msg 'arch_stat_plat')" "${OPK_BUILD_PLAT}"
	ob_info "$(ob_get_msg 'host_arch_stat_header')"
	ob_info "$(ob_get_msg 'arch_stat_arch')" \
		"${OPK_HOST_ARCH}" "${OPK_HOST_ARCH_GNU}"
	ob_info "$(ob_get_msg 'arch_stat_plat')" "${OPK_HOST_PLAT}"
}

setup_build()
{
	OPK_PACKAGES="$(ob_get_binary_packages -a "${OPK_HOST_ARCH}") $(
		ob_get_binary_packages -a 'all')"

	eval "$('@@BINDIR@@/ob-buildenv' | sed 's/^/export /')"

	'@@BINDIR@@/ob-unpacksource'
	'@@BINDIR@@/ob-applypatches'
	'@@BINDIR@@/ob-installplatconf'
}

build()
{
	case "${opt_target}" in
		'')
			../build build
			${opt_uid0_cmd} sh -s <<-EOF
				../build install
				'@@BINDIR@@/ob-installdocs'
				'@@BINDIR@@/ob-gencontrol'
				'@@BINDIR@@/ob-buildopk'
				EOF
			;;
		'install'|'install-'*)
			${opt_uid0_cmd} ../build "${opt_target}"
			;;
		*)
			../build "${opt_target}"
			;;
	esac

	: '@@BINDIR@@/ob-genchanges'
}

clean()
{
	if "${opt_clean}"; then
		cd ..
		rm -Rf 'tmp'
	fi
}

main "${@}"