SPDX: Mass-update to GPL-2.0-or-later
[exim.git] / src / scripts / lookups-Makefile
1 #! /bin/sh
2
3 # Copyright (c) The Exim Maintainers 1995 - 2021
4 # SPDX-License-Identifier: GPL-2.0-or-later
5
6 # We turn the configure-built build-$foo/lookups/Makefile.predynamic into Makefile
7
8 # We always re-exec ourselves at least once, because it's the cleanest and
9 # most portable way to turn on various features we expect of POSIX sh.
10 if [ -z "$EXIM_LOOKUP_MAKEFILE_ADJUSTED" ]
11 then
12   SHELL=/bin/sh
13   EXIM_LOOKUP_MAKEFILE_ADJUSTED=yes
14   export EXIM_LOOKUP_MAKEFILE_ADJUSTED
15
16   # Solaris sh and tr are problematic until we get xpg4 variants
17   if [ -x /usr/xpg4/bin/sh ]
18   then
19     PATH="/usr/xpg4/bin:$PATH"
20     export PATH
21     SHELL=/usr/xpg4/bin/sh
22     export SHELL
23   fi
24
25   # IRIX uses /bin/ksh for sh but in a compatibility mode unless $_XPG == 1,
26   # where said compatibility mode disables $(...)
27   _XPG=1
28   export _XPG
29
30   # We need the _right_ tr, so must do that first; but if a shell which
31   # we're more confident is sane is available, let's try that.  Mostly,
32   # the problem is that "local" is not actually in "the" standard, it's
33   # just in every not-insane shell.  Though arguably, there are no shells
34   # with POSIX-ish syntax which qualify as "not insane".
35   for b in /bin/dash /bin/bash /usr/local/bin/bash
36   do
37     if [ -x "$b" ]
38     then
39       SHELL="$b"
40       break
41     fi
42   done
43   # if we get a report of a system with zsh but not bash, we can add that
44   # to the list, but be sure to enable sh_word_split in that case.
45
46   exec "$SHELL" "$0" "$@"
47 fi
48
49 input=lookups/Makefile.predynamic
50 target=lookups/Makefile.postdynamic
51 defs_source=Makefile-t
52 tag_marker='MAGIC-TAG-MODS-OBJ-RULES-GO-HERE'
53
54 tab='   '
55
56 # We rely on tr(1) for translating case below.  Some people export
57 # values of LC_CTYPE and LC_COLLATE which apparently break our assumptions.
58 # We're a script expecting certain output based on known inputs and not dealing
59 # with UTF8, so we should be safe doingthis:
60 LC_ALL=C
61 export LC_ALL
62
63 if [ -f "$defs_source" ]
64 then
65   :
66   # we are happy
67 else
68   echo >&2 "$0: ERROR: MISSING FILE '${defs_source}'"
69   echo >&2 "$0: SHOULD HAVE BEEN CALLED FROM scripts/Configure-Makefile"
70   exit 1
71 fi
72
73 # nb: do not permit leading whitespace for this, as CFLAGS_DYNAMIC is exported
74 # to the lookups subdir via a line with leading whitespace which otherwise
75 # matches
76 if grep -q "^CFLAGS_DYNAMIC[ $tab?:]*=" "$defs_source"
77 then
78   # we have a definition, we're good to go
79   echo >&2 ">>> Creating lookups/Makefile for building dynamic modules"
80   enable_dynamic=yes
81 else
82   echo >&2 ">>> Creating lookups/Makefile without dynamic module support"
83   enable_dynamic=''
84   # We always do something now, since there should always be a lookup,
85   # and now we need to run in order to put the OBJ=$(OBJ)+ rules in.  So we
86   # continue on.
87 fi
88
89 # For the want_ checks, we need to let the user override values from the make
90 # command-line, not just check the Makefile.
91
92 want_dynamic() {
93   local dyn_name="$1"
94   local re="LOOKUP_${dyn_name}[ $tab]*=[ $tab]*2"
95   env | grep -q "^$re"
96   if [ $? -eq 0 ]; then return 0; fi
97   grep -q "^[ $tab]*$re" "$defs_source"
98 }
99
100 want_at_all() {
101   local want_name="$1"
102   local re="LOOKUP_${want_name}[ $tab]*=[ $tab]*."
103   env | grep -q "^$re"
104   if [ $? -eq 0 ]; then return 0; fi
105   grep -q "^[ $tab]*$re" "$defs_source"
106 }
107
108 # Adapted want_at_all above to work for EXPERIMENTAL features
109 want_experimental() {
110   local want_name="$1"
111   local re="EXPERIMENTAL_${want_name}[ $tab]*=[ $tab]*."
112   env | grep -q "^$re"
113   if [ $? -eq 0 ]; then return 0; fi
114   grep -q "^[ $tab]*$re" "$defs_source"
115 }
116
117 # The values of these variables will be emitted into the Makefile.
118
119 MODS=""
120 OBJ=""
121
122 emit_module_rule() {
123   local lookup_name="$1"
124   local mod_name pkgconf
125   if [ "${lookup_name%:*}" = "$lookup_name" ]
126   then
127     # Square brackets are redundant but benign for POSIX compliant tr,
128     # however Solaris /usr/bin/tr requires them. Sometimes Solaris
129     # gets installed without a complete set of xpg4 tools, sigh.
130     mod_name=$(echo $lookup_name | tr [A-Z] [a-z])
131   else
132     mod_name="${lookup_name#*:}"
133     lookup_name="${lookup_name%:*}"
134   fi
135
136   if want_dynamic "$lookup_name"
137   then
138     if [ -z "$enable_dynamic" ]; then
139       echo >&2 "Missing CFLAGS_DYNAMIC prevents building dynamic $lookup_name"
140       exit 1
141     fi
142     MODS="${MODS} ${mod_name}.so"
143     pkgconf=$(grep "^LOOKUP_${lookup_name}_PC" "$defs_source")
144     if [ $? -eq 0 ]; then
145       pkgconf=$(echo $pkgconf | sed 's/^.*= *//')
146       echo "LOOKUP_${mod_name}_INCLUDE = $(pkg-config --cflags $pkgconf)"
147       echo "LOOKUP_${mod_name}_LIBS = $(pkg-config --libs $pkgconf)"
148     else
149       grep "^LOOKUP_${lookup_name}_" "$defs_source"
150       echo "LOOKUP_${mod_name}_INCLUDE = \$(LOOKUP_${lookup_name}_INCLUDE)"
151       echo "LOOKUP_${mod_name}_LIBS = \$(LOOKUP_${lookup_name}_LIBS)"
152     fi
153   elif want_at_all "$lookup_name"
154   then
155     OBJ="${OBJ} ${mod_name}.o"
156   fi
157 }
158
159 rm -f "$target"
160 exec 5>&1
161 exec > "$target"
162
163 sed -n "1,/$tag_marker/p" < "$input"
164
165 for name_mod in \
166     CDB DBM:dbmdb DNSDB DSEARCH IBASE JSON LMDB LSEARCH MYSQL NIS NISPLUS ORACLE \
167     PASSWD PGSQL REDIS SQLITE TESTDB WHOSON
168 do
169   emit_module_rule $name_mod
170 done
171
172 if want_at_all LDAP
173 then
174   OBJ="${OBJ} ldap.o"
175 fi
176
177 # Because the variable is EXPERIMENTAL_SPF and not LOOKUP_SPF we
178 # always include spf.o and compile a dummy if EXPERIMENTAL_SPF is not
179 # defined.
180
181 OBJ="${OBJ} spf.o"
182
183 # readsock is always wanted as it implements the ${readsock } expansion
184 OBJ="${OBJ} readsock.o"
185
186 echo "MODS = $MODS"
187 echo "OBJ = $OBJ"
188
189 sed -n "/$tag_marker/,\$p" < "$input"
190
191 exec >&5
192
193 # Configure-Makefile will move $target into place
194
195 # vim: set ft=sh sw=2 :