]> git.armaanb.net Git - opendoas.git/blob - configure
55dabdfc0dc1cdee7342aaa17d77d9ec42334666
[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
19   --build=build-alias    a cpu-vendor-opsys for the system where the application will be built
20   --host=host-alias      a cpu-vendor-opsys for the system where the application will run
21   --target=target-alias  the machine that CC will produce code for
22
23   --enable-debug         enable debugging
24   --enable-static        prepare for static build
25
26   --without-pam          disable pam support
27   --without-shadow       disable shadow support
28
29   --with-timestamp       enable timestamp support
30
31   --with-kiss-insults    enable kiss insults
32
33   --uid-max=NUM          set UID_MAX (default 65535)
34   --gid-max=NUM          set GID_MAX (default 65535)
35
36   --help, -h             display this help and exit
37 EOF
38         exit 0
39 }
40
41 # defaults
42 WITHOUT_TIMESTAMP=yes
43 WITHOUT_KISS_INSULTS=""
44 UID_MAX=65535
45 GID_MAX=65535
46
47 for x; do
48         opt=${x%%=*}
49         var=${x#*=}
50         case "$opt" in
51         --prefix) PREFIX=$var ;;
52         --exec-prefix) EPREFIX=$var ;;
53         --bindir) BINDIR=$var ;;
54         --datadir) SHAREDIR=$var ;;
55         --mandir) MANDIR=$var ;;
56         --sysconfdir) SYSCONFDIR=$var ;;
57         --build) BUILD=$var ;;
58         --host) HOST=$var ;;
59         --target) TARGET=$var ;;
60         --enable-debug) DEBUG=yes ;;
61         --enable-static) BUILD_STATIC=yes ;;
62         --with-pam) WITHOUT_PAM=; WITHOUT_SHADOW=yes ;;
63         --with-shadow) WITHOUT_SHADOW=; WITHOUT_PAM=yes ;;
64         --without-pam) WITHOUT_PAM=yes ;;
65         --without-shadow) WITHOUT_SHADOW=yes ;;
66         --with-kiss-insults) WITH_KISS_INSULTS=yes ;;
67         --with-timestamp) WITHOUT_TIMESTAMP= ;;
68         --without-timestamp) WITHOUT_TIMESTAMP=yes ;;
69         --uid-max) UID_MAX=$var ;;
70         --gid-max) UID_MAX=$var ;;
71         --help|-h) usage ;;
72         *) die "Error: unknown option $opt" ;;
73         esac
74 done
75
76 CONFIG_MK=config.mk
77 CONFIG_H=config.h
78 rm -f "$CONFIG_MK" "$CONFIG_H"
79
80 cat <<! >$CONFIG_H
81 #ifndef CONFIG_H
82 #define CONFIG_H
83
84 !
85
86 if [ -z "$BUILD" ]; then
87         BUILD="$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')"
88 fi
89 if [ -z "$HOST" ]; then
90         [ -z "$TARGET" ] && TARGET=$BUILD
91         HOST=$TARGET
92 fi
93 if [ -z "$TARGET" ]; then
94         [ -z "$HOST" ] && HOST=$BUILD
95         TARGET=$HOST
96 fi
97
98 if [ -z "$OS" ]; then
99         # Derive OS from cpu-manufacturer-os-kernel
100         CPU=${TARGET%%-*}
101         REST=${TARGET#*-}
102         MANU=${REST%%-*}
103         REST=${REST#*-}
104         OS=${REST%%-*}
105         REST=${REST#*-}
106         KERNEL=${REST%%-*}
107 fi
108
109 OS_CFLAGS="-D__${OS}__"
110
111 case "$OS" in
112         linux)
113                 printf 'Setting UID_MAX\t\t\t\t%d.\n' "$UID_MAX" >&2
114                 printf '#define UID_MAX %s\n' "$UID_MAX" >>$CONFIG_H
115                 printf 'Setting GID_MAX\t\t\t\t%d.\n' "$GID_MAX" >&2
116                 printf '#define GID_MAX %s\n' "$GID_MAX" >>$CONFIG_H
117                 OS_CFLAGS="$OS_CFLAGS -D_DEFAULT_SOURCE -D_GNU_SOURCE"
118                 ;;
119         netbsd)
120                 OS_CFLAGS="$OS_CFLAGS -D_OPENBSD_SOURCE"
121                 printf 'LDLIBS +=       -lutil\n' >>$CONFIG_MK
122                 : ${BINGRP:=wheel}
123                 ;;
124         freebsd)
125                 printf 'LDLIBS +=       -lutil\n' >>$CONFIG_MK
126                 : ${BINGRP:=wheel}
127                 ;;
128         darwin)
129                 : ${BINGRP:=wheel}
130                 ;;
131 esac
132
133 : ${PREFIX:=/usr/local}
134 : ${EPREFIX:=${PREFIX}}
135 : ${BINDIR:=${PREFIX}/bin}
136 : ${SHAREDIR:=${PREFIX}/share}
137 : ${MANDIR:=${SHAREDIR}/man}
138 : ${SYSCONFDIR:=/etc}
139 : ${BINMODE:=4755}
140 : ${BINOWN:=root}
141 : ${BINGRP:=root}
142
143 cat <<EOF >>$CONFIG_MK
144 PREFIX   ?=     ${PREFIX}
145 EPREFIX  ?=     ${EPREFIX}
146 BINDIR   ?=     ${BINDIR}
147 SHAREDIR ?=     ${SHAREDIR}
148 MANDIR   ?=     ${MANDIR}
149 SYSCONFDIR?=    ${SYSCONFDIR}
150 BINMODE  ?=     ${BINMODE}
151 BINOWN  ?=      ${BINOWN}
152 BINGRP  ?=      ${BINGRP}
153 EOF
154
155 [ -n "$OS_CFLAGS" ] && \
156         printf 'OS_CFLAGS   +=  %s\n' "$OS_CFLAGS" >>$CONFIG_MK
157
158 [ -n "$DEBUG" ] && \
159         printf 'CFLAGS   +=     -O0 -g\n' >>$CONFIG_MK
160
161 [ -n "$BUILD_STATIC" ] && \
162         printf 'CFLAGS   +=     -static\n' >>$CONFIG_MK
163
164 # Add CPPFLAGS/CFLAGS/LDFLAGS/LDLIBS to CC for testing features
165 XCC="${CC:=cc} $CFLAGS $OS_CFLAGS $CPPFLAGS $LDFLAGS $LDLIBS"
166 # Make sure to disable --as-needed for CC tests.
167
168 case "$OS" in
169         darwin) ;;
170         *) XCC="$XCC -Wl,--no-as-needed" ;;
171 esac
172
173 check_func() {
174         func="$1"; src="$2"; shift 2
175         printf 'Checking for %-14s\t\t' "$func ..." >&2
176         printf '%s\n' "$src" >"_$func.c"
177         $XCC "_$func.c" -o "_$func" 2>/dev/null
178         ret=$?
179         rm -f "_$func.c" "_$func"
180         upperfunc="$(printf '%s\n' "$func" | tr '[[:lower:]]' '[[:upper:]]')"
181         if [ $ret -eq 0 ]; then
182                 printf 'yes.\n' >&2
183                 printf '#define HAVE_%s\n' "$upperfunc" >>$CONFIG_H
184                 return 0
185         else
186                 printf '/* #define HAVE_%s */\n' "$upperfunc" >>$CONFIG_H
187                 printf 'no.\n' >&2
188                 return 1
189         fi
190 }
191
192 authmethod() {
193         #
194         # Check for pam_appl.h.
195         #
196         src='
197 #include <security/pam_appl.h>
198 int main(void) {
199         return 0;
200 }'
201         [ -z "$WITHOUT_PAM" ] && check_func "pam_appl_h" "$src" && {
202                 printf 'SRCS     +=     pam.c\n' >>$CONFIG_MK
203                 printf 'LDLIBS +=       -lpam\n' >>$CONFIG_MK
204                 printf '#define USE_PAM\n' >>$CONFIG_H
205                 printf 'pam\n'
206                 return 0
207         }
208
209         #
210         # Check for shadow.h.
211         #
212         src='
213 #include <shadow.h>
214 int main(void) {
215         return 0;
216 }'
217         [ -z "$WITHOUT_SHADOW" ] && check_func "shadow_h" "$src" && {
218                 printf 'SRCS     +=     shadow.c\n' >>$CONFIG_MK
219                 printf 'LDLIBS +=       -lcrypt\n' >>$CONFIG_MK
220                 printf '#define USE_SHADOW\n' >>$CONFIG_H
221                 printf 'shadow\n'
222                 return 0
223         }
224
225         return 1
226 }
227
228 definsults() {
229         printf 'SRCS    += insults.c\n' >>$CONFIG_MK
230         [ -n "$WITH_KISS_INSULTS" ] && {
231                 printf '#define DOAS_INSULTS_KISS\n' >>$CONFIG_H
232         }
233         return 0
234 }
235
236
237 persistmethod() {
238         [ -z "$WITHOUT_TIMESTAMP" ] && {
239                 printf '#define USE_TIMESTAMP\n' >>$CONFIG_H
240                 printf 'SRCS    += timestamp.c\n' >>$CONFIG_MK
241                 printf 'timestamp\n'
242                 return 0
243         }
244         return 1
245 }
246
247 #
248 # Check for explicit_bzero().
249 #
250 src='
251 #include <string.h>
252 int main(void) {
253         explicit_bzero(NULL, 0);
254         return 0;
255 }'
256 check_func "explicit_bzero" "$src" || {
257         printf 'SRCS += libopenbsd/explicit_bzero.c\n' >>$CONFIG_MK
258 }
259
260 #
261 # Check for strlcat().
262 #
263 src='
264 #include <string.h>
265 int main(void) {
266         const char s1[] = "foo";
267         char s2[10];
268         strlcat(s2, s1, sizeof(s2));
269         return 0;
270 }'
271 check_func "strlcat" "$src" || {
272         printf 'SRCS += libopenbsd/strlcat.c\n' >>$CONFIG_MK
273 }
274
275 #
276 # Check for strlcpy().
277 #
278 src='
279 #include <string.h>
280 int main(void) {
281         const char s1[] = "foo";
282         char s2[10];
283         strlcpy(s2, s1, sizeof(s2));
284         return 0;
285 }'
286 check_func "strlcpy" "$src" || {
287         printf 'SRCS += libopenbsd/strlcpy.c\n' >>$CONFIG_MK
288 }
289
290 #
291 # Check for errc().
292 #
293 src='
294 #include <err.h>
295 int main(void) {
296         errc(0, 0, "");
297         return 0;
298 }'
299 check_func "errc" "$src" || {
300         printf 'SRCS += libopenbsd/errc.c\n' >>$CONFIG_MK
301 }
302
303 #
304 # Check for verrc().
305 #
306 src='
307 #include <stddef.h>
308 #include <err.h>
309 int main(void) {
310         verrc(0, 0, "x", NULL);
311         return 0;
312 }'
313 check_func "verrc" "$src" || {
314         printf 'SRCS += libopenbsd/verrc.c\n' >>$CONFIG_MK
315 }
316
317 #
318 # Check for setprogname().
319 #
320 src='
321 #include <stdlib.h>
322 int main(void) {
323         setprogname("");
324         return 0;
325 }'
326 check_func "setprogname" "$src" || {
327         printf 'SRCS += libopenbsd/progname.c\n' >>$CONFIG_MK
328 }
329
330 #
331 # Check for readpassphrase().
332 #
333 src='
334 #include <readpassphrase.h>
335 int main(void) {
336         char buf[12];
337         readpassphrase("", buf, sizeof(buf), 0);
338         return 0;
339 }'
340 check_func "readpassphrase" "$src" || {
341         printf 'SRCS += libopenbsd/readpassphrase.c\n' >>$CONFIG_MK
342 }
343
344 #
345 # Check for strtonum().
346 #
347 src='
348 #include <stdlib.h>
349 int main(void) {
350         const char *errstr;
351         strtonum("", 1, 64, &errstr);
352         return 0;
353 }'
354 check_func "strtonum" "$src" || {
355         printf 'SRCS += libopenbsd/strtonum.c\n' >>$CONFIG_MK
356 }
357
358 #
359 # Check for reallocarray().
360 #
361 src='
362 #include <stdlib.h>
363 int main(void) {
364         reallocarray(NULL, 0, 0);
365         return 0;
366 }'
367 check_func "reallocarray" "$src" || {
368         printf 'SRCS += libopenbsd/reallocarray.c\n' >>$CONFIG_MK
369 }
370
371 #
372 # Check for execvpe().
373 #
374 src='
375 #include <unistd.h>
376 int main(void) {
377         const char *p = { "", NULL };
378         execvpe("", p, p);
379         return 0;
380 }'
381 check_func "execvpe" "$src" || {
382         printf 'SRCS += libopenbsd/execvpe.c\n' >>$CONFIG_MK
383 }
384
385 #
386 # Check for setresuid().
387 #
388 src='
389 #include <unistd.h>
390 int main(void) {
391         setresuid(0, 0, 0);
392         return 0;
393 }'
394 check_func "setresuid" "$src"
395 have_setresuid=$?
396
397 #
398 # Check for setresgid().
399 #
400 src='
401 #include <unistd.h>
402 int main(void) {
403         setresgid(0, 0, 0);
404         return 0;
405 }'
406 check_func "setresgid" "$src"
407 have_setresgid=$?
408
409 if [ $have_setresuid -eq 1 -o $have_setresgid -eq 1 ]; then
410         printf 'SRCS += libopenbsd/bsd-setres_id.c\n' >>$CONFIG_MK
411 fi
412
413 #
414 # Check for setreuid().
415 #
416 src='
417 #include <unistd.h>
418 int main(void) {
419         setreuid(0, 0);
420         return 0;
421 }'
422 check_func "setreuid" "$src"
423
424
425 #
426 # Check for setregid().
427 #
428 src='
429 #include <unistd.h>
430 int main(void) {
431         setregid(0, 0);
432         return 0;
433 }'
434 check_func "setregid" "$src"
435
436 #
437 # Check for closefrom().
438 #
439 src='
440 #include <unistd.h>
441 int main(void) {
442         closefrom(0);
443         return 0;
444 }'
445 check_func "closefrom" "$src" || {
446         printf 'SRCS += libopenbsd/closefrom.c\n' >>$CONFIG_MK
447 }
448
449 #
450 # Check for sysconf().
451 #
452 src='
453 #include <unistd.h>
454 int main(void) {
455         (void)sysconf(0);
456         return 0;
457 }'
458 check_func "sysconf" "$src"
459
460 #
461 # Check for dirfd().
462 #
463 src='
464 #include <dirent.h>
465 int main(void) {
466         (void)dirfd(0);
467         return 0;
468 }'
469 check_func "dirfd" "$src"
470
471 #
472 # Check for fcntl.h.
473 #
474 src='
475 #include <fcntl.h>
476 int main(void) {
477         return 0;
478 }'
479 check_func "fcntl_h" "$src"
480
481 #
482 # Check for F_CLOSEM.
483 #
484 src='
485 #include <fcntl.h>
486 #ifndef F_CLOSEM
487 #error no F_CLOSEM
488 #endif
489 int main(void) {
490         return 0;
491 }'
492 check_func "F_CLOSEM" "$src"
493
494 #
495 # Check for dirent.h.
496 #
497 src='
498 #include <dirent.h>
499 int main(void) {
500         return 0;
501 }'
502 check_func "dirent_h" "$src"
503
504 #
505 # Check for sys/ndir.h.
506 #
507 src='
508 #include <sys/ndir.h>
509 int main(void) {
510         return 0;
511 }'
512 check_func "sys_ndir_h" "$src"
513
514 #
515 # Check for sys/dir.h.
516 #
517 src='
518 #include <sys/dir.h>
519 int main(void) {
520         return 0;
521 }'
522 check_func "sys_dir_h" "$src"
523
524 #
525 # Check for ndir.h.
526 #
527 src='
528 #include <ndir.h>
529 int main(void) {
530         return 0;
531 }'
532 check_func "ndir_h" "$src"
533
534 #
535 # Check for login_cap.h.
536 #
537 src='
538 #include <sys/types.h>
539 #include <login_cap.h>
540 int main(void) {
541         return 0;
542 }'
543 check_func "login_cap_h" "$src"
544
545 #
546 #
547 #
548 src='
549 #include <stdlib.h>
550 int main(void){return 0;}
551 __attribute__((__unused__)) static void foo(void){return;}
552 '
553 check_func "__attribute__" "$src" || {
554         printf 'OS_CFLAGS       +=      -DNO_ATTRIBUTE_ON_RETURN_TYPE=1\n' >>$CONFIG_MK
555 }
556
557 auth=$(authmethod)
558 if [ $? -eq 0 ]; then
559         printf 'Using auth method\t\t\t%s.\n' "$auth" >&2
560 else
561         printf 'Error auth method\t\t\n' >&2
562         exit 1
563 fi
564
565 insults=$(definsults)
566
567 persist=$(persistmethod)
568 if [ $? -eq 0 ]; then
569         printf 'Using persist method\t\t\t%s.\n' "$persist" >&2
570 else
571         printf 'Using persist method\t\t\tnone.\n' >&2
572 fi
573
574 printf '#define DOAS_CONF "%s/doas.conf"\n' "${SYSCONFDIR}" >>$CONFIG_H
575
576 printf '\n#endif /* CONFIG_H */\n' >>$CONFIG_H