as released as part of build91
[pspp] / make-builder.as
1 AS_INIT[]dnl                                            -*- shell-script -*-
2 m4_divert_push([HEADER-COPYRIGHT])dnl
3 # @configure_input@
4 # make-builder -- create PSPP build scripts
5
6 # Copyright (C) 1992, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
7 # 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation,
8 # Inc.
9
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 3 of the License, or
13 # (at your option) any later version.
14
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.
19
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 m4_divert_pop([HEADER-COPYRIGHT])dnl back to BODY
24 AS_ME_PREPARE[]dnl
25
26 usage=["\
27 Usage: $0 [OPTION]... REPOSITORY BRANCH
28 where REPO and BRANCH are a Git repository and branch to clone.
29
30 Options:
31   -h, --help                Print this usage message and exit
32   -b, --build=NUMBER        Set build number (default: next available)
33   -o, --output=DIRECTORY    Set build directory (default: build\$BUILD_NUMBER)
34   -f, --force               Overwrite files in existing directory
35
36 Report bugs to <bug-gnu-pspp@gnu.org>.
37 GNU PSPP: <http://www.gnu.org/software/pspp/>.
38 General help using GNU software: <http://www.gnu.org/gethelp/>."]
39
40 version=["\
41 make-builder (@PACKAGE_NAME@) @VERSION@
42 Copyright (C) 2010 Free Software Foundation, Inc.
43 License GPLv3+/Autoconf: GNU GPL version 3 or later
44 <http://gnu.org/licenses/gpl.html>, <http://gnu.org/licenses/exceptions.html>
45 This is free software: you are free to change and redistribute it.
46 There is NO WARRANTY, to the extent permitted by law.
47
48 Written by Ben Pfaff."]
49
50 help="\
51 Try \`$as_me --help' for more information."
52
53 exit_missing_arg='
54   m4_bpatsubst([AS_ERROR([option `$[1]' requires an argument$as_nl$help])],
55     ['], ['\\''])'
56 # restore font-lock: '
57
58 # Variables.
59 outdir=
60 overwrite=false
61 BUILD_NUMBER=
62 GNULIB_REPO=git://git.sv.gnu.org/gnulib.git
63
64 # Parse command line.
65 while test $# -gt 0 ; do
66   option=[`expr "x$1" : 'x\(--[^=]*\)' \| \
67                "x$1" : 'x\(-.\)'`]
68   optarg=[`expr "x$1" : 'x--[^=]*=\(.*\)' \| \
69                "x$1" : 'x-.\(.*\)'`]
70   case $1 in
71     --version | -V )
72        echo "$version" ; exit ;;
73     --help | -h )
74        AS_ECHO(["$usage"]); exit ;;
75
76     --verbose | -v )
77        verbose=:
78        autom4te_options="$autom4te_options $1"; shift ;;
79
80     --output=* | -o?* )
81        outdir=$optarg
82        shift ;;
83     --output | -o )
84        test $# = 1 && eval "$exit_missing_arg"
85        outdir=$2
86        shift; shift ;;
87
88     --build=* | -b?* )
89        BUILD_NUMBER=$optarg
90        shift ;;
91     --build | -b )
92        test $# = 1 && eval "$exit_missing_arg"
93        BUILD_NUMBER=$2
94        shift; shift ;;
95
96     --force | -f )
97        overwrite=true ;;
98
99     -- )     # Stop option processing
100        shift; break ;;
101     - ) # Use stdin as input.
102        break ;;
103     -* )
104        exec >&2
105        AS_ERROR([invalid option `$[1]'$as_nl$help]) ;; #`
106     * )
107        break ;;
108   esac
109 done
110
111 # Find the input file.
112 case $# in
113   2)
114     PSPP_REPO=$1
115     PSPP_BRANCH=$2
116     ;;
117   *) exec >&2
118      AS_ERROR([invalid number of arguments$as_nl$help]) ;;
119 esac
120
121 # Choose build number.
122 if test "x$BUILD_NUMBER" = x; then
123     lock_file () {
124         echo $$ > $[1].$$
125         if ln $[1].$$ $[1].lock; then
126             rm $[1].$$
127             return 0
128         fi
129         pid=`cat $[1].lock`
130         if kill -0 pid; then
131             :
132         else
133             rm -f $[1].lock
134         fi
135         return 1
136     }
137     until lock_file build_number; do
138         sleep 1
139     done
140
141     if test -e buildnr; then
142         BUILD_NUMBER=`cat buildnr`
143     else
144         BUILD_NUMBER=1
145     fi
146     echo `expr $BUILD_NUMBER + 1` > buildnr
147
148     rm build_number.lock
149 fi
150
151 # Check that build number is a number.
152 case $BUILD_NUMBER in # (
153     "" | *[!0-9]*)
154         exec >&2
155         AS_ERROR([invalid build number `$BUILD_NUMBER'$as_nl$help]) #`
156         ;;
157 esac
158
159 # Ensure that git repository exists.
160 GIT_DIR=$PWD/repo.git
161 export GIT_DIR
162 if git init >/dev/null; then
163     :
164 else
165     exec >&2
166     AS_ERROR([initializing Git repository in $GIT_DIR failed])
167 fi
168
169 # Fetch PSPP source.
170 if git fetch $PSPP_REPO +$PSPP_BRANCH:refs/builds/$BUILD_NUMBER/pspp >/dev/null 2>&1; then
171     :
172 else
173     exec >&2
174     AS_ERROR([failed to fetch branch $PSPP_BRANCH from $PSPP_REPO])
175 fi
176
177 # Get PSPP commit number.
178 PSPP_COMMIT=`git rev-parse refs/builds/$BUILD_NUMBER/pspp`
179
180 # Get gnulib commit number.
181 GNULIB_COMMIT=`\
182     git show refs/builds/$BUILD_NUMBER/pspp:README.Git | \
183     sed -n 's/^[[       ]]*commit \([[0-9a-fA-F]]\{8,\}\)$/\1/p' | \
184     head -1`
185
186 # Fetch Gnulib source.
187 if git rev-parse --verify --quiet $GNULIB_COMMIT^0 > /dev/null; then
188     :
189 else
190     # We don't have the right commit yet, update Gnulib.
191     if git fetch $GNULIB_REPO '+refs/heads/*:refs/remotes/gnulib/*'; then
192         :
193     else
194         exec >&2
195         AS_ERROR([failed to fetch commit $GNULIB_COMMIT from $GNULIB_REPO])
196     fi
197 fi
198 git update-ref refs/builds/$BUILD_NUMBER/gnulib $GNULIB_COMMIT
199
200 # Choose and create output directory.
201 test -z "$outdir" && outdir=build$BUILD_NUMBER
202 if test -e "$outdir" && test $overwrite = false; then
203     exec >&2
204     AS_ERROR([$outdir exists and --force not specified$as_nl$help])
205 fi
206 AS_MKDIR_P([$outdir])
207
208 # Make source tarballs.
209 mkdir "$outdir/repo"
210 git archive --format=tar --prefix=pspp-$PSPP_COMMIT/ $PSPP_COMMIT \
211     | gzip > $outdir/repo/pspp-$PSPP_COMMIT.tar.gz
212 git archive --format=tar --prefix=gnulib-$GNULIB_COMMIT/ $GNULIB_COMMIT \
213     | gzip > $outdir/repo/gnulib-$GNULIB_COMMIT.tar.gz
214
215 # Make build scripts.
216 for base in make-src-dist make-bin-dist; do
217     sed < $base.in > "$outdir/$base" "
218 s,[@]BUILD_NUMBER[@],$BUILD_NUMBER,g
219 s,[@]PSPP_REPO[@],$PSPP_REPO,g
220 s,[@]PSPP_BRANCH[@],$PSPP_BRANCH,g
221 s,[@]PSPP_COMMIT[@],$PSPP_COMMIT,g
222 s,[@]GNULIB_COMMIT[@],$GNULIB_COMMIT,g
223 s,[@]GNULIB_REPO[@],$GNULIB_REPO,g
224 "
225     chmod +x "$outdir/$base"
226 done
227
228 # Copy in auxiliary files.
229 mkdir "$outdir/build-aux"
230 for base in gendocs.sh gendocs_template gendocs_template_min; do
231     cp "build-aux/$base" "$outdir/build-aux/"
232 done
233
234 cat <<EOF
235 OUTDIR='$outdir'
236 BUILD_NUMBER='$BUILD_NUMBER'
237 PSPP_REPO='$PSPP_REPO'
238 PSPP_BRANCH='$PSPP_BRANCH'
239 PSPP_COMMIT='$PSPP_COMMIT'
240 GNULIB_COMMIT='$GNULIB_COMMIT'
241 GNULIB_REPO='$GNULIB_REPO'
242 EOF