buildsystem tooling
[exim.git] / src / scripts / routers-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/routers/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_ROUTER_MAKEFILE_ADJUSTED" ]
11 then
12   SHELL=/bin/sh
13   EXIM_ROUTER_MAKEFILE_ADJUSTED=yes
14   export EXIM_ROUTER_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=routers/Makefile.predynamic
50 target=routers/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 doing this:
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 routers/Makefile for building dynamic modules"
80   enable_dynamic=yes
81 else
82   echo >&2 ">>> Creating routers/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="ROUTER_${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="ROUTER_${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 name="$1"
124   local mod_name pkgconf
125   if [ "${name%:*}" = "$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 $name | tr [A-Z] [a-z])
131   else
132     mod_name="${name#*:}"
133     name="${name%:*}"
134   fi
135
136   if want_dynamic "$name"
137   then
138     if [ -z "$enable_dynamic" ]; then
139       echo >&2 "Missing CFLAGS_DYNAMIC prevents building dynamic $name"
140       exit 1
141     fi
142     MODS="${MODS} ${mod_name}.so"
143 #    pkgconf=$(grep "^ROUTER_${name}_PC" "$defs_source")
144 #    if [ $? -eq 0 ]; then
145 #      pkgconf=$(echo $pkgconf | sed 's/^.*= *//')
146 #      echo "ROUTER_${mod_name}_INCLUDE = $(pkg-config --cflags $pkgconf)"
147 #      echo "ROUTER_${mod_name}_LIBS = $(pkg-config --libs $pkgconf)"
148 #    else
149 #      grep "^ROUTER_${name}_" "$defs_source"
150 #      echo "ROUTER_${mod_name}_INCLUDE = \$(ROUTER_${name}_INCLUDE)"
151 #      echo "ROUTER_${mod_name}_LIBS = \$(ROUTER_${name}_LIBS)"
152 #    fi
153   elif want_at_all "$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     ACCEPT DNSLOOKUP IPLITERAL IPLOOKUP MANUALROUTE QUERYPROGRAM REDIRECT
167 do
168   emit_module_rule $name_mod
169 done
170
171 echo "MODS = $MODS"
172 echo "OBJ = $OBJ"
173
174 sed -n "/$tag_marker/,\$p" < "$input"
175
176 exec >&5
177
178 # Configure-Makefile will move $target into place
179
180 # vim: set ft=sh sw=2 :