summaryrefslogtreecommitdiffstats
path: root/configure
blob: 60d7a92f5aefbaf267e1243bf71fe0bae5c84c12 (plain)
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
#!/bin/sh

# Copyright © Tavian Barnes <tavianator@tavianator.com>
# SPDX-License-Identifier: 0BSD

# bfs build configuration script

set -eu

# Default to `make`
MAKE="${MAKE:-make}"

# Pass -j$(nproc) unless MAKEFLAGS is set
if [ "${MAKEFLAGS+y}" ]; then
    j=""
else
    j="-j$({ nproc || sysctl -n hw.ncpu || getconf _NPROCESSORS_ONLN || echo 1; } 2>/dev/null)"
fi

# Convert kebab-case to UPPER_CASE
toupper() {
    printf '%s' "$1" | tr 'a-z-' 'A-Z_'
}

# Report an argument parsing error
invalid() {
    printf 'error: Unrecognized option "%s"\n\n' "$1" >&2
    printf 'Run %s --help for more information.\n' "$0" >&2
    exit 1
}

for arg; do
    # --[(enable|disable|with|without)-]$name[=$value]
    value="${arg#*=}"
    name="${arg%%=*}"
    name="${name#--}"
    case "$arg" in
        --enable-*|--disable-*|--with-*|--without-*)
            name="${name#*-}"
            ;;
    esac
    NAME=$(printf '%s' "$name" | tr 'a-z-' 'A-Z_')

    # y/n modality
    case "$arg" in
        --enable-*|--with-*)
            case "$arg" in
                *=y|*=yes) yn=y ;;
                *=n|*=no) yn=n ;;
                *=*) invalid "$arg" ;;
                *) yn=y ;;
            esac
            ;;
        --disable-*|--without-*)
            case "$arg" in
                *=*) invalid "arg" ;;
                *) yn=n ;;
            esac
            ;;
    esac

    # Fix up --enable-lib* to --with-lib*
    case "$arg" in
        --enable-*|--disable-*)
            case "$name" in
                libacl|libcap|libselinux|liburing|oniguruma)
                    old="$arg"
                    case "$arg" in
                        --enable-*) arg="--with-${arg#--*-}" ;;
                        --disable-*) arg="--without-${arg#--*-}" ;;
                    esac
                    printf 'warning: Treating "%s" like "%s"\n' "$old" "$arg" >&2
                    ;;
            esac
        ;;
    esac

    case "$arg" in
        -h|--help)
            cat <<EOF
Usage:

  \$ $0 [--enable-*|--disable-*] [--with-*|--without-*] [CC=...] [...]
  \$ $MAKE $j

Variables set in the environment or on the command line will be picked up:

  MAKE
      The make implementation to use
  CC
      The C compiler to use

  CPPFLAGS="-I... -D..."
  CFLAGS="-W... -f..."
  LDFLAGS="-L... -Wl,..."
      Preprocessor/compiler/linker flags

  LDLIBS="-l... -l..."
      Dynamic libraries to link

  EXTRA_{CPPFLAGS,CFLAGS,LDFLAGS,LDLIBS}
      Adds to the default flags, instead of replacing them

The default flags result in a plain debug build.  Other build profiles include:

  --enable-release
      Enable optimizations, disable assertions
  --enable-{asan,lsan,msan,tsan,ubsan}
      Enable sanitizers
  --enable-gcov
      Enable code coverage instrumentation

External dependencies are auto-detected by default, but you can build --with or
--without them explicitly:

  --with-libacl      --without-libacl
  --with-libcap      --without-libcap
  --with-libselinux  --without-libselinux
  --with-liburing    --without-liburing
  --with-oniguruma   --without-oniguruma

Packaging:

  --prefix=/path
      Set the installation prefix (default: /usr)
  --mandir=/path
      Set the man page directory (default: \$PREFIX/share/man)

This script is a thin wrapper around a makefile-based configuration system.
Any other arguments will be passed directly to the $MAKE invocation, e.g.

  \$ $0 $j V=1
EOF
            exit 0
            ;;

        --enable-*|--disable-*)
            case "$name" in
                release|asan|lsan|msan|tsan|ubsan|lint|gcov)
                    shift
                    set -- "$@" "$NAME=$yn"
                    ;;
                *)
                    invalid "$arg"
                    ;;
            esac
            ;;

        --with-*|--without-*)
            case "$name" in
                libacl|libcap|libselinux|liburing|oniguruma)
                    shift
                    set -- "$@" "WITH_$NAME=$yn"
                    ;;
                *)
                    invalid "$arg"
                    ;;
            esac
            ;;

        --prefix=*|--mandir=*)
            shift
            set -- "$@" "$NAME=$value"
            ;;

        --infodir=*|--build=*|--host=*|--target=*)
            shift
            printf 'warning: Ignoring option "%s"\n' "$arg" >&2
            ;;

        MAKE=*)
            shift
            MAKE="$value"
            ;;

        # make flag (-j2) or variable (CC=clang)
        -*|*=*)
            continue
            ;;

        *)
            invalid "$arg"
            ;;
    esac
done

# Get the relative path to the source tree based on how the script was run
DIR=$(dirname -- "$0")

# Set up symbolic links for out-of-tree builds
for f in Makefile build completions docs src tests; do
    test -e "$f" || ln -s "$DIR/$f" "$f"
done

# Set MAKEFLAGS to -j$(nproc) if it's unset
export MAKEFLAGS="${MAKEFLAGS-$j}"

$MAKE -rf build/config.mk "$@"