Change make license
[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-2020 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 <http://www.gnu.org/licenses/>.
19
20 # Get configure-generated values
21 . ./build.cfg
22
23 : ${OUTDIR:=.}
24 OUTLIB="$OUTDIR/lib"
25
26 # Directory to find libraries in for '-lXXX'.
27 libdir=$exec_prefix/lib
28 # Directory to search by default for included makefiles.
29 includedir=$prefix/include
30
31 localedir=$prefix/share/locale
32
33 defines="-DLOCALEDIR=\"$localedir\" -DLIBDIR=\"$libdir\" -DINCLUDEDIR=\"$includedir\""
34
35 # Look up a make variable value.
36 # It can handle simple recursion where variables are separate words.
37 # Print the value to stdout.
38 get_mk_var ()
39 {
40   file=$1
41   var=$2
42
43   val=
44   v=$(sed -e :a -e '/\\$/N; s/\\\n//; ta' "$file" | sed -n "s=^ *$var *\= *==p")
45   for w in $v; do
46     case $w in
47       (\$[\(\{]*[\)\}]) w=${w#\$[\(\{]}; w=$(get_mk_var "$file" "${w%[\)\}]}") ;;
48     esac
49     val="${val:+$val }$w"
50   done
51
52   printf '%s\n' "$val"
53 }
54
55 # Compile source files.  Object files are put into $objs.
56 compile ()
57 {
58   objs=
59   for ofile in "$@"; do
60     file="${ofile%.$OBJEXT}.c"
61     echo "compiling $file..."
62     of="$OUTDIR/$ofile"
63     mkdir -p "${of%/*}"
64     $CC $cflags $CPPFLAGS $CFLAGS -c -o "$of" "$top_srcdir/$file"
65     objs="${objs:+$objs }$of"
66   done
67 }
68
69 # Use config.status to convert a .in file.  Output file is put into $out.
70 # $out will be empty if no conversion was needed.
71 convert ()
72 {
73   out=
74   base=$1
75   var="GENERATE_$(echo $base | tr 'a-z./+' A-Z__X)"
76
77   # Is this file disabled?
78   grep "${var}_FALSE\"]=\"\"" config.status >/dev/null && return
79
80   # Not disabled, so create it
81   in="$top_srcdir/lib/$(echo ${base%.*}.in.${base##*.} | tr / _)"
82   out="$OUTLIB/$base"
83   mkdir -p "${out%/*}"
84
85   # First perform the normal replacements, using config.status
86   sed -e 's|@GUARD_PREFIX@|GL|g' \
87       -e 's/@GNULIB_UNISTD_H_GETOPT@/0/g' \
88       "$in" > "${out}_"
89   ./config.status --file "${out}__:${out}_"
90   int="${out}__"
91
92   # Then see if there any files we need to include.  Unfortunately there's no
93   # algorithmic conversion so we just have to hard-code it.
94   incls=$(sed -n 's/.*definitions* of \(_[^ $]*\).*/\1/p' "$in")
95
96   for inc in $incls; do
97     case $inc in
98       (_GL_FUNCDECL_RPL) fn=$(get_mk_var lib/Makefile CXXDEFS_H) ;;
99       (_GL_ARG_NONNULL)  fn=$(get_mk_var lib/Makefile ARG_NONNULL_H) ;;
100       (_GL_WARN_ON_USE)  fn=$(get_mk_var lib/Makefile WARN_ON_USE_H) ;;
101       (_Noreturn)        fn=$(get_mk_var lib/Makefile _NORETURN_H) ;;
102       (*) echo "Unknown file replacement: $inc"; exit 1 ;;
103     esac
104
105     fn="$top_srcdir/lib/${fn##*/}"
106     [ -f "$fn" ] || { echo "Missing file: $fn"; exit 1; }
107
108     sed "/definitions* of $inc/r $fn" "$int" > "${int}_"
109     int=${int}_
110   done
111
112   # Done!
113   mv "$int" "$out"
114 }
115
116 # Get source files provided from gnulib and convert to object files
117 LIBOBJS=
118 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
119   LIBOBJS="${LIBOBJS:+$LIBOBJS }lib/$lo"
120 done
121
122 # Get object files from the Makefile
123 OBJS=$(get_mk_var Makefile make_OBJECTS | sed "s=\$[\(\{]OBJEXT[\)\}]=$OBJEXT=g")
124
125 # Exit as soon as any command fails.
126 set -e
127
128 # Generate gnulib header files that would normally be created by make
129 for b in $(get_mk_var lib/Makefile BUILT_SOURCES); do
130     convert $b
131 done
132
133 # Build the gnulib library
134 cflags="$DEFS -I$OUTLIB -Ilib -I$top_srcdir/lib -I$OUTDIR/src -Isrc -I$top_srcdir/src"
135 compile $LIBOBJS
136
137 echo "creating libgnu.a..."
138 $AR $ARFLAGS "$OUTLIB"/libgnu.a $objs
139
140 # Compile the source files into those objects.
141 cflags="$DEFS $defines -I$OUTDIR/src -Isrc -I$top_srcdir/src -I$OUTLIB -Ilib -I$top_srcdir/lib"
142 compile $OBJS
143
144 # Link all the objects together.
145 echo "linking make..."
146 $CC $CFLAGS $LDFLAGS -L"$OUTLIB" $objs -lgnu $LOADLIBES -o "$OUTDIR/makenew$EXEEXT"
147 mv -f "$OUTDIR/makenew$EXEEXT" "$OUTDIR/make$EXEEXT"
148
149 echo done.