-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdpkg-mkdeb.sh
executable file
·1359 lines (1179 loc) · 39.8 KB
/
dpkg-mkdeb.sh
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash --posix
############################################################################
# Copyright (C) 2007-2008 Lawrence Livermore National Security, LLC
# Produced at Lawrence Livermore National Laboratory.
# Written by Jim Garlick <[email protected]>.
# UCRL-CODE-235516
#
# This file is part of dpkg-scripts, a set of utilities for managing
# packages in /usr/local with dpkg.
#
# dpkg-scripts 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 2 of the License, or (at your option)
# any later version.
#
# dpkg-scripts 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 dpkg-scripts; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
############################################################################
# constants
declare -r prog=dpkg-mkdeb
declare -r dpkgroot=/usr/local
declare -r dpkg_infodir=${dpkgroot}/dpkg-db/info
declare -r dkinit="/usr/share/dpkg-dotkit/init -b"
# Paths
if ! workdir=$(mktemp -d ${TMPDIR:-/tmp}/${prog}.XXXXXXXXXX); then
echo "mktemp failed" >&2
exit 1
fi
tmpsrc="${workdir}/src"
pkgconf="${workdir}/package.conf"
buddha="${workdir}/META"
tmproot="${workdir}/root-main"
savedtmproot="" # save ${tmproot} in case we have to move it
tmproot2="${workdir}/root-dflt"
tmpout="${workdir}/build.log"
# Args related globals
buildsrc="" # path/URL to original package source
snapshot=$(date +%Y%m%d%H%M%S) # snapshot string (NULL if not a snapshot build)
subpackage="" # subpackage we are building (if any)
variant="" # variant we are building (if any)
vopt=0 # -v verbose flag
fopt=0 # -f force overwrite deb flag
uopt=0 # -u secret restart flag
popt="" # -p package.conf
mopt="" # -m metafile
kopt=0 # -k keep build dir
topt=0 # -t don't use tmplocal unless notmproot
query="" # -q query package information
outfile="" # main package deb file
outfile2="" # default package deb file
selfpath="dpkg-mkdeb"
argscpy="" # copy of args for restart
#############################################################################
# general support functions
#############################################################################
# Cleanup temporary files/directories, if any
cleanup()
{
if [ ${kopt} = 0 ]; then
rm -rf ${workdir}
else
warn "work directory preserved: ${workdir}"
fi
}
clean_exit ()
{
cleanup
exit $1
}
# Print message on stderr, cleanup, then exit
# If no -v, spew forth any reserved output for debugging.
die()
{
echo ${prog}: $* >&2
clean_exit 1
}
# Print message on stderr
warn()
{
echo ${prog}: $* >&2
}
# split comma or space-separated words
split ()
{
local word
(IFS=$IFS","; for word in $*; do echo $word; done)
}
# match glob ($1) against remaining args
match_glob ()
{
local glob=$1; shift
local result=1 # no match
local dir
if dir=$(mktemp -d); then
pushd ${dir} >/dev/null
touch "$@"
if [ -f "${glob}" ]; then
result=0 # direct match
elif [ "$(echo -n ${glob})" != "${glob}" ]; then
result=0 # globbed match
fi
popd >/dev/null
rm -rf ${dir}
fi
return $result
}
# Turn dirname into a fully qualified path on stdout.
# Usage: fullpath dirname
fullpath()
{
case $1 in
/*) echo $1 ;;
.) echo $(pwd) ;;
./*) echo $(pwd)/$(echo $1|sed 's/\.\///') ;;
*) echo $(pwd)/$1 ;;
esac
}
# Get system build architecture from 'dpkg-architecture' (part of dpkg)
get_build_arch ()
{
local arch=$(dpkg-architecture | grep DEB_BUILD_ARCH= | cut -d= -f2)
if [ -z "${arch}" ]; then
warn "could not determine build arch"
return 1
fi
echo ${arch}
}
# Verify that a package build requirement is satisfied.
checkreq ()
{
local pkg
if echo $1 | grep -q "=" ; then # handle [=version] (glob OK)
for pkg in $(dpkg-query -Wf '${Package}=${Version}\n'); do
match_glob $1 ${pkg} && return 0
done
else
for pkg in $(dpkg-query -Wf '${Package}\n${Provides}\n'); do
[ ${pkg} = $1 ] && return 0
done
fi
return 1
}
get_tmproot ()
{
if [ $# = 1 ] && [ "$1" = "-d" ]; then
echo ${tmproot2}
else
echo ${tmproot}
fi
}
#############################################################################
# package.conf accessors, etc
#############################################################################
# Parse build-buddah META file and set meta_* vars for each field
# Usage: set_meta (in a subshell!)
set_meta ()
{
local line key val
if [ -r "${buddha}" ]; then
eval $(cat ${buddha} | while read line; do
line=$(echo $line | sed -e 's/#.*$//')
if [ -n "${line}" ]; then
key=$(echo $line | cut -d: -f1 | sed -e 's/[ \t]*//')
val=$(echo $line | cut -d: -f2 | sed -e 's/^[ \t]*//')
echo meta_$(echo $key | tr [:upper:] [:lower:])=\"$val\"
fi
done)
fi
}
# Parse package.conf and return value for specified variable (undef is error).
# If buddha META file is present, parse it first so meta_* values can be used.
# Usage: get_pkg_var var
get_pkg_var ()
{
local tmp=$(set_meta && source ${pkgconf} >/dev/null && eval echo \$$1)
[ -n "${tmp}" ] || return 1
echo -e ${tmp}
}
# Same as get_pkg_var but warn if undefined
# Usage: get_pkg_var_req var
get_pkg_var_req ()
{
local tmp
if ! tmp=$(get_pkg_var $1); then
warn "$1 not found in package.conf (required)"
return 1
fi
echo ${tmp}
}
# Extract the base name of a side installed package
get_pkg_basename ()
{
get_pkg_var_req PKG_NAME | sed -e 's/-[0-9].*//'
}
# Extract the base version of a side installed package
get_pkg_basever ()
{
local base name
base=$(get_pkg_basename) || return 1
name=$(get_pkg_var_req PKG_NAME) || return 1
echo ${name} | sed -e "s/${base}//" -e 's/^-//'
}
# Construct package name
# Usage: make_pkg_name [-d] [subpackage]
make_pkg_name ()
{
local name base bver res
local dflt=0 sub=""
while [ $# -gt 0 ]; do
case $1 in
-d) dflt=1; shift ;;
-*) warn "make_pkg_name: bad option: $1"; return 1 ;;
*) sub=$1; shift ;;
esac
done
name=$(get_pkg_var_req PKG_NAME) || return 1
base=$(get_pkg_basename) || return 1
# From debian policy manual:
# Package names must consist only of lower case letters (a-z),
# digits (0-9), plus (+) and minus (-) signs, and periods (.).
# They must be at least two characters long and must start with
# an alphanumeric character.
if echo ${name} | grep -q '[/_!@#$%^&*()=<>]'; then
warn "PKG_NAME allows only alphanumeric, plus (+), minus (-), and period (.) chars"
return 1
fi
# NOTE: apt presumes lower case
if echo ${name} | grep -q '[A-Z]'; then
warn "PKG_NAME alpha chars should be lower case"
return 1
fi
if [ $(echo -n ${name} | wc -c) -lt 2 ]; then
warn "PKG_NAME should be at least two characters long"
return 1
fi
res=${base}${sub:+"-${sub}"}
if [ ${dflt} = 1 ]; then
res=${res}
else
bver=$(get_pkg_basever) || return 1
res=${res}${bver:+"-${bver}"}
fi
echo ${res}
return 0
}
# Get package name, modified if building subpackage
# Usage: get_pkg_name [-d]
get_pkg_name ()
{
make_pkg_name $* ${subpackage}
}
# Get package version, modified if building a snapshot or variant
# Usage: get_pkg_version [-d]
get_pkg_version ()
{
local ver res
ver=$(get_pkg_var_req PKG_VERSION) || return 1
if echo ${ver} | grep -q "_"; then
warn "``_'' is illegal in PKG_VERSION"
return 1
fi
res=${ver}
if [ $# = 1 ] && [ "$1" = "-d" ]; then
res=default-$(get_pkg_basever)-${res} || return 1
fi
res=${res}${variant:+".${variant}"}
res=${res}${snapshot:+".snapshot.${snapshot}"}
echo ${res}
}
# Query package names in name_version form. Leave variants out of it
# unless a single ${variant} is defined via command line.
# Usage: list_pkg_names [-d]
list_pkg_names ()
{
local subs sub ver
if [ "$1" = "-d" ]; then
get_pkg_var PKG_DEFAULT >/dev/null || return
fi
subs=$(get_pkg_var PKG_SUBPACKAGES);
ver=$(get_pkg_version $*) || exit 1
if [ -n "${subs}" ]; then
for sub in $(split ${subs}); do
echo $(make_pkg_name $* ${sub})_${ver}
done
else
echo $(make_pkg_name $*)_${ver}
fi
}
# Get package prefix.
# Usage: get_pkg_prefix [-d]
get_pkg_prefix ()
{
local name=$(get_pkg_name $*) || return 1
case "$(get_pkg_var_req PKG_SECTION)" in
root) echo ${dpkgroot} ;;
tools) echo ${dpkgroot}/tools/${name} ;;
storage) echo ${dpkgroot}/storage/${name} ;;
viz) echo ${dpkgroot}/viz/${name} ;;
opt) echo ${dpkgroot}/opt/${name} ;;
*) warn "invalid PKG_SECTION"; return 1 ;;
esac
}
# Set macros used in package.conf
# Usage: set_macros [-d] (in a subshell!)
set_macros ()
{
local pfx name
name=$(get_pkg_name $*) || return 1
pfx=$(get_pkg_prefix $*) || return 1
prefix=${pfx}
bindir=${pfx}/bin
etcdir=${pfx}/etc
mandir=${pfx}/man
infodir=${pfx}/info
docdir=${pfx}/doc
sbindir=${pfx}/sbin
includedir=${pfx}/include
libdir=${pfx}/lib
libexecdir=${pfx}/libexec
srcdir=${pfx}/src
vardir=${pfx}/var
lbindir=${dpkgroot}/bin
dotkitdir=${dpkgroot}/etc/dotkit
lccdir=${dpkgroot}/etc/lcc
pkgname=${name}
# provide default PKG_ARCH value in case undefined
PKG_ARCH=$(get_pkg_arch)
}
# Like get_pkg_var except define macros prior to sourcing the file.
# Usage: get_pkg_var_withmacros [-d] var
get_pkg_var_withmacros ()
{
local tmp
if [ $# = 2 ] && [ $1 = "-d" ]; then
tmp=$(set_meta; set_macros -d; source ${pkgconf} >/dev/null; eval echo \$$2)
else
tmp=$(set_meta; set_macros; source ${pkgconf} >/dev/null; eval echo \$$1)
fi
[ -z "${tmp}" ] && return 1
echo -e ${tmp}
}
# Get package arch
# Usage: get_pkg_arch
get_pkg_arch ()
{
local arch buildarch
buildarch=$(get_build_arch) || return 1
if arch=$(get_pkg_var PKG_ARCH); then
if [ "${arch}" != "all" ] && [ "${buildarch}" != "${arch}" ]; then
warn "PKG_ARCH should be set to all or ${buildarch} to build here"
return 1
fi
else
arch=${buildarch}
fi
echo ${arch}
}
# Test for presence of package option
# Usage: get_pkg_opt option
get_pkg_opt ()
{
local flags opt
flags=$(get_pkg_var PKG_FLAGS) || return 1
for opt in $(split ${flags}); do
[ ${opt} = $1 ] && return 0
done
return 1
}
list_variants ()
{
local names
names=$(get_pkg_var PKG_VARIANTS) || return 1
split ${names}
return 0
}
# List packages this package depends on
list_depends ()
{
local deps pkg
deps=$(get_pkg_var PKG_DEPENDS) || return 0
if echo ${deps} | grep -q '|'; then
warn "can't handle |'s in PKG_DEPENDS with auto-generated dotkits yet "
return 1
fi
# drop the (version) from package name for dotkit processing
for pkg in $(split ${deps}); do
echo $pkg | sed -e 's/(.*)//g'
done
}
# Query data about a package
query_vars ()
{
local var result
if [ "$1" = "all" ]; then
set - "name version basename basever subpackages variants buildrequires names default maintainer"
fi
for var in $*; do
case ${var} in
subpackages) result=$(split $(get_pkg_var PKG_SUBPACKAGES)) ;;
variants) result=$(split $(get_pkg_var PKG_VARIANTS)) ;;
buildrequires) result=$(get_pkg_var PKG_BUILDREQUIRES) ;;
name) result=$(get_pkg_var PKG_NAME) ;;
version) result=$(get_pkg_var PKG_VERSION) ;;
names) result=$(list_pkg_names) ;;
basename) result=$(get_pkg_basename) ;;
basever) result=$(get_pkg_basever) ;;
default) result=$(list_pkg_names -d) ;;
maintainer) result=$(get_pkg_var PKG_MAINTAINER) ;;
*) die "unknown field '${var}'" ;;
esac
echo ${var}: ${result}
done
}
#############################################################################
# package metadata processing
#############################################################################
# Make macros macros file
# Usage: make_macros [-d] (in a subshell!)
make_macros ()
{
local pfx name vers arch maint desc section depends buildrequires
local flags subpackages variants default
pfx=$(get_pkg_prefix $*) || return 1
name=$(get_pkg_name $*) || return 1
vers=$(get_pkg_version $*) || return 1
arch=$(get_pkg_arch) || return 1
maint=$(get_pkg_var_req PKG_MAINTAINER) || return 1
desc=$(get_pkg_var_req PKG_SHORT_DESCRIPTION) || return 1
section=$(get_pkg_var_req PKG_SECTION) || return 1
depends=$(get_pkg_var PKG_DEPENDS)
buildrequires=$(get_pkg_var PKG_BUILDREQUIRES)
flags=$(get_pkg_var PKG_FLAGS)
subpackages=$(get_pkg_var PKG_SUBPACKAGES)
variants=$(get_pkg_var PKG_VARIANTS)
default=$(get_pkg_var PKG_DEFAULT)
echo export dpkg_prefix=${pfx}
echo export dpkg_subpackage=${subpackage}
echo export dpkg_variant=${variant}
echo export dpkg_default=${default}
echo export dpkg_name=${name}
echo export dpkg_version=${vers}
echo export dpkg_arch=${arch}
echo export dpkg_maintainer=\"${maint}\"
echo export dpkg_short_description=\"${desc}\"
echo export dpkg_section=${section}
echo export dpkg_depends=\"${depends}\"
echo export dpkg_buildrequires=\"${buildrequires}\"
echo export dpkg_flags=\"${flags}\"
echo export dpkg_subpackages=\"${subpackages}\"
echo export dpkg_variants=\"${variants}\"
}
# Emit a deb control file on stdout
# Usage: make_control [-d]
make_control()
{
local pkg name vers arch maint desc section
local depends provides conflicts mname mvers
name=$(get_pkg_name $*) || return 1
vers=$(get_pkg_version $*) || return 1
arch=$(get_pkg_arch) || return 1
maint=$(get_pkg_var_req PKG_MAINTAINER) || return 1
desc=$(get_pkg_var_req PKG_SHORT_DESCRIPTION) || return 1
section=$(get_pkg_var_req PKG_SECTION) || return 1
if [ $# = 1 ] && [ $1 = "-d" ]; then
mname=$(get_pkg_name) || return 1
mvers=$(get_pkg_version) || return 1
depends="${mname} (= ${mvers})"
else
depends=$(get_pkg_var PKG_DEPENDS)
provides=$(get_pkg_var PKG_PROVIDES)
conflicts=$(get_pkg_var PKG_CONFLICTS)
fi
if get_pkg_opt variantsconflict; then
for pkg in $(list_variants); do
if [ ${pkg} != ${variant} ]; then
conflicts=${conflicts}${conflicts:+,}${pkg}
fi
done
fi
echo Package: ${name}
echo Version: ${vers}
echo Architecture: ${arch}
echo Maintainer: ${maint}
echo Description: ${desc}
echo Section: ${section}
if [ -n "${depends}" ]; then
echo Depends: ${depends}
fi
if [ -n "${provides}" ]; then
echo Provides: ${provides}
fi
if [ -n "${conflicts}" ]; then
echo Conflicts: ${conflicts}
fi
}
# Emit build info file on stdout
# Usage: make_buildinfo
make_buildinfo ()
{
echo "Source: ${buildsrc}"
echo "Date: $(date)"
echo "Hostname: $(hostname)"
}
# Helper for make_dk()
# Usage: find_libdirs tmproot libdir
find_libdirs ()
{
local file
# note: trailing / needed for libdir that is a symlink
# note: path is relative to tmproot so we chop off leading ./ in result
# FIXME need to handle arches with different archive suffix
pushd $1 >/dev/null || die "cannot chdir to $1"
for file in $(find .$2/ -name \*.so\* 2>/dev/null | sed -e 's/^\.//'); do
dirname ${file}
done | sort | uniq
popd >/dev/null
}
# Create a dotkit for this package on stdout.
# Usage make_dk [-d]
make_dk ()
{
local pfx name ver root tmpstr bname
pfx=$(get_pkg_prefix $*) || return 1
name=$(get_pkg_name $*) || return 1
ver=$(get_pkg_version $*) || return 1
root=$(get_tmproot $*)
get_pkg_opt nodk && return 0
if get_pkg_opt dkhide \
|| ( [ "$1" = "-d" ] && get_pkg_opt dkhidedefault ) \
|| ( [ "$1" != "-d" ] && get_pkg_opt dkhidemain ); then
echo "#a"
fi
if tmpstr=$(get_pkg_var PKG_DK_CATEGORY); then
echo '#c' "${tmpstr}"
elif tmpstr=$(get_pkg_var PKG_CATEGORY); then
warn "PKG_CATEGORY is deprecated, use PKG_DK_CATEGORY"
echo '#c' "${tmpstr}"
elif tmpstr=$(get_pkg_var_req PKG_SECTION); then
echo '#c' local-${tmpstr}
else
return 1
fi
tmpstr=$(get_pkg_var_req PKG_SHORT_DESCRIPTION) || return 1
echo '#d' "${tmpstr}"
if tmpstr=$(get_pkg_var_withmacros PKG_DK_HELP); then
echo -e "${tmpstr}" | sed 's/^/#h /'
elif tmpstr=$(get_pkg_var_withmacros PKG_HELP); then
warn "PKG_HELP is deprecated, use PKG_DK_HELP"
echo -e "${tmpstr}" | sed 's/^/#h /'
fi
# unuse all other forms of the package if dkmutex
if get_pkg_opt dkmutex; then
bname=$(get_pkg_basename) || return 1
echo "unuse -q \`dk_rep '${bname}-[0-9]*'\` ${bname}"
fi
# load dotkits of dependent packages unles decoupledk
if ! get_pkg_opt decoupledk; then
for tmpstr in $(list_depends); do
echo "dk_op -q ${tmpstr}";
done
fi
if [ -d ${root}${pfx}/bin ] || [ -h ${root}${pfx}/bin ]; then
echo "dk_alter PATH ${pfx}/bin"
fi
if [ -d ${root}${pfx}/info ] || [ -h ${root}${pfx}/info ]; then
echo "dk_alter INFOPATH ${pfx}/info"
fi
if [ -d ${root}${pfx}/man ] || [ -h ${root}${pfx}/man ]; then
echo "dk_alter MANPATH ${pfx}/man"
fi
if ! get_pkg_opt noldpath; then
for tmpstr in $(find_libdirs ${root} ${pfx}/lib); do
echo "dk_alter LD_LIBRARY_PATH ${tmpstr}"
done
fi
echo "dpkg-logger -p ${name} -d pid=\$\$ op=\$_dk_op"
}
# Validate that files referenced in 'doc' file actually exist
# Usage: check_doc
check_doc()
{
local file
local result=0
if [ -f ${tmproot}/DEBIAN/doc ]; then
if [ $(wc -l < ${tmproot}/DEBIAN/doc) != $(grep : ${tmproot}/DEBIAN/doc | wc -l) ]; then
warn "check_doc: malformed doc entry"
return 1
fi
for file in $(sed -e 's/^[^:]*:[ \t]*//' ${tmproot}/DEBIAN/doc); do
if ! [ -f ${tmproot}${file} ] && ! [ -h ${tmproot}${file} ]; then
warn "check_doc: no such file: ${tmproot}${file}"
result=1 # keep going to warn of all missing files
fi
done
fi
return ${result}
}
# Emit md5sumps for all package files to stdout
# Usage: make_md5sums [-d]
make_md5sums()
{
local root=$(get_tmproot $*)
# FIXME: use ${dpkgroot} instead of hard coded directory
pushd ${root} >/dev/null || return 1
if ! [ -d usr/local ]; then
warn "${root}/usr/local does not exist!"
return 1
fi
find usr/local -type f -exec md5sum {} \; # skips symlinks
popd >/dev/null
}
make_linkdata2()
{
local root=$(get_tmproot $*)
local file
which base64 >/dev/null || die "base64 is missing"
# FIXME: use ${dpkgroot} instead of hard coded directory
pushd ${root} >/dev/null || return 1
if ! [ -d usr/local ]; then
warn "${root}/usr/local does not exist!"
return 1
fi
find usr/local -type l | while read file; do # only symlinks
echo "$(readlink -n "${file}" | base64 -w 0) ${file}"
done
popd >/dev/null
}
# Append stdin to tmpout (create it if necessary).
# Also cc stdout if -v option.
logappend ()
{
if [ $vopt = 0 ]; then
cat >>${tmpout}
else
tee -a ${tmpout} >&2
fi
}
# Create wrapper scripts listed in PKG_WRAPPERS.
# Usage create_wrappers [-d]
create_wrappers ()
{
local root ext name lbindir bindir lmandir mandir wrappers basever wrap sec
if wrappers=$(get_pkg_var PKG_WRAPPERS); then
sec=$(get_pkg_var_req PKG_SECTION) || return 1
if [ ${sec} = "root" ]; then
warn "cannot use PKG_WRAPPERS with PKG_SECTION=root"
return 1
fi
name=$(get_pkg_name $*) || return 1
pfx=$(get_pkg_prefix $*) || return 1
root=$(get_tmproot $*)
lbindir=${root}${dpkgroot}/bin
bindir=${root}${pfx}/bin
lmandir=${root}${dpkgroot}/man
mandir=${root}${pfx}/man
# construct hyphenated extension if no -d
if [ $# = 0 ]; then
basever=$(get_pkg_basever) || return 1
if get_pkg_opt nodashwrap; then
ext=${basever:+"${basever}"}
else
ext=${basever:+"-${basever}"}
fi
fi
mkdir -p ${lbindir} || return 1
mkdir -p ${lmandir}/man1 || return 1
# create wrappers and their man pages
for wrap in $(split ${wrappers}); do
if ! get_pkg_opt nocheckwrap && ! [ -x ${bindir}/${wrap} ] \
&& ! [ -h ${bindir}/${wrap} ] && ! [ -h ${bindir} ]; then
warn "trying to wrap nonexistant executable: ${wrap}"
return 1
fi
cat >${lbindir}/${wrap}${ext} <<EOT || return 1
#!/bin/bash --posix
exec /usr/bin/dpkg-wrap ${name} ${wrap} "\$@"
EOT
chmod 555 ${lbindir}/${wrap}${ext} || return 1
if [ -f ${mandir}/man1/${wrap}.1 ] \
|| [ -h ${mandir}/man1/${wrap}.1 ] ; then
ln -s ${pfx}/man/man1/${wrap}.1 ${lmandir}/man1/${wrap}${ext}.1
elif [ -f ${mandir}/man1/${wrap}.1.gz ] \
|| [ -h ${mandir}/man1/${wrap}.1.gz ] ; then
ln -s ${pfx}/man/man1/${wrap}.1.gz ${lmandir}/man1/${wrap}${ext}.1.gz
fi
done
fi
}
# get source into tmpsrc
copy_tmpsrc ()
{
local name=$(get_pkg_name)
if [ -d ${buildsrc} ]; then
warn "copying '${name}' source to tmpsrc"
(echo "*** copy begin"; set -e; set -x; \
rsync -av --exclude .svn ${buildsrc}/ ${tmpsrc}; \
echo "*** copy status=$?") 2>&1 | logappend
if ! grep -q "*** copy status=0" ${tmpout}; then
[ ${vopt} = 1 ] || cat ${tmpout} >&2
die "copy failed"
fi
else
warn "exporting '${name}' to tmpsrc"
(echo "*** export begin"; set -e; set -x; \
svn --force export ${buildsrc} ${tmpsrc}; \
echo "*** export status=$?") 2>&1 | logappend
if ! grep -q "*** export status=0" ${tmpout}; then
[ ${vopt} = 1 ] || cat ${tmpout} >&2
die "export failed"
fi
fi
}
#############################################################################
# main package
#############################################################################
# Install buildrequires into 'tmplocal'
install_buildrequires ()
{
local pkg deps
local ret=0
if deps=$(get_pkg_var PKG_BUILDREQUIRES); then
if ! apt-userinst $(split ${deps}); then
warn "apt-userinst failed"
return 1
fi
for pkg in $(split ${deps}); do
if ! checkreq ${pkg}; then
warn "package ${pkg} is not installed"
ret=1
fi
done
fi
[ "${ret}" = 1 ] && warn "package build requirements were not met"
return ${ret}
}
# Remove buildrequires from 'tmplocal'
remove_buildrequires ()
{
if [ -d ${dpkgroot}/dpkg-db ]; then
if ! dpkg -r --force-not-root $(dpkg-query -W -f='${Package}\n'); then
warn "failed to remove all PKG_BUILDREQUIRES packages from image"
return 1
fi
rm -rf ${dpkgroot}/dpkg-db
fi
return 0
}
# Use (in the dotkit sense) all the packages needed to build this package
# Helper for main_package_build().
use_buildreqs ()
{
local pkg deps
if deps=$(get_pkg_var PKG_BUILDREQUIRES); then
unset DK_ROOT
unset DK_NODE
unset DK_SUBNODE
unset _dk_inuse
eval $(${dkinit})
for pkg in $(split ${deps}); do
pkg=$(echo -n ${pkg} | cut -d= -f1) # drop [=version]
[ -e ${dpkg_infodir}/${pkg}.dk ] && use -q ${pkg}
done
fi
}
# Execute package.conf::pkg_build() in a subshell environment.
main_package_build ()
{
local name=$(get_pkg_name)
if [ $uopt = 1 ]; then
warn "installing packages required for build in private /usr/local"
(install_buildrequires; \
echo "*** install_buildrequires status=$?" ) 2>&1 | logappend
if ! grep -q "*** install_buildrequires status=0" ${tmpout}; then
[ ${vopt} = 1 ] || cat ${tmpout} >&2
die "failed to install buildrequires packages"
fi
fi
warn "building '${name}' in tmpsrc"
(cd ${tmpsrc}; set_meta; use_buildreqs; set_macros; source ${pkgconf}; \
echo "*** pkg_build begin"; \
set -e; set -x; pkg_build ${tmproot}; \
echo "*** pkg_build status=$?") 2>&1 | logappend
if ! grep -q "*** pkg_build status=0" ${tmpout}; then
[ ${vopt} = 1 ] || cat ${tmpout} >&2
die "pkg_build script failed"
fi
}
# Convert /usr/local sandbox into a tmproot-like thing.
# Helper for main_package_install().
# Usage: convert_root /usr/local
convert_root()
{
local newroot ldir file
newroot=$(mktemp -d $1/tmp.XXXXXXXXXX) || die "mktemp failed"
ldir=${newroot}$1
mkdir -p ${ldir} || die "could not mkdir ${ldir}"
pushd $1 >/dev/null || die "could not chdir to $1"
for file in $(/bin/ls -a1 .); do
case ${file} in
.|..|$(basename ${newroot}) ) ;;
*) mv ${file} ${ldir} || die "mv ${file} ${ldir} failed" ;;
esac
done
popd >/dev/null
echo ${newroot}
}
# Execute package.conf::pkg_install() in a subshell environment.
main_package_install ()
{
local name=$(get_pkg_name)
local n
mkdir -p ${tmproot} || die "mkdir ${tmproot} failed"
if get_pkg_opt notmproot; then
savedtmproot=${tmproot}
tmproot=/
warn "installing '${name}' to private /usr/local"
else
warn "installing '${name}' to tmproot"
fi
(cd ${tmpsrc}; set_meta; use_buildreqs; set_macros; source ${pkgconf}; \
echo "*** pkg_install begin"; \
mkdir -p ${tmproot}${dpkgroot}
set -e; set -x; pkg_install ${tmproot}; \
echo "*** pkg_install status=$?") 2>&1 | logappend
if ! grep -q "*** pkg_install status=0" ${tmpout}; then
[ ${vopt} = 1 ] || cat ${tmpout} >&2
die "pkg_install script failed"
fi
if get_pkg_opt notmproot; then
warn "removing packages required for build from private /usr/local"
(remove_buildrequires; \
echo "*** remove_buildrequires status=$?" ) 2>&1 | logappend
if ! grep -q "*** remove_buildrequires status=0" ${tmpout}; then
[ ${vopt} = 1 ] || cat ${tmpout} >&2
die "failed to remove buildrequires packages"
fi
tmproot=$(convert_root ${dpkgroot})
fi
create_wrappers || die "failed to create wrappers"
chmod -R go=u-w ${tmproot}
find ${tmproot} -type d -exec chmod u+w {} \;
}
# If file has zero length, remove it.
# If file exists and has size greater than zero, chmod it.
# Usage: check_mfile mode path
check_mfile ()
{
local mode=$1
local path=$2
if [ -e ${path} ]; then
if [ -s ${path} ]; then
chmod ${mode} ${path}
else
rm -f ${path}
fi
fi
}
# Create metadata files under ${tmproot}/DEBIAN.
main_package_metadata ()
{
warn "creating package metadata in tmproot"
mkdir -p ${tmproot}/DEBIAN
make_md5sums >${tmproot}/DEBIAN/md5sums || die "failed to make md5sums"
check_mfile 444 ${tmproot}/DEBIAN/md5sums
make_linkdata2 >${tmproot}/DEBIAN/linkdata2 || die "failed to make linkdata"
check_mfile 444 ${tmproot}/DEBIAN/linkdata2
make_control >${tmproot}/DEBIAN/control || die "failed to make control"
check_mfile 444 ${tmproot}/DEBIAN/control
make_buildinfo >${tmproot}/DEBIAN/buildinfo