1#!/usr/bin/env bash
2#   Copyright (C) 1990-2020 Free Software Foundation
3#
4# This file is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8# 
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13# 
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16#
17
18# This script creates release packages for gdb, binutils, and other
19# packages which live in src.  It used to be implemented as the src-release
20# Makefile and prior to that was part of the top level Makefile, but that
21# turned out to be very messy and hard to maintain.
22
23set -e
24
25BZIPPROG=bzip2
26GZIPPROG=gzip
27LZIPPROG=lzip
28XZPROG=xz
29MD5PROG=md5sum
30MAKE=make
31CC=gcc
32CXX=g++
33
34# Default to avoid splitting info files by setting the threshold high.
35MAKEINFOFLAGS=--split-size=5000000
36
37#
38# Support for building net releases
39
40# Files in root used in any net release.
41DEVO_SUPPORT="ar-lib ChangeLog compile config config-ml.in config.guess \
42	config.rpath config.sub configure configure.ac COPYING COPYING.LIB \
43	COPYING3 COPYING3.LIB depcomp install-sh libtool.m4 ltgcc.m4 \
44	ltmain.sh ltoptions.m4 ltsugar.m4 ltversion.m4 lt~obsolete.m4 \
45	MAINTAINERS Makefile.def Makefile.in Makefile.tpl missing mkdep \
46	mkinstalldirs move-if-change README README-maintainer-mode \
47	src-release.sh symlink-tree test-driver ylwrap"
48
49# Files in devo/etc used in any net release.
50ETC_SUPPORT="Makefile.in configure configure.in standards.texi \
51	make-stds.texi standards.info* configure.texi configure.info* \
52	ChangeLog configbuild.* configdev.* fdl.texi texi2pod.pl gnu-oids.texi"
53
54# Get the version number of a given tool
55getver()
56{
57    tool=$1
58    if grep 'AC_INIT.*BFD_VERSION' $tool/configure.ac >/dev/null 2>&1; then
59	bfd/configure --version | sed -n -e '1s,.* ,,p'
60    elif test -f $tool/common/create-version.sh; then
61	$tool/common/create-version.sh $tool 'dummy-host' 'dummy-target' VER.tmp
62	cat VER.tmp | grep 'version\[\]' | sed 's/.*"\([^"]*\)".*/\1/' | sed 's/-git$//'
63        rm -f VER.tmp
64    elif test $tool = "gdb"; then
65	./gdbsupport/create-version.sh $tool 'dummy-host' 'dummy-target' VER.tmp
66	cat VER.tmp | grep 'version\[\]' | sed 's/.*"\([^"]*\)".*/\1/' | sed 's/-git$//'
67        rm -f VER.tmp
68    elif test -f $tool/version.in; then
69	head -n 1 $tool/version.in
70    else
71	echo VERSION
72    fi
73}
74
75# Setup build directory for building release tarball
76do_proto_toplev()
77{
78    package=$1
79    ver=$2
80    tool=$3
81    support_files=$4
82
83    echo "==> Cleaning sources."
84    find \( -name "*.orig" -o  -name "*.rej" -o -name "*~" -o -name ".#*" -o -name "*~$bkpat" \) -exec rm {} \;
85    
86    echo "==> Making $package-$ver/"
87    # Take out texinfo from a few places.
88    sed -e '/^all\.normal: /s/\all-texinfo //' \
89	-e '/^	install-texinfo /d' \
90	<Makefile.in >tmp
91    mv -f tmp Makefile.in
92    # configure.  --enable-gold is needed to ensure .c/.h from .y are
93    # built in the gold dir.  The disables speed the build a little.
94    enables=
95    disables=
96    for dir in binutils gas gdb gold gprof ld libctf libdecnumber readline sim; do
97	case " $tool $support_files " in
98	    *" $dir "*) enables="$enables --enable-$dir" ;;
99	    *) disables="$disables --disable-$dir" ;;
100	esac
101    done
102    echo "==> configure --target=i386-pc-linux-gnu $disables $enables"
103    ./configure --target=i386-pc-linux-gnu $disables $enables
104    $MAKE configure-host configure-target \
105	ALL_GCC="" ALL_GCC_C="" ALL_GCC_CXX="" \
106	CC_FOR_TARGET="$CC" CXX_FOR_TARGET="$CXX"
107    # Make links, and run "make diststuff" or "make info" when needed.
108    rm -rf proto-toplev
109    mkdir proto-toplev
110    dirs="$DEVO_SUPPORT $support_files $tool"
111    for d in $dirs ; do
112	if [ -d $d ]; then
113	    if [ ! -f $d/Makefile ] ; then
114		true
115	    elif grep '^diststuff:' $d/Makefile >/dev/null ; then
116		(cd $d ; $MAKE MAKEINFOFLAGS="$MAKEINFOFLAGS" diststuff) \
117		    || exit 1
118	    elif grep '^info:' $d/Makefile >/dev/null ; then
119		(cd $d ; $MAKE MAKEINFOFLAGS="$MAKEINFOFLAGS" info) \
120		    || exit 1
121	    fi
122	    if [ -d $d/proto-$d.dir ]; then
123		ln -s ../$d/proto-$d.dir proto-toplev/$d
124	    else
125		ln -s ../$d proto-toplev/$d
126	    fi
127	else
128	    if (echo x$d | grep / >/dev/null); then
129	      mkdir -p proto-toplev/`dirname $d`
130	      x=`dirname $d`
131	      ln -s ../`echo $x/ | sed -e 's,[^/]*/,../,g'`$d proto-toplev/$d
132	    else
133	      ln -s ../$d proto-toplev/$d
134	    fi
135	  fi
136	done
137	(cd etc; $MAKE MAKEINFOFLAGS="$MAKEINFOFLAGS" info)
138	$MAKE distclean
139	mkdir proto-toplev/etc
140	(cd proto-toplev/etc;
141	    for i in $ETC_SUPPORT; do
142		ln -s ../../etc/$i .
143		done)
144	#
145	# Take out texinfo from configurable dirs
146	rm proto-toplev/configure.ac
147	sed -e '/^host_tools=/s/texinfo //' \
148	    <configure.ac >proto-toplev/configure.ac
149	#
150	mkdir proto-toplev/texinfo
151	ln -s ../../texinfo/texinfo.tex	proto-toplev/texinfo/
152	if test -r texinfo/util/tex3patch ; then
153	    mkdir proto-toplev/texinfo/util && \
154		ln -s ../../../texinfo/util/tex3patch proto-toplev/texinfo/util
155	else
156	    true
157	fi
158	chmod -R og=u . || chmod og=u `find . -print`
159	#
160	# Create .gmo files from .po files.
161	for f in `find . -name '*.po' -type f -print`; do
162	    msgfmt -o `echo $f | sed -e 's/\.po$/.gmo/'` $f
163	done
164	#
165	rm -f $package-$ver
166	ln -s proto-toplev $package-$ver
167}
168
169CVS_NAMES='-name CVS -o -name .cvsignore'
170
171# Add an md5sum to the built tarball
172do_md5sum()
173{
174    echo "==> Adding md5 checksum to top-level directory"
175    (cd proto-toplev && find * -follow \( $CVS_NAMES \) -prune \
176	-o -type f -print \
177	| xargs $MD5PROG > ../md5.new)
178    rm -f proto-toplev/md5.sum
179    mv md5.new proto-toplev/md5.sum
180}
181
182# Build the release tarball
183do_tar()
184{
185    package=$1
186    ver=$2
187    echo "==> Making $package-$ver.tar"
188    rm -f $package-$ver.tar
189    find $package-$ver -follow \( $CVS_NAMES \) -prune \
190	-o -type f -print \
191	| tar cTfh - $package-$ver.tar
192}
193
194# Compress the output with bzip2
195do_bz2()
196{
197    package=$1
198    ver=$2
199    echo "==> Bzipping $package-$ver.tar.bz2"
200    rm -f $package-$ver.tar.bz2
201    $BZIPPROG -k -v -9 $package-$ver.tar
202}
203
204# Compress the output with gzip
205do_gz()
206{
207    package=$1
208    ver=$2
209    echo "==> Gzipping $package-$ver.tar.gz"
210    rm -f $package-$ver.tar.gz
211    $GZIPPROG -k -v -9 $package-$ver.tar
212}
213
214# Compress the output with lzip
215do_lz()
216{
217    package=$1
218    ver=$2
219    echo "==> Lzipping $package-$ver.tar.lz"
220    rm -f $package-$ver.tar.lz
221    $LZIPPROG -k -v -9 $package-$ver.tar
222}
223
224# Compress the output with xz
225do_xz()
226{
227    package=$1
228    ver=$2
229    echo "==> Xzipping $package-$ver.tar.xz"
230    rm -f $package-$ver.tar.xz
231    $XZPROG -k -v -9 $package-$ver.tar
232}
233
234# Compress the output with all selected compresion methods
235do_compress()
236{
237    package=$1
238    ver=$2
239    compressors=$3
240    for comp in $compressors; do
241	case $comp in
242	    bz2)
243		do_bz2 $package $ver;;
244	    gz)
245		do_gz $package $ver;;
246	    lz)
247		do_lz $package $ver;;
248	    xz)
249		do_xz $package $ver;;
250	    *)
251		echo "Unknown compression method: $comp" && exit 1;;
252	esac
253    done
254}
255
256# Add djunpack.bat the tarball
257do_djunpack()
258{
259    package=$1
260    ver=$2
261    echo "==> Adding updated djunpack.bat to top-level directory"
262    echo - 's /gdb-[0-9\.]*/$package-'"$ver"'/'
263    sed < djunpack.bat > djunpack.new \
264	-e 's/gdb-[0-9][0-9\.]*/$package-'"$ver"'/'
265    rm -f proto-toplev/djunpack.bat
266    mv djunpack.new proto-toplev/djunpack.bat
267}
268
269# Create a release package, tar it and compress it
270tar_compress()
271{
272    package=$1
273    tool=$2
274    support_files=$3
275    compressors=$4
276    verdir=${5:-$tool}
277    ver=$(getver $verdir)
278    do_proto_toplev $package $ver $tool "$support_files"
279    do_md5sum
280    do_tar $package $ver
281    do_compress $package $ver "$compressors"
282}
283
284# Create a gdb release package, tar it and compress it
285gdb_tar_compress()
286{
287    package=$1
288    tool=$2
289    support_files=$3
290    compressors=$4
291    ver=$(getver $tool)
292    do_proto_toplev $package $ver $tool "$support_files"
293    do_md5sum
294    do_djunpack $package $ver
295    do_tar $package $ver
296    do_compress $package $ver "$compressors"
297}
298
299# The FSF "binutils" release includes gprof and ld.
300BINUTILS_SUPPORT_DIRS="bfd gas include libiberty libctf opcodes ld elfcpp gold gprof intl setup.com makefile.vms cpu zlib"
301binutils_release()
302{
303    compressors=$1
304    package=binutils
305    tool=binutils
306    tar_compress $package $tool "$BINUTILS_SUPPORT_DIRS" "$compressors"
307}
308
309GAS_SUPPORT_DIRS="bfd include libiberty opcodes intl setup.com makefile.vms zlib"
310gas_release()
311{
312    compressors=$1
313    package=gas
314    tool=gas
315    tar_compress $package $tool "$GAS_SUPPORT_DIRS" "$compressors"
316}
317
318GDB_SUPPORT_DIRS="bfd include libiberty libctf opcodes readline sim intl libdecnumber cpu zlib contrib gnulib gdbsupport gdbserver"
319gdb_release()
320{
321    compressors=$1
322    package=gdb
323    tool=gdb
324    gdb_tar_compress $package $tool "$GDB_SUPPORT_DIRS" "$compressors"
325}
326
327# Corresponding to the CVS "sim" module.
328SIM_SUPPORT_DIRS="bfd opcodes libiberty include intl gdb/version.in gdb/common/create-version.sh makefile.vms zlib"
329sim_release()
330{
331    compressors=$1
332    package=sim
333    tool=sim
334    tar_compress $package $tool "$SIM_SUPPORT_DIRS" "$compressors" gdb
335}
336
337usage()
338{
339    echo "src-release.sh <options> <release>"
340    echo "options:"
341    echo "  -b: Compress with bzip2"
342    echo "  -g: Compress with gzip"
343    echo "  -l: Compress with lzip"
344    echo "  -x: Compress with xz"
345    exit 1
346}
347
348build_release()
349{
350    release=$1
351    compressors=$2
352    case $release in
353	binutils)
354	    binutils_release "$compressors";;
355	gas)
356	    gas_release "$compressors";;
357	gdb)
358	    gdb_release "$compressors";;
359	sim)
360	    sim_release "$compressors";;
361	*)
362	    echo "Unknown release name: $release" && usage;;
363    esac
364}
365
366compressors=""
367
368while getopts ":bglx" opt; do
369    case $opt in
370	b)
371	    compressors="$compressors bz2";;
372	g)
373	    compressors="$compressors gz";;
374	l)
375	    compressors="$compressors lz";;
376	x)
377	    compressors="$compressors xz";;
378	\?)
379	    echo "Invalid option: -$OPTARG" && usage;;
380  esac
381done
382shift $((OPTIND -1))
383release=$1
384
385build_release $release "$compressors"
386