summaryrefslogtreecommitdiff
path: root/scripts/build/build_libpq.sh
blob: 107460f8416a3a2edc3b8805cdbbd6d415690ad5 (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
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
#!/bin/bash

# Build a modern version of libpq and depending libs from source on Centos 5, Alpine or macOS

set -euo pipefail
set -x

# Last release: https://www.postgresql.org/ftp/source/
# IMPORTANT! Change the cache key in packages.yml when upgrading libraries
postgres_version="${LIBPQ_VERSION}"

# last release: https://www.openssl.org/source/
openssl_version="${OPENSSL_VERSION}"

# last release: https://kerberos.org/dist/
krb5_version="1.21.3"

# last release: https://openldap.org/software/download/
ldap_version="2.6.8"

# last release: https://github.com/cyrusimap/cyrus-sasl/releases
sasl_version="2.1.28"

export LIBPQ_BUILD_PREFIX=${LIBPQ_BUILD_PREFIX:-/tmp/libpq.build}

case "$(uname)" in
    Darwin)
        ID=macos
        library_suffix=dylib
        ;;

    Linux)
        source /etc/os-release
        library_suffix=so
        ;;

    *)
        echo "$0: unexpected Operating system: '$(uname)'" >&2
        exit 1
        ;;
esac

if [[ -f "${LIBPQ_BUILD_PREFIX}/lib/libpq.${library_suffix}" ]]; then
    echo "libpq already available: build skipped" >&2
    exit 0
fi

case "$ID" in
    centos)
        yum update -y
        yum install -y zlib-devel krb5-devel pam-devel
        ;;

    alpine)
        apk upgrade
        apk add --no-cache zlib-dev krb5-dev linux-pam-dev openldap-dev openssl-dev
        ;;

    macos)
        brew install automake m4 libtool
        # If available, libpq seemingly insists on linking against homebrew's
        # openssl no matter what so remove it. Since homebrew's curl depends on
        # it, force use of system curl.
        brew uninstall --force --ignore-dependencies openssl gettext curl
        if [ -z "${MACOSX_ARCHITECTURE:-}" ]; then
            MACOSX_ARCHITECTURE="$(uname -m)"
        fi
        # Set the deployment target to be <= to that of the oldest supported Python version.
        # e.g. https://www.python.org/downloads/release/python-380/
        if [ "$MACOSX_ARCHITECTURE" == "x86_64" ]; then
            export MACOSX_DEPLOYMENT_TARGET=10.9
        else
            export MACOSX_DEPLOYMENT_TARGET=11.0
        fi
        ;;

    *)
        echo "$0: unexpected Linux distribution: '$ID'" >&2
        exit 1
        ;;
esac


if [ "$ID" == "macos" ]; then
    make_configure_standard_flags=( \
        --prefix=${LIBPQ_BUILD_PREFIX} \
        "CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ -arch $MACOSX_ARCHITECTURE" \
        "LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib -arch $MACOSX_ARCHITECTURE" \
    )
else
    make_configure_standard_flags=( \
        --prefix=${LIBPQ_BUILD_PREFIX} \
        CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ \
        LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib \
    )
fi


if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then

    # Build openssl if needed
    openssl_tag="OpenSSL_${openssl_version//./_}"
    openssl_dir="openssl-${openssl_tag}"
    if [ ! -d "${openssl_dir}" ]; then
        curl -sL \
            https://github.com/openssl/openssl/archive/${openssl_tag}.tar.gz \
            | tar xzf -

        pushd "${openssl_dir}"

        options=(--prefix=${LIBPQ_BUILD_PREFIX} --openssldir=${LIBPQ_BUILD_PREFIX} \
            zlib -fPIC shared)
        if [ -z "${MACOSX_ARCHITECTURE:-}" ]; then
            ./config $options
        else
            ./configure "darwin64-$MACOSX_ARCHITECTURE-cc" $options
        fi

        make depend
        make
    else
        pushd "${openssl_dir}"
    fi

    # Install openssl
    make install_sw
    popd

