Bump to 4.4
[platform/upstream/make.git] / build.sh
1 #!/bin/sh
2 # Shell script to build GNU Make in the absence of any 'make' program.
3
4 # Copyright (C) 1993-2022 Free Software Foundation, Inc.
5 # This file is part of GNU Make.
6 #
7 # GNU Make is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # this program.  If not, see <https://www.gnu.org/licenses/>.
19
20 # Get configure-generated values
21 . ./build.cfg
22
23 die () { echo "$*" 1>&2; exit 1; }
24 usage () { echo "$0 [-k]"; exit $1; }
25
26 keep_going=false
27
28 : ${OUTDIR:=.}
29 OUTLIB="$OUTDIR/lib"
30
31 # Directory to find libraries in for '-lXXX'.
32 libdir=$exec_prefix/lib
33 # Directory to search by default for included makefiles.
34 includedir=$prefix/include
35
36 localedir=$prefix/share/locale
37
38 defines="-DLOCALEDIR=\"$localedir\" -DLIBDIR=\"$libdir\" -DINCLUDEDIR=\"$includedir\""
39
40 # Look up a make variable value.
41 # It can handle simple recursion where variables are separate words.
42 # Print the value to stdout.
43 get_mk_var ()
44 {
45   file=$1
46   var=$2
47
48   val=
49   v=$(sed -e :a -e '/\\$/N; s/\\\n//; ta' "$file" | sed -n "s=^ *$var *\= *==p")
50   for w in $v; do
51     case $w in
52       (\$[\(\{]*[\)\}]) w=${w#\$[\(\{]}; w=$(get_mk_var "$file" "${w%[\)\}]}") ;;
53     esac
54     val="${val:+$val }$w"
55   done
56
57   printf '%s\n' "$val"
58 }
59
60 # Compile source files.  Object files are put into $objs.
61 compile ()
62 {
63   success=true
64   objs=
65   for ofile in "$@"; do
66     # We should try to use a Makefile variable like libgnu_a_SOURCES or
67     # something but just hardcode it.
68     file="${ofile%.$OBJEXT}.c"
69     case $file in
70         (lib/libgnu_a-*.c) file="lib/${file#lib/libgnu_a-}" ;;
71     esac
72     echo "compiling $file..."
73     of="$OUTDIR/$ofile"
74     mkdir -p "${of%/*}" || exit 1
75     if $CC $cflags $CPPFLAGS $CFLAGS -c -o "$of" "$top_srcdir/$file"; then
76         : worked
77     else
78         $keep_going || die "Compilation failed."
79         success=false
80     fi
81
82     objs="${objs:+$objs }$of"
83   done
84
85   $success
86 }
87
88 # Use config.status to convert a .in file.  Output file is put into $out.
89 # $out will be empty if no conversion was needed.
90 convert ()
91 {
92   out=
93   base=$1
94   var="GENERATE_$(echo $base | tr 'a-z./+' A-Z__X)"
95
96   # Is this file disabled?
97   grep "${var}_FALSE\"]=\"\"" config.status >/dev/null && return 0
98
99   # If there's no .in file then no conversion needed
100   in="$top_srcdir/lib/$(echo ${base%.*}.in.${base##*.} | tr / _)"
101   test -f "$in" || return 0
102
103   # Not disabled, so create it
104   out="$OUTLIB/$base"
105   mkdir -p "${out%/*}"
106
107   # First perform the normal replacements, using config.status
108   sed -e 's|@GUARD_PREFIX@|GL|g' \
109       -e 's/@GNULIB_UNISTD_H_GETOPT@/0/g' \
110       "$in" > "${out}_"
111   ./config.status --file "${out}__:${out}_"
112   int="${out}__"
113
114   # Then see if there any files we need to include.  Unfortunately there's no
115   # algorithmic conversion so we just have to hard-code it.
116   incls=$(sed -n 's/.*definitions* of \(_[^ $]*\).*/\1/p' "$in")
117
118   for inc in $incls; do
119     case $inc in
120       (_GL_FUNCDECL_RPL) fn=$(get_mk_var lib/Makefile CXXDEFS_H) ;;
121       (_GL_ARG_NONNULL)  fn=$(get_mk_var lib/Makefile ARG_NONNULL_H) ;;
122       (_GL_WARN_ON_USE)  fn=$(get_mk_var lib/Makefile WARN_ON_USE_H) ;;
123       (_Noreturn)        fn=$(get_mk_var lib/Makefile _NORETURN_H) ;;
124       (*) echo "Unknown file replacement: $inc"; exit 1 ;;
125     esac
126
127     fn="$top_srcdir/lib/${fn##*/}"
128     test -f "$fn" || { echo "Missing file: $fn"; exit 1; }
129
130     sed "/definitions* of $inc/r $fn" "$int" > "${int}_"
131     int=${int}_
132   done
133
134   # Done!
135   mv "$int" "$out"
136 }
137
138 # Get source files provided from gnulib and convert to object files
139 LIBOBJS=
140 for lo in $( (get_mk_var lib/Makefile libgnu_a_OBJECTS; get_mk_var lib/Makefile libgnu_a_LIBADD) | sed "s=\$[\(\{]OBJEXT[\)\}]=$OBJEXT=g"); do
141   LIBOBJS="${LIBOBJS:+$LIBOBJS }lib/$lo"
142 done
143
144 # Get object files from the Makefile
145 OBJS=$(get_mk_var Makefile make_OBJECTS | sed "s=\$[\(\{]OBJEXT[\)\}]=$OBJEXT=g")
146
147 while test -n "$1"; do
148     case $1 in
149         (-k) keep_going=true; shift ;;
150         (--) shift; break ;;
151         (-[h?]) usage 0 ;;
152         (-*) echo "Unknown option: $1"; usage 1 ;;
153     esac
154 done
155
156 test -z "$1" || die "Unknown argument: $*"
157
158 # Generate gnulib header files that would normally be created by make
159 set -e
160 for b in $(get_mk_var lib/Makefile BUILT_SOURCES); do
161     convert $b
162 done
163 set +e
164
165 # Build the gnulib library
166 cflags="$DEFS -I$OUTLIB -Ilib -I$top_srcdir/lib -I$OUTDIR/src -Isrc -I$top_srcdir/src"
167 compile $LIBOBJS || die "Compilation failed."
168
169 echo "creating libgnu.a..."
170 $AR $ARFLAGS "$OUTLIB"/libgnu.a $objs || die "Archive of libgnu failed."
171
172 # Compile the source files into those objects.
173 cflags="$DEFS $defines -I$OUTDIR/src -Isrc -I$top_srcdir/src -I$OUTLIB -Ilib -I$top_srcdir/lib"
174 compile $OBJS || die "Compilation failed."
175
176 # Link all the objects together.
177 echo "linking make..."
178 $CC $CFLAGS $LDFLAGS -L"$OUTLIB" -o "$OUTDIR/makenew$EXEEXT" $objs -lgnu $LOADLIBES || die "Link failed."
179
180 mv -f "$OUTDIR/makenew$EXEEXT" "$OUTDIR/make$EXEEXT" || exit 1
181
182 echo done.