summaryrefslogtreecommitdiffstats
path: root/configure
blob: 882b8f4bb2d4badae62cdb1674db268a34feee44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/bin/sh
#
# Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

set -e

[ -e config.log ] && mv config.log config.log.old
[ -e config.h   ] && mv config.h config.h.old

# Output file descriptor usage:
# 1 (stdout): config.h, Makefile.local
# 2 (stderr): original stderr, usually to the console
# 3: config.log

exec 3> config.log
echo "config.log: writing..."

# --- default settings -------------------------------------------------
# Initialize all variables here,
# such that nothing can leak in from the environment.

MANPATH_DEFAULT="/usr/share/man:/usr/X11R6/man:/usr/local/man"
OSNAME=

CC=`printf "all:\\n\\t@echo \\\$(CC)\\n" | make -f -`
CFLAGS="-g -W -Wall -Wstrict-prototypes -Wno-unused-parameter -Wwrite-strings"
DBLIB=
STATIC="-static"

BUILD_DB=1
BUILD_CGI=0

HAVE_DIRENT_NAMLEN=
HAVE_ERR=
HAVE_FGETLN=
HAVE_FTS=
HAVE_GETSUBOPT=
HAVE_ISBLANK=
HAVE_MKDTEMP=
HAVE_MMAP=
HAVE_PLEDGE=
HAVE_PROGNAME=
HAVE_REALLOCARRAY=
HAVE_REWB_BSD=
HAVE_REWB_SYSV=
HAVE_STRCASESTR=
HAVE_STRINGLIST=
HAVE_STRLCAT=
HAVE_STRLCPY=
HAVE_STRPTIME=
HAVE_STRSEP=
HAVE_STRTONUM=
HAVE_VASPRINTF=
HAVE_WCHAR=

HAVE_SQLITE3=
HAVE_SQLITE3_ERRSTR=
HAVE_OHASH=
HAVE_MANPATH=

PREFIX="/usr/local"
BINDIR=
SBINDIR=
INCLUDEDIR=
LIBDIR=
MANDIR=
EXAMPLEDIR=
HOMEBREWDIR=

WWWPREFIX="/var/www"
HTDOCDIR=
CGIBINDIR=

BINM_APROPOS="apropos"
BINM_MAKEWHATIS="makewhatis"
BINM_MAN="man"
BINM_SOELIM="soelim"
BINM_WHATIS="whatis"
MANM_MAN="man"
MANM_MANCONF="man.conf"
MANM_MDOC="mdoc"
MANM_ROFF="roff"
MANM_EQN="eqn"
MANM_TBL="tbl"

INSTALL="install"
INSTALL_PROGRAM=
INSTALL_LIB=
INSTALL_MAN=
INSTALL_DATA=

# --- manual settings from configure.local -----------------------------

if [ -e ./configure.local ]; then
	echo "configure.local: reading..." 1>&2
	echo "configure.local: reading..." 1>&3
	cat ./configure.local 1>&3
	. ./configure.local
else
	echo "configure.local: no (fully automatic configuration)" 1>&2
	echo "configure.local: no (fully automatic configuration)" 1>&3
fi
echo 1>&3

# --- tests for config.h  ----------------------------------------------

COMP="${CC} ${CFLAGS} -Wno-unused -Werror"

# Check whether this HAVE_ setting is manually overridden.
# If yes, use the override, if no, do not decide anything yet.
# Arguments: lower-case test name, manual value
ismanual() {
	[ -z "${2}" ] && return 1
	echo "${1}: manual (${2})" 1>&2
	echo "${1}: manual (${2})" 1>&3
	echo 1>&3
	return 0
}

# Run a single autoconfiguration test.
# In case of success, enable the feature.
# In case of failure, do not decide anything yet.
# Arguments: lower-case test name, upper-case test name, additional CFLAGS
singletest() {
	cat 1>&3 << __HEREDOC__
${1}: testing...
${COMP} ${3} -o test-${1} test-${1}.c
__HEREDOC__

	if ${COMP} ${3} -o "test-${1}" "test-${1}.c" 1>&3 2>&3; then
		echo "${1}: ${CC} succeeded" 1>&3
	else
		echo "${1}: ${CC} failed with $?" 1>&3
		echo 1>&3
		return 1
	fi

	if ./test-${1} 1>&3 2>&3; then
		echo "${1}: yes" 1>&2
		echo "${1}: yes" 1>&3
		echo 1>&3
		eval HAVE_${2}=1
		rm "test-${1}"
		return 0
	else
		echo "${1}: execution failed with $?" 1>&3
		echo 1>&3
		rm "test-${1}"
		return 1
	fi
}

