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