cvsimport
[findutils.git] / import-gnulib.sh
blob9d9f9656489e563b5a2fb8a8c1aed0f6bc09904c
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 3 of the License, or
9 # (at your option) 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, see <http://www.gnu.org/licenses/>.
19 ##########################################################################
21 # This script is intended to populate the "gnulib" directory
22 # with a subset of the gnulib code, as provided by "gnulib-tool".
24 # To use it, just run this script with the top-level sourec directory
25 # as your working directory.
27 # If CDPATH is set, it will sometimes print the name of the directory
28 # to which you have moved. Unsetting CDPATH prevents this, as does
29 # prefixing it with ".".
30 unset CDPATH
32 ## Defaults
33 # cvsdir=/doesnotexist
34 configfile="./import-gnulib.config"
35 need_checkout=yes
38 # Remember arguments for comments we inject into output files
39 original_cmd_line_args="$@"
41 usage() {
42 cat >&2 <<EOF
43 usage: $0 [-d gnulib-directory]
45 The default behaviour is to check out the Gnulib code via anonymous
46 CVS (or update it if there is a version already checked out). The
47 checkout or update is performed to the gnulib version indicated in
48 the configuration file $configfile.
50 If you wish to work with a different version of gnulib, use the -d option
51 to specify the directory containing the gnulib code.
52 EOF
56 do_checkout () {
57 local cvsdir="$1"
58 echo checking out gnulib from CVS in $cvsdir
60 if [ -z "$gnulib_version" ] ; then
61 echo "Error: There should be a gnulib_version setting in $configfile, but there is not." >&2
62 exit 1
66 if ! [ -d "$cvsdir" ] ; then
67 if mkdir "$cvsdir" ; then
68 echo "Created $cvsdir"
69 else
70 echo "Failed to create $cvsdir" >&2
71 exit 1
75 # Decide if gnulib_version is probably a date or probably a tag.
76 if date -d yesterday >/dev/null ; then
77 # It looks like GNU date is available
78 if date -d "$gnulib_version" >/dev/null ; then
79 # Looks like a date.
80 cvs_sticky_option="-D"
81 else
82 echo "Warning: assuming $gnulib_version is a CVS tag rather than a date" >&2
83 cvs_sticky_option="-r"
85 else
86 # GNU date unavailable, assume the version is a date
87 cvs_sticky_option="-D"
93 # Change directory unconditionally (rater than using checkout
94 # -d) so that cvs does not pick up defaults from ./CVS. Those
95 # defaults refer to our own CVS repository for our code, not
96 # to gnulib.
97 cd $cvsdir
99 # gnulib now uses git as master repository, but used to use
100 # CVS. Check that we are not running against an old working
101 # directory which is still pointing at the old CVS repository.
102 rootfile=gnulib/CVS/Root
103 cvs_git_root=":pserver:anonymous@pserver.git.sv.gnu.org:/gnulib.git"
105 if test -d gnulib/CVS
106 then
107 if test x"$(cat $rootfile)" == x"$cvs_git_root"; then
108 echo "Using the git repository via git-cvs-pserver..."
109 else
110 echo "WARNING: Migrating from old CVS repository" >&2
111 # Force use of "cvs checkout" as opposed to update.
112 mv gnulib gnulib.before-git-migration
116 if test -d gnulib/CVS ; then
117 cd gnulib
118 cmd=update
119 root="" # use previous
120 args=
121 else
122 cmd=checkout
123 root="-d $cvs_git_root"
124 args="-d gnulib HEAD"
126 set -x
127 cvs -q $root $cmd $cvs_sticky_option "$gnulib_version" $args
128 set +x
132 run_gnulib_tool() {
133 local tool="$1"
134 if test -f "$tool"
135 then
136 true
137 else
138 echo "$tool does not exist, did you specify the right directory?" >&2
139 exit 1
142 if test -x "$tool"
143 then
144 true
145 else
146 echo "$tool is not executable" >&2
147 exit 1
151 if [ -d gnulib ]
152 then
153 echo "Warning: directory gnulib already exists." >&2
154 else
155 mkdir gnulib
158 set -x
159 if "$tool" --import --symlink --with-tests --dir=. --lib=libgnulib --source-base=gnulib/lib --m4-base=gnulib/m4 $modules
160 then
161 set +x
162 else
163 set +x
164 echo "$tool failed, exiting." >&2
165 exit 1
168 # gnulib-tool does not remove broken symlinks leftover from previous runs;
169 # this assumes GNU find, but should be a safe no-op if it is not
170 find -L gnulib -lname '*' -delete 2>/dev/null || :
173 rehack() {
174 echo "Updating the license of $1"
175 # Use cp to get the permissions right first
176 cp -fp "$1" "$1".new
177 sed -e \
178 's/Free Software Foundation\([;,]\) either version [2]/Free Software Foundation\1 either version 3/' < "$1" > "$1".new
179 if cmp "$1" "$1".new >/dev/null
180 then
181 rm -f "$1".new
182 else
183 rm -f "$1" && mv "$1".new "$1"
189 copyhack() {
190 src="$1"
191 dst="$2"
192 shift 2
193 if test -d "$dst"
194 then
195 dst="$dst"/"$(basename $src)"
197 cp -fp "$src" "$dst" && rehack "$dst"
202 update_licenses() {
203 for f in $gpl3_update_files
205 rehack "$f" || exit
206 done
211 hack_gnulib_tool_output() {
212 local gnulibdir="${1}"
213 for file in $extra_files; do
214 case $file in
215 */mdate-sh | */texinfo.tex) dest=doc;;
216 *) dest=build-aux;;
217 esac
218 copyhack "${gnulibdir}"/"$file" "$dest" || exit
219 done
224 cat > gnulib/Makefile.am <<EOF
225 # Copyright (C) 2004 Free Software Foundation, Inc.
227 # This file is free software, distributed under the terms of the GNU
228 # General Public License. As a special exception to the GNU General
229 # Public License, this file may be distributed as part of a program
230 # that contains a configuration script generated by Automake, under
231 # the same distribution terms as the rest of that program.
233 # This file was generated by $0 $original_cmd_line_args.
235 SUBDIRS = lib
240 refresh_output_files() {
241 aclocal -I m4 -I gnulib/m4 &&
242 autoheader &&
243 autoconf &&
244 automake --add-missing --copy
248 update_version_file() {
249 local ver
250 outfile="lib/gnulib-version.c"
251 if [ -z "$gnulib_version" ] ; then
252 ver="unknown (locally modified code; no version number available)"
253 else
254 ver="$gnulib_version"
258 cat > "${outfile}".new <<EOF
259 /* This file is automatically generated by $0 and simply records which version of gnulib we used. */
260 const char * const gnulib_version = "$ver";
262 if test -f "$outfile" ; then
263 if diff "${outfile}".new "${outfile}" > /dev/null ; then
264 rm "${outfile}".new
265 return 0
268 mv "${outfile}".new "${outfile}"
272 main() {
273 ## Option parsing
274 local gnulibdir=/doesnotexist
275 while getopts "d:a" opt
277 case "$opt" in
278 d) gnulibdir="$OPTARG" ; need_checkout=no ;;
279 a) refresh_output_files && update_licenses ; exit $? ;;
280 **) usage; exit 1;;
281 esac
282 done
284 # We need the config file to tell us which modules
285 # to use, even if we don't want to know the CVS version.
286 . $configfile || exit 1
288 ## If -d was not given, do CVS checkout/update
289 if [ $need_checkout = yes ] ; then
290 do_checkout gnulib-cvs
291 gnulibdir=gnulib-cvs/gnulib
292 else
293 echo "Warning: using gnulib code which already exists in $gnulibdir" >&2
296 ## Invoke gnulib-tool to import the code.
297 local tool="${gnulibdir}"/gnulib-tool
299 if run_gnulib_tool "${tool}" &&
300 hack_gnulib_tool_output "${gnulibdir}" &&
301 refresh_output_files &&
302 update_licenses &&
303 update_version_file ; then
304 echo Done.
305 else
306 echo FAILED >&2
307 exit 1
311 main "$@"