summaryrefslogtreecommitdiff
path: root/pkgtools/pbulk/files/pbulk/scripts/pkg-build
blob: f0eb90c1928906214b1675aa2e1bef02dc2c3711 (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
#!@SH@
# $NetBSD: pkg-build,v 1.20 2010/02/24 22:51:37 joerg Exp $
#
# Copyright (c) 2007, 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
# All rights reserved.
#
# This code was developed as part of Google's Summer of Code 2007 program.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

. @PBULK_CONFIG@

if [ "${config_version}" != "@PBULK_CONFIG_VERSION@" ]; then
	echo "Your configuration has version ${config_version}."
	echo "This version of pbulk expects version @PBULK_CONFIG_VERSION@."
	exit 1
fi

cleanup() {
	if [ "$keep_wrkdir" = yes ]; then
		${make} pbulk-save-wrkdir INTO=${bulklog}/${pkgname}/wrkdir.tar.gz TAR="${tar}"
	fi
	if [ "$keep_prefix" = yes ] && [ -f ${bulklog}/${pkgname}/install.log ]; then
		if [ "$cross_compile" != "no" ]; then
			cur_destdir=${target_destdir}
		else
			cur_destdir=
		fi
		${tar} -czf ${bulklog}/${pkgname}/prefix.tar.gz ${cur_destdir}${prefix}
	fi
	${make} clean > /dev/null 2>&1 || true
	exit 1
}

run_direct() { "$@"; }
run_su() { su ${unprivileged_user} -c '"$@"' make "$@"; }

run_make() {
	local run_cmd
	run_cmd=$1
	shift
	${run_cmd} ${make} $1 \
		BATCH=1 \
		DEPENDS_TARGET=/nonexistent \
		${MAKE_FLAGS} \
		WRKLOG=${bulklog}/${pkgname}/work.log

}

while read build_info_line; do
	case "${build_info_line}" in
	PKGNAME=*)
		pkgname=${build_info_line#PKGNAME=}
		;;
	PKG_LOCATION=*)
		pkgdir=${build_info_line#PKG_LOCATION=}
		;;
	DEPENDS=*)
		dependencies=${build_info_line#DEPENDS=}
		;;
	MULTI_VERSION=*)
		MAKE_FLAGS=${build_info_line#MULTI_VERSION=}
		;;
	USE_DESTDIR=*)
		use_destdir=${build_info_line#USE_DESTDIR=}
		;;
	BOOTSTRAP_PKG=*)
		is_bootstrap=${build_info_line#BOOTSTRAP_PKG=}
		;;
	esac
done

case "$use_destdir" in
user-destdir)
	run_build=run_su
	run_install=run_su
	;;
destdir)
	run_build=run_su
	run_install=run_direct
	;;
*)
	run_build=run_direct
	run_install=run_direct
	;;
esac

${pkg_up_to_date_script} ${pkgname} ${dependencies} && exit 0

set -e

@PREFIX@/libexec/pbulk/client-clean

# Create the output directory and clean it up
mkdir -p ${bulklog}/${pkgname}
rm -f ${bulklog}/${pkgname}/*

if [ "$use_destdir" = "destdir" -o "$use_destdir" = "user-destdir" ]; then
	touch ${bulklog}/${pkgname}/work.log
	@CHOWN@ ${unprivileged_user} ${bulklog}/${pkgname}/work.log
fi

pkg_add_normal() {
	PKG_PATH=${packages}/All ${pkg_add} -K ${cur_pkgdb} "$@"
}

pkg_add_cross() {
	PKG_PATH=${packages}/All ${pkg_add} -K ${cur_pkgdb} \
	    -m ${target_arch} -I -p ${target_destdir}${prefix} "$@"
	echo "Fixing recorded cwd..."
	${pkg_info} -K ${target_destdir}${pkgdb} | while read pkg junk; do
		${sed} -e 's|@cwd '"${target_destdir}"'|@cwd |' \
		    ${cur_pkgdb}/"${pkg}"/+CONTENTS > \
		    ${cur_pkgdb}/"${pkg}"/+CONTENTS.tmp
		mv ${cur_pkgdb}/"${pkg}"/+CONTENTS.tmp \
		    ${cur_pkgdb}/"${pkg}"/+CONTENTS
	done
}

if [ "$cross_compile" != "no" ]; then
	pkg_add_cmd=pkg_add_cross
	cur_pkgdb="${target_destdir}${pkgdb}"
else
	pkg_add_cmd=pkg_add_normal
	cur_pkgdb="${pkgdb}"
fi

# Go to target directory
cd ${pkgsrc}/${pkgdir}
# Clean build area, just in case
${make} clean > ${bulklog}/${pkgname}/pre-clean.log 2>&1
# Install all dependencies the package said it would need
if [ ! -z "$dependencies" ]; then
	${pkg_add_cmd} $dependencies > ${bulklog}/${pkgname}/depends.log 2>&1
fi
# Build package, create a separate log file for each major phase
run_make run_direct checksum  > ${bulklog}/${pkgname}/checksum.log 2>&1 || cleanup
run_make ${run_build} configure  > ${bulklog}/${pkgname}/configure.log 2>&1 || cleanup
run_make ${run_build} all  > ${bulklog}/${pkgname}/build.log 2>&1 || cleanup
run_make ${run_install} stage-install  > ${bulklog}/${pkgname}/install.log 2>&1 || cleanup
run_make run_direct package  > ${bulklog}/${pkgname}/package.log 2>&1 || cleanup

# When using DESTDIR build, add the package once to test install rules.
# This is not done for potential bootstrap packages as they might already
# be installed.
if [ "${use_destdir}" != "no" ] && \
   [ -z "${is_bootstrap}" ]; then
	${pkg_add_cmd} ${pkgname} \
	    > ${bulklog}/${pkgname}/package.log 2>&1 || cleanup
fi

# Clean build area
${make} clean > ${bulklog}/${pkgname}/clean.log 2>&1

# Test uninstall rules. This is not for cross-compiling as the install script
# is not run in that case anyway. This is also not done for packages marked as
# part of the bootstrap, those have the preserve flag set.
if [ "$cross_compile" = "no" ] && \
   [ -z "${is_bootstrap}" ]; then
	${pkg_delete} -K ${cur_pkgdb} ${pkgname} > ${bulklog}/${pkgname}/deinstall.log 2>&1
fi

# Comment the following out if you want to test all deinstall scripts.
# This is quite expensive and mostly redundant, so it is disabled by default.
#${pkg_delete} -r \* > /dev/null 2>&1 || true

# Cleanup build logs on success
rm -R ${bulklog}/${pkgname}