# Run a complete autoconfiguration test, including the check for
# a manual override and disabling the feature on failure.
# Arguments: lower case name, upper case name, additional CFLAGS
runtest() {
	eval _manual=\${HAVE_${2}}
	ismanual "${1}" "${_manual}" && return 0
	singletest "${1}" "${2}" "${3}" && return 0
	echo "${1}: no" 1>&2
	eval HAVE_${2}=0
	return 1
}

# --- library functions ---
runtest dirent-namlen	DIRENT_NAMLEN	|| true
runtest err		ERR		|| true
runtest fgetln		FGETLN		|| true
runtest fts		FTS		|| true
runtest getsubopt	GETSUBOPT	|| true
runtest isblank		ISBLANK		|| true
runtest mkdtemp		MKDTEMP		|| true
runtest mmap		MMAP		|| true
runtest pledge		PLEDGE		|| true
runtest progname	PROGNAME	|| true
runtest reallocarray	REALLOCARRAY	|| true
runtest rewb-bsd	REWB_BSD	|| true
runtest rewb-sysv	REWB_SYSV	|| true
runtest strcasestr	STRCASESTR	|| true
runtest stringlist	STRINGLIST	|| true
runtest strlcat		STRLCAT		|| true
runtest strlcpy		STRLCPY		|| true
runtest strptime	STRPTIME	|| true
runtest strsep		STRSEP		|| true
runtest strtonum	STRTONUM	|| true
runtest vasprintf	VASPRINTF	|| true
runtest wchar		WCHAR		|| true

# --- sqlite3 ---
DETECTLIB=
if [ ${BUILD_DB} -eq 0 ]; then
	echo "BUILD_DB=0 (manual)" 1>&2
	echo "BUILD_DB=0 (manual)" 1>&3
	echo 1>&3
	HAVE_SQLITE3=0
elif ismanual sqlite3 "${HAVE_SQLITE3}"; then
	DETECTLIB="-lsqlite3"
elif [ -n "${DBLIB}" ]; then
	runtest sqlite3 SQLITE3 "${DBLIB}" || true
elif singletest sqlite3 SQLITE3 "-lsqlite3"; then
	DETECTLIB="-lsqlite3"
elif runtest sqlite3 SQLITE3 \
		"-I/usr/local/include -L/usr/local/lib -lsqlite3"; then
	DETECTLIB="-L/usr/local/lib -lsqlite3"
	CFLAGS="${CFLAGS} -I/usr/local/include"
fi
if [ ${BUILD_DB} -gt 0 -a ${HAVE_SQLITE3} -eq 0 ]; then
	echo "BUILD_DB=0 (no sqlite3)" 1>&2
	echo "BUILD_DB=0 (no sqlite3)" 1>&3
	echo 1>&3
	BUILD_DB=0
fi

# --- sqlite3_errstr ---
if [ ${BUILD_DB} -eq 0 ]; then
	HAVE_SQLITE3_ERRSTR=1
elif ismanual sqlite3_errstr "${HAVE_SQLITE3_ERRSTR}"; then
	:
elif [ -n "${DBLIB}" ]; then
	runtest sqlite3_errstr SQLITE3_ERRSTR "${DBLIB}" || true
else
	runtest sqlite3_errstr SQLITE3_ERRSTR "${DETECTLIB}" || true
fi

# --- ohash ---
if [ ${BUILD_DB} -eq 0 ]; then
	HAVE_OHASH=1
elif ismanual ohash "${HAVE_OHASH}"; then
	:
elif [ -n "${DBLIB}" ]; then
	runtest ohash OHASH "${DBLIB}" || true
elif singletest ohash OHASH; then
	:
elif runtest ohash OHASH "-lutil"; then
	DETECTLIB="${DETECTLIB} -lutil"
fi

# --- DBLIB ---
if [ ${BUILD_DB} -eq 0 ]; then
	DBLIB="-lz"
elif [ -z "${DBLIB}" ]; then
	DBLIB="${DETECTLIB} -lz"
	echo "DBLIB=\"${DBLIB}\"" 1>&2
	echo "DBLIB=\"${DBLIB}\"" 1>&3
	echo 1>&3
fi

# --- manpath ---
if ismanual manpath "${HAVE_MANPATH}"; then
	:
elif manpath 1>&3 2>&3; then
	echo "manpath: yes" 1>&2
	echo "manpath: yes" 1>&3
	echo 1>&3
	HAVE_MANPATH=1
else
	echo "manpath: no" 1>&2
	echo "manpath: no" 1>&3
	echo 1>&3
	HAVE_MANPATH=0
fi

# --- write config.h ---

exec > config.h

cat << __HEREDOC__
#ifndef MANDOC_CONFIG_H
#define MANDOC_CONFIG_H

#if defined(__linux__) || defined(__MINT__)
#define _GNU_SOURCE	/* See test-*.c what needs this. */
#endif

__HEREDOC__

