summaryrefslogtreecommitdiffstats
path: root/scripts/build-older-versions/build-older-versions.sh
blob: c1a1a12e3873d86a93eed031537a1c35352f1b60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/sh
#
# Copyright (C) 2019-2020 Free Software Foundation, Inc.
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.
#
# Written by Assaf Gordon and Bernhard Voelker.


# To build all versions since 5.0 (if possible):
# git tag \
#   | grep '^v[5678]' \
#   | sed 's/^v//' \
#   | sort -V \
#   | while read f; do \
#       ./build-older-versions.sh $f 2>&1 \
#          | tee build/build.$f.log ; \
#     done

PREFIX="${PREFIX:=$HOME/old-coreutils}"

base=$(basename "$0")

die()
{
  echo "$base: error: $*" >&2
  exit 1
}

warn()
{
  echo "$base: warning: $*" >&2
}

get_patch_file()
{
  case "$1" in
    5.0)  echo coreutils-5.0-on-glibc-2.28.diff ;;
    5.97|6.[345679]) echo coreutils-5.97-on-glibc-2.28.diff ;;
    6.10) echo coreutils-6.10-on-glibc-2.28.diff ;;
    6.11) echo coreutils-6.11-on-glibc-2.28.diff ;;
    6.12) echo coreutils-6.12-on-glibc-2.28.diff ;;
    7.[23456]|8.[123]) echo coreutils-7.2-on-glibc-2.28.diff ;;
    8.[456789]|8.1[012]) echo coreutils-8.4-on-glibc-2.28.diff ;;
    8.1[3456]) echo coreutils-8.13-on-glibc-2.28.diff ;;
    8.17) echo coreutils-8.17-on-glibc-2.28.diff ;;
    8.1[89]|8.2[0123]) echo coreutils-8.18-on-glibc-2.28.diff ;;
    8.2[456789]) echo coreutils-8.24-on-glibc-2.28.diff ;;
    8.[3456789]*) warn "patch not needed for version '$1'" ;;
    5.[12]*|5.9*)  die "version '$1' does not have a patch (yet) " \
                       "use versions 5.0 or 5.97" ;;
    7.1)  die "version '$1' does not have a patch (yet)" \
                       "use versions 6.12 or 7.2" ;;
    5*|6*|7*|8*) die "non-existing version" ;;
    *) die "unknown version" ;;
  esac
}

get_url()
{
  _base_url="https://ftp.gnu.org/gnu/coreutils/coreutils-$1.tar"
  case "$1" in
    5.*|6.*|7.*) echo "$_base_url.gz" ;;
    8.*)         echo "$_base_url.xz" ;;
    *) die "unknown version" ;;
  esac
}

##
## Setup
##
test -n "$1" \
  || die "missing coreutils version to build (e.g. '6.12')"

cd $(dirname "$0")

patch_file=$(get_patch_file "$1") \
  || die "cannot build version '$1'"

# Test for the patch file if the above returned one.
if test "$patch_file"; then
  test -e "$patch_file" \
    || die "internal error: patch file '$patch_file' does not exist"
fi

url=$(get_url "$1")
tarball=$(basename "$url")

mkdir -p "build" \
  && cd "build" \
  || die "creating version build dir 'build' failed"

##
## Download tarball (if needed)
##
if ! test -e "$tarball" ; then
   wget -O "$tarball.t" "$url" \
        && mv "$tarball.t" "$tarball" \
        || die "failed to download '$url'"
fi

##
## Extract tarball (if needed)
##
srcdir=${tarball%.tar.*}
if ! test -d "$srcdir" ; then
    tar -xvf "$tarball" || die "failed to extract '$tarball'"
fi

##
## Patch (if needed)
##
cd "$srcdir" \
  || die "changing directory to '$srcdir' failed"

# Patch will fail if it was already applied (using "--forward" turns
# that into a no-op). So don't check for failure.
# Is there a way to differentiate between 'already applied' and
# 'failed to apply' ?
test "$patch_file" \
  && patch --ignore-whitespace --batch --forward -p1 < "../../$patch_file"

##
## Configure
##
version="${srcdir#coreutils}" # note:  this keeps the '-' in '$version'
vprefix="$PREFIX/coreutils$version"
if ! test -e "Makefile" ; then
  ./configure \
    --program-suffix="$version" \
    --prefix="$vprefix" \
    || die "failed to run configure in 'build/$srcdir/'"
fi

##
## Build
##
make -j4 \
  || die "build failed in 'build/$srcdir'"

##
## Install
##
make install \
  || die "make-install failed in 'build/$srcdir' (to '$vprefix')"


# Create convenience links for the executables and manpages in common directory.
(
  mkdir -p "$PREFIX/bin" "$PREFIX/man/man1" \
    || die "creating common bin or man directory failed"
  cd $vprefix/bin \
    || die "changing directory to just-installed 'bin' directory failed"
  for f in *; do
    ln -snvf "../coreutils$version/bin/$f" "$PREFIX/bin/$f" \
      || die "creating symlink of executable '$f' failed"
  done

  share=  # older versions do not have 'share'.
  cd "$vprefix/share/man/man1" 2>/dev/null \
    && share='/share' \
    || cd "$vprefix/man/man1" \
    || die "changing directory to just-installed 'man/man1' directory failed"
  for f in *; do
    ln -snfv "../../coreutils$version$share/man/man1/$f" "$PREFIX/man/man1/$f" \
      || die "creating symlink of man page '$f' failed"
  done
) || exit 1

# Build and install PDF (if possible).
if make SUBDIRS=. pdf; then
  make SUBDIRS=. install-pdf \
    || die "make-install-pdf failed in 'build/$srcdir' (to '$vprefix')"
else
  echo "$0: no PDF available"
fi

# Print summary
cat<<EOF


=================================================================

GNU Coreutils$version successfully installed.

Source code in $PWD/build/$srcdir
Installed in $vprefix

symlinks for all programs (all versions) in $PREFIX/bin
manual pages for all programs in $PREFIX/share/man/man1

Run the following command to add all programs to your \$PATH:

    export PATH=\$PATH:\$HOME/old-coreutils/bin
    export MANPATH=\$MANPATH:\$HOME/old-coreutils/man

EOF