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