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}
87 default_depends sbuild sbuildsrv
88 default_depends sign signkey
89 default_depends upload uploadpath
90 default_depends dput dputtarget
91 : ${DEB_BUILD_OPTIONS=parallel=4}; export DEB_BUILD_OPTIONS
93 ###--------------------------------------------------------------------------
100 Usage: $prog [-v] BUILDOPT
109 [no]upload[=SERVER:PATH]
120 ## Parse simple options.
122 while getopts "hv" opt
; do
129 shift $
((OPTIND
- 1))
131 ## Parse the build options.
134 eval "p=\${$want+t}\${$want-nil}"
137 nil
) echo >&2 "$prog: $want not set"; exit 1 ;;
142 checkout
) checkout
=yes checkoutrev
=HEAD
;;
143 checkout
=*) checkout
=yes checkoutrev
=${opt#*=} ;;
144 release
) build
=release
;;
145 norelease
) build
=test ;;
146 setup
) setup
=yes setupcmd
=mdw-setup
;;
147 setup
=*) setup
=yes setupcmd
=${opt#*=} ;;
148 upload
) maybe_set upload uploadpath
;;
149 upload
=*) upload
=yes uploadpath
=${opt#*=} ;;
150 sign
) maybe_set sign signkey
;;
151 sign
=*) sign
=yes signkey
=${opt#*=} ;;
152 sbuild
) maybe_set sbuild sbuildsrv
;;
153 sbuild
=*) sbuild
=yes sbuildsrv
=${opt#*=} ;;
154 dput
) maybe_set dput dputtarget
;;
155 dput
=*) dput
=yes dputtarget
=${opt#*=} ;;
156 make=*) make=${opt#*=} ;;
158 distcheck | debian | clean | vpath | native
)
161 nocheckout | nosetup | nodistcheck | nodebian | \
162 noupload | nodput | noclean | novpath | nonative | nosbuild | nosign
)
172 ## Parse DEB_BUILD_OPTIONS.
174 set -- $DEB_BUILD_OPTIONS
177 parallel
=*) jobs=${opt#*=} ;;
182 case $jobs in 1) ;; *) makeopts
="$makeopts -j$jobs" ;; esac
184 ###--------------------------------------------------------------------------
185 ### Utility functions.
187 ## File descriptor assignments:
189 ## 0 -- original stdin (never touched)
190 ## 1, 2 -- stdout, stderr, redirected to 3 while running comamnds
191 ## log -- original stderr (verbose), or logfile (quiet); captures command
193 ## diag -- null (verbose), or logfile (quiet); primary diagnostic output
194 ## term -- orginal stderr; secondary diagnostic output (with colours)
196 exec {log
}>&2 {diag
}>/dev
/null
{term
}>&2
200 echo $message >&$diag
201 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&$term
211 release
) fail
"$*" ;;
212 *) notify
5 "??? $*" ;;
227 "$@" 2>&$log {log
}>&- {diag
}>&- {term
}>&- || fail
"$1: exit $?"
230 run
() { runx
"$@" >&$log; }
233 echo -n
"(test $*)" >&$diag
234 if "$@" >&$diag 2>&$diag {log
}>&- {diag
}>&- {term
}>&-; then
243 ###--------------------------------------------------------------------------
246 ## Find the top-level package directory.
247 while [ ! -f configure.ac
-a
! -f configure.
in -a \
248 ! -f .links
-a
! -d .git
]; do
251 fail
"couldn't find top-level directory"
256 assign toppath $
(pwd)
257 assign srcpath
$toppath
259 ## Build any necessary qualifiers.
261 case ${SBOX_SESSION_DIR+t},${DEB_BUILD_ARCH+t} in
262 t
,t
) qual
=$qual$sep$DEB_BUILD_ARCH; sep
=- ;;
263 t
,*) fail
"unknown build arch" ;;
266 ## Construct the output directory.
267 assign releasepath
$srcpath/dist-
$build$qual
268 chmod -R
+w
$releasepath 2>/dev
/null ||
:
269 rm -rf
$releasepath 2>/dev
/null ||
:
273 exec {log
}>&- {diag
}>&-
274 exec {diag
}>$releasepath/mdw-build.log
{log
}>&$diag ||
275 fail
"Failed to create log."
279 ## Do we have a Git repository?
280 case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
282 fail
"Inconsistent options: can't check out without setup."
285 info
"No Git repository found."
286 checkout
=no gitver
=none
290 [ "$(git ls-files -m)" = "" ] ||
291 warn
"working tree has uncommitted changes"
295 ## Is there Debian build equipment?
296 case "$debian,$(yesno [ -d $srcpath/debian ])" in
298 info
"No debian directory found."
299 debian
=no debver
=none
305 debver
=$
(dpkg-parsechangelog |
sed -n
's/^Version: //p')
306 debsrc
=$
(dpkg-parsechangelog |
sed -n
's/^Source: //p')
307 debname
=$
(git config user.name
) debemail
=$
(git config user.email
)
311 ## Maybe check out a copy of the source.
315 run git clone
-sn
$srcpath/.git _source
316 assign srcpath
$releasepath/_source
318 run git update-ref refs
/heads
/mdw-build
$checkoutrev ""
319 run git symbolic-ref HEAD refs
/heads
/mdw-build
320 run git read-tree
--reset refs
/heads
/mdw-build
321 run git checkout-index
-afu
322 assign gitversion
"$(git describe --abbrev=4)"
326 ## Check the version number.
328 case "$gitversion,$debver" in
332 dvref
=$
(echo "$debver" |
tr '~' '_')
333 if [ "$gitversion" = "$dvref" ]; then
334 assign debversion
"$debver"
336 warn
"Git version $gitversion doesn't match Debian version $debver"
338 dver
=$
(echo $gitversion |
sed 's/-/+/; s/-/./g')
339 case $debver in *~
) dver
=$debver$dver ;; esac
340 assign debversion
"$dver"
346 ## Maybe refresh the build machinery.
353 ## Initialize the build directory.
354 case "$vpath,$(yesno [ -e $srcpath/configure ])" in
356 assign buildpath
$releasepath/_build
359 run
$srcpath/configure
362 info
"VPATH build disabled"
363 assign buildpath
$srcpath
369 info
"no configure script"
370 assign buildpath
$srcpath
375 ## Discover the release name.
376 cat >find-distdir.mk
<<'EOF'
379 @echo >&$(fd) $(distdir)
382 $
({ $make -f find-distdir.mk print-distdir fd
=$t >/dev
/null
2>&1; } {t
}>&1)
384 ## Get a tarball distribution.
387 run
$make $makeopts distcheck
390 run
$make $makeopts dist
398 if ! tar tf
$buildpath/$distdir.
tar.gz
2>/dev
/null |
grep -q RELEASE
400 fail
"missing RELEASE file in distribution"
405 run
mv $buildpath/$distdir.
tar.gz .
408 run gpg
-u
$signkey -ab
-o
$distdir.
tar.gz.gpg
$distdir.
tar.gz
412 ## Maybe build the Debian packages.
415 run
tar xvfz
$distdir.
tar.gz
419 cat - debian
/changelog
>debian
/changelog.new
<<EOF
420 $debsrc ($debversion) experimental; urgency=low
422 * Hacking in process, not intended for release.
424 -- $debname <$debemail> $now
427 mv debian
/changelog.new debian
/changelog
430 sbuildargs
=$sbuildsrv
431 case $sbuild,$build in
433 case $sign in yes) sbuildargs
="-k$signkey $sbuildargs" ;; esac
436 if [ -d
$toppath/dist-
$build.pkgs
]; then
437 sbuildargs
="-p$toppath/dist-$build.pkgs $sbuildargs"
441 case $sbuild,$build,$sign in
442 yes,*) run mdw-sbuild
$sbuildargs ;;
443 no
,release
,yes) run dpkg-buildpackage
-k
$signkey ;;
444 no
,*) run dpkg-buildpackage
-us
-uc
;;
449 ## Maybe upload Debian packages.
451 case "$upload,$build" in
452 yes,test) info
"Test build: not uploading." ;;
453 yes,release
) run rsync
$distdir.
tar.gz
$distdir.
tar.gz.gpg
$uploadpath ;;
455 case "$debian,$upload,$dput,$build" in
456 yes,yes,yes,release
) run dput
-f
$dputtarget *.changes
;;
462 rm -rf
$releasepath/$distdir
463 rm -rf
$releasepath/_source
464 rm -rf
$releasepath/_build
471 ###----- That's all, folks --------------------------------------------------