cvsimport
[findutils.git] / import-gnulib.sh
blob3872f031a16824b900c74f59726557f4c40196ed
1 #! /bin/sh
3 # import-gnulib.sh -- imports a copy of gnulib into findutils
4 # Copyright (C) 2003,2004,2005,2006,2007 Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 # USA.
21 ##########################################################################
23 # This script is intended to populate the "gnulib" directory
24 # with a subset of the gnulib code, as provided by "gnulib-tool".
26 # To use it, just run this script with the top-level sourec directory
27 # as your working directory.
29 # If CDPATH is set, it will sometimes print the name of the directory
30 # to which you have moved. Unsetting CDPATH prevents this, as does
31 # prefixing it with ".".
32 unset CDPATH
34 ## Defaults
35 # cvsdir=/doesnotexist
36 configfile="./import-gnulib.config"
37 need_checkout=yes
40 usage() {
41 cat >&2 <<EOF
42 usage: $0 [-d gnulib-directory]
44 The default behaviour is to check out the Gnulib code via anonymous
45 CVS (or update it if there is a version already checked out). The
46 checkout or update is performed to the gnulib version indicated in
47 the configuration file $configfile.
49 If you wish to work with a different version of gnulib, use the -d option
50 to specify the directory containing the gnulib code.
51 EOF
55 do_checkout () {
56 local cvsdir="$1"
57 echo checking out gnulib from CVS in $cvsdir
59 if [ -z "$gnulib_version" ] ; then
60 echo "Error: There should be a gnulib_version setting in $configfile, but there is not." >&2
61 exit 1
65 if ! [ -d "$cvsdir" ] ; then
66 if mkdir "$cvsdir" ; then
67 echo "Created $cvsdir"
68 else
69 echo "Failed to create $cvsdir" >&2
70 exit 1
74 # Decide if gnulib_version is probably a date or probably a tag.
75 if date -d yesterday >/dev/null ; then
76 # It looks like GNU date is available
77 if date -d "$gnulib_version" >/dev/null ; then
78 # Looks like a date.
79 cvs_sticky_option="-D"
80 else
81 echo "Warning: assuming $gnulib_version is a CVS tag rather than a date" >&2
82 cvs_sticky_option="-r"
84 else
85 # GNU date unavailable, assume the version is a date
86 cvs_sticky_option="-D"
92 # Change directory unconditionally (rater than using checkout -d) so that
93 # cvs does not pick up defaults from ./CVS. Those defaults refer to our
94 # own CVS repository for our code, not to gnulib.
95 cd $cvsdir
96 if test -d gnulib/CVS ; then
97 cd gnulib
98 cmd=update
99 root="" # use previous
100 else
101 root="-d :pserver:anonymous@cvs.sv.gnu.org:/sources/gnulib"
102 cmd=checkout
103 args=gnulib
105 set -x
106 cvs -q -z3 $root $cmd $cvs_sticky_option "$gnulib_version" $args
107 set +x
111 run_gnulib_tool() {
112 local tool="$1"
113 if test -f "$tool"
114 then
115 true
116 else
117 echo "$tool does not exist, did you specify the right directory?" >&2
118 exit 1
121 if test -x "$tool"
122 then
123 true
124 else
125 echo "$tool is not executable" >&2
126 exit 1
130 if [ -d gnulib ]
131 then
132 echo "Warning: directory gnulib already exists, removing it." >&2
133 rm -rf gnulib
135 mkdir gnulib
137 set -x
138 if "$tool" --import --symlink --with-tests --dir=. --lib=libgnulib --source-base=gnulib/lib --m4-base=gnulib/m4 $modules
139 then
140 set +x
141 else
142 set +x
143 echo "$tool failed, exiting." >&2
144 exit 1
149 hack_gnulib_tool_output() {
150 local gnulibdir="${1}"
151 for file in $extra_files; do
152 case $file in
153 */mdate-sh | */texinfo.tex) dest=doc;;
154 *) dest=build-aux;;
155 esac
156 cp -fp "${gnulibdir}"/"$file" "$dest" || exit
157 done
162 cat > gnulib/Makefile.am <<EOF
163 # Copyright (C) 2004 Free Software Foundation, Inc.
165 # This file is free software, distributed under the terms of the GNU
166 # General Public License. As a special exception to the GNU General
167 # Public License, this file may be distributed as part of a program
168 # that contains a configuration script generated by Automake, under
169 # the same distribution terms as the rest of that program.
171 # This file was generated by $0 $@.
173 SUBDIRS = lib
178 refresh_output_files() {
179 aclocal -I m4 -I gnulib/m4 &&
180 autoheader &&
181 autoconf &&
182 automake --add-missing --copy
186 update_version_file() {
187 local ver
188 outfile="lib/gnulib-version.c"
189 if [ -z "$gnulib_version" ] ; then
190 ver="unknown (locally modified code; no version number available)"
191 else
192 ver="$gnulib_version"
196 cat > "${outfile}".new <<EOF
197 /* This file is automatically generated by $0 and simply records which version of gnulib we used. */
198 const char * const gnulib_version = "$ver";
200 if test -f "$outfile" ; then
201 if diff "${outfile}".new "${outfile}" > /dev/null ; then
202 rm "${outfile}".new
203 return 0
206 mv "${outfile}".new "${outfile}"
210 main() {
211 ## Option parsing
212 local gnulibdir=/doesnotexist
213 while getopts "d:a" opt
215 case "$opt" in
216 d) gnulibdir="$OPTARG" ; need_checkout=no ;;
217 a) refresh_output_files ; exit $? ;;
218 **) usage; exit 1;;
219 esac
220 done
222 # We need the config file to tell us which modules
223 # to use, even if we don't want to know the CVS version.
224 . $configfile || exit 1
226 ## If -d was not given, do CVS checkout/update
227 if [ $need_checkout = yes ] ; then
228 do_checkout gnulib-cvs
229 gnulibdir=gnulib-cvs/gnulib
230 else
231 echo "Warning: using gnulib code which already exists in $gnulibdir" >&2
234 ## Invoke gnulib-tool to import the code.
235 local tool="${gnulibdir}"/gnulib-tool
237 if run_gnulib_tool "${tool}" &&
238 hack_gnulib_tool_output "${gnulibdir}" &&
239 refresh_output_files &&
240 update_version_file ; then
241 echo Done.
242 else
243 echo FAILED >&2
244 exit 1
248 main "$@"