[ ${HAVE_FGETLN} -eq 0 -o ${HAVE_REALLOCARRAY} -eq 0 -o \
  ${HAVE_STRLCAT} -eq 0 -o ${HAVE_STRLCPY} -eq 0 ] \
	&& echo "#include <sys/types.h>"
[ ${HAVE_VASPRINTF} -eq 0 ] && echo "#include <stdarg.h>"
[ ${HAVE_FGETLN} -eq 0 ] && echo "#include <stdio.h>"

echo
echo "#define MAN_CONF_FILE \"/etc/${MANM_MANCONF}\""
echo "#define MANPATH_DEFAULT \"${MANPATH_DEFAULT}\""
[ -n "${OSNAME}" ] && echo "#define OSNAME \"${OSNAME}\""
[ -n "${HOMEBREWDIR}" ] && echo "#define HOMEBREWDIR \"${HOMEBREWDIR}\""

cat << __HEREDOC__
#define HAVE_DIRENT_NAMLEN ${HAVE_DIRENT_NAMLEN}
#define HAVE_ERR ${HAVE_ERR}
#define HAVE_FGETLN ${HAVE_FGETLN}
#define HAVE_FTS ${HAVE_FTS}
#define HAVE_GETSUBOPT ${HAVE_GETSUBOPT}
#define HAVE_ISBLANK ${HAVE_ISBLANK}
#define HAVE_MKDTEMP ${HAVE_MKDTEMP}
#define HAVE_MMAP ${HAVE_MMAP}
#define HAVE_PLEDGE ${HAVE_PLEDGE}
#define HAVE_PROGNAME ${HAVE_PROGNAME}
#define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
#define HAVE_REWB_BSD ${HAVE_REWB_BSD}
#define HAVE_REWB_SYSV ${HAVE_REWB_SYSV}
#define HAVE_STRCASESTR ${HAVE_STRCASESTR}
#define HAVE_STRINGLIST ${HAVE_STRINGLIST}
#define HAVE_STRLCAT ${HAVE_STRLCAT}
#define HAVE_STRLCPY ${HAVE_STRLCPY}
#define HAVE_STRPTIME ${HAVE_STRPTIME}
#define HAVE_STRSEP ${HAVE_STRSEP}
#define HAVE_STRTONUM ${HAVE_STRTONUM}
#define HAVE_VASPRINTF ${HAVE_VASPRINTF}
#define HAVE_WCHAR ${HAVE_WCHAR}
#define HAVE_SQLITE3 ${HAVE_SQLITE3}
#define HAVE_SQLITE3_ERRSTR ${HAVE_SQLITE3_ERRSTR}
#define HAVE_OHASH ${HAVE_OHASH}
#define HAVE_MANPATH ${HAVE_MANPATH}

#define BINM_APROPOS "${BINM_APROPOS}"
#define BINM_MAKEWHATIS "${BINM_MAKEWHATIS}"
#define BINM_MAN "${BINM_MAN}"
#define BINM_SOELIM "${BINM_SOELIM}"
#define BINM_WHATIS "${BINM_WHATIS}"

#if !defined(__BEGIN_DECLS)
#  ifdef __cplusplus
#  define	__BEGIN_DECLS		extern "C" {
#  else
#  define	__BEGIN_DECLS
#  endif
#endif
#if !defined(__END_DECLS)
#  ifdef __cplusplus
#  define	__END_DECLS		}
#  else
#  define	__END_DECLS
#  endif
#endif

__HEREDOC__

if [ ${HAVE_ERR} -eq 0 ]; then
	echo "extern	void	  err(int, const char *, ...);"
	echo "extern	void	  warn(const char *, ...);"
	echo "extern	void	  warnx(const char *, ...);"
fi

[ ${HAVE_FGETLN} -eq 0 ] && \
	echo "extern	char	 *fgetln(FILE *, size_t *);"

[ ${HAVE_GETSUBOPT} -eq 0 ] && \
	echo "extern	int	  getsubopt(char **, char * const *, char **);"

[ ${HAVE_ISBLANK} -eq 0 ] && \
	echo "extern	int	  isblank(int);"

[ ${HAVE_MKDTEMP} -eq 0 ] && \
	echo "extern	char	 *mkdtemp(char *);"

if [ ${HAVE_PROGNAME} -eq 0 ]; then
	echo "extern 	const char *getprogname(void);"
	echo "extern	void	  setprogname(const char *);"
fi

[ ${HAVE_REALLOCARRAY} -eq 0 ] && \
	echo "extern	void	 *reallocarray(void *, size_t, size_t);"

[ ${BUILD_DB} -gt 0 -a ${HAVE_SQLITE3_ERRSTR} -eq 0 ] &&
	echo "extern	const char *sqlite3_errstr(int);"

[ ${HAVE_STRCASESTR} -eq 0 ] && \
	echo "extern	char	 *strcasestr(const char *, const char *);"

