configure revision 316420
1#!/bin/sh
2#
3# $Id: configure,v 1.55 2017/01/12 15:45:05 schwarze Exp $
4#
5# Copyright (c) 2014, 2015, 2016 Ingo Schwarze <schwarze@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19set -e
20
21[ -w config.log ] && mv config.log config.log.old
22[ -w config.h   ] && mv config.h config.h.old
23
24# Output file descriptor usage:
25# 1 (stdout): config.h, Makefile.local
26# 2 (stderr): original stderr, usually to the console
27# 3: config.log
28
29exec 3> config.log
30echo "config.log: writing..."
31
32# --- default settings -------------------------------------------------
33# Initialize all variables here,
34# such that nothing can leak in from the environment.
35
36MANPATH_DEFAULT="/usr/share/man:/usr/X11R6/man:/usr/local/man"
37OSNAME=
38UTF8_LOCALE=
39
40CC=`printf "all:\\n\\t@echo \\\$(CC)\\n" | env -i make -sf -`
41CFLAGS="-g -W -Wall -Wmissing-prototypes -Wstrict-prototypes -Wwrite-strings"
42CFLAGS="${CFLAGS} -Wno-unused-parameter"
43LDADD=
44LDFLAGS=
45LD_NANOSLEEP=
46LD_OHASH=
47STATIC="-static"
48
49BUILD_CGI=0
50INSTALL_LIBMANDOC=0
51
52HAVE_DIRENT_NAMLEN=
53HAVE_EFTYPE=
54HAVE_ENDIAN=
55HAVE_ERR=
56HAVE_FTS=
57HAVE_FTS_COMPARE_CONST=
58HAVE_GETLINE=
59HAVE_GETSUBOPT=
60HAVE_ISBLANK=
61HAVE_MKDTEMP=
62HAVE_NANOSLEEP=
63HAVE_NTOHL=
64HAVE_OHASH=
65HAVE_PATH_MAX=
66HAVE_PLEDGE=
67HAVE_PROGNAME=
68HAVE_REALLOCARRAY=
69HAVE_REWB_BSD=
70HAVE_REWB_SYSV=
71HAVE_SANDBOX_INIT=
72HAVE_STRCASESTR=
73HAVE_STRINGLIST=
74HAVE_STRLCAT=
75HAVE_STRLCPY=
76HAVE_STRPTIME=
77HAVE_STRSEP=
78HAVE_STRTONUM=
79HAVE_SYS_ENDIAN=
80HAVE_VASPRINTF=
81HAVE_WCHAR=
82
83PREFIX="/usr/local"
84BINDIR=
85SBINDIR=
86INCLUDEDIR=
87LIBDIR=
88MANDIR=
89HOMEBREWDIR=
90
91WWWPREFIX="/var/www"
92HTDOCDIR=
93CGIBINDIR=
94
95BINM_APROPOS="apropos"
96BINM_MAKEWHATIS="makewhatis"
97BINM_MAN="man"
98BINM_SOELIM="soelim"
99BINM_WHATIS="whatis"
100MANM_MAN="man"
101MANM_MANCONF="man.conf"
102MANM_MDOC="mdoc"
103MANM_ROFF="roff"
104MANM_EQN="eqn"
105MANM_TBL="tbl"
106
107INSTALL="install"
108INSTALL_PROGRAM=
109INSTALL_LIB=
110INSTALL_MAN=
111INSTALL_DATA=
112
113# --- manual settings from configure.local -----------------------------
114
115if [ -r ./configure.local ]; then
116	echo "configure.local: reading..." 1>&2
117	echo "configure.local: reading..." 1>&3
118	cat ./configure.local 1>&3
119	. ./configure.local
120else
121	echo "configure.local: no (fully automatic configuration)" 1>&2
122	echo "configure.local: no (fully automatic configuration)" 1>&3
123fi
124echo 1>&3
125
126# --- tests for config.h  ----------------------------------------------
127
128COMP="${CC} ${CFLAGS} -Wno-unused -Werror"
129
130# Check whether this HAVE_ setting is manually overridden.
131# If yes, use the override, if no, do not decide anything yet.
132# Arguments: lower-case test name, manual value
133ismanual() {
134	[ -z "${3}" ] && return 1
135	echo "${1}: manual (HAVE_${2}=${3})" 1>&2
136	echo "${1}: manual (HAVE_${2}=${3})" 1>&3
137	echo 1>&3
138	return 0
139}
140
141# Run a single autoconfiguration test.
142# In case of success, enable the feature.
143# In case of failure, do not decide anything yet.
144# Arguments: lower-case test name, upper-case test name, additional CFLAGS
145singletest() {
146	cat 1>&3 << __HEREDOC__
147${1}${3}: testing...
148${COMP} -o test-${1} test-${1}.c ${3}
149__HEREDOC__
150
151	if ${COMP} -o "test-${1}" "test-${1}.c" ${3} 1>&3 2>&3; then
152		echo "${1}${3}: ${CC} succeeded" 1>&3
153	else
154		echo "${1}${3}: ${CC} failed with $?" 1>&3
155		echo 1>&3
156		return 1
157	fi
158
159	if ./test-${1} 1>&3 2>&3; then
160		echo "${1}${3}: yes" 1>&2
161		echo "${1}${3}: yes" 1>&3
162		echo 1>&3
163		eval HAVE_${2}=1
164		rm "test-${1}"
165		return 0
166	else
167		echo "${1}${3}: execution failed with $?" 1>&3
168		echo 1>&3
169		rm "test-${1}"
170		return 1
171	fi
172}
173
174# Run a complete autoconfiguration test, including the check for
175# a manual override and disabling the feature on failure.
176# Arguments: lower case name, upper case name, additional CFLAGS
177runtest() {
178	eval _manual=\${HAVE_${2}}
179	ismanual "${1}" "${2}" "${_manual}" && return 0
180	singletest "${1}" "${2}" "${3}" && return 0
181	echo "${1}${3}: no" 1>&2
182	eval HAVE_${2}=0
183	return 1
184}
185
186# Select a UTF-8 locale.
187get_locale() {
188	[ -n "${HAVE_WCHAR}" ] && [ "${HAVE_WCHAR}" -eq 0 ] && return 0
189	ismanual UTF8_LOCALE UTF8_LOCALE "$UTF8_LOCALE" && return 0
190	echo "UTF8_LOCALE: testing..." 1>&3
191	UTF8_LOCALE=`locale -a | grep -i '^en_US\.UTF-*8$' | head -n 1`
192	if [ -z "${UTF8_LOCALE}" ]; then
193		UTF8_LOCALE=`locale -a | grep -i '\.UTF-*8' | head -n 1`
194		[ -n "${UTF8_LOCALE}" ] || return 1
195	fi
196	echo "UTF8_LOCALE=${UTF8_LOCALE}" 1>&2
197	echo "UTF8_LOCALE=${UTF8_LOCALE}" 1>&3
198	echo 1>&3
199	return 0;
200}
201
202
203# --- library functions ---
204runtest dirent-namlen	DIRENT_NAMLEN	|| true
205runtest be32toh		ENDIAN		|| true
206runtest be32toh		SYS_ENDIAN	-DSYS_ENDIAN || true
207runtest EFTYPE		EFTYPE		|| true
208runtest err		ERR		|| true
209runtest getline		GETLINE		|| true
210runtest getsubopt	GETSUBOPT	|| true
211runtest isblank		ISBLANK		|| true
212runtest mkdtemp		MKDTEMP		|| true
213runtest ntohl		NTOHL		|| true
214runtest PATH_MAX	PATH_MAX	|| true
215runtest pledge		PLEDGE		|| true
216runtest sandbox_init	SANDBOX_INIT	|| true
217runtest progname	PROGNAME	|| true
218runtest reallocarray	REALLOCARRAY	|| true
219runtest rewb-bsd	REWB_BSD	|| true
220runtest rewb-sysv	REWB_SYSV	|| true
221runtest strcasestr	STRCASESTR	|| true
222runtest stringlist	STRINGLIST	|| true
223runtest strlcat		STRLCAT		|| true
224runtest strlcpy		STRLCPY		|| true
225runtest strptime	STRPTIME	|| true
226runtest strsep		STRSEP		|| true
227runtest strtonum	STRTONUM	|| true
228runtest vasprintf	VASPRINTF	|| true
229
230if [ ${HAVE_ENDIAN} -eq 0 -a \
231     ${HAVE_SYS_ENDIAN} -eq 0 -a \
232     ${HAVE_NTOHL} -eq 0 ]; then
233	echo "FATAL: no endian conversion functions found" 1>&2
234	echo "FATAL: no endian conversion functions found" 1>&3
235	exit 1
236fi
237
238if ismanual fts FTS ${HAVE_FTS}; then
239	HAVE_FTS_COMPARE_CONST=0
240elif runtest fts FTS_COMPARE_CONST -DFTS_COMPARE_CONST; then
241	HAVE_FTS=1
242else
243	runtest fts FTS || true
244fi
245
246# --- wide character and locale support ---
247if get_locale; then
248	runtest wchar WCHAR -DUTF8_LOCALE=\"${UTF8_LOCALE}\" || true
249else
250	HAVE_WCHAR=0
251	echo "wchar: no (no UTF8_LOCALE)" 1>&2
252	echo "wchar: no (no UTF8_LOCALE)" 1>&3
253fi
254
255# --- nanosleep ---
256if [ -n "${LD_NANOSLEEP}" ]; then
257	runtest nanosleep NANOSLEEP "${LD_NANOSLEEP}" || true
258elif singletest nanosleep NANOSLEEP; then
259	:
260elif runtest nanosleep NANOSLEEP "-lrt"; then
261	LD_NANOSLEEP="-lrt"
262fi
263if [ "${HAVE_NANOSLEEP}" -eq 0 ]; then
264	echo "FATAL: nanosleep: no" 1>&2
265	echo "FATAL: nanosleep: no" 1>&3
266	exit 1
267fi
268
269# --- ohash ---
270if ismanual ohash OHASH "${HAVE_OHASH}"; then
271	:
272elif [ -n "${LD_OHASH}" ]; then
273	runtest ohash OHASH "${LD_OHASH}" || true
274elif singletest ohash OHASH; then
275	:
276elif runtest ohash OHASH "-lutil"; then
277	LD_OHASH="-lutil"
278fi
279if [ "${HAVE_OHASH}" -eq 0 ]; then
280	LD_OHASH=
281fi
282
283# --- LDADD ---
284LDADD="${LDADD} ${LD_NANOSLEEP} ${LD_OHASH} -lz"
285echo "LDADD=\"${LDADD}\"" 1>&2
286echo "LDADD=\"${LDADD}\"" 1>&3
287echo 1>&3
288
289# --- write config.h ---
290
291exec > config.h
292
293cat << __HEREDOC__
294#ifdef __cplusplus
295#error "Do not use C++.  See the INSTALL file."
296#endif
297
298#if !defined(__GNUC__) || (__GNUC__ < 4)
299#define __attribute__(x)
300#endif
301
302#if defined(__linux__) || defined(__MINT__)
303#define _GNU_SOURCE	/* See test-*.c what needs this. */
304#endif
305
306__HEREDOC__
307
308[ ${HAVE_GETLINE} -eq 0 -o ${HAVE_REALLOCARRAY} -eq 0 -o \
309  ${HAVE_STRLCAT} -eq 0 -o ${HAVE_STRLCPY} -eq 0 ] \
310	&& echo "#include <sys/types.h>"
311[ ${HAVE_VASPRINTF} -eq 0 ] && echo "#include <stdarg.h>"
312[ ${HAVE_GETLINE} -eq 0 ] && echo "#include <stdio.h>"
313
314echo
315echo "#define MAN_CONF_FILE \"/etc/${MANM_MANCONF}\""
316echo "#define MANPATH_DEFAULT \"${MANPATH_DEFAULT}\""
317[ -n "${OSNAME}" ] && echo "#define OSNAME \"${OSNAME}\""
318[ -n "${UTF8_LOCALE}" ] && echo "#define UTF8_LOCALE \"${UTF8_LOCALE}\""
319[ -n "${HOMEBREWDIR}" ] && echo "#define HOMEBREWDIR \"${HOMEBREWDIR}\""
320[ ${HAVE_EFTYPE} -eq 0 ] && echo "#define EFTYPE EINVAL"
321[ ${HAVE_PATH_MAX} -eq 0 ] && echo "#define PATH_MAX 4096"
322if [ ${HAVE_ENDIAN} -eq 0 -a ${HAVE_SYS_ENDIAN} -eq 0 ]; then
323	echo "#define be32toh ntohl"
324	echo "#define htobe32 htonl"
325fi
326
327cat << __HEREDOC__
328#define HAVE_DIRENT_NAMLEN ${HAVE_DIRENT_NAMLEN}
329#define HAVE_ENDIAN ${HAVE_ENDIAN}
330#define HAVE_ERR ${HAVE_ERR}
331#define HAVE_FTS ${HAVE_FTS}
332#define HAVE_FTS_COMPARE_CONST ${HAVE_FTS_COMPARE_CONST}
333#define HAVE_GETLINE ${HAVE_GETLINE}
334#define HAVE_GETSUBOPT ${HAVE_GETSUBOPT}
335#define HAVE_ISBLANK ${HAVE_ISBLANK}
336#define HAVE_MKDTEMP ${HAVE_MKDTEMP}
337#define HAVE_NTOHL ${HAVE_NTOHL}
338#define HAVE_PLEDGE ${HAVE_PLEDGE}
339#define HAVE_PROGNAME ${HAVE_PROGNAME}
340#define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
341#define HAVE_REWB_BSD ${HAVE_REWB_BSD}
342#define HAVE_REWB_SYSV ${HAVE_REWB_SYSV}
343#define HAVE_SANDBOX_INIT ${HAVE_SANDBOX_INIT}
344#define HAVE_STRCASESTR ${HAVE_STRCASESTR}
345#define HAVE_STRINGLIST ${HAVE_STRINGLIST}
346#define HAVE_STRLCAT ${HAVE_STRLCAT}
347#define HAVE_STRLCPY ${HAVE_STRLCPY}
348#define HAVE_STRPTIME ${HAVE_STRPTIME}
349#define HAVE_STRSEP ${HAVE_STRSEP}
350#define HAVE_STRTONUM ${HAVE_STRTONUM}
351#define HAVE_SYS_ENDIAN ${HAVE_SYS_ENDIAN}
352#define HAVE_VASPRINTF ${HAVE_VASPRINTF}
353#define HAVE_WCHAR ${HAVE_WCHAR}
354#define HAVE_OHASH ${HAVE_OHASH}
355
356#define BINM_APROPOS "${BINM_APROPOS}"
357#define BINM_MAKEWHATIS "${BINM_MAKEWHATIS}"
358#define BINM_MAN "${BINM_MAN}"
359#define BINM_SOELIM "${BINM_SOELIM}"
360#define BINM_WHATIS "${BINM_WHATIS}"
361
362__HEREDOC__
363
364if [ ${HAVE_ERR} -eq 0 ]; then
365	echo "extern	void	  err(int, const char *, ...);"
366	echo "extern	void	  errx(int, const char *, ...);"
367	echo "extern	void	  warn(const char *, ...);"
368	echo "extern	void	  warnx(const char *, ...);"
369fi
370
371[ ${HAVE_GETLINE} -eq 0 ] && \
372	echo "extern	ssize_t	  getline(char **, size_t *, FILE *);"
373
374[ ${HAVE_GETSUBOPT} -eq 0 ] && \
375	echo "extern	int	  getsubopt(char **, char * const *, char **);"
376
377[ ${HAVE_ISBLANK} -eq 0 ] && \
378	echo "extern	int	  isblank(int);"
379
380[ ${HAVE_MKDTEMP} -eq 0 ] && \
381	echo "extern	char	 *mkdtemp(char *);"
382
383if [ ${HAVE_PROGNAME} -eq 0 ]; then
384	echo "extern 	const char *getprogname(void);"
385	echo "extern	void	  setprogname(const char *);"
386fi
387
388[ ${HAVE_REALLOCARRAY} -eq 0 ] && \
389	echo "extern	void	 *reallocarray(void *, size_t, size_t);"
390
391[ ${HAVE_STRCASESTR} -eq 0 ] && \
392	echo "extern	char	 *strcasestr(const char *, const char *);"
393
394[ ${HAVE_STRLCAT} -eq 0 ] && \
395	echo "extern	size_t	  strlcat(char *, const char *, size_t);"
396
397[ ${HAVE_STRLCPY} -eq 0 ] && \
398	echo "extern	size_t	  strlcpy(char *, const char *, size_t);"
399
400[ ${HAVE_STRSEP} -eq 0 ] && \
401	echo "extern	char	 *strsep(char **, const char *);"
402
403[ ${HAVE_STRTONUM} -eq 0 ] && \
404	echo "extern	long long strtonum(const char *, long long, long long, const char **);"
405
406[ ${HAVE_VASPRINTF} -eq 0 ] && \
407	echo "extern	int	  vasprintf(char **, const char *, va_list);"
408
409echo "config.h: written" 1>&2
410echo "config.h: written" 1>&3
411
412# --- tests for Makefile.local -----------------------------------------
413
414exec > Makefile.local
415
416[ -z "${BINDIR}"     ] && BINDIR="${PREFIX}/bin"
417[ -z "${SBINDIR}"    ] && SBINDIR="${PREFIX}/sbin"
418[ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include/mandoc"
419[ -z "${LIBDIR}"     ] && LIBDIR="${PREFIX}/lib/mandoc"
420[ -z "${MANDIR}"     ] && MANDIR="${PREFIX}/man"
421
422[ -z "${HTDOCDIR}"   ] && HTDOCDIR="${WWWPREFIX}/htdocs"
423[ -z "${CGIBINDIR}"  ] && CGIBINDIR="${WWWPREFIX}/cgi-bin"
424
425[ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
426[ -z "${INSTALL_LIB}"     ] && INSTALL_LIB="${INSTALL} -m 0444"
427[ -z "${INSTALL_MAN}"     ] && INSTALL_MAN="${INSTALL} -m 0444"
428[ -z "${INSTALL_DATA}"    ] && INSTALL_DATA="${INSTALL} -m 0444"
429
430BUILD_TARGETS=
431[ ${BUILD_CGI} -gt 0 ] && BUILD_TARGETS="cgi-build"
432INSTALL_TARGETS=
433[ ${INSTALL_LIBMANDOC} -gt 0 ] && INSTALL_TARGETS="lib-install"
434[ ${BUILD_CGI} -gt 0 ] && INSTALL_TARGETS="${INSTALL_TARGETS} cgi-install"
435
436cat << __HEREDOC__
437BUILD_TARGETS	= ${BUILD_TARGETS}
438INSTALL_TARGETS	= ${INSTALL_TARGETS}
439CC		= ${CC}
440CFLAGS		= ${CFLAGS}
441LDADD		= ${LDADD}
442LDFLAGS		= ${LDFLAGS}
443STATIC		= ${STATIC}
444PREFIX		= ${PREFIX}
445BINDIR		= ${BINDIR}
446SBINDIR		= ${SBINDIR}
447INCLUDEDIR	= ${INCLUDEDIR}
448LIBDIR		= ${LIBDIR}
449MANDIR		= ${MANDIR}
450WWWPREFIX	= ${WWWPREFIX}
451HTDOCDIR	= ${HTDOCDIR}
452CGIBINDIR	= ${CGIBINDIR}
453BINM_APROPOS	= ${BINM_APROPOS}
454BINM_MAKEWHATIS	= ${BINM_MAKEWHATIS}
455BINM_MAN	= ${BINM_MAN}
456BINM_SOELIM	= ${BINM_SOELIM}
457BINM_WHATIS	= ${BINM_WHATIS}
458MANM_MAN	= ${MANM_MAN}
459MANM_MANCONF	= ${MANM_MANCONF}
460MANM_MDOC	= ${MANM_MDOC}
461MANM_ROFF	= ${MANM_ROFF}
462MANM_EQN	= ${MANM_EQN}
463MANM_TBL	= ${MANM_TBL}
464INSTALL		= ${INSTALL}
465INSTALL_PROGRAM	= ${INSTALL_PROGRAM}
466INSTALL_LIB	= ${INSTALL_LIB}
467INSTALL_MAN	= ${INSTALL_MAN}
468INSTALL_DATA	= ${INSTALL_DATA}
469__HEREDOC__
470
471echo "Makefile.local: written" 1>&2
472echo "Makefile.local: written" 1>&3
473
474exit 0
475