cvsimport
[findutils.git] / import-gnulib.sh
1 #! /bin/sh
2 #
3 # import-gnulib.sh -- imports a copy of gnulib into findutils
4 # Copyright (C) 2003,2004,2005,2006,2007 Free Software Foundation, Inc.
5 #
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.
10 #
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.
15 #
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/>.
18 #
19 ##########################################################################
20 #
21 # This script is intended to populate the "gnulib" directory
22 # with a subset of the gnulib code, as provided by "gnulib-tool".
23 #
24 # To use it, just run this script with the top-level sourec directory
25 # as your working directory.
26
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
31
32 ## Defaults
33 # cvsdir=/doesnotexist
34 git_repo="git://git.savannah.gnu.org/gnulib.git"
35 configfile="./import-gnulib.config"
36 need_checkout=yes
37
38
39 # Remember arguments for comments we inject into output files
40 original_cmd_line_args="$@"
41
42 usage() {
43 cat >&2 <<EOF
44 usage: $0 [-d gnulib-directory]
45
46 The default behaviour is to check out the Gnulib code via anonymous
47 CVS (or update it if there is a version already checked out). The
48 checkout or update is performed to the gnulib version indicated in
49 the configuration file $configfile.
50
51 If you wish to work with a different version of gnulib, use the -d option
52 to specify the directory containing the gnulib code.
53 EOF
54 }
55
56
57 do_checkout () {
58 local gitdir="$1"
59 echo checking out gnulib from GIT in $gitdir
60
61 if [ -z "$gnulib_version" ] ; then
62 echo "Error: There should be a gnulib_version setting in $configfile, but there is not." >&2
63 exit 1
64 fi
65
66
67 if ! [ -d "$gitdir" ] ; then
68 if mkdir "$gitdir" ; then
69 echo "Created $gitdir"
70 else
71 echo "Failed to create $gitdir" >&2
72 exit 1
73 fi
74 fi
75
76 (
77 # Change directory unconditionally. We used to do this to avoid
78 # the cvs client picking up defaults from findutils' ./CVS/*, but
79 # now we just do it for the sake of a minimum change.
80 cd $gitdir
81
82 if test -d gnulib/.git ; then
83 echo "Git repository was already initialised."
84 else
85 echo "Cloning the git repository..."
86 # In the future we may use a shallow clone to
87 # save bandwidth.
88 git clone "$git_repo"
89 fi
90 cd gnulib
91 set -x
92 git fetch origin
93 git checkout "$gnulib_version"
94 set +x
95 )
96 }
97
98 run_gnulib_tool() {
99 local tool="$1"
100 if test -f "$tool"
101 then
102 true
103 else
104 echo "$tool does not exist, did you specify the right directory?" >&2
105 exit 1
106 fi
107
108 if test -x "$tool"
109 then
110 true
111 else
112 echo "$tool is not executable" >&2
113 exit 1
114 fi
115
116
117 if [ -d gnulib ]
118 then
119 echo "Warning: directory gnulib already exists." >&2
120 else
121 mkdir gnulib
122 fi
123
124 set -x
125 if "$tool" --import --symlink --with-tests --dir=. --lib=libgnulib --source-base=gnulib/lib --m4-base=gnulib/m4 $modules
126 then
127 set +x
128 else
129 set +x
130 echo "$tool failed, exiting." >&2
131 exit 1
132 fi
133
134 # gnulib-tool does not remove broken symlinks leftover from previous runs;
135 # this assumes GNU find, but should be a safe no-op if it is not
136 find -L gnulib -lname '*' -delete 2>/dev/null || :
137 }
138
139 rehack() {
140 echo "Updating the license of $1"
141 # Use cp to get the permissions right first
142 cp -fp "$1" "$1".new
143 sed -e \
144 's/Free Software Foundation\([;,]\) either version [2]/Free Software Foundation\1 either version 3/' < "$1" > "$1".new
145 if cmp "$1" "$1".new >/dev/null
146 then
147 rm -f "$1".new
148 else
149 rm -f "$1" && mv "$1".new "$1"
150 fi
151 }
152
153
154
155 copyhack() {
156 src="$1"
157 dst="$2"
158 shift 2
159 if test -d "$dst"
160 then
161 dst="$dst"/"$(basename $src)"
162 fi
163 cp -fp "$src" "$dst" && rehack "$dst"
164
165 }
166
167
168 update_licenses() {
169 for f in $gpl3_update_files
170 do
171 rehack "$f" || exit
172 done
173 }
174
175
176
177 hack_gnulib_tool_output() {
178 local gnulibdir="${1}"
179 for file in $extra_files; do
180 case $file in
181 */mdate-sh | */texinfo.tex) dest=doc;;
182 *) dest=build-aux;;
183 esac
184 copyhack "${gnulibdir}"/"$file" "$dest" || exit
185 done
186
187
188
189
190 cat > gnulib/Makefile.am <<EOF
191 # Copyright (C) 2004 Free Software Foundation, Inc.
192 #
193 # This file is free software, distributed under the terms of the GNU
194 # General Public License. As a special exception to the GNU General
195 # Public License, this file may be distributed as part of a program
196 # that contains a configuration script generated by Automake, under
197 # the same distribution terms as the rest of that program.
198 #
199 # This file was generated by $0 $original_cmd_line_args.
200 #
201 SUBDIRS = lib
202 EOF
203 }
204
205
206 refresh_output_files() {
207 aclocal -I m4 -I gnulib/m4 &&
208 autoheader &&
209 autoconf &&
210 automake --add-missing --copy
211 }
212
213
214 update_version_file() {
215 local ver
216 outfile="lib/gnulib-version.c"
217 if [ -z "$gnulib_version" ] ; then
218 ver="unknown (locally modified code; no version number available)"
219 else
220 ver="$gnulib_version"
221 fi
222
223
224 cat > "${outfile}".new <<EOF
225 /* This file is automatically generated by $0 and simply records which version of gnulib we used. */
226 const char * const gnulib_version = "$ver";
227 EOF
228 if test -f "$outfile" ; then
229 if diff "${outfile}".new "${outfile}" > /dev/null ; then
230 rm "${outfile}".new
231 return 0
232 fi
233 fi
234 mv "${outfile}".new "${outfile}"
235 }
236
237
238 move_cvsdir() {
239 local cvs_git_root=":pserver:anonymous@pserver.git.sv.gnu.org:/gnulib.git"
240
241 if test -d gnulib-cvs/gnulib/CVS
242 then
243 if test x"$(cat gnulib-cvs/gnulib/CVS/Root)" == x"$cvs_git_root"; then
244 # We cannot use the git-cvspserver interface because
245 # "update -D" doesn't work.
246 echo "WARNING: Migrating from git-cvs-pserver to native git..." >&2
247 savedir=gnulib-cvs.before-nativegit-migration
248 else
249 # The old CVS repository is not updated any more.
250 echo "WARNING: Migrating from old CVS repository to native git" >&2
251 savedir=gnulib-cvs.before-git-migration
252 fi
253 mv gnulib-cvs $savedir || exit 1
254 echo "Please delete $savedir eventually"
255 fi
256 }
257
258 main() {
259 ## Option parsing
260 local gnulibdir=/doesnotexist
261 while getopts "d:a" opt
262 do
263 case "$opt" in
264 d) gnulibdir="$OPTARG" ; need_checkout=no ;;
265 a) refresh_output_files && update_licenses ; exit $? ;;
266 **) usage; exit 1;;
267 esac
268 done
269
270 # We need the config file to tell us which modules
271 # to use, even if we don't want to know the CVS version.
272 . $configfile || exit 1
273
274 ## If -d was not given, do update
275 if [ $need_checkout = yes ] ; then
276 if ! git version > /dev/null; then
277 cat >&2 <<EOF
278 You now need the tool 'git' in order to check out the correct version
279 of the gnulib code. See http://git.or.cz/ for more information about git.
280 EOF
281 exit 1
282 fi
283 move_cvsdir
284 do_checkout gnulib-git
285 gnulibdir=gnulib-git/gnulib
286 else
287 echo "Warning: using gnulib code which already exists in $gnulibdir" >&2
288 fi
289
290 ## Invoke gnulib-tool to import the code.
291 local tool="${gnulibdir}"/gnulib-tool
292
293 if run_gnulib_tool "${tool}" &&
294 hack_gnulib_tool_output "${gnulibdir}" &&
295 refresh_output_files &&
296 update_licenses &&
297 update_version_file ; then
298 echo Done.
299 else
300 echo FAILED >&2
301 exit 1
302 fi
303 }
304
305 main "$@"