3 ### Build script for Debian packages
5 ### (c) 2008 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This program is free software; you can redistribute it and/or modify
11 ### it under the terms of the GNU General Public License as published by
12 ### the Free Software Foundation; either version 2 of the License, or
13 ### (at your option) any later version.
15 ### This program is distributed in the hope that it will be useful,
16 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ### GNU General Public License for more details.
20 ### You should have received a copy of the GNU General Public License
21 ### along with this program; if not, write to the Free Software Foundation,
22 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ###--------------------------------------------------------------------------
25 ### Conventions for build systems.
27 ### This script is designed to work with a variety of `make'-based build
28 ### systems, but there are a number of conventions which must be followed if
29 ### this is going to work properly.
31 ### * There must be a `configure.ac', `configure.in', or `.links' file, or
32 ### a `.git' directory in the project top-level, so that we can find it.
34 ### * The following `make' variables must be assigned in the top-level
35 ### Makefile, after `mdw-build' has constructed it.
37 ### distdir The name of the top-level project directory in the
38 ### source distribution, and the base name for
39 ### distribution archives; should be of the form
40 ### `PROJECT-VERSION'.
42 ### The following `make' targets must be available in the top-level
45 ### dist Write to $(distdir).tar.gz a source distribution of
48 ### distcheck As for `dist', but also build and test the project.
50 ### * The source distribution constructed by `make dist' must contain a file
51 ### $(distdir)/RELEASE containing the release name. This isn't currently
52 ### tested, but it might be later.
56 ###--------------------------------------------------------------------------
59 unset checkout checkoutrev
62 unset sbuild sbuildsrv
63 unset upload uploadpath
65 unset build distcheck debian clean vpath native
67 "/etc/mdw-build.conf" \
68 "${XDG_CONFIG_HOME-$HOME/.config}/mdw-build.conf" \
71 if [ -f "$i" ]; then . "$i"; fi
75 eval "p=\${$var+t} q=\${$want+t}"
76 case $p,$q in t,*) ;; *,t) eval "$var=yes" ;; *) eval "$var=no" ;; esac
78 : ${checkout=yes} ${checkoutrev=HEAD}
80 : ${setup=yes} ${setupcmd=mdw-setup}
86 default_depends sbuild sbuildsrv
87 default_depends sign signkey
88 default_depends upload uploadpath
89 default_depends dput dputtarget
90 : ${DEB_BUILD_OPTIONS=parallel=4}; export DEB_BUILD_OPTIONS
92 ###--------------------------------------------------------------------------
99 Usage: $prog [-v] BUILDOPT
108 [no]upload[=SERVER:PATH]
118 ## Parse simple options.
120 while getopts "hv" opt; do
127 shift $((OPTIND - 1))
129 ## Parse the build options.
132 eval "p=\${$want+t}\${$want-nil}"
135 nil) echo >&2 "$prog: $want not set"; exit 1 ;;
140 checkout) checkout=yes checkoutrev=HEAD ;;
141 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
142 release) build=release ;;
143 norelease) build=test ;;
144 setup) setup=yes setupcmd=mdw-setup ;;
145 setup=*) setup=yes setupcmd=${opt#*=} ;;
146 upload) maybe_set upload uploadpath ;;
147 upload=*) upload=yes uploadpath=${opt#*=} ;;
148 sign) maybe_set sign signkey ;;
149 sign=*) sign=yes signkey=${opt#*=} ;;
150 sbuild) maybe_set sbuild sbuildsrv ;;
151 sbuild=*) sbuild=yes sbuildsrv=${opt#*=} ;;
152 dput) maybe_set dput dputtarget ;;
153 dput=*) dput=yes dputtarget=${opt#*=} ;;
155 distcheck | debian | clean | vpath | native)
158 nocheckout | nosetup | nodistcheck | nodebian | \
159 noupload | nodput | noclean | novpath | nonative | nosbuild | nosign)
169 ## Parse DEB_BUILD_OPTIONS.
171 set -- $DEB_BUILD_OPTIONS
174 parallel=*) jobs=${opt#*=} ;;
179 case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
181 ###--------------------------------------------------------------------------
182 ### Utility functions.
184 exec 3>&2 4>/dev/null 5>&2
189 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
199 release) fail "$*" ;;
200 *) notify 5 "??? $*" ;;
215 "$@" 2>&3 || fail "$1: exit $?"
218 run () { runx "$@" >&3; }
221 echo -n "(test $*)" >&4
222 if "$@" >&4 2>&4; then
231 ###--------------------------------------------------------------------------
234 ## Find the top-level package directory.
235 while [ ! -f configure.ac -a ! -f configure.in -a \
236 ! -f .links -a ! -d .git ]; do
239 fail "couldn't find top-level directory"
244 assign toppath $(pwd)
245 assign srcpath $toppath
247 ## Build any necessary qualifiers.
249 case ${SBOX_SESSION_DIR+t},${DEB_BUILD_ARCH+t} in
250 t,t) qual=$qual$sep$DEB_BUILD_ARCH; sep=- ;;
251 t,*) fail "unknown build arch" ;;
254 ## Construct the output directory.
255 assign releasepath $srcpath/dist-$build$qual
256 chmod -R +w $releasepath 2>/dev/null || :
257 rm -rf $releasepath 2>/dev/null || :
261 exec 4>$releasepath/mdw-build.log 3>&4 ||
262 fail "Failed to create log."
266 ## Do we have a Git repository?
267 case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
269 fail "Inconsistent options: can't check out without setup."
272 info "No Git repository found."
273 checkout=no gitver=none
277 [ "$(git ls-files -m)" = "" ] ||
278 warn "working tree has uncommitted changes"
282 ## Is there Debian build equipment?
283 case "$debian,$(yesno [ -d $srcpath/debian ])" in
285 info "No debian directory found."
286 debian=no debver=none
292 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p')
293 debsrc=$(dpkg-parsechangelog | sed -n 's/^Source: //p')
294 debname=$(git config user.name) debemail=$(git config user.email)
298 ## Maybe check out a copy of the source.
302 run git clone -sn $srcpath/.git _source
303 assign srcpath $releasepath/_source
305 run git checkout -b mdw-build $checkoutrev
306 assign gitversion "$(git describe --abbrev=4)"
310 ## Check the version number.
312 case "$gitversion,$debver" in
316 dvref=$(echo "$debver" | tr '~' '_')
317 if [ "$gitversion" = "$dvref" ]; then
318 assign debversion "$debver"
320 warn "Git version $gitversion doesn't match Debian version $debver"
322 dver=$(echo $gitversion | sed 's/-/+/; s/-/./g')
323 case $debver in *~) dver=$debver$dver ;; esac
324 assign debversion "$dver"
330 ## Maybe refresh the build machinery.
337 ## Initialize the build directory.
338 case "$vpath,$(yesno [ -e $srcpath/configure ])" in
340 assign buildpath $releasepath/_build
343 run $srcpath/configure
346 info "VPATH build disabled"
347 assign buildpath $srcpath
353 info "no configure script"
354 assign buildpath $srcpath
359 ## Discover the release name.
360 cat >find-distdir.mk <<'EOF'
366 $({ make -f find-distdir.mk print-distdir >/dev/null 2>&1; } 3>&1)
368 ## Get a tarball distribution.
371 run make $makeopts distcheck
374 run make $makeopts dist
382 if ! tar tf $buildpath/$distdir.tar.gz 2>/dev/null | grep -q RELEASE
384 fail "missing RELEASE file in distribution"
389 run mv $buildpath/$distdir.tar.gz .
392 run gpg -u$signkey -ab -o$distdir.tar.gz.gpg $distdir.tar.gz
396 ## Maybe build the Debian packages.
399 run tar xvfz $distdir.tar.gz
403 cat - debian/changelog >debian/changelog.new <<EOF
404 $debsrc ($debversion) experimental; urgency=low
406 * Hacking in process, not intended for release.
408 -- $debname <$debemail> $now
411 mv debian/changelog.new debian/changelog
414 sbuildargs=$sbuildsrv
415 case $sbuild,$build in
417 case $sign in yes) sbuildargs="-k$signkey $sbuildargs" ;; esac
420 if [ -d $toppath/dist-$build.pkgs ]; then
421 sbuildargs="-p$toppath/dist-$build.pkgs $sbuildargs"
425 case $sbuild,$build,$sign in
426 yes,*) run mdw-sbuild $sbuildargs ;;
427 no,release,yes) run dpkg-buildpackage -k$signkey ;;
428 no,*) run dpkg-buildpackage -us -uc ;;
433 ## Maybe upload Debian packages.
435 case "$upload,$build" in
436 yes,test) info "Test build: not uploading." ;;
437 yes,release) run rsync $distdir.tar.gz $distdir.tar.gz.gpg $uploadpath ;;
439 case "$debian,$upload,$dput,$build" in
440 yes,yes,yes,release) run dput -f $dputtarget *.changes ;;
446 rm -rf $releasepath/$distdir
447 rm -rf $releasepath/_source
448 rm -rf $releasepath/_build
455 ###----- That's all, folks --------------------------------------------------