]> git.armaanb.net Git - opendoas.git/blob - configure
configure: error out if no authentication found and fix default CC
[opendoas.git] / configure
1 #!/bin/sh
2
3 die() {
4         printf "$1\n" >&2
5         exit 1
6 }
7
8 usage() {
9         cat <<EOF
10 usage: configure [options]
11
12   --prefix=PREFIX        installation prefix [/usr]
13   --exec-prefix=EPREFIX  installation prefix for executable files [PREFIX]
14   --bindir=DIR           user executables [PREFIX/bin]
15   --datadir=DIR          architecture-independent data files [PREFIX/share]
16   --mandir=DIR           manual pages [DATADIR/man]
17   --sysconfdir=DIR       directory for configuration files [/etc]
18   --pamdir=DIR           PAM directory [SYSCONFDIR/pam.d]
19
20   --build=build-alias    a cpu-vendor-opsys for the system where the application will be built
21   --host=host-alias      a cpu-vendor-opsys for the system where the application will run
22   --target=target-alias  the machine that CC will produce code for
23
24   --enable-debug         enable debugging
25   --enable-static        prepare for static build
26
27   --without-pam          disable pam support
28   --without-pam          disable shadow support
29
30   --help, -h             display this help and exit
31 EOF
32         exit 0
33 }
34
35 for x; do
36         opt=${x%%=*}
37         var=${x#*=}
38         case "$opt" in
39         --prefix) PREFIX=$var ;;
40         --exec-prefix) EPREFIX=$var ;;
41         --bindir) BINDIR=$var ;;
42         --datadir) SHAREDIR=$var ;;
43         --mandir) MANDIR=$var ;;
44         --sysconfdir) SYSCONFDIR=$var ;;
45         --pamdir) PAMDIR=$var ;;
46         --build) BUILD=$var ;;
47         --host) HOST=$var ;;
48         --target) TARGET=$var ;;
49         --enable-debug) DEBUG=yes ;;
50         --enable-static) BUILD_STATIC=yes ;;
51         --with-pam) WITHOUT_PAM= ;;
52         --with-shadow) WITHOUT_SHADOW= ;;
53         --without-pam) WITHOUT_PAM=yes ;;
54         --without-shadow) WITHOUT_SHADOW=yes ;;
55         --help|-h) usage ;;
56         *) die "Error: unknown option $opt" ;;
57         esac
58 done
59
60 CONFIG_MK=config.mk
61 rm -f "$CONFIG_MK"
62
63 # : ${VERSION:="$(git describe --dirty --tags --long --always)"}
64 : ${VERSION:="0.3.2"}
65
66 cat <<EOF >>$CONFIG_MK
67 PREFIX   ?=     ${PREFIX:="/usr"}
68 EPREFIX  ?=     ${EPREFIX:="${PREFIX}"}
69 BINDIR   ?=     ${BINDIR:="${PREFIX}/bin"}
70 SHAREDIR ?=     ${SHAREDIR:="${PREFIX}/share"}
71 MANDIR   ?=     ${MANDIR:="${SHAREDIR}/man"}
72 SYSCONFDIR?=    ${SYSCONFDIR:="/etc"}
73 PAMDIR   ?=     ${PAMDIR:="${SYSCONFDIR}/pam.d"}
74 CFLAGS   +=     -DVERSION="\"${VERSION}\""
75 EOF
76
77 if [ -z "$BUILD" ]; then
78         BUILD="$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')"
79 fi
80 if [ -z "$HOST" ]; then
81         [ -z "$TARGET" ] && TARGET=$BUILD
82         HOST=$TARGET
83 fi
84 if [ -z "$TARGET" ]; then
85         [ -z "$HOST" ] && HOST=$BUILD
86         TARGET=$HOST
87 fi
88
89 if [ -z "$OS" ]; then
90         # Derive OS from cpu-manufacturer-os-kernel
91         CPU=${TARGET%%-*}
92         REST=${TARGET#*-}
93         MANU=${REST%%-*}
94         REST=${REST#*-}
95         OS=${REST%%-*}
96         REST=${REST#*-}
97         KERNEL=${REST%%-*}
98 fi
99
100 OS_CFLAGS="-D__${OS}__"
101
102 case "$OS" in
103         linux)
104                 OS_CFLAGS="$OS_CFLAGS -D_DEFAULT_SOURCE -D_GNU_SOURCE -DUID_MAX=60000 -DGID_MAX=60000"
105                 printf 'CURDIR   :=     .\n' >>$CONFIG_MK
106                 [ -z "$WITHOUT_PAM" ] && \
107                         printf 'PAM_DOAS  =     pam.d__doas__linux\n' >>$CONFIG_MK
108                 ;;
109 esac
110
111 [ -n "$OS_CFLAGS" ] && \
112         printf 'CFLAGS   +=     %s\n' "$OS_CFLAGS" >>$CONFIG_MK
113
114 [ -n "$DEBUG" ] && \
115         printf 'CFLAGS   +=     -O0 -g\n' >>$CONFIG_MK
116
117 [ -n "$BUILD_STATIC" ] && \
118         printf 'CFLAGS   +=     -static\n' >>$CONFIG_MK
119
120 # Add CPPFLAGS/CFLAGS/LDFLAGS to CC for testing features
121 XCC="${CC:=cc} $CFLAGS $OS_CFLAGS $CPPFLAGS $LDFLAGS"
122 # Make sure to disable --as-needed for CC tests.
123 XCC="$XCC -Wl,--no-as-needed"
124
125 check_func() {
126         func="$1"; src="$2"; shift 2
127         printf 'Checking for %-14s\t\t' "$func ..." >&2
128         printf '%s\n' "$src" >"_$func.c"
129         $XCC "_$func.c" -o "_$func" 2>/dev/null
130         ret=$?
131         rm -f "_$func.c" "_$func"
132         if [ $ret -eq 0 ]; then
133                 printf 'yes.\n' >&2
134                 upperfunc="$(printf '%s\n' "$func" | tr '[[:lower:]]' '[[:upper:]]')"
135                 printf 'CFLAGS   +=     -DHAVE_%s\n' "$upperfunc" >>$CONFIG_MK
136                 return 0
137         else
138                 printf 'no.\n' >&2
139                 return 1
140         fi
141 }
142
143 authmethod() {
144         #
145         # Check for bsd_auth.h.
146         #
147         src='
148 #include <bsd_auth.h>
149 int main(void) {
150         return 0;
151 }'
152         check_func "bsd_auth_h" "$src" && {
153                 have_bsd_auth_h=1
154                 printf 'bsd\n'
155                 return 0
156         }
157
158         #
159         # Check for pam_appl.h.
160         #
161         src='
162 #include <security/pam_appl.h>
163 int main(void) {
164         return 0;
165 }'
166         [ -z "$WITHOUT_PAM" ] && check_func "pam_appl_h" "$src" && {
167                 printf 'SRCS     +=     pam.c\n' >>$CONFIG_MK
168                 printf 'LDFLAGS  +=     -lpam\n' >>$CONFIG_MK
169                 printf 'pam\n'
170                 return 0
171         }
172
173         #
174         # Check for shadow.h.
175         #
176         src='
177 #include <shadow.h>
178 int main(void) {
179         return 0;
180 }'
181         [ -z "$WITHOUT_SHADOW" ] && check_func "shadow_h" "$src" && {
182                 printf 'LDFLAGS  +=     -lcrypt\n' >>$CONFIG_MK
183                 printf 'shadow\n'
184                 return 0
185         }
186
187         return 1
188 }
189
190 #
191 # Check for explicit_bzero().
192 #
193 src='
194 #include <string.h>
195 int main(void) {
196         explicit_bzero(NULL, 0);
197         return 0;
198 }'
199 check_func "explicit_bzero" "$src" || {
200         printf 'OPENBSD  +=     explicit_bzero.o\n' >>$CONFIG_MK
201 }
202
203 #
204 # Check for strlcat().
205 #
206 src='
207 #include <string.h>
208 int main(void) {
209         const char s1[] = "foo";
210         char s2[10];
211         strlcat(s2, s1, sizeof(s2));
212         return 0;
213 }'
214 check_func "strlcat" "$src" || {
215         printf 'OPENBSD  +=     strlcat.o\n' >>$CONFIG_MK
216 }
217
218 #
219 # Check for strlcpy().
220 #
221 src='
222 #include <string.h>
223 int main(void) {
224         const char s1[] = "foo";
225         char s2[10];
226         strlcpy(s2, s1, sizeof(s2));
227         return 0;
228 }'
229 check_func "strlcpy" "$src" || {
230         printf 'OPENBSD  +=     strlcpy.o\n' >>$CONFIG_MK
231 }
232
233 #
234 # Check for errc().
235 #
236 src='
237 #include <err.h>
238 int main(void) {
239         errc(0, 0, "");
240         return 0;
241 }'
242 check_func "errc" "$src" || {
243         printf 'OPENBSD  +=     errc.o\n' >>$CONFIG_MK
244 }
245
246 #
247 # Check for verrc().
248 #
249 src='
250 #include <err.h>
251 int main(void) {
252         verrc(0, 0, "");
253         return 0;
254 }'
255 check_func "verrc" "$src" || {
256         printf 'OPENBSD  +=     verrc.o\n' >>$CONFIG_MK
257 }
258
259 #
260 # Check for setprogname().
261 #
262 src='
263 #include <stdlib.h>
264 int main(void) {
265         setprogname("");
266         return 0;
267 }'
268 check_func "setprogname" "$src" || {
269         printf 'OPENBSD  +=     progname.o\n' >>$CONFIG_MK
270 }
271
272 #
273 # Check for readpassphrase().
274 #
275 src='
276 #include <readpassphrase.h>
277 int main(void) {
278         char buf[12];
279         readpassphrase("", buf, sizeof(buf), 0);
280         return 0;
281 }'
282 check_func "readpassphrase" "$src" || {
283         printf 'OPENBSD  +=     readpassphrase.o\n' >>$CONFIG_MK
284 }
285
286 #
287 # Check for strtonum().
288 #
289 src='
290 #include <stdlib.h>
291 int main(void) {
292         const char *errstr;
293         strtonum("", 1, 64, &errstr);
294         return 0;
295 }'
296 check_func "strtonum" "$src" || {
297         printf 'OPENBSD  +=     strtonum.o\n' >>$CONFIG_MK
298 }
299
300 #
301 # Check for reallocarray().
302 #
303 src='
304 #include <stdlib.h>
305 int main(void) {
306         reallocarray(NULL, 0, 0);
307         return 0;
308 }'
309 check_func "reallocarray" "$src" || {
310         printf 'OPENBSD  +=     reallocarray.o\n' >>$CONFIG_MK
311 }
312
313 #
314 # Check for execvpe().
315 #
316 src='
317 #include <unistd.h>
318 int main(void) {
319         const char *p = { "", NULL };
320         execvpe("", p, p);
321         return 0;
322 }'
323 check_func "execvpe" "$src" || {
324         printf 'OPENBSD  +=     execvpe.o\n' >>$CONFIG_MK
325 }
326
327 #
328 # Check for setresuid().
329 #
330 src='
331 #include <unistd.h>
332 int main(void) {
333         setresuid(0, 0, 0);
334         return 0;
335 }'
336 check_func "setresuid" "$src" || {
337         printf 'OPENBSD  +=     setresuid.o\n' >>$CONFIG_MK
338 }
339
340 #
341 # Check for pledge().
342 #
343 src='
344 #include <unistd.h>
345 int main(void) {
346         pledge("", NULL);
347         return 0;
348 }'
349 check_func "pledge" "$src" || {
350         printf 'OPENBSD  +=     pledge-noop.o\n' >>$CONFIG_MK
351 }
352
353 #
354 #
355 #
356 src='
357 #include <stdlib.h>
358 int main(void){return 0;}
359 __attribute__((__unused__)) static void foo(void){return;}
360 '
361 check_func "__attribute__" "$src" || {
362         printf 'CFLAGS  +=      -DNO_ATTRIBUTE_ON_RETURN_TYPE=1\n' >>$CONFIG_MK
363 }
364
365 auth=$(authmethod)
366 if [ $? -eq 0 ]; then
367         printf 'Using auth method\t\t\t%s.\n' "$auth" >&2
368 else
369         printf 'Error auth method\t\t\n' >&2
370         exit 1
371 fi