]> git.armaanb.net Git - opendoas.git/blob - configure
link libutil for setusercontext on NetBSD
[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-shadow       disable shadow support
29
30   --with-timestamp       enable timestamp support
31
32   --uid-max=NUM          set UID_MAX (default 65535)
33   --gid-max=NUM          set GID_MAX (default 65535)
34
35   --help, -h             display this help and exit
36 EOF
37         exit 0
38 }
39
40 # defaults
41 WITHOUT_TIMESTAMP=yes
42 UID_MAX=65535
43 GID_MAX=65535
44
45 for x; do
46         opt=${x%%=*}
47         var=${x#*=}
48         case "$opt" in
49         --prefix) PREFIX=$var ;;
50         --exec-prefix) EPREFIX=$var ;;
51         --bindir) BINDIR=$var ;;
52         --datadir) SHAREDIR=$var ;;
53         --mandir) MANDIR=$var ;;
54         --sysconfdir) SYSCONFDIR=$var ;;
55         --pamdir) PAMDIR=$var ;;
56         --build) BUILD=$var ;;
57         --host) HOST=$var ;;
58         --target) TARGET=$var ;;
59         --enable-debug) DEBUG=yes ;;
60         --enable-static) BUILD_STATIC=yes ;;
61         --with-pam) WITHOUT_PAM=; WITHOUT_SHADOW=yes ;;
62         --with-shadow) WITHOUT_SHADOW=; WITHOUT_PAM=yes ;;
63         --without-pam) WITHOUT_PAM=yes ;;
64         --without-shadow) WITHOUT_SHADOW=yes ;;
65         --with-timestamp) WITHOUT_TIMESTAMP= ;;
66         --without-timestamp) WITHOUT_TIMESTAMP=yes ;;
67         --uid-max) UID_MAX=$var ;;
68         --gid-max) UID_MAX=$var ;;
69         --help|-h) usage ;;
70         *) die "Error: unknown option $opt" ;;
71         esac
72 done
73
74 CONFIG_MK=config.mk
75 CONFIG_H=config.h
76 rm -f "$CONFIG_MK" "$CONFIG_H"
77
78 cat <<! >$CONFIG_H
79 #ifndef CONFIG_H
80 #define CONFIG_H
81
82 !
83
84 cat <<EOF >>$CONFIG_MK
85 PREFIX   ?=     ${PREFIX:="/usr"}
86 EPREFIX  ?=     ${EPREFIX:="${PREFIX}"}
87 BINDIR   ?=     ${BINDIR:="${PREFIX}/bin"}
88 SHAREDIR ?=     ${SHAREDIR:="${PREFIX}/share"}
89 MANDIR   ?=     ${MANDIR:="${SHAREDIR}/man"}
90 SYSCONFDIR?=    ${SYSCONFDIR:="/etc"}
91 PAMDIR   ?=     ${PAMDIR:="${SYSCONFDIR}/pam.d"}
92 EOF
93
94 if [ -z "$BUILD" ]; then
95         BUILD="$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')"
96 fi
97 if [ -z "$HOST" ]; then
98         [ -z "$TARGET" ] && TARGET=$BUILD
99         HOST=$TARGET
100 fi
101 if [ -z "$TARGET" ]; then
102         [ -z "$HOST" ] && HOST=$BUILD
103         TARGET=$HOST
104 fi
105
106 if [ -z "$OS" ]; then
107         # Derive OS from cpu-manufacturer-os-kernel
108         CPU=${TARGET%%-*}
109         REST=${TARGET#*-}
110         MANU=${REST%%-*}
111         REST=${REST#*-}
112         OS=${REST%%-*}
113         REST=${REST#*-}
114         KERNEL=${REST%%-*}
115 fi
116
117 OS_CFLAGS="-D__${OS}__"
118
119 case "$OS" in
120         linux)
121                 printf 'Setting UID_MAX\t\t\t\t%d.\n' "$UID_MAX" >&2
122                 printf '#define UID_MAX %s\n' "$UID_MAX" >>$CONFIG_H
123                 printf 'Setting GID_MAX\t\t\t\t%d.\n' "$GID_MAX" >&2
124                 printf '#define GID_MAX %s\n' "$GID_MAX" >>$CONFIG_H
125                 OS_CFLAGS="$OS_CFLAGS -D_DEFAULT_SOURCE -D_GNU_SOURCE"
126                 printf 'CURDIR   :=     .\n' >>$CONFIG_MK
127                 ;;
128         netbsd)
129                 OS_CFLAGS="$OS_CFLAGS -D_OPENBSD_SOURCE"
130                 printf 'LDFLAGS  +=     -lutil\n' >>$CONFIG_MK
131                 ;;
132 esac
133
134 [ -n "$OS_CFLAGS" ] && \
135         printf 'CFLAGS   +=     %s\n' "$OS_CFLAGS" >>$CONFIG_MK
136
137 [ -n "$DEBUG" ] && \
138         printf 'CFLAGS   +=     -O0 -g\n' >>$CONFIG_MK
139
140 [ -n "$BUILD_STATIC" ] && \
141         printf 'CFLAGS   +=     -static\n' >>$CONFIG_MK
142
143 # Add CPPFLAGS/CFLAGS/LDFLAGS to CC for testing features
144 XCC="${CC:=cc} $CFLAGS $OS_CFLAGS $CPPFLAGS $LDFLAGS"
145 # Make sure to disable --as-needed for CC tests.
146 XCC="$XCC -Wl,--no-as-needed"
147
148 check_func() {
149         func="$1"; src="$2"; shift 2
150         printf 'Checking for %-14s\t\t' "$func ..." >&2
151         printf '%s\n' "$src" >"_$func.c"
152         $XCC "_$func.c" -o "_$func" 2>/dev/null
153         ret=$?
154         rm -f "_$func.c" "_$func"
155         upperfunc="$(printf '%s\n' "$func" | tr '[[:lower:]]' '[[:upper:]]')"
156         if [ $ret -eq 0 ]; then
157                 printf 'yes.\n' >&2
158                 printf '#define HAVE_%s\n' "$upperfunc" >>$CONFIG_H
159                 return 0
160         else
161                 printf '/* #define HAVE_%s */\n' "$upperfunc" >>$CONFIG_H
162                 printf 'no.\n' >&2
163                 return 1
164         fi
165 }
166
167 authmethod() {
168         #
169         # Check for pam_appl.h.
170         #
171         src='
172 #include <security/pam_appl.h>
173 int main(void) {
174         return 0;
175 }'
176         [ -z "$WITHOUT_PAM" ] && check_func "pam_appl_h" "$src" && {
177                 printf 'SRCS     +=     pam.c\n' >>$CONFIG_MK
178                 printf 'LDFLAGS  +=     -lpam\n' >>$CONFIG_MK
179                 printf '#define USE_PAM\n' >>$CONFIG_H
180                 printf 'pam\n'
181
182                 pam_file="pam.d__doas__${OS}"
183                 [ -e "$pam_file" ] && printf 'PAM_DOAS  =       %s\n' "$pam_file" >>$CONFIG_MK
184                 return 0
185         }
186
187         #
188         # Check for shadow.h.
189         #
190         src='
191 #include <shadow.h>
192 int main(void) {
193         return 0;
194 }'
195         [ -z "$WITHOUT_SHADOW" ] && check_func "shadow_h" "$src" && {
196                 printf 'SRCS     +=     shadow.c\n' >>$CONFIG_MK
197                 printf 'LDFLAGS  +=     -lcrypt\n' >>$CONFIG_MK
198                 printf '#define USE_SHADOW\n' >>$CONFIG_H
199                 printf 'shadow\n'
200                 return 0
201         }
202
203         return 1
204 }
205
206 persistmethod() {
207         [ -z "$WITHOUT_TIMESTAMP" ] && {
208                 printf '#define USE_TIMESTAMP\n' >>$CONFIG_H
209                 printf 'SRCS    += timestamp.c\n' >>$CONFIG_MK
210                 printf 'timestamp\n'
211                 return 0
212         }
213         return 1
214 }
215
216 #
217 # Check for explicit_bzero().
218 #
219 src='
220 #include <string.h>
221 int main(void) {
222         explicit_bzero(NULL, 0);
223         return 0;
224 }'
225 check_func "explicit_bzero" "$src" || {
226         printf 'SRCS += libopenbsd/explicit_bzero.c\n' >>$CONFIG_MK
227 }
228
229 #
230 # Check for strlcat().
231 #
232 src='
233 #include <string.h>
234 int main(void) {
235         const char s1[] = "foo";
236         char s2[10];
237         strlcat(s2, s1, sizeof(s2));
238         return 0;
239 }'
240 check_func "strlcat" "$src" || {
241         printf 'SRCS += libopenbsd/strlcat.c\n' >>$CONFIG_MK
242 }
243
244 #
245 # Check for strlcpy().
246 #
247 src='
248 #include <string.h>
249 int main(void) {
250         const char s1[] = "foo";
251         char s2[10];
252         strlcpy(s2, s1, sizeof(s2));
253         return 0;
254 }'
255 check_func "strlcpy" "$src" || {
256         printf 'SRCS += libopenbsd/strlcpy.c\n' >>$CONFIG_MK
257 }
258
259 #
260 # Check for errc().
261 #
262 src='
263 #include <err.h>
264 int main(void) {
265         errc(0, 0, "");
266         return 0;
267 }'
268 check_func "errc" "$src" || {
269         printf 'SRCS += libopenbsd/errc.c\n' >>$CONFIG_MK
270 }
271
272 #
273 # Check for verrc().
274 #
275 src='
276 #include <err.h>
277 int main(void) {
278         verrc(0, 0, "");
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" || die "system has no execvpe(3): not supported"
350
351 #
352 # Check for setresuid().
353 #
354 src='
355 #include <unistd.h>
356 int main(void) {
357         setresuid(0, 0, 0);
358         return 0;
359 }'
360 check_func "setresuid" "$src" || {
361         printf 'SRCS += libopenbsd/bsd-setres_id.c\n' >>$CONFIG_MK
362 }
363
364 #
365 # Check for closefrom().
366 #
367 src='
368 #include <unistd.h>
369 int main(void) {
370         closefrom(0);
371         return 0;
372 }'
373 check_func "closefrom" "$src" || {
374         printf 'SRCS += libopenbsd/closefrom.c\n' >>$CONFIG_MK
375 }
376
377 #
378 # Check for sysconf().
379 #
380 src='
381 #include <unistd.h>
382 int main(void) {
383         (void)sysconf(0);
384         return 0;
385 }'
386 check_func "sysconf" "$src"
387
388 #
389 # Check for dirfd().
390 #
391 src='
392 #include <dirent.h>
393 int main(void) {
394         (void)dirfd(0);
395         return 0;
396 }'
397 check_func "dirfd" "$src"
398
399 #
400 # Check for fcntl.h.
401 #
402 src='
403 #include <fcntl.h>
404 int main(void) {
405         return 0;
406 }'
407 check_func "fcntl_h" "$src"
408
409 #
410 # Check for F_CLOSEM.
411 #
412 src='
413 #include <fcntl.h>
414 #ifndef F_CLOSEM
415 #error no F_CLOSEM
416 #endif
417 int main(void) {
418         return 0;
419 }'
420 check_func "F_CLOSEM" "$src"
421
422 #
423 # Check for dirent.h.
424 #
425 src='
426 #include <dirent.h>
427 int main(void) {
428         return 0;
429 }'
430 check_func "dirent_h" "$src"
431
432 #
433 # Check for sys/ndir.h.
434 #
435 src='
436 #include <sys/ndir.h>
437 int main(void) {
438         return 0;
439 }'
440 check_func "sys_ndir_h" "$src"
441
442 #
443 # Check for sys/dir.h.
444 #
445 src='
446 #include <sys/dir.h>
447 int main(void) {
448         return 0;
449 }'
450 check_func "sys_dir_h" "$src"
451
452 #
453 # Check for ndir.h.
454 #
455 src='
456 #include <ndir.h>
457 int main(void) {
458         return 0;
459 }'
460 check_func "ndir_h" "$src"
461
462 #
463 # Check for login_cap.h.
464 #
465 src='
466 #include <sys/types.h>
467 #include <login_cap.h>
468 int main(void) {
469         return 0;
470 }'
471 check_func "login_cap_h" "$src"
472
473 #
474 #
475 #
476 src='
477 #include <stdlib.h>
478 int main(void){return 0;}
479 __attribute__((__unused__)) static void foo(void){return;}
480 '
481 check_func "__attribute__" "$src" || {
482         printf 'CFLAGS  +=      -DNO_ATTRIBUTE_ON_RETURN_TYPE=1\n' >>$CONFIG_MK
483 }
484
485 auth=$(authmethod)
486 if [ $? -eq 0 ]; then
487         printf 'Using auth method\t\t\t%s.\n' "$auth" >&2
488 else
489         printf 'Error auth method\t\t\n' >&2
490         exit 1
491 fi
492
493 persist=$(persistmethod)
494 if [ $? -eq 0 ]; then
495         printf 'Using persist method\t\t\t%s.\n' "$persist" >&2
496 else
497         printf 'Using persist method\t\t\tnone.\n' >&2
498 fi
499
500 printf '#define DOAS_CONF "%s/doas.conf"\n' "${SYSCONFDIR}" >>$CONFIG_H
501
502 printf '\n#endif /* CONFIG_H */\n' >>$CONFIG_H