]> git.armaanb.net Git - opendoas.git/blob - configure
simplify makefile
[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 if [ -z "$BUILD" ]; then
85         BUILD="$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')"
86 fi
87 if [ -z "$HOST" ]; then
88         [ -z "$TARGET" ] && TARGET=$BUILD
89         HOST=$TARGET
90 fi
91 if [ -z "$TARGET" ]; then
92         [ -z "$HOST" ] && HOST=$BUILD
93         TARGET=$HOST
94 fi
95
96 if [ -z "$OS" ]; then
97         # Derive OS from cpu-manufacturer-os-kernel
98         CPU=${TARGET%%-*}
99         REST=${TARGET#*-}
100         MANU=${REST%%-*}
101         REST=${REST#*-}
102         OS=${REST%%-*}
103         REST=${REST#*-}
104         KERNEL=${REST%%-*}
105 fi
106
107 OS_CFLAGS="-D__${OS}__"
108
109 case "$OS" in
110         linux)
111                 printf 'Setting UID_MAX\t\t\t\t%d.\n' "$UID_MAX" >&2
112                 printf '#define UID_MAX %s\n' "$UID_MAX" >>$CONFIG_H
113                 printf 'Setting GID_MAX\t\t\t\t%d.\n' "$GID_MAX" >&2
114                 printf '#define GID_MAX %s\n' "$GID_MAX" >>$CONFIG_H
115                 OS_CFLAGS="$OS_CFLAGS -D_DEFAULT_SOURCE -D_GNU_SOURCE"
116                 ;;
117         netbsd)
118                 OS_CFLAGS="$OS_CFLAGS -D_OPENBSD_SOURCE"
119                 printf 'LDFLAGS  +=     -lutil\n' >>$CONFIG_MK
120                 : ${BINGRP:=wheel}
121                 ;;
122 esac
123
124 : ${PREFIX:=/usr/local}
125 : ${EPREFIX:=${PREFIX}}
126 : ${BINDIR:=${PREFIX}/bin}
127 : ${SHAREDIR:=${PREFIX}/share}
128 : ${MANDIR:=${SHAREDIR}/man}
129 : ${SYSCONFDIR:=/etc}
130 : ${PAMDIR:=${SYSCONFDIR}/pam.d}
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 PAMDIR   ?=     ${PAMDIR}
143 BINMODE  ?=     ${BINMODE}
144 BINOWN  ?=      ${BINOWN}
145 BINGRP  ?=      ${BINGRP}
146 EOF
147
148 [ -n "$OS_CFLAGS" ] && \
149         printf 'CFLAGS   +=     %s\n' "$OS_CFLAGS" >>$CONFIG_MK
150
151 [ -n "$DEBUG" ] && \
152         printf 'CFLAGS   +=     -O0 -g\n' >>$CONFIG_MK
153
154 [ -n "$BUILD_STATIC" ] && \
155         printf 'CFLAGS   +=     -static\n' >>$CONFIG_MK
156
157 # Add CPPFLAGS/CFLAGS/LDFLAGS to CC for testing features
158 XCC="${CC:=cc} $CFLAGS $OS_CFLAGS $CPPFLAGS $LDFLAGS"
159 # Make sure to disable --as-needed for CC tests.
160 XCC="$XCC -Wl,--no-as-needed"
161
162 check_func() {
163         func="$1"; src="$2"; shift 2
164         printf 'Checking for %-14s\t\t' "$func ..." >&2
165         printf '%s\n' "$src" >"_$func.c"
166         $XCC "_$func.c" -o "_$func" 2>/dev/null
167         ret=$?
168         rm -f "_$func.c" "_$func"
169         upperfunc="$(printf '%s\n' "$func" | tr '[[:lower:]]' '[[:upper:]]')"
170         if [ $ret -eq 0 ]; then
171                 printf 'yes.\n' >&2
172                 printf '#define HAVE_%s\n' "$upperfunc" >>$CONFIG_H
173                 return 0
174         else
175                 printf '/* #define HAVE_%s */\n' "$upperfunc" >>$CONFIG_H
176                 printf 'no.\n' >&2
177                 return 1
178         fi
179 }
180
181 authmethod() {
182         #
183         # Check for pam_appl.h.
184         #
185         src='
186 #include <security/pam_appl.h>
187 int main(void) {
188         return 0;
189 }'
190         [ -z "$WITHOUT_PAM" ] && check_func "pam_appl_h" "$src" && {
191                 printf 'SRCS     +=     pam.c\n' >>$CONFIG_MK
192                 printf 'LDFLAGS  +=     -lpam\n' >>$CONFIG_MK
193                 printf '#define USE_PAM\n' >>$CONFIG_H
194                 printf 'pam\n'
195
196                 pam_file="pam.d__doas__${OS}"
197                 [ -e "$pam_file" ] && printf 'PAM_DOAS  =       %s\n' "$pam_file" >>$CONFIG_MK
198                 return 0
199         }
200
201         #
202         # Check for shadow.h.
203         #
204         src='
205 #include <shadow.h>
206 int main(void) {
207         return 0;
208 }'
209         [ -z "$WITHOUT_SHADOW" ] && check_func "shadow_h" "$src" && {
210                 printf 'SRCS     +=     shadow.c\n' >>$CONFIG_MK
211                 printf 'LDFLAGS  +=     -lcrypt\n' >>$CONFIG_MK
212                 printf '#define USE_SHADOW\n' >>$CONFIG_H
213                 printf 'shadow\n'
214                 return 0
215         }
216
217         return 1
218 }
219
220 persistmethod() {
221         [ -z "$WITHOUT_TIMESTAMP" ] && {
222                 printf '#define USE_TIMESTAMP\n' >>$CONFIG_H
223                 printf 'SRCS    += timestamp.c\n' >>$CONFIG_MK
224                 printf 'timestamp\n'
225                 return 0
226         }
227         return 1
228 }
229
230 #
231 # Check for explicit_bzero().
232 #
233 src='
234 #include <string.h>
235 int main(void) {
236         explicit_bzero(NULL, 0);
237         return 0;
238 }'
239 check_func "explicit_bzero" "$src" || {
240         printf 'SRCS += libopenbsd/explicit_bzero.c\n' >>$CONFIG_MK
241 }
242
243 #
244 # Check for strlcat().
245 #
246 src='
247 #include <string.h>
248 int main(void) {
249         const char s1[] = "foo";
250         char s2[10];
251         strlcat(s2, s1, sizeof(s2));
252         return 0;
253 }'
254 check_func "strlcat" "$src" || {
255         printf 'SRCS += libopenbsd/strlcat.c\n' >>$CONFIG_MK
256 }
257
258 #
259 # Check for strlcpy().
260 #
261 src='
262 #include <string.h>
263 int main(void) {
264         const char s1[] = "foo";
265         char s2[10];
266         strlcpy(s2, s1, sizeof(s2));
267         return 0;
268 }'
269 check_func "strlcpy" "$src" || {
270         printf 'SRCS += libopenbsd/strlcpy.c\n' >>$CONFIG_MK
271 }
272
273 #
274 # Check for errc().
275 #
276 src='
277 #include <err.h>
278 int main(void) {
279         errc(0, 0, "");
280         return 0;
281 }'
282 check_func "errc" "$src" || {
283         printf 'SRCS += libopenbsd/errc.c\n' >>$CONFIG_MK
284 }
285
286 #
287 # Check for verrc().
288 #
289 src='
290 #include <err.h>
291 int main(void) {
292         verrc(0, 0, "");
293         return 0;
294 }'
295 check_func "verrc" "$src" || {
296         printf 'SRCS += libopenbsd/verrc.c\n' >>$CONFIG_MK
297 }
298
299 #
300 # Check for setprogname().
301 #
302 src='
303 #include <stdlib.h>
304 int main(void) {
305         setprogname("");
306         return 0;
307 }'
308 check_func "setprogname" "$src" || {
309         printf 'SRCS += libopenbsd/progname.c\n' >>$CONFIG_MK
310 }
311
312 #
313 # Check for readpassphrase().
314 #
315 src='
316 #include <readpassphrase.h>
317 int main(void) {
318         char buf[12];
319         readpassphrase("", buf, sizeof(buf), 0);
320         return 0;
321 }'
322 check_func "readpassphrase" "$src" || {
323         printf 'SRCS += libopenbsd/readpassphrase.c\n' >>$CONFIG_MK
324 }
325
326 #
327 # Check for strtonum().
328 #
329 src='
330 #include <stdlib.h>
331 int main(void) {
332         const char *errstr;
333         strtonum("", 1, 64, &errstr);
334         return 0;
335 }'
336 check_func "strtonum" "$src" || {
337         printf 'SRCS += libopenbsd/strtonum.c\n' >>$CONFIG_MK
338 }
339
340 #
341 # Check for reallocarray().
342 #
343 src='
344 #include <stdlib.h>
345 int main(void) {
346         reallocarray(NULL, 0, 0);
347         return 0;
348 }'
349 check_func "reallocarray" "$src" || {
350         printf 'SRCS += libopenbsd/reallocarray.c\n' >>$CONFIG_MK
351 }
352
353 #
354 # Check for execvpe().
355 #
356 src='
357 #include <unistd.h>
358 int main(void) {
359         const char *p = { "", NULL };
360         execvpe("", p, p);
361         return 0;
362 }'
363 check_func "execvpe" "$src" || die "system has no execvpe(3): not supported"
364
365 #
366 # Check for setresuid().
367 #
368 src='
369 #include <unistd.h>
370 int main(void) {
371         setresuid(0, 0, 0);
372         return 0;
373 }'
374 check_func "setresuid" "$src" || {
375         printf 'SRCS += libopenbsd/bsd-setres_id.c\n' >>$CONFIG_MK
376 }
377
378 #
379 # Check for closefrom().
380 #
381 src='
382 #include <unistd.h>
383 int main(void) {
384         closefrom(0);
385         return 0;
386 }'
387 check_func "closefrom" "$src" || {
388         printf 'SRCS += libopenbsd/closefrom.c\n' >>$CONFIG_MK
389 }
390
391 #
392 # Check for sysconf().
393 #
394 src='
395 #include <unistd.h>
396 int main(void) {
397         (void)sysconf(0);
398         return 0;
399 }'
400 check_func "sysconf" "$src"
401
402 #
403 # Check for dirfd().
404 #
405 src='
406 #include <dirent.h>
407 int main(void) {
408         (void)dirfd(0);
409         return 0;
410 }'
411 check_func "dirfd" "$src"
412
413 #
414 # Check for fcntl.h.
415 #
416 src='
417 #include <fcntl.h>
418 int main(void) {
419         return 0;
420 }'
421 check_func "fcntl_h" "$src"
422
423 #
424 # Check for F_CLOSEM.
425 #
426 src='
427 #include <fcntl.h>
428 #ifndef F_CLOSEM
429 #error no F_CLOSEM
430 #endif
431 int main(void) {
432         return 0;
433 }'
434 check_func "F_CLOSEM" "$src"
435
436 #
437 # Check for dirent.h.
438 #
439 src='
440 #include <dirent.h>
441 int main(void) {
442         return 0;
443 }'
444 check_func "dirent_h" "$src"
445
446 #
447 # Check for sys/ndir.h.
448 #
449 src='
450 #include <sys/ndir.h>
451 int main(void) {
452         return 0;
453 }'
454 check_func "sys_ndir_h" "$src"
455
456 #
457 # Check for sys/dir.h.
458 #
459 src='
460 #include <sys/dir.h>
461 int main(void) {
462         return 0;
463 }'
464 check_func "sys_dir_h" "$src"
465
466 #
467 # Check for ndir.h.
468 #
469 src='
470 #include <ndir.h>
471 int main(void) {
472         return 0;
473 }'
474 check_func "ndir_h" "$src"
475
476 #
477 # Check for login_cap.h.
478 #
479 src='
480 #include <sys/types.h>
481 #include <login_cap.h>
482 int main(void) {
483         return 0;
484 }'
485 check_func "login_cap_h" "$src"
486
487 #
488 #
489 #
490 src='
491 #include <stdlib.h>
492 int main(void){return 0;}
493 __attribute__((__unused__)) static void foo(void){return;}
494 '
495 check_func "__attribute__" "$src" || {
496         printf 'CFLAGS  +=      -DNO_ATTRIBUTE_ON_RETURN_TYPE=1\n' >>$CONFIG_MK
497 }
498
499 auth=$(authmethod)
500 if [ $? -eq 0 ]; then
501         printf 'Using auth method\t\t\t%s.\n' "$auth" >&2
502 else
503         printf 'Error auth method\t\t\n' >&2
504         exit 1
505 fi
506
507 persist=$(persistmethod)
508 if [ $? -eq 0 ]; then
509         printf 'Using persist method\t\t\t%s.\n' "$persist" >&2
510 else
511         printf 'Using persist method\t\t\tnone.\n' >&2
512 fi
513
514 printf '#define DOAS_CONF "%s/doas.conf"\n' "${SYSCONFDIR}" >>$CONFIG_H
515
516 printf '\n#endif /* CONFIG_H */\n' >>$CONFIG_H