Improve gnulib-tool usage.
[findutils.git] / import-gnulib.sh
blob8e04b91911dc416bd9321dbb8f37b3e164c42b6c
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." >&2
133 else
134 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
147 # gnulib-tool does not remove broken symlinks leftover from previous runs;
148 # this assumes GNU find, but should be a safe no-op if it is not
149 find -L gnulib -lname '*' -delete 2>/dev/null || :
153 hack_gnulib_tool_output() {
154 local gnulibdir="${1}"
155 for file in $extra_files; do
156 case $file in
157 */mdate-sh | */texinfo.tex) dest=doc;;
158 *) dest=build-aux;;
159 esac
160 test -d "$dest" || mkdir "$dest"
161 cp -fp "${gnulibdir}"/"$file" "$dest" || exit
162 done
167 cat > gnulib/Makefile.am <<EOF
168 # Copyright (C) 2004 Free Software Foundation, Inc.
170 # This file is free software, distributed under the terms of the GNU
171 # General Public License. As a special exception to the GNU General
172 # Public License, this file may be distributed as part of a program
173 # that contains a configuration script generated by Automake, under
174 # the same distribution terms as the rest of that program.
176 # This file was generated by $0 $@.
178 SUBDIRS = lib
183 refresh_output_files() {
184 aclocal -I m4 -I gnulib/m4 &&
185 autoheader &&
186 autoconf &&
187 automake --add-missing --copy
191 update_version_file() {
192 local ver
193 outfile="lib/gnulib-version.c"
194 if [ -z "$gnulib_version" ] ; then
195 ver="unknown (locally modified code; no version number available)"
196 else
197 ver="$gnulib_version"
201 cat > "${outfile}".new <<EOF
202 /* This file is automatically generated by $0 and simply records which version of gnulib we used. */
203 const char * const gnulib_version = "$ver";
205 if test -f "$outfile" ; then
206 if diff "${outfile}".new "${outfile}" > /dev/null ; then
207 rm "${outfile}".new
208 return 0
211 mv "${outfile}".new "${outfile}"
215 main() {
216 ## Option parsing
217 local gnulibdir=/doesnotexist
218 while getopts "d:" opt
220 case "$opt" in
221 d) gnulibdir="$OPTARG" ; need_checkout=no ;;
222 **) usage; exit 1;;
223 esac
224 done
226 # We need the config file to tell us which modules
227 # to use, even if we don't want to know the CVS version.
228 . $configfile || exit 1
230 ## If -d was not given, do CVS checkout/update
231 if [ $need_checkout = yes ] ; then
232 do_checkout gnulib-cvs
233 gnulibdir=gnulib-cvs/gnulib
234 else
235 echo "Warning: using gnulib code which already exists in $gnulibdir" >&2
238 ## Invoke gnulib-tool to import the code.
239 local tool="${gnulibdir}"/gnulib-tool
241 if run_gnulib_tool "${tool}" &&
242 hack_gnulib_tool_output "${gnulibdir}" &&
243 refresh_output_files &&
244 update_version_file ; then
245 echo Done.
246 else
247 echo FAILED >&2
248 exit 1
252 main "$@"