|
| 1 | +#!/bin/sh |
| 2 | +# Copyright (c) 2017-2019 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +# Install libdb4.8 (Berkeley DB). |
| 7 | + |
| 8 | +export LC_ALL=C |
| 9 | +set -e |
| 10 | + |
| 11 | +if [ -z "${1}" ]; then |
| 12 | + echo "Usage: $0 <base-dir> [<extra-bdb-configure-flag> ...]" |
| 13 | + echo |
| 14 | + echo "Must specify a single argument: the directory in which db4 will be built." |
| 15 | + echo "This is probably \`pwd\` if you're at the root of the bitcoin repository." |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +expand_path() { |
| 20 | + cd "${1}" && pwd -P |
| 21 | +} |
| 22 | + |
| 23 | +BDB_PREFIX="$(expand_path ${1})/db4"; shift; |
| 24 | +BDB_VERSION='db-4.8.30.NC' |
| 25 | +BDB_HASH='12edc0df75bf9abd7f82f821795bcee50f42cb2e5f76a6a281b85732798364ef' |
| 26 | +BDB_URL="https://download.oracle.com/berkeley-db/${BDB_VERSION}.tar.gz" |
| 27 | + |
| 28 | +check_exists() { |
| 29 | + command -v "$1" >/dev/null |
| 30 | +} |
| 31 | + |
| 32 | +sha256_check() { |
| 33 | + # Args: <sha256_hash> <filename> |
| 34 | + # |
| 35 | + if check_exists sha256sum; then |
| 36 | + echo "${1} ${2}" | sha256sum -c |
| 37 | + elif check_exists sha256; then |
| 38 | + if [ "$(uname)" = "FreeBSD" ]; then |
| 39 | + sha256 -c "${1}" "${2}" |
| 40 | + else |
| 41 | + echo "${1} ${2}" | sha256 -c |
| 42 | + fi |
| 43 | + else |
| 44 | + echo "${1} ${2}" | shasum -a 256 -c |
| 45 | + fi |
| 46 | +} |
| 47 | + |
| 48 | +http_get() { |
| 49 | + # Args: <url> <filename> <sha256_hash> |
| 50 | + # |
| 51 | + # It's acceptable that we don't require SSL here because we manually verify |
| 52 | + # content hashes below. |
| 53 | + # |
| 54 | + if [ -f "${2}" ]; then |
| 55 | + echo "File ${2} already exists; not downloading again" |
| 56 | + elif check_exists curl; then |
| 57 | + curl --insecure --retry 5 "${1}" -o "${2}" |
| 58 | + else |
| 59 | + wget --no-check-certificate "${1}" -O "${2}" |
| 60 | + fi |
| 61 | + |
| 62 | + sha256_check "${3}" "${2}" |
| 63 | +} |
| 64 | + |
| 65 | +mkdir -p "${BDB_PREFIX}" |
| 66 | +http_get "${BDB_URL}" "${BDB_VERSION}.tar.gz" "${BDB_HASH}" |
| 67 | +tar -xzvf ${BDB_VERSION}.tar.gz -C "$BDB_PREFIX" |
| 68 | +cd "${BDB_PREFIX}/${BDB_VERSION}/" |
| 69 | + |
| 70 | +# Apply a patch necessary when building with clang and c++11 (see https://community.oracle.com/thread/3952592) |
| 71 | +patch --ignore-whitespace -p1 << 'EOF' |
| 72 | +commit 3311d68f11d1697565401eee6efc85c34f022ea7 |
| 73 | +Author: fanquake <[email protected]> |
| 74 | +Date: Mon Aug 17 20:03:56 2020 +0800 |
| 75 | + Fix C++11 compatibility |
| 76 | +diff --git a/dbinc/atomic.h b/dbinc/atomic.h |
| 77 | +index 0034dcc..7c11d4a 100644 |
| 78 | +--- a/dbinc/atomic.h |
| 79 | ++++ b/dbinc/atomic.h |
| 80 | +@@ -70,7 +70,7 @@ typedef struct { |
| 81 | + * These have no memory barriers; the caller must include them when necessary. |
| 82 | + */ |
| 83 | + #define atomic_read(p) ((p)->value) |
| 84 | +-#define atomic_init(p, val) ((p)->value = (val)) |
| 85 | ++#define atomic_init_db(p, val) ((p)->value = (val)) |
| 86 | + #ifdef HAVE_ATOMIC_SUPPORT |
| 87 | +@@ -144,7 +144,7 @@ typedef LONG volatile *interlocked_val; |
| 88 | + #define atomic_inc(env, p) __atomic_inc(p) |
| 89 | + #define atomic_dec(env, p) __atomic_dec(p) |
| 90 | + #define atomic_compare_exchange(env, p, o, n) \ |
| 91 | +- __atomic_compare_exchange((p), (o), (n)) |
| 92 | ++ __atomic_compare_exchange_db((p), (o), (n)) |
| 93 | + static inline int __atomic_inc(db_atomic_t *p) |
| 94 | + { |
| 95 | + int temp; |
| 96 | +@@ -176,7 +176,7 @@ static inline int __atomic_dec(db_atomic_t *p) |
| 97 | + * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html |
| 98 | + * which configure could be changed to use. |
| 99 | + */ |
| 100 | +-static inline int __atomic_compare_exchange( |
| 101 | ++static inline int __atomic_compare_exchange_db( |
| 102 | + db_atomic_t *p, atomic_value_t oldval, atomic_value_t newval) |
| 103 | + { |
| 104 | + atomic_value_t was; |
| 105 | +@@ -206,7 +206,7 @@ static inline int __atomic_compare_exchange( |
| 106 | + #define atomic_dec(env, p) (--(p)->value) |
| 107 | + #define atomic_compare_exchange(env, p, oldval, newval) \ |
| 108 | + (DB_ASSERT(env, atomic_read(p) == (oldval)), \ |
| 109 | +- atomic_init(p, (newval)), 1) |
| 110 | ++ atomic_init_db(p, (newval)), 1) |
| 111 | + #else |
| 112 | + #define atomic_inc(env, p) __atomic_inc(env, p) |
| 113 | + #define atomic_dec(env, p) __atomic_dec(env, p) |
| 114 | +diff --git a/mp/mp_fget.c b/mp/mp_fget.c |
| 115 | +index 5fdee5a..0b75f57 100644 |
| 116 | +--- a/mp/mp_fget.c |
| 117 | ++++ b/mp/mp_fget.c |
| 118 | +@@ -617,7 +617,7 @@ alloc: /* Allocate a new buffer header and data space. */ |
| 119 | + /* Initialize enough so we can call __memp_bhfree. */ |
| 120 | + alloc_bhp->flags = 0; |
| 121 | +- atomic_init(&alloc_bhp->ref, 1); |
| 122 | ++ atomic_init_db(&alloc_bhp->ref, 1); |
| 123 | + #ifdef DIAGNOSTIC |
| 124 | + if ((uintptr_t)alloc_bhp->buf & (sizeof(size_t) - 1)) { |
| 125 | + __db_errx(env, |
| 126 | +@@ -911,7 +911,7 @@ alloc: /* Allocate a new buffer header and data space. */ |
| 127 | + MVCC_MPROTECT(bhp->buf, mfp->stat.st_pagesize, |
| 128 | + PROT_READ); |
| 129 | +- atomic_init(&alloc_bhp->ref, 1); |
| 130 | ++ atomic_init_db(&alloc_bhp->ref, 1); |
| 131 | + MUTEX_LOCK(env, alloc_bhp->mtx_buf); |
| 132 | + alloc_bhp->priority = bhp->priority; |
| 133 | + alloc_bhp->pgno = bhp->pgno; |
| 134 | +diff --git a/mp/mp_mvcc.c b/mp/mp_mvcc.c |
| 135 | +index 34467d2..f05aa0c 100644 |
| 136 | +--- a/mp/mp_mvcc.c |
| 137 | ++++ b/mp/mp_mvcc.c |
| 138 | +@@ -276,7 +276,7 @@ __memp_bh_freeze(dbmp, infop, hp, bhp, need_frozenp) |
| 139 | + #else |
| 140 | + memcpy(frozen_bhp, bhp, SSZA(BH, buf)); |
| 141 | + #endif |
| 142 | +- atomic_init(&frozen_bhp->ref, 0); |
| 143 | ++ atomic_init_db(&frozen_bhp->ref, 0); |
| 144 | + if (mutex != MUTEX_INVALID) |
| 145 | + frozen_bhp->mtx_buf = mutex; |
| 146 | + else if ((ret = __mutex_alloc(env, MTX_MPOOL_BH, |
| 147 | +@@ -428,7 +428,7 @@ __memp_bh_thaw(dbmp, infop, hp, frozen_bhp, alloc_bhp) |
| 148 | + #endif |
| 149 | + alloc_bhp->mtx_buf = mutex; |
| 150 | + MUTEX_LOCK(env, alloc_bhp->mtx_buf); |
| 151 | +- atomic_init(&alloc_bhp->ref, 1); |
| 152 | ++ atomic_init_db(&alloc_bhp->ref, 1); |
| 153 | + F_CLR(alloc_bhp, BH_FROZEN); |
| 154 | + } |
| 155 | +diff --git a/mp/mp_region.c b/mp/mp_region.c |
| 156 | +index e6cece9..ddbe906 100644 |
| 157 | +--- a/mp/mp_region.c |
| 158 | ++++ b/mp/mp_region.c |
| 159 | +@@ -224,7 +224,7 @@ __memp_init(env, dbmp, reginfo_off, htab_buckets, max_nreg) |
| 160 | + MTX_MPOOL_FILE_BUCKET, 0, &htab[i].mtx_hash)) != 0) |
| 161 | + return (ret); |
| 162 | + SH_TAILQ_INIT(&htab[i].hash_bucket); |
| 163 | +- atomic_init(&htab[i].hash_page_dirty, 0); |
| 164 | ++ atomic_init_db(&htab[i].hash_page_dirty, 0); |
| 165 | + } |
| 166 | + /* |
| 167 | +@@ -269,7 +269,7 @@ __memp_init(env, dbmp, reginfo_off, htab_buckets, max_nreg) |
| 168 | + hp->mtx_hash = (mtx_base == MUTEX_INVALID) ? MUTEX_INVALID : |
| 169 | + mtx_base + i; |
| 170 | + SH_TAILQ_INIT(&hp->hash_bucket); |
| 171 | +- atomic_init(&hp->hash_page_dirty, 0); |
| 172 | ++ atomic_init_db(&hp->hash_page_dirty, 0); |
| 173 | + #ifdef HAVE_STATISTICS |
| 174 | + hp->hash_io_wait = 0; |
| 175 | + hp->hash_frozen = hp->hash_thawed = hp->hash_frozen_freed = 0; |
| 176 | +diff --git a/mutex/mut_method.c b/mutex/mut_method.c |
| 177 | +index 2588763..5c6d516 100644 |
| 178 | +--- a/mutex/mut_method.c |
| 179 | ++++ b/mutex/mut_method.c |
| 180 | +@@ -426,7 +426,7 @@ atomic_compare_exchange(env, v, oldval, newval) |
| 181 | + MUTEX_LOCK(env, mtx); |
| 182 | + ret = atomic_read(v) == oldval; |
| 183 | + if (ret) |
| 184 | +- atomic_init(v, newval); |
| 185 | ++ atomic_init_db(v, newval); |
| 186 | + MUTEX_UNLOCK(env, mtx); |
| 187 | + return (ret); |
| 188 | +diff --git a/mutex/mut_tas.c b/mutex/mut_tas.c |
| 189 | +index f3922e0..e40fcdf 100644 |
| 190 | +--- a/mutex/mut_tas.c |
| 191 | ++++ b/mutex/mut_tas.c |
| 192 | +@@ -46,7 +46,7 @@ __db_tas_mutex_init(env, mutex, flags) |
| 193 | + #ifdef HAVE_SHARED_LATCHES |
| 194 | + if (F_ISSET(mutexp, DB_MUTEX_SHARED)) |
| 195 | +- atomic_init(&mutexp->sharecount, 0); |
| 196 | ++ atomic_init_db(&mutexp->sharecount, 0); |
| 197 | + else |
| 198 | + #endif |
| 199 | + if (MUTEX_INIT(&mutexp->tas)) { |
| 200 | +@@ -486,7 +486,7 @@ __db_tas_mutex_unlock(env, mutex) |
| 201 | + F_CLR(mutexp, DB_MUTEX_LOCKED); |
| 202 | + /* Flush flag update before zeroing count */ |
| 203 | + MEMBAR_EXIT(); |
| 204 | +- atomic_init(&mutexp->sharecount, 0); |
| 205 | ++ atomic_init_db(&mutexp->sharecount, 0); |
| 206 | + } else { |
| 207 | + DB_ASSERT(env, sharecount > 0); |
| 208 | + MEMBAR_EXIT(); |
| 209 | +EOF |
| 210 | + |
| 211 | +# The packaged config.guess and config.sub are ancient (2009) and can cause build issues. |
| 212 | +# Replace them with modern versions. |
| 213 | +# See https://github.com/bitcoin/bitcoin/issues/16064 |
| 214 | +CONFIG_GUESS_URL='https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=55eaf3e779455c4e5cc9f82efb5278be8f8f900b' |
| 215 | +CONFIG_GUESS_HASH='2d1ff7bca773d2ec3c6217118129220fa72d8adda67c7d2bf79994b3129232c1' |
| 216 | +CONFIG_SUB_URL='https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=55eaf3e779455c4e5cc9f82efb5278be8f8f900b' |
| 217 | +CONFIG_SUB_HASH='3a4befde9bcdf0fdb2763fc1bfa74e8696df94e1ad7aac8042d133c8ff1d2e32' |
| 218 | + |
| 219 | +rm -f "dist/config.guess" |
| 220 | +rm -f "dist/config.sub" |
| 221 | + |
| 222 | +http_get "${CONFIG_GUESS_URL}" dist/config.guess "${CONFIG_GUESS_HASH}" |
| 223 | +http_get "${CONFIG_SUB_URL}" dist/config.sub "${CONFIG_SUB_HASH}" |
| 224 | + |
| 225 | +cd build_unix/ |
| 226 | + |
| 227 | +"${BDB_PREFIX}/${BDB_VERSION}/dist/configure" \ |
| 228 | + --enable-cxx --disable-shared --disable-replication --with-pic --prefix="${BDB_PREFIX}" \ |
| 229 | + "${@}" |
| 230 | + |
| 231 | +make install |
| 232 | + |
| 233 | +echo |
| 234 | +echo "db4 build complete." |
| 235 | +echo |
| 236 | +# shellcheck disable=SC2016 |
| 237 | +echo 'When compiling bitcoind, run `./configure` in the following way:' |
| 238 | +echo |
| 239 | +echo " export BDB_PREFIX='${BDB_PREFIX}'" |
| 240 | +# shellcheck disable=SC2016 |
| 241 | +echo ' ./configure BDB_LIBS="-L${BDB_PREFIX}/lib -ldb_cxx-4.8" BDB_CFLAGS="-I${BDB_PREFIX}/include" ...' |
0 commit comments