fi


if [ "$ID" == "macos" ]; then

    # Build kerberos if needed
    krb5_dir="krb5-${krb5_version}/src"
    if [ ! -d "${krb5_dir}" ]; then
        curl -sL "https://kerberos.org/dist/krb5/${krb5_version%.*}/krb5-${krb5_version}.tar.gz" \
            | tar xzf -

        pushd "${krb5_dir}"
        ./configure "${make_configure_standard_flags[@]}"
        make
    else
        pushd "${krb5_dir}"
    fi

    make install
    popd

fi


if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then

    # Build libsasl2 if needed
    # The system package (cyrus-sasl-devel) causes an amazing error on i686:
    # "unsupported version 0 of Verneed record"
    # https://github.com/pypa/manylinux/issues/376
    sasl_tag="cyrus-sasl-${sasl_version}"
    sasl_dir="cyrus-sasl-${sasl_tag}"
    if [ ! -d "${sasl_dir}" ]; then
        curl -sL \
            https://github.com/cyrusimap/cyrus-sasl/archive/${sasl_tag}.tar.gz \
            | tar xzf -

        pushd "${sasl_dir}"

        autoreconf -i
        ./configure "${make_configure_standard_flags[@]}" --disable-macos-framework
        make
    else
        pushd "${sasl_dir}"
    fi

    # Install libsasl2
    # requires missing nroff to build
    touch saslauthd/saslauthd.8
    make install
    popd

fi


if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then

    # Build openldap if needed
    ldap_tag="${ldap_version}"
    ldap_dir="openldap-${ldap_tag}"
    if [ ! -d "${ldap_dir}" ]; then
        curl -sL \
            https://www.openldap.org/software/download/OpenLDAP/openldap-release/openldap-${ldap_tag}.tgz \
            | tar xzf -

        pushd "${ldap_dir}"

        ./configure "${make_configure_standard_flags[@]}" --enable-backends=no --enable-null

        make depend
        make -C libraries/liblutil/
        make -C libraries/liblber/
        make -C libraries/libldap/
    else
        pushd "${ldap_dir}"
    fi

    # Install openldap
    make -C libraries/liblber/ install
    make -C libraries/libldap/ install
    make -C include/ install
    chmod +x ${LIBPQ_BUILD_PREFIX}/lib/{libldap,liblber}*.${library_suffix}*
    popd

fi


# Build libpq if needed
postgres_tag="REL_${postgres_version//./_}"
postgres_dir="postgres-${postgres_tag}"
if [ ! -d "${postgres_dir}" ]; then
    curl -sL \
        https://github.com/postgres/postgres/archive/${postgres_tag}.tar.gz \
        | tar xzf -

    pushd "${postgres_dir}"

    if [ "$ID" != "macos" ]; then
        # Match the default unix socket dir default with what defined on Ubuntu and
        # Red Hat, which seems the most common location
        sed -i 's|#define DEFAULT_PGSOCKET_DIR .*'\
'|#define DEFAULT_PGSOCKET_DIR "/var/run/postgresql"|' \
            src/include/pg_config_manual.h
    fi

    # Often needed, but currently set by the workflow
    # export LD_LIBRARY_PATH="${LIBPQ_BUILD_PREFIX}/lib"

    ./configure "${make_configure_standard_flags[@]}" --sysconfdir=/etc/postgresql-common \
        --with-gssapi --with-openssl --with-pam --with-ldap \
        --without-readline --without-icu
    make -C src/interfaces/libpq
    make -C src/bin/pg_config
    make -C src/include
else
    pushd "${postgres_dir}"
fi

# Install libpq
make -C src/interfaces/libpq install
make -C src/bin/pg_config install
make -C src/include install
popd

find ${LIBPQ_BUILD_PREFIX} -name \*.${library_suffix}.\* -type f -exec strip --strip-unneeded {} \;