blob: 865c350a725d2133892c497877235a362ec4f4b9 (
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
|
#!@@SHELL@@
#
# opkhelper
# 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/>.
. @@LIBDIR@@/opkhelper/messages
. @@LIBDIR@@/opkhelper/strings/en
. @@LIBDIR@@/opkhelper/changelog
# Environment variables:
export OH_SOURCE=
export OH_SOURCE_VERSION=
export OH_SOURCE_VERSION_UPSTREAM=
export OH_BINARY_VERSION=
export OH_BUILD_ARCH=
export OH_BUILD_ARCH_CPU=
export OH_BUILD_ARCH_KERNEL=
export OH_BUILD_ARCH_LIBS=
export OH_BUILD_PLATFORM=
export OH_BUILD_ARCH_GNU=
export OH_HOST_ARCH=
export OH_HOST_ARCH_CPU=
export OH_HOST_ARCH_KERNEL=
export OH_HOST_ARCH_LIBS=
export OH_HOST_PLATFORM=
export OH_HOST_ARCH_GNU=
export OH_TOOL_PREFIX=
export AR=
export AS=
export CC=
export CPP=
export CXXLD=
export NM=
export OBJCOPY=
export OBJDUMP=
export RANLIB=
export READELF=
export SIZE=
export STRINGS=
export STRIP=
# Constant global parameters:
SOURCE_VERSION_ID_RE='^'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'[0-9a-z.~]+'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'(\+sip[1-9][0-9]*)?'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'(-[1-9][0-9]*)?'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'(\+[a-z0-9]+-[1-9][0-9]*)?'
SOURCE_VERSION_ID_RE="${SOURCE_VERSION_ID_RE}"'$'
# Global parameters:
OPT_BUILD=
OPT_TARGET=
OPT_HOST_ARCH=
OPT_HOST_PLAT=
OPT_CHECK_BUILD_DEPS=
OPT_CLEAN=
OPT_UID0_CMD=
main()
{
get_options
test_uid0_cmd
find_sanity
oh_changelog_parse changelog setup_package
if [ "${OPT_BUILD}" = 'source' -o "${OPT_BUILD}" = 'full' ]; then
build_source
fi
if [ "${OPT_BUILD}" != 'source' ]; then
"${OPT_CHECK_BUILD_DEPS}" && oh-checkbuilddeps
setup_build
print_arch_stats
if [ -n "${OH_CV_STEP}" ]; then
"step_${OH_CV_STEP}"
else
make_work_area
fi
fi
}
get_options()
{
# Parse and handle command-line options.
while getopts 'bBASFT:a:P:Ddr:hV' opt; do
case "${opt}" in
b)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=binary
;;
B)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=binary-arch
;;
A)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=binary-indep
;;
S)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_mutex}"
fi
OPT_BUILD=source
;;
F)
if [ -n "${OPT_BUILD}" ]; then
oh_error "${oh_str_basf_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)
oh_help
exit 0
;;
V)
oh_version
exit 0
;;
?)
oh_error "${oh_str_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 default target and cleaning behavior.
if [ -n "${OPT_TARGET}" ]; then
[ -z "${OPT_CLEAN}" ] && OPT_CLEAN=false
else
if [ "${OPT_BUILD}" = 'full' ]; then
OPT_TARGET=binary
else
OPT_TARGET="${OPT_BUILD}"
fi
[ -z "${OPT_CLEAN}" ] && OPT_CLEAN=true
fi
}
test_uid0_cmd()
{
# Verify that the UID 0 command works.
test_uid=$("${OPT_UID_CMD}" id -u)
if [ "${?}" -ne 0 ]; then
oh_error "${oh_str_uid0_cmd_not_found}" "${OPT_UID_CMD}"
fi
if [ "${test_uid}" -ne 0 ]; then
oh_error "${oh_str_uid0_cmd_bad_uid}" "${OPT_UID_CMD}"
fi
}
find_sanity()
{
oh_info "${oh_str_find_sanity}"
format="$(cat format 2>/dev/null)"
if [ "${?}" -ne 0 ]; then
oh_error "${oh_str_no_format}"
fi
if [ "${format}" -ne 0 ]; then
oh_error "${oh_str_unsupported_format}"
fi
# Check for all files immediately required by SPF 2.0.
[ ! -f 'control' ] && oh_error "${oh_str_no_control}"
[ ! -x 'config' ] && oh_error "${oh_str_no_config}"
[ ! -f 'copyright' ] && oh_error "${oh_str_no_copyright}"
[ ! -f 'changelog' ] && oh_error "${oh_str_no_changelog}"
}
setup_package()
{
[ -n "${OH_SOURCE}" ] && return
OH_SOURCE="${OH_CHANGELOG_SOURCE}"
OH_SOURCE_VERSION="${OH_CHANGELOG_VERSION}"
# Validate source package version identifier.
echo "${OH_SOURCE_VERSION}" | grep -E "${SOURCE_VERSION_ID_RE}" \
>/dev/null 2>&1
if [ "${?}" -ne 0 ]; then
oh_error "${oh_str_bad_source_version}" "${OH_SOURCE_VERSION}"
fi
OH_SOURCE_VERSION_UPSTREAM="${OH_SOURCE_VERSION:%%-}"
OH_BINARY_VERSION="${OH_SOURCE_VERSION}"
}
build_source()
{
# TODO: Build source package.
}
setup_build()
{
setup_build_arch
setup_build_flags
setup_build_platform
setup_host_arch
setup_host_platform
setup_toolchain
}
setup_build_arch()
{
# TODO: Get build arch from opkg.
# TODO: Get GNU system type from table.
IFS=- read OH_BUILD_ARCH_CPU OH_BUILD_ARCH_KERNEL OH_BUILD_ARCH_LIBS <<EOF
${OH_BUILD_ARCH}
EOF
}
setup_build_flags()
{
# TODO: Don't hardcode path.
if [ -f "/usr/local/share/opkhelper/buildflags/${OH_HOST_ARCH_CPU}" ]; then
. /usr/local/share/opkhelper/buildflags/${OH_HOST_ARCH_CPU}
fi
}
setup_build_platform()
{
# TODO: Get build platform from opkg.
}
setup_host_arch()
{
if [ -n "${OPT_HOST_ARCH}" ]; then
OH_HOST_ARCH="${OPT_HOST_ARCH}"
else
OH_HOST_ARCH="${OH_BUILD_ARCH}"
fi
IFS=- read OH_HOST_ARCH_CPU OH_HOST_ARCH_KERNEL OH_HOST_ARCH_LIBS <<EOF
${OH_HOST_ARCH}
EOF
}
setup_host_platform()
{
if [ -n "${OPT_HOST_PLAT}" ]; then
OH_HOST_PLATFORM="${OPT_HOST_PLAT}"
else
OH_HOST_PLATFORM="${OH_BUILD_PLATFORM}"
fi
}
setup_toolchain()
{
OH_TOOL_PREFIX="${OH_HOST_ARCH}-"
# Set toolchain environment variables.
# NB: This is written for GNU Binutils and GCC and won't work for, e.g., LLVM.
AR="${OH_TOOL_PREFIX}ar"
AS="${OH_TOOL_PREFIX}as"
CC="${OH_TOOL_PREFIX}gcc"
CPP="${OH_TOOL_PREFIX}cpp"
CXX="${OH_TOOL_PREFIX}g++"
LD="${OH_TOOL_PREFIX}ld"
NM="${OH_TOOL_PREFIX}nm"
OBJCOPY="${OH_TOOL_PREFIX}objcopy"
OBJDUMP="${OH_TOOL_PREFIX}objdump"
RANLIB="${OH_TOOL_PREFIX}ranlib"
READELF="${OH_TOOL_PREFIX}readelf"
SIZE="${OH_TOOL_PREFIX}size"
STRINGS="${OH_TOOL_PREFIX}strings"
STRIP="${OH_TOOL_PREFIX}strip"
}
print_arch_stats()
{
oh_info "${oh_str_build_arch_stat_header}"
oh_info "${oh_str_arch_stat_arch}" "${OH_BUILD_ARCH}"
oh_info "${oh_str_arch_stat_plat}" "${OH_BUILD_PLATFORM}"
oh_info "${oh_str_host_arch_stat_header}"
oh_info "${oh_str_arch_stat_arch}" "${OH_HOST_ARCH}"
oh_info "${oh_str_arch_stat_plat}" "${OH_HOST_PLATFORM}"
}
make_work_area()
{
mkdir tmp || oh_error "${oh_str_cant_make_work_area}"
cd tmp || oh_error "${oh_str_cant_enter_work_area}"
step_unpack
}
step_unpack()
{
oh_cv_set step unpack
# TODO: Put this in oh-unpacksource.
if [ -d ../src ]; then
oh_info "${oh_str_unpacking_native}"
cp -Rp ../src src || oh_error "${oh_str_cant_unpack_native}"
else
archive_base_name="${OH_SOURCE}-${OH_SOURCE_VERSION_UPSTREAM}.orig.tar."
archive_matches=$(find .. -name "${archive_base_name}*")
case "$(echo "${archive_matches}" | wc -l)" in
0)
oh_error "${oh_str_no_sources}"
;;
1)
;;
*)
oh_error "${oh_str_multiple_upstream_sources}"
;;
esac
z_ext="${archive_matches#../${archive_base_name}}"
case ${z_ext} in
gz)
z=z
;;
bz2)
z=j
;;
lz)
z=a
;;
Z)
z=Z
;;
*)
oh_error "${oh_str_unsupported_archive_compression}" "${z_ext}"
;;
esac
sed 's@^./@@' | grep '^[^/]*/$'
top_dirs=$(tar "-t${z}f" "${archive_matches}${z_ext}" | \
sed -n 's@^\./\([^/]*\)$@\1@p')
case "$(echo "${top_dirs}" | wc -l)" in
0)
oh_error "${oh_str_no_upstream_dirs}"
;;
1)
;;
*)
oh_error "${oh_str_multiple_top_upstream_dirs}"
esac
oh_info "${oh_str_unpacking_upstream}"
tar "-x${z}f" "${archive_matches}${z_ext}" || \
oh_error "${oh_str_cant_unpack_upstream}"
mv "${top_dirs}" src || oh_error "${oh_str_cant_move_native}"
fi
step_patch
}
step_patch()
{
oh_cv_set step patch
# TODO: Put this in oh-applypatches.
applied=false
if [ -d ../patches ]; then
# Iterate over patches ordered alphabetically by name.
for patch in $(ls -1 ../patches | sort); do
oh_info "${oh_str_applying_patch}" "${patch}"
patch -N -p 1 -u -d src -i "../../patches/${patch}" || \
oh_error "${oh_str_cant_apply_patch}" "${patch}"
applied=true
done
fi
${applied} || oh_info "${oh_str_no_patches}"
step_config
}
step_config()
{
oh_cv_set step config
./config
step_platconf
}
step_platconf()
{
oh_cv_set step platconf
# TODO: Put this in oh-copyconfig.
if [ -f ../config ]; then
while read -r src dest; do
if [ -z "${src}" -o -z "${dest}" ]; then
oh_error
fi
# Make sure the destination directory exists.
mkdir -p "${dest%/*}" || oh_error
# Find the config package files.
# TODO: Don't hardcode path.
config_dir_base="/usr/share/config/${OH_HOST_PLATFORM}/${OH_SOURCE}"
if [ -d "${config_dir_base}-${OH_SOURCE_VERSION_UPSTREAM}" ]; then
src_base="${config_dir_base}-${OH_SOURCE_VERSION_UPSTREAM}"
elif [ -d "${config_dir_base}" ]; then
src_base="${config_dir_base}"
else
# This shouldn't happen unless the package maintainer neglected
# to add the config package to the package's Build-Depends
# field.
oh_error
fi
# Copy the config file(s).
oh_info
cp -p "${src_base}/${src}" "${dest}" || oh_error
done <../config
fi
for bin_config in ../*.pkg/config; do
while read -r src dest; do
if [ -z "${src}" -o -z "${dest}" ]; then
oh_error
fi
# Make sure the destination directory exists.
mkdir -p "dest/${dest%/*}" || oh_error
# Find the config package files.
# TODO: Don't hardcode path.
config_dir_base="/usr/share/config/${OH_HOST_PLATFORM}/${OH_SOURCE}"
if [ -d "${config_dir_base}-${OH_SOURCE_VERSION_UPSTREAM}" ]; then
src_base="${config_dir_base}-${OH_SOURCE_VERSION_UPSTREAM}"
elif [ -d "${config_dir_base}" ]; then
src_base="${config_dir_base}"
else
# This shouldn't happen unless the package maintainer neglected
# to add the config package to the package's Build-Depends
# field.
oh_error
fi
# Copy the config file(s).
oh_info
cp -p "${src_base}/${src}" "dest/${dest}" || oh_error
done <"${bin_config}"
done
step_build
}
step_build()
{
oh_cv_set step build
./build "${OPT_TARGET}"
"${OPT_CLEAN}" && step_clean
}
step_clean()
{
oh_cv_set step clean
./config clean
step_cleanwork
}
step_cleanwork()
{
oh_cv_clear
cd ..
rm -Rf tmp
}
main "${@}"
|