12f0ddd9c1f82454127972b38ec42125bc32a7f7
[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 - 2024
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 # Linux now whines about egrep, saying "use grep -E".
67 # Solarix doesn't support -E on grep.  Thanks so much for
68 # going non-back-compatible, Linux.
69 if [ "$ostype" != "SunOS5" ] ; then
70   egrep="grep -E"
71 else
72   egrep="egrep"
73 fi
74
75
76 # If Makefile is up-to-date, no need to rebuild it.
77
78 if [ $rebuild = no ] ; then
79   echo "\`Makefile' is up to date."
80   echo " "
81   exit
82 fi
83
84 # Makefile needs to be rebuilt in the current directory by joining
85 # the generic default makefile, the OS base makefile, and then local
86 # generic, OS-specific, architecture-specific, and OS+architecture-specific
87 # makefiles, if they exist. These files all contain macro definitions, with
88 # later definitions overriding earlier ones. Make a temporary file first, in
89 # case things go wrong. A second temporary is needed for sorting out the
90 # default Perl stuff. Use short macro names to save typing.
91
92 mf=Makefile
93 mft=$mf-t
94 mftt=$mf-tt
95 mftepcp=$mf-tepcp
96 mftepcp2=$mf-tepcp2
97
98 look_mf=lookups/Makefile
99 look_mf_pre=${look_mf}.predynamic
100 look_mf_post=${look_mf}.postdynamic
101
102 # Ensure the temporary does not exist and start the new one by setting
103 # the OSTYPE and ARCHTYPE variables.
104
105 rm -f $mft $mftt $mftepcp $mftepcp2 $look_mf-t
106 (echo "OSTYPE=$ostype"; echo "ARCHTYPE=$archtype"; echo "") > $mft || exit 1
107
108 # Now concatenate the files to the temporary file. Copy the files using sed to
109 # remove comments, blank lines, and trailing white space.
110
111 # BEWARE: a tab character is needed in the sed command below. It has had
112 # a nasty tendency to get lost in the past, causing a problem if a tab has
113 # actually been present in one of the files. Use a variable to hold a space
114 # and a tab to keep the tab in one place.
115
116 st='     '
117
118 for f in OS/Makefile-Default \
119          OS/Makefile-$ostype \
120          Local/Makefile \
121          Local/Makefile-$ostype \
122          Local/Makefile-$archtype \
123          Local/Makefile-$ostype-$archtype \
124          $mfb
125 do   if test -r ../$f
126      then   echo "# From $f"
127             sed "/^#/d;/^[$st]*\$/d;s/[$st]*\$//" ../$f || exit 1
128             echo "# End of $f"
129             echo ""
130      fi
131 done \
132      | sed 's/^TMPDIR=/EXIM_&/' \
133      >> $mft || exit 1
134
135 # handle PKG_CONFIG_PATH because we need it in our env, and we want to handle
136 # wildcards; note that this logic means all setting _appends_ values, never
137 # replacing; if that's a problem, we can revisit.
138 sed -n "s/^[$st]*PKG_CONFIG_PATH[$st]*[+]*=[$st]*//p" $mft | \
139   sed "s/[$st]*\$//" >> $mftepcp
140 if test -s ./$mftepcp
141 then
142   # expand any wildcards and strip spaces, to make it a real PATH-like variable
143   ( IFS=":${IFS-$st}"; for P in `cat ./$mftepcp`; do echo "$P"; done ) | xargs | sed "s/[$st]/:/g" >./$mftepcp2
144   sed "s/^/PKG_CONFIG_PATH='/" < ./$mftepcp2 | sed "s/\$/'/" > ./$mftepcp
145   . ./$mftepcp
146   export PKG_CONFIG_PATH
147   $egrep -v "^[$st]*PKG_CONFIG_PATH[$st]*=" ./$mft > ./$mftt
148   rm -f ./$mft
149   (
150     echo "# Collapsed PKG_CONFIG_PATH in build-prep:"
151     sed "s/'//g" ./$mftepcp
152     echo "# End of collapsed PKG_CONFIG_PATH"
153     echo ""
154     cat ./$mftt
155   ) > ./$mft
156   rm -f ./$mftt
157 fi
158 rm -f ./$mftepcp ./$mftepcp2
159
160 # handle pkg-config
161 # beware portability of extended regexps with sed.
162 $egrep "^[$st]*(AUTH|LOOKUP)_[A-Z0-9_]*[$st]*=[$st]*" $mft | \
163   sed "s/[$st]*=/='/" | \
164   sed "s/\$/'/" > $mftt
165 $egrep "^[$st]*((USE_(OPENSSL|GNUTLS)_PC)|SUPPORT_TLS|USE_GNUTLS|PCRE2?_CONFIG|AVOID_GNUTLS_PKCS11)[$st]*=[$st]*" $mft | \
166   sed "s/[$st]*=/='/" | \
167   sed "s/\$/'/" >> $mftt
168 if test -s $mftt
169 then
170   (
171   echo "# pkg-config fixups"
172   . ./$mftt
173   for var in `cut -d = -f 1 < $mftt`; do
174     case $var in
175
176       USE_*_PC)
177         eval "pc_value=\"\$$var\""
178         need_this=''
179         need_core=''
180         if [ ".$DISABLE_TLS" = .yes ]; then
181           # no TLS, not referencing
182           true
183         elif [ ".$var" = ".USE_GNUTLS_PC" ] && [ ".$USE_GNUTLS" != "." ]; then
184           need_this=t
185           need_core="gnutls-special"
186         elif [ ".$var" = ".USE_OPENSSL_PC" ] && [ ".$USE_GNUTLS" = "." ]; then
187           need_this=t
188           need_core=t
189         fi
190         if [ ".$need_this" != "." ]; then
191           tls_include=`pkg-config --cflags $pc_value`
192           if [ $? -ne 0 ]; then
193             echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
194             exit 1
195           fi
196           tls_libs=`pkg-config --libs $pc_value`
197           echo "TLS_INCLUDE=$tls_include"
198           echo "TLS_LIBS=$tls_libs"
199           # With hash.h pulling crypto into the core, we need to also handle that
200           if [ ".$need_this" = ".t" ]; then
201             echo "CFLAGS += $tls_include"
202             echo "LDFLAGS += $tls_libs"
203           elif [ ".$need_this" = ".gnutls-special" ]; then
204             if pkg-config --atleast-version=2.10 gnutls ; then
205               echo "CFLAGS += $tls_include"
206               echo "LDFLAGS += $tls_libs"
207             else
208               echo "CFLAGS += `libgcrypt-config --cflags`"
209               echo "LDFLAGS += `libgcrypt-config --libs`"
210             fi
211           fi
212         fi
213         ;;
214
215       *_PC)
216         eval "pc_value=\"\$$var\""
217         base=`echo $var | sed 's/_PC$//'`
218         eval "basevalue=\"\$$base\""
219         if [ ".$basevalue" = "." ]; then
220           # not pulling in this module, _PC defined as default? Ignore
221           true
222         elif [ $basevalue = 2 ]; then
223           # module; handled in scripts/lookups-Makefile
224           true
225         else
226           # main binary
227           cflags=`pkg-config --cflags $pc_value`
228           if [ $? -ne 0 ]; then
229             echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
230             exit 1
231           fi
232           libs=`pkg-config --libs $pc_value`
233           if [ "$var" != "${var#LOOKUP_}" ]; then
234             echo "LOOKUP_INCLUDE += $cflags"
235             echo "LOOKUP_LIBS += $libs"
236           elif [ "$var" != "${var#AUTH_}" ]; then
237             echo "CFLAGS += $cflags"
238             echo "AUTH_LIBS += $libs"
239           else
240             echo >&2 "Don't know how to handle pkg-config for $var"
241           fi
242         fi
243         ;;
244
245       PCRE_CONFIG)
246         case $PCRE_CONFIG in
247           yes|YES|y|Y)
248              echo >&2 "pcre is no longer supported; migrate to pcre2"
249              exit 1
250
251 #            cflags=`pcre-config --cflags`
252 #            if [ $? -ne 0 ]; then
253 #              echo >&2 "*** Missing pcre-config for regular expression support"
254 #              exit 1
255 #            fi
256 #            libs=`pcre-config --libs`
257 #            if [ ".$cflags" != "." ]; then
258 #              echo "INCLUDE += $cflags"
259 #            fi
260 #            echo "PCRE_LIBS=$libs"
261             ;;
262         esac
263         ;;
264
265       PCRE2_CONFIG)
266         case $PCRE2_CONFIG in
267           yes|YES|y|Y)
268             cflags=`pcre2-config --cflags`
269             if [ $? -ne 0 ]; then
270               echo >&2 "*** Missing pcre2-config for regular expression support"
271               exit 1
272             fi
273             libs=`pcre2-config --libs8`
274             if [ ".$cflags" != "." ]; then
275               echo "INCLUDE += $cflags"
276             fi
277             echo "PCRE_LIBS=$libs"
278             ;;
279         esac
280         ;;
281
282       AVOID_GNUTLS_PKCS11)
283         echo "$var=yes"
284         ;;
285
286     esac
287   done
288   echo "# End of pkg-config fixups"
289   echo
290   ) >> $mft
291   subexit=$?
292   if [ $subexit -ne 0 ]; then
293     exit $subexit
294   fi
295 fi
296 rm -f $mftt
297
298 # make the lookups Makefile with the definitions
299 # the auxiliary script generates $look_mf_post from $look_mf_pre
300
301 cp ../src/lookups/Makefile $look_mf_pre
302 ../scripts/lookups-Makefile
303
304 while read class classdef names
305 do
306   cp ../src/$class/Makefile $class/Makefile.predynamic
307   CLASS=$class CLASSDEF=$classdef DRNAMES="$names" ../scripts/drivers-Makefile
308   mv $class/Makefile.postdynamic $class/Makefile
309   rm $class/Makefile.predynamic
310 done <<-END
311  routers    ROUTER      ACCEPT DNSLOOKUP IPLITERAL IPLOOKUP MANUALROUTE QUERYPROGRAM REDIRECT
312  transports TRANSPORT   APPENDFILE AUTOREPLY LMTP PIPE QUEUEFILE SMTP
313  auths      AUTH        CRAM_MD5 CYRUS_SASL DOVECOT EXTERNAL GSASL HEIMDAL_GSSAPI PLAINTEXT SPA TLS
314  miscmods   SUPPORT     _DKIM DMARC SPF
315 END
316
317 # See if there is a definition of EXIM_PERL in what we have built so far.
318 # If so, run Perl to find the default values for PERL_CC, PERL_CCOPTS,
319 # and PERL_LIBS. These need to be put at the top of the Makefile, so we rename
320 # what we have so far and then copy it afterwards. Use the value of PERL_COMMAND
321 # if it has been defined.
322
323 EXIM_PERL=`grep EXIM_PERL $mft`
324
325 PERL_COMMAND=`grep PERL_COMMAND $mft | sed -e "\\$!d;s/^[$st]*PERL_COMMAND[$st]*=[$st]*//"`
326 if [ "${PERL_COMMAND}" = "" ] ; then
327   PERL_COMMAND='perl'
328 fi
329
330 if [ "${EXIM_PERL}" != "" ] ; then
331   testperl=`$PERL_COMMAND --version`
332   if [ "$testperl" = "" ] ; then
333     echo "*** EXIM_PERL is set, but '$PERL_COMMAND --version' failed"
334     exit 1
335   fi
336
337   EXTUTILS_EMBED_NOT_INSTALLED=`$PERL_COMMAND -MExtUtils::Embed -e ";" 2>&1`
338   if [ "${EXTUTILS_EMBED_NOT_INSTALLED}" != "" ] ; then
339     echo "Please install ExtUtils::Embed for $PERL_COMMAND"
340     exit 1;
341   fi
342
343   mv $mft $mftt
344   echo "PERL_CC=`$PERL_COMMAND -MConfig -e 'print $Config{cc}'`" >>$mft
345   echo "PERL_CCOPTS=`$PERL_COMMAND -MExtUtils::Embed -e ccopts`" >>$mft
346   echo "PERL_LIBS=`$PERL_COMMAND -MExtUtils::Embed -e ldopts`" >>$mft
347   echo "" >>$mft
348   cat $mftt >> $mft
349   rm -f $mftt
350 fi
351
352 # Record the build variable in the Makefile.
353
354 echo "build=$build" >>$mft
355 echo "" >>$mft
356
357 # Finally, join on the generic base make file, which contains the actual
358 # rules and stuff.
359
360 echo "# From ../OS/Makefile-Base" >> $mft
361 cat ../OS/Makefile-Base >> $mft || exit 1
362
363 # If the new makefile is the same as the existing one, say so, and just
364 # update the timestamp. Otherwise remove the old and install the new.
365
366 if      [ -s $mf ] && cmp -s $mft $mf && [ -s $look_mf ] && cmp -s $look_mf_post $look_mf
367 then    echo ">>> rebuilt $mf unchanged"
368         echo " "
369         touch $mf || exit
370         rm -f $mft $look_mf_pre $look_mf_post
371 elif    rm -f $mf $look_mf $look_mf_pre
372         mv $mft $mf
373         mv $look_mf_post $look_mf
374 then    echo ">>> New $mf & $look_mf installed"
375         echo '>>> Use "make makefile" if you need to force rebuilding of the makefile'
376         echo " "
377 else    echo " "
378         echo "*** Failed to install $mf - see $mft"
379         echo "    (or $look_mft)"
380         echo " "
381         exit 1;
382 fi
383
384 # vim: set ft=sh :
385 # End of Configure-Makefile