summaryrefslogtreecommitdiffstats
path: root/configure
blob: 1dda04a90b42ee662a960dacfdebb670aeaa7819 (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
199
200
201
202
203
204
205
206
207
208
209
210
#! /bin/sh

OS=`uname -s`
DEBUG=0
PREFIX=/usr/local

config_h_append_generic_endian() {
    cat <<EOF >> "$1"

/*
 * Generic compatibility
 */
#include <endian.h>
EOF
}

config_h_append_darwin_endian() {
    cat <<EOF >> "$1"

/*
 * Darwin compatibility
 */
#include <libkern/OSByteOrder.h>

#define htobe16(n) OSSwapHostToBigInt16(n)
#define htole16(n) OSSwapHostToLittleInt16(n)
#define be16toh(n) OSSwapBigToHostInt16(n)
#define le16toh(n) OSSwapLittleToHostInt16(n)

#define htobe32(n) OSSwapHostToBigInt32(n)
#define htole32(n) OSSwapHostToLittleInt32(n)
#define be32toh(n) OSSwapBigToHostInt32(n)
#define le32toh(n) OSSwapLittleToHostInt32(n)

#define htobe64(n) OSSwapHostToBigInt64(n)
#define htole64(n) OSSwapHostToLittleInt64(n)
#define be64toh(n) OSSwapBigToHostInt64(n)
#define le64toh(n) OSSwapLittleToHostInt64(n)
EOF
}

config_h_append_bsd_pty() {
    cat <<EOF >> "$1"

/*
 * BSD openpty(3) compatibility
 */
#include <util.h>
EOF
}

config_h_append_linux_pty() {
    cat <<EOF >> "$1"

/*
 * Linux openpty(3) compatibility
 */
#include <pty.h>
EOF
}

config_h_create() {
    cat <<EOF > "$1"
#ifndef _CONFIG_H
#define _CONFIG_H
EOF

    if [ "$OS" = "Darwin" ]; then
        config_h_append_darwin_endian "$1"
    else
        config_h_append_generic_endian "$1"
    fi

    if [ "$OS" = "Linux" ]; then
        config_h_append_linux_pty "$1"
    else
        config_h_append_bsd_pty "$1"
    fi

    cat <<EOF >> "$1"

#endif /* _CONFIG_H */
EOF
}

build_mk_append_generic() {
    cat <<'EOF' >> "$1"
LDFLAGS_SO	= -shared -Wl,-soname=$(SONAME)

STATIC		= lib$(LIBNAME).a

SONAME_SHORT	= lib$(LIBNAME).so
SONAME		= $(SONAME_SHORT).$(VERSION_MAJOR)
SONAME_FULL	= $(SONAME_SHORT).$(VERSION)

EOF

    cat <<EOF >> "$1"
PREFIX		= $PREFIX
EOF
}

build_mk_append_man_generic() {
    cat <<'EOF' >> "$1"
MANDIR		= $(PREFIX)/share/man
EOF
}

build_mk_append_man_bsd() {
    if [ "$PREFIX" = "/usr" ]; then
        local mandir='$(PREFIX)/share/man'
    else
        local mandir='$(PREFIX)/man'
    fi

    cat <<EOF >> "$1"
MANDIR		= $mandir
EOF
}

build_mk_append_darwin() {
    cat <<'EOF' >> "$1"
LDFLAGS_SO	= -dynamiclib -current_version $(VERSION)

STATIC      = lib$(LIBNAME).a

SONAME_SHORT	= lib$(LIBNAME).dylib
SONAME		= lib$(LIBNAME).$(VERSION_MAJOR).dylib
SONAME_FULL	= lib$(LIBNAME).$(VERSION).dylib

EOF

    cat <<EOF >> "$1"
PREFIX      = $PREFIX
EOF
}

build_mk_create() {
    if [ "$DEBUG" = 1 ]; then
        cat <<'EOF' > "$1"
CFLAGS_DEBUG	= -g -fno-inline
EOF
    else
        cat <<'EOF' > "$1"
CFLAGS_DEBUG	=
EOF
    fi

    if [ "$OS" = "Darwin" ]; then
        build_mk_append_darwin "$1"
    else
        build_mk_append_generic "$1"
    fi

    case $OS in
        FreeBSD|NetBSD|OpenBSD)
            build_mk_append_man_bsd "$1"
        ;;

        *)
            build_mk_append_man_generic "$1"
        ;;
    esac

    cat <<'EOF' >> "$1"
CFLAGS_WARN	= -Wall
CFLAGS_OPT	= -O2
CFLAGS_GEN	= -fPIC
CFLAGS		= $(CFLAGS_DEBUG) $(CFLAGS_WARN) $(CFLAGS_OPT) $(CFLAGS_GEN)

AR		= $(CROSS)ar
RANLIB		= $(CROSS)ranlib

RM		= /bin/rm
LN		= /bin/ln
RMDIR		= /bin/rmdir
INSTALL		= /usr/bin/install
EOF
}

parse_opt() {
    local _IFS="$IFS"

    IFS="="

    set -- $1

    echo $2

    IFS="$_IFS"
}

for arg in $@; do
    case $arg in
        --enable-debug)
            DEBUG=1
        ;;

        --prefix=*)
            PREFIX="`parse_opt "$arg"`"
        ;;
    esac
done

if [ ! -d "mk" ]; then
    mkdir -m 0755 mk
fi

config_h_create "src/config.h"
build_mk_create "mk/build.mk"