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