SPDX: Mass-update to GPL-2.0-or-later
[exim.git] / src / scripts / Configure-Makefile
1 #! /bin/sh
2 # Shell script to build Makefile in a build directory. It must be called
3 # from inside the directory. It does its own checking of when to rebuild; it
4 # just got too horrendous to get it right in "make", because of the optionally
5 # existing configuration files.
6 #
7 # Copyright (c) The Exim Maintainers 1995 - 2021
8 # SPDX-License-Identifier: GPL-2.0-or-later
9
10
11 LC_ALL=C
12 export LC_ALL
13
14
15 # First off, get the OS type, and check that there is a make file for it.
16
17 ostype=`../scripts/os-type -generic` || exit 1
18
19 if [ ! -r ../OS/Makefile-$ostype ] ; then
20   echo ""
21   echo "*** Sorry - operating system $ostype is not supported"
22   echo "*** See OS/Makefile-* for supported systems" 1>&2
23   echo ""
24   exit 1
25 fi
26
27 # We also need the architecture type, in order to test for any architecture-
28 # specific configuration files.
29
30 archtype=`../scripts/arch-type` || exit 1
31
32 # Now test for either the non-existence of Makefile, or for any of its
33 # components being newer. Note that the "newer" script gives the right
34 # answer (for our purposes) when the first file is non-existent.
35
36 editme=../Local/Makefile
37 rebuild=yes
38
39 if [ -f Makefile ] ; then
40   rebuild=no
41   if ../scripts/newer $editme Makefile || \
42      ../scripts/newer $editme-$ostype Makefile || \
43      ../scripts/newer $editme-$archtype Makefile || \
44      ../scripts/newer $editme-$ostype-$archtype Makefile || \
45      ../scripts/newer ../scripts/Configure-Makefile Makefile || \
46      ../scripts/newer ../OS/Makefile-Base Makefile || \
47      ../scripts/newer ../OS/Makefile-Default Makefile
48   then
49     rebuild=yes
50   fi
51 fi
52
53 # If the "build" variable is set it means that a build name was explicitly
54 # given. Arrange to pick up a build-specific configuration file.
55
56 if [ "X$build" != "X" ] ; then
57   mfb=Local/Makefile-$build
58   if ../scripts/newer $editme-$build Makefile ; then
59     rebuild=yes
60   fi
61 else
62   mfb=
63 fi
64
65
66 # If Makefile is up-to-date, no need to rebuild it.
67
68 if [ $rebuild = no ] ; then
69   echo "\`Makefile' is up to date."
70   echo " "
71   exit
72 fi
73
74 # Makefile needs to be rebuilt in the current directory by joining
75 # the generic default makefile, the OS base makefile, and then local
76 # generic, OS-specific, architecture-specific, and OS+architecture-specific
77 # makefiles, if they exist. These files all contain macro definitions, with
78 # later definitions overriding earlier ones. Make a temporary file first, in
79 # case things go wrong. A second temporary is needed for sorting out the
80 # default Perl stuff. Use short macro names to save typing.
81
82 mf=Makefile
83 mft=$mf-t
84 mftt=$mf-tt
85 mftepcp=$mf-tepcp
86 mftepcp2=$mf-tepcp2
87
88 look_mf=lookups/Makefile
89 look_mf_pre=${look_mf}.predynamic
90 look_mf_post=${look_mf}.postdynamic
91
92 # Ensure the temporary does not exist and start the new one by setting
93 # the OSTYPE and ARCHTYPE variables.
94
95 rm -f $mft $mftt $mftepcp $mftepcp2 $look_mf-t
96 (echo "OSTYPE=$ostype"; echo "ARCHTYPE=$archtype"; echo "") > $mft || exit 1
97
98 # Now concatenate the files to the temporary file. Copy the files using sed to
99 # remove comments, blank lines, and trailing white space.
100
101 # BEWARE: a tab character is needed in the sed command below. It has had
102 # a nasty tendency to get lost in the past, causing a problem if a tab has
103 # actually been present in one of the files. Use a variable to hold a space
104 # and a tab to keep the tab in one place.
105
106 st='     '
107
108 for f in OS/Makefile-Default \
109          OS/Makefile-$ostype \
110          Local/Makefile \
111          Local/Makefile-$ostype \
112          Local/Makefile-$archtype \
113          Local/Makefile-$ostype-$archtype \
114          $mfb
115 do   if test -r ../$f
116      then   echo "# From $f"
117             sed "/^#/d;/^[$st]*\$/d;s/[$st]*\$//" ../$f || exit 1
118             echo "# End of $f"
119             echo ""
120      fi
121 done \
122      | sed 's/^TMPDIR=/EXIM_&/' \
123      >> $mft || exit 1
124
125 # handle PKG_CONFIG_PATH because we need it in our env, and we want to handle
126 # wildcards; note that this logic means all setting _appends_ values, never
127 # replacing; if that's a problem, we can revisit.
128 sed -n "s/^[$st]*PKG_CONFIG_PATH[$st]*[+]*=[$st]*//p" $mft | \
129   sed "s/[$st]*\$//" >> $mftepcp
130 if test -s ./$mftepcp
131 then
132   # expand any wildcards and strip spaces, to make it a real PATH-like variable
133   ( IFS=":${IFS-$st}"; for P in `cat ./$mftepcp`; do echo "$P"; done ) | xargs | sed "s/[$st]/:/g" >./$mftepcp2
134   sed "s/^/PKG_CONFIG_PATH='/" < ./$mftepcp2 | sed "s/\$/'/" > ./$mftepcp
135   . ./$mftepcp
136   export PKG_CONFIG_PATH
137   egrep -v "^[$st]*PKG_CONFIG_PATH[$st]*=" ./$mft > ./$mftt
138   rm -f ./$mft
139   (
140     echo "# Collapsed PKG_CONFIG_PATH in build-prep:"
141     sed "s/'//g" ./$mftepcp
142     echo "# End of collapsed PKG_CONFIG_PATH"
143     echo ""
144     cat ./$mftt
145   ) > ./$mft
146   rm -f ./$mftt
147 fi
148 rm -f ./$mftepcp ./$mftepcp2
149
150 # handle pkg-config
151 # beware portability of extended regexps with sed.
152 egrep "^[$st]*(AUTH|LOOKUP)_[A-Z0-9_]*[$st]*=[$st]*" $mft | \
153   sed "s/[$st]*=/='/" | \
154   sed "s/\$/'/" > $mftt
155 egrep "^[$st]*((USE_(OPENSSL|GNUTLS)_PC)|SUPPORT_TLS|USE_GNUTLS|PCRE2?_CONFIG|AVOID_GNUTLS_PKCS11)[$st]*=[$st]*" $mft | \
156   sed "s/[$st]*=/='/" | \
157   sed "s/\$/'/" >> $mftt
158 if test -s $mftt
159 then
160   (
161   echo "# pkg-config fixups"
162   . ./$mftt
163   for var in `cut -d = -f 1 < $mftt`; do
164     case $var in
165
166       USE_*_PC)
167         eval "pc_value=\"\$$var\""
168         need_this=''
169         need_core=''
170         if [ ".$DISABLE_TLS" = .yes ]; then
171           # no TLS, not referencing
172           true
173         elif [ ".$var" = ".USE_GNUTLS_PC" ] && [ ".$USE_GNUTLS" != "." ]; then
174           need_this=t
175           need_core="gnutls-special"
176         elif [ ".$var" = ".USE_OPENSSL_PC" ] && [ ".$USE_GNUTLS" = "." ]; then
177           need_this=t
178           need_core=t
179         fi
180         if [ ".$need_this" != "." ]; then
181           tls_include=`pkg-config --cflags $pc_value`
182           if [ $? -ne 0 ]; then
183             echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
184             exit 1
185           fi
186           tls_libs=`pkg-config --libs $pc_value`
187           echo "TLS_INCLUDE=$tls_include"
188           echo "TLS_LIBS=$tls_libs"
189           # With hash.h pulling crypto into the core, we need to also handle that
190           if [ ".$need_this" = ".t" ]; then
191             echo "CFLAGS += $tls_include"
192             echo "LDFLAGS += $tls_libs"
193           elif [ ".$need_this" = ".gnutls-special" ]; then
194             if pkg-config --atleast-version=2.10 gnutls ; then
195               echo "CFLAGS += $tls_include"
196               echo "LDFLAGS += $tls_libs"
197             else
198               echo "CFLAGS += `libgcrypt-config --cflags`"
199               echo "LDFLAGS += `libgcrypt-config --libs`"
200             fi
201           fi
202         fi
203         ;;
204
205       *_PC)
206         eval "pc_value=\"\$$var\""
207         base=`echo $var | sed 's/_PC$//'`
208         eval "basevalue=\"\$$base\""
209         if [ ".$basevalue" = "." ]; then
210           # not pulling in this module, _PC defined as default? Ignore
211           true
212         elif [ $basevalue = 2 ]; then
213           # module; handled in scripts/lookups-Makefile
214           true
215         else
216           # main binary
217           cflags=`pkg-config --cflags $pc_value`
218           if [ $? -ne 0 ]; then
219             echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
220             exit 1
221           fi
222           libs=`pkg-config --libs $pc_value`
223           if [ "$var" != "${var#LOOKUP_}" ]; then
224             echo "LOOKUP_INCLUDE += $cflags"
225             echo "LOOKUP_LIBS += $libs"
226           elif [ "$var" != "${var#AUTH_}" ]; then
227             echo "CFLAGS += $cflags"
228             echo "AUTH_LIBS += $libs"
229           else
230             echo >&2 "Don't know how to handle pkg-config for $var"
231           fi
232         fi
233         ;;
234
235       PCRE_CONFIG)
236         case $PCRE_CONFIG in
237           yes|YES|y|Y)
238              echo >&2 "pcre is no longer supported; migrate to pcre2"
239              exit 1
240
241 #            cflags=`pcre-config --cflags`
242 #            if [ $? -ne 0 ]; then
243 #              echo >&2 "*** Missing pcre-config for regular expression support"
244 #              exit 1
245 #            fi
246 #            libs=`pcre-config --libs`
247 #            if [ ".$cflags" != "." ]; then
248 #              echo "INCLUDE += $cflags"
249 #            fi
250 #            echo "PCRE_LIBS=$libs"
251             ;;
252         esac
253         ;;
254
255       PCRE2_CONFIG)
256         case $PCRE2_CONFIG in
257           yes|YES|y|Y)
258             cflags=`pcre2-config --cflags`
259             if [ $? -ne 0 ]; then
260               echo >&2 "*** Missing pcre2-config for regular expression support"
261               exit 1
262             fi
263             libs=`pcre2-config --libs8`
264             if [ ".$cflags" != "." ]; then
265               echo "INCLUDE += $cflags"
266             fi
267             echo "PCRE_LIBS=$libs"
268             ;;
269         esac
270         ;;
271
272       AVOID_GNUTLS_PKCS11)
273         echo "$var=yes"
274         ;;
275
276     esac
277   done
278   echo "# End of pkg-config fixups"
279   echo
280   ) >> $mft
281   subexit=$?
282   if [ $subexit -ne 0 ]; then
283     exit $subexit
284   fi
285 fi
286 rm -f $mftt
287
288 # make the lookups Makefile with the definitions
289 # the auxiliary script generates $look_mf_post from $look_mf_pre
290
291 cp ../src/lookups/Makefile $look_mf_pre
292 ../scripts/lookups-Makefile
293
294 # See if there is a definition of EXIM_PERL in what we have built so far.
295 # If so, run Perl to find the default values for PERL_CC, PERL_CCOPTS,
296 # and PERL_LIBS. These need to be put at the top of the Makefile, so we rename
297 # what we have so far and then copy it afterwards. Use the value of PERL_COMMAND
298 # if it has been defined.
299
300 EXIM_PERL=`grep EXIM_PERL $mft`
301
302 PERL_COMMAND=`grep PERL_COMMAND $mft | sed -e "\\$!d;s/^[$st]*PERL_COMMAND[$st]*=[$st]*//"`
303 if [ "${PERL_COMMAND}" = "" ] ; then
304   PERL_COMMAND='perl'
305 fi
306
307 if [ "${EXIM_PERL}" != "" ] ; then
308   testperl=`$PERL_COMMAND --version`
309   if [ "$testperl" = "" ] ; then
310     echo "*** EXIM_PERL is set, but '$PERL_COMMAND --version' failed"
311     exit 1
312   fi
313
314   EXTUTILS_EMBED_NOT_INSTALLED=`$PERL_COMMAND -MExtUtils::Embed -e ";" 2>&1`
315   if [ "${EXTUTILS_EMBED_NOT_INSTALLED}" != "" ] ; then
316     echo "Please install ExtUtils::Embed for $PERL_COMMAND"
317     exit 1;
318   fi
319
320   mv $mft $mftt
321   echo "PERL_CC=`$PERL_COMMAND -MConfig -e 'print $Config{cc}'`" >>$mft
322   echo "PERL_CCOPTS=`$PERL_COMMAND -MExtUtils::Embed -e ccopts`" >>$mft
323   echo "PERL_LIBS=`$PERL_COMMAND -MExtUtils::Embed -e ldopts`" >>$mft
324   echo "" >>$mft
325   cat $mftt >> $mft
326   rm -f $mftt
327 fi
328
329 # Record the build variable in the Makefile.
330
331 echo "build=$build" >>$mft
332 echo "" >>$mft
333
334 # Finally, join on the generic base make file, which contains the actual
335 # rules and stuff.
336
337 echo "# From ../OS/Makefile-Base" >> $mft
338 cat ../OS/Makefile-Base >> $mft || exit 1
339
340 # If the new makefile is the same as the existing one, say so, and just
341 # update the timestamp. Otherwise remove the old and install the new.
342
343 if      [ -s $mf ] && cmp -s $mft $mf && [ -s $look_mf ] && cmp -s $look_mf_post $look_mf
344 then    echo ">>> rebuilt $mf unchanged"
345         echo " "
346         touch $mf || exit
347         rm -f $mft $look_mf_pre $look_mf_post
348 elif    rm -f $mf $look_mf $look_mf_pre
349         mv $mft $mf
350         mv $look_mf_post $look_mf
351 then    echo ">>> New $mf & $look_mf installed"
352         echo '>>> Use "make makefile" if you need to force rebuilding of the makefile'
353         echo " "
354 else    echo " "
355         echo "*** Failed to install $mf - see $mft"
356         echo "    (or $look_mft)"
357         echo " "
358         exit 1;
359 fi
360
361 # vim: set ft=sh :
362 # End of Configure-Makefile