-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpkg.sh
executable file
·288 lines (239 loc) · 6.03 KB
/
buildpkg.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
#!/bin/bash
# vim
set -euo pipefail
SCRIPTPATH="$( realpath $0 )"
WORKDIR_ROOT=/opt/workdir
PKG_OUT_ROOT=/opt/pkg
STAGE_ROOT=/opt/stage
PREFIX_ROOT=/opt/sw
CACHE_DIR=/opt/cache
PORTS_DIR=${PORTS_DIR:-/opt/ports}
# For software that will want configuration and data to persist across versions,
# they may need to know that location at build time (or to set sane defaults).
SYSCONF_DIR=/opt/local/etc
SYSSTATE_DIR=/opt/local/var
export MAKEFLAGS="-j$(nproc)"
export STRIPPROG="$TARGET-strip"
echo_info() {
echo -e "\e[34m==>\e[0m \e[1m$@\e[0m" >&2
}
echo_debug() {
[ "$verbose" = false ] && return 0
echo -e "\e[2m==> $@\e[0m" >&2
}
echo_warn() {
echo -e "\e[33m==>\e[0m \e[1m$@\e[0m" >&2
}
echo_error() {
echo -e "\e[91m==>\e[0m \e[21m$@\e[0m" >&2
}
echo_success() {
echo -e "\e[92m==>\e[0m \e[1m$@\e[0m" >&2
}
pkg=$1; shift
verbose=false
raw_opts="$@"
remaining_args=()
for arg in "$@"; do
case $arg in
--verbose|-v)
verbose=true
shift
;;
*)
remaining_args+=($1)
shift
;;
esac
done
if [ ${#remaining_args[@]} -gt 0 ]; then
echo_warn "Unused arguments: ${remaining_args[*]}"
fi
usage() {
cat <<-EOF
Usage: $0 <pkgname>
EOF
}
[ -z "$pkg" ] && (usage; exit 1)
pkgbuild_file=$PORTS_DIR/$pkg/PKGBUILD
if [ ! -f "$pkgbuild_file" ]; then
echo_error "Could not find build script for '$pkg' at $pkgbuild_file"
exit 1
fi
prepare() { return 0; }
check() { return 0; }
depends=()
broken=""
echo_info "Loading $pkgbuild_file"
source $pkgbuild_file
if [ -n "$broken" ]; then
echo_info "Package is marked as broken. Aborting build. Reason:"
echo
echo "$broken" | sed 's/^/ /'
echo
exit 0
fi
workdir=$WORKDIR_ROOT/$pkgname/$pkgver
pkgpath=$PKG_OUT_ROOT/$pkgname-$pkgver-$TARGET.pkg.tar.gz
pkginfopath=$PKG_OUT_ROOT/$pkgname-$pkgver-$TARGET.pkginfo.txt
pkgprefix=$PREFIX_ROOT/$pkgname/$pkgver
pkgdir=$STAGE_ROOT/$pkgname
pkgcache=$CACHE_DIR/$pkgname
find_dep_path() {
find $STAGE_ROOT/$1$PREFIX_ROOT/$1 -maxdepth 1 -type d 2>/dev/null | tail -n 1 || :
}
whereis_dep() {
path=$(find_dep_path $1)
if [ -z "$path" ]; then
echo_error "Unable to find path for dependency '$1'"
return 1
fi
echo $path
}
is_dep_installed() {
[ -n "$(find_dep_path $1)" ] && echo "yes" || echo "no"
}
[ -d $workdir ] && rm -rf $workdir
# always purge the staging dir, otherwise multiple builds of the same package
# could produce unexpected files, such as if you enable or disable features
[ -d $pkgdir ] && rm -rf $pkgdir
[ -d $workdir ] || mkdir -p $workdir
[ -d $pkgdir ] || mkdir -p $pkgdir
[ -d $pkgcache ] || mkdir -p $pkgcache
cd $workdir
echo_debug "Working in $workdir"
extract() {
case "$1" in
*.tar|*.tar.gz|*tar.bz2|*.tar.xz)
tar xf $1 ;;
*.zip)
unzip $1 ;;
*)
echo_debug "Not extracting unknown archive type $1" ;;
esac
}
verify_source() {
local sum=""
local index=$1
local filename=$2
echo_debug "Verifying source index=$index filename=$filename"
for type in sha{1,256,512} md5; do
local varname=${type}sums
if [ -v $varname ]; then
sums="${varname}[@]"
sums=(${!sums})
sum=${sums[$index]}
break
fi
done
if [ -z "$sum" ]; then
echo_warn "No checksums found"
return 0
elif [ "$sum" = "SKIP" ]; then
echo_info "Skipping validation of $filename"
return 0
fi
echo_info "Verifying $type checksum $sum for $filename"
echo "$sum $filename" | ${type}sum --check --quiet
}
stage_size() {
du -sb $pkgdir | awk '{print $1}'
}
# To help detect when a package was rebuilt without changes, generate a revision
# based on the contents of the port's directory. If none of the file contents.
build_revision() {
tar c $PORTS_DIR/$1 | sha1sum | awk '{print $1}'
}
mk_pkginfo() {
cat <<-EOF
pkgname = $pkgname
pkgver = $pkgver
pkgdesc = $pkgdesc
url = $url
builddate = $(date +%s)
buildrev = $(build_revision $pkgname)
size = $(stage_size)
packager = $0
arch = $TARGET
EOF
for f in "${depends[@]}"; do
echo "depend = $f";
done
}
quiet_run() {
if [ "$verbose" = true ]; then
("$@")
else
logfile=$(tempfile)
("$@") >>$logfile 2>&1 || {
echo "command failed with exit code $?: $@"
echo "Last lines of log $logfile:"
echo "--------------------------------------------------"
echo "... [$(wc -l $logfile | awk '{print $1}') lines]"
tail -n20 $logfile
return 1
}
fi
}
DEP_CPPFLAGS=()
DEP_LDFLAGS=()
DEP_BINPATH=()
if [ -v depends ]; then
for dep in "${depends[@]}"; do
dep_installed=$(is_dep_installed $dep)
if [ "$dep_installed" == "yes" ]; then
echo_debug "Dependency '$dep' is already built"
else
echo_info "Building dependency '$dep'"
$SCRIPTPATH $dep $raw_opts
fi
# assume the dependency is needed for build time, so add its include and
# lib dirs to the compiler flags so it can find them
DEP_CPPFLAGS+=("-I$(whereis_dep $dep)/include")
DEP_LDFLAGS+=("-L$(whereis_dep $dep)/lib")
DEP_BINPATH+=($(whereis_dep $dep)/bin)
done
fi
for findex in "${!sources[@]}"; do
source="${sources[$findex]}"
archive=$(basename "$source")
archive_path=$pkgcache/$archive
do_download="yes"
if [ -f $archive_path ]; then
verify_source $findex $archive_path && do_download="no" || :
[ $do_download == "no" ] && echo_info "Using cached file $archive"
fi
if [ $do_download == "yes" ]; then
case "$source" in
http://*|https://*|ftp://*)
echo_info "Fetching source $source"
curl -# -L -o $archive_path "$source"
;;
*)
echo_info "Copying local file $source"
cp $PORTS_DIR/$pkgname/$source $workdir/.
;;
esac
verify_source $findex $archive_path
fi
extract $archive_path
done
echo_info "Preparing"
(quiet_run "prepare")
echo_info "Building"
(
dep_bin_paths=$(printf ":%s" "${DEP_BINPATH[@]}")
export CPPFLAGS="${DEP_CPPFLAGS[*]} ${CPPFLAGS-}"
export LDFLAGS="${DEP_LDFLAGS[*]} ${LDFLAGS-}"
export PATH="${PATH}$dep_bin_paths"
quiet_run "build"
)
echo_info "Staging install to $pkgdir"
(quiet_run "package")
echo_info "Creating archive $pkgpath"
mk_pkginfo > $pkginfopath
cp $pkginfopath $pkgdir/.PKGINFO
# the xform arg strips the leading ./ from paths in the archive
tar -czf $pkgpath --xform s:'./':: -C $pkgdir .
echo_success "Built $pkgname!"
echo_success "Done"