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