[ ${HAVE_STRLCAT} -eq 0 ] && \
	echo "extern	size_t	  strlcat(char *, const char *, size_t);"

[ ${HAVE_STRLCPY} -eq 0 ] && \
	echo "extern	size_t	  strlcpy(char *, const char *, size_t);"

[ ${HAVE_STRSEP} -eq 0 ] && \
	echo "extern	char	 *strsep(char **, const char *);"

[ ${HAVE_STRTONUM} -eq 0 ] && \
	echo "extern	long long strtonum(const char *, long long, long long, const char **);"

[ ${HAVE_VASPRINTF} -eq 0 ] && \
	echo "extern	int	  vasprintf(char **, const char *, va_list);"

echo
echo "#endif /* MANDOC_CONFIG_H */"

echo "config.h: written" 1>&2
echo "config.h: written" 1>&3

# --- tests for Makefile.local -----------------------------------------

exec > Makefile.local

[ -z "${BINDIR}"     ] && BINDIR="${PREFIX}/bin"
[ -z "${SBINDIR}"    ] && SBINDIR="${PREFIX}/sbin"
[ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include/mandoc"
[ -z "${LIBDIR}"     ] && LIBDIR="${PREFIX}/lib/mandoc"
[ -z "${MANDIR}"     ] && MANDIR="${PREFIX}/man"
[ -z "${EXAMPLEDIR}" ] && EXAMPLEDIR="${PREFIX}/share/examples/mandoc"

[ -z "${HTDOCDIR}"   ] && HTDOCDIR="${WWWPREFIX}/htdocs"
[ -z "${CGIBINDIR}"  ] && CGIBINDIR="${WWWPREFIX}/cgi-bin"

[ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
[ -z "${INSTALL_LIB}"     ] && INSTALL_LIB="${INSTALL} -m 0444"
[ -z "${INSTALL_MAN}"     ] && INSTALL_MAN="${INSTALL} -m 0444"
[ -z "${INSTALL_DATA}"    ] && INSTALL_DATA="${INSTALL} -m 0444"

if [ ${BUILD_DB} -eq 0 -a ${BUILD_CGI} -gt 0 ]; then
	echo "BUILD_CGI=0 (no BUILD_DB)" 1>&2
	echo "BUILD_CGI=0 (no BUILD_DB)" 1>&3
	BUILD_CGI=0
fi

BUILD_TARGETS="base-build"
[ ${BUILD_CGI} -gt 0 ] && BUILD_TARGETS="${BUILD_TARGETS} cgi-build"
INSTALL_TARGETS="base-install"
[ ${BUILD_DB}  -gt 0 ] && INSTALL_TARGETS="${INSTALL_TARGETS} db-install"
[ ${BUILD_CGI} -gt 0 ] && INSTALL_TARGETS="${INSTALL_TARGETS} cgi-install"

cat << __HEREDOC__
BUILD_TARGETS	= ${BUILD_TARGETS}
INSTALL_TARGETS	= ${INSTALL_TARGETS}
CC		= ${CC}
CFLAGS		= ${CFLAGS}
DBLIB		= ${DBLIB}
STATIC		= ${STATIC}
PREFIX		= ${PREFIX}
BINDIR		= ${BINDIR}
SBINDIR		= ${SBINDIR}
INCLUDEDIR	= ${INCLUDEDIR}
LIBDIR		= ${LIBDIR}
MANDIR		= ${MANDIR}
EXAMPLEDIR	= ${EXAMPLEDIR}
WWWPREFIX	= ${WWWPREFIX}
HTDOCDIR	= ${HTDOCDIR}
CGIBINDIR	= ${CGIBINDIR}
BINM_APROPOS	= ${BINM_APROPOS}
BINM_MAKEWHATIS	= ${BINM_MAKEWHATIS}
BINM_MAN	= ${BINM_MAN}
BINM_SOELIM	= ${BINM_SOELIM}
BINM_WHATIS	= ${BINM_WHATIS}
MANM_MAN	= ${MANM_MAN}
MANM_MANCONF	= ${MANM_MANCONF}
MANM_MDOC	= ${MANM_MDOC}
MANM_ROFF	= ${MANM_ROFF}
MANM_EQN	= ${MANM_EQN}
MANM_TBL	= ${MANM_TBL}
INSTALL		= ${INSTALL}
INSTALL_PROGRAM	= ${INSTALL_PROGRAM}
INSTALL_LIB	= ${INSTALL_LIB}
INSTALL_MAN	= ${INSTALL_MAN}
INSTALL_DATA	= ${INSTALL_DATA}
__HEREDOC__

[ ${BUILD_DB} -gt 0 ] && \
	echo "MAIN_OBJS	= \$(BASE_OBJS) \$(DB_OBJS)"

echo "Makefile.local: written" 1>&2
echo "Makefile.local: written" 1>&3

exit 0