Build: Allow arbitrary *_PC settings in EDITME
[users/heiko/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 - 2020
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             # remove comments, leading/trailing spaces, spaces around assignment ops
116             # (There should be not need to check for [$st]* anymore in
117             # the following lines
118             sed "/^#/d;/^[$st]*\$/d;s/[$st]*\$//;s/[$st]*\\(\\+\\?=\\)[$st]*/\\1/" ../$f || exit 1
119             echo "# End of $f"
120             echo ""
121      fi
122 done \
123      | sed 's/^TMPDIR=/EXIM_&/' \
124      > $mft || exit 1
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 {
153     egrep "^(AUTH|LOOKUP)_[A-Z0-9_]=" $mft
154     egrep "^(USE_GNUTLS|PCRE_CONFIG|AVOID_GNUTLS_PKCS11)=" $mft
155     egrep "^[A-Z][A-Z0-9_]+_PC=" $mft \
156     | while IFS== read name value; do
157         egrep "^${name%_PC}=" $mft && echo "$name=$value"
158     done
159 } | sed "s/[$st]*=/='/" \
160   | sed "s/\$/'/" \
161   | sort | uniq > $mftt
162 if test -s $mftt
163 then
164   (
165   echo "# pkg-config fixups"
166   . ./$mftt
167   for var in `cut -d = -f 1 < $mftt`; do
168     case $var in
169
170       USE_*_PC)
171         eval "pc_value=\"\$$var\""
172         need_this=''
173         need_core=''
174         if [ ".$DISABLE_TLS" = .yes ]; then
175           # no TLS, not referencing
176           true
177         elif [ ".$var" = ".USE_GNUTLS_PC" ] && [ ".$USE_GNUTLS" != "." ]; then
178           need_this=t
179           need_core="gnutls-special"
180         elif [ ".$var" = ".USE_OPENSSL_PC" ] && [ ".$USE_GNUTLS" = "." ]; then
181           need_this=t
182           need_core=t
183         fi
184         if [ ".$need_this" != "." ]; then
185           tls_include=`pkg-config --cflags $pc_value`
186           if [ $? -ne 0 ]; then
187             echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
188             exit 1
189           fi
190           tls_libs=`pkg-config --libs $pc_value`
191           echo "TLS_INCLUDE=$tls_include"
192           echo "TLS_LIBS=$tls_libs"
193           # With hash.h pulling crypto into the core, we need to also handle that
194           if [ ".$need_this" = ".t" ]; then
195             echo "CFLAGS += $tls_include"
196             echo "LDFLAGS += $tls_libs"
197           elif [ ".$need_this" = ".gnutls-special" ]; then
198             if pkg-config --atleast-version=2.10 gnutls ; then
199               echo "CFLAGS += $tls_include"
200               echo "LDFLAGS += $tls_libs"
201             else
202               echo "CFLAGS += `libgcrypt-config --cflags`"
203               echo "LDFLAGS += `libgcrypt-config --libs`"
204             fi
205           fi
206         fi
207         ;;
208
209       *_PC)
210         eval "pc_value=\"\$$var\""
211         base=`echo $var | sed 's/_PC$//'`
212         eval "basevalue=\"\$$base\""
213         if [ ".$basevalue" = "." ]; then
214           # not pulling in this module, _PC defined as default? Ignore
215           true
216         elif [ $basevalue = 2 ]; then
217           # module; handled in scripts/lookups-Makefile
218           true
219         else
220           # main binary
221           cflags=`pkg-config --cflags $pc_value`
222           if [ $? -ne 0 ]; then
223             echo >&2 "*** Missing pkg-config for package $pc_value (for Exim $var build option)"
224             exit 1
225           fi
226           libs=`pkg-config --libs $pc_value`
227           if [ "$var" != "${var#LOOKUP_}" ]; then
228             echo "LOOKUP_INCLUDE += $cflags"
229             echo "LOOKUP_LIBS += $libs"
230           elif [ "$var" != "${var#AUTH_}" ]; then
231             echo "CFLAGS += $cflags"
232             echo "AUTH_LIBS += $libs"
233           else
234             echo "CFLAGS += $cflags"
235             echo "LIBS += $libs"
236           fi
237         fi
238         ;;
239
240       PCRE_CONFIG)
241         case $PCRE_CONFIG in
242           yes|YES|y|Y)
243             cflags=`pcre-config --cflags`
244             if [ $? -ne 0 ]; then
245               echo >&2 "*** Missing pcre-config for regular expression support"
246               exit 1
247             fi
248             libs=`pcre-config --libs`
249             if [ ".$cflags" != "." ]; then
250               echo "INCLUDE += $cflags"
251             fi
252             echo "PCRE_LIBS=$libs"
253             ;;
254         esac
255         ;;
256
257       AVOID_GNUTLS_PKCS11)
258         echo "$var=yes"
259         ;;
260
261     esac
262   done
263   echo "# End of pkg-config fixups"
264   echo
265   ) >> $mft
266   subexit=$?
267   if [ $subexit -ne 0 ]; then
268     exit $subexit
269   fi
270 fi
271 rm -f $mftt
272
273 # make the lookups Makefile with the definitions
274 # the auxiliary script generates $look_mf_post from $look_mf_pre
275
276 cp ../src/lookups/Makefile $look_mf_pre
277 ../scripts/lookups-Makefile
278
279 # See if there is a definition of EXIM_PERL in what we have built so far.
280 # If so, run Perl to find the default values for PERL_CC, PERL_CCOPTS,
281 # and PERL_LIBS. These need to be put at the top of the Makefile, so we rename
282 # what we have so far and then copy it afterwards. Use the value of PERL_COMMAND
283 # if it has been defined.
284
285 EXIM_PERL=`grep EXIM_PERL $mft`
286
287 PERL_COMMAND=`grep PERL_COMMAND $mft | sed -e "\\$!d;s/^[$st]*PERL_COMMAND[$st]*=[$st]*//"`
288 if [ "${PERL_COMMAND}" = "" ] ; then
289   PERL_COMMAND='perl'
290 fi
291
292 if [ "${EXIM_PERL}" != "" ] ; then
293   testperl=`$PERL_COMMAND --version`
294   if [ "$testperl" = "" ] ; then
295     echo "*** EXIM_PERL is set, but '$PERL_COMMAND --version' failed"
296     exit 1
297   fi
298
299   EXTUTILS_EMBED_NOT_INSTALLED=`$PERL_COMMAND -MExtUtils::Embed -e ";" 2>&1`
300   if [ "${EXTUTILS_EMBED_NOT_INSTALLED}" != "" ] ; then
301     echo "Please install ExtUtils::Embed for $PERL_COMMAND"
302     exit 1;
303   fi
304
305   mv $mft $mftt
306   echo "PERL_CC=`$PERL_COMMAND -MConfig -e 'print $Config{cc}'`" >>$mft
307   echo "PERL_CCOPTS=`$PERL_COMMAND -MExtUtils::Embed -e ccopts`" >>$mft
308   echo "PERL_LIBS=`$PERL_COMMAND -MExtUtils::Embed -e ldopts`" >>$mft
309   echo "" >>$mft
310   cat $mftt >> $mft
311   rm -f $mftt
312 fi
313
314 # Record the build variable in the Makefile.
315
316 echo "build=$build" >>$mft
317 echo "" >>$mft
318
319 # Finally, join on the generic base make file, which contains the actual
320 # rules and stuff.
321
322 echo "# From ../OS/Makefile-Base" >> $mft
323 cat ../OS/Makefile-Base >> $mft || exit 1
324
325 # If the new makefile is the same as the existing one, say so, and just
326 # update the timestamp. Otherwise remove the old and install the new.
327
328 if      [ -s $mf ] && cmp -s $mft $mf && [ -s $look_mf ] && cmp -s $look_mf_post $look_mf
329 then    echo ">>> rebuilt $mf unchanged"
330         echo " "
331         touch $mf || exit
332         rm -f $mft $look_mf_pre $look_mf_post
333 elif    rm -f $mf $look_mf $look_mf_pre
334         mv $mft $mf
335         mv $look_mf_post $look_mf
336 then    echo ">>> New $mf & $look_mf installed"
337         echo '>>> Use "make makefile" if you need to force rebuilding of the makefile'
338         echo " "
339 else    echo " "
340         echo "*** Failed to install $mf - see $mft"
341         echo "    (or $look_mft)"
342         echo " "
343         exit 1;
344 fi
345
346 # vim: set ft=sh :
347 # End of Configure-Makefile