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
8export LC_ALL=C
9set -e
10
11if [ -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
17fi
18
19expand_path() {
20  cd "${1}" && pwd -P
21}
22
23BDB_PREFIX="$(expand_path ${1})/db4"; shift;
24BDB_VERSION='db-4.8.30.NC'
25BDB_HASH='12edc0df75bf9abd7f82f821795bcee50f42cb2e5f76a6a281b85732798364ef'
26BDB_URL="https://download.oracle.com/berkeley-db/${BDB_VERSION}.tar.gz"
27
28check_exists() {
29  command -v "$1" >/dev/null
30}
31
32sha256_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
48http_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
65mkdir -p "${BDB_PREFIX}"
66http_get "${BDB_URL}" "${BDB_VERSION}.tar.gz" "${BDB_HASH}"
67tar -xzvf ${BDB_VERSION}.tar.gz -C "$BDB_PREFIX"
68cd "${BDB_PREFIX}/${BDB_VERSION}/"
69
70# Apply a patch necessary when building with clang and c++11 (see https://community.oracle.com/thread/3952592)
71patch --ignore-whitespace -p1 << 'EOF'
72commit 3311d68f11d1697565401eee6efc85c34f022ea7
73Author: fanquake <fanquake@gmail.com>
74Date:   Mon Aug 17 20:03:56 2020 +0800
75
76    Fix C++11 compatibility
77
78diff --git a/dbinc/atomic.h b/dbinc/atomic.h
79index 0034dcc..7c11d4a 100644
80--- a/dbinc/atomic.h
81+++ b/dbinc/atomic.h
82@@ -70,7 +70,7 @@ typedef struct {
83  * These have no memory barriers; the caller must include them when necessary.
84  */
85 #define	atomic_read(p)		((p)->value)
86-#define	atomic_init(p, val)	((p)->value = (val))
87+#define	atomic_init_db(p, val)	((p)->value = (val))
88
89 #ifdef HAVE_ATOMIC_SUPPORT
90
91@@ -144,7 +144,7 @@ typedef LONG volatile *interlocked_val;
92 #define	atomic_inc(env, p)	__atomic_inc(p)
93 #define	atomic_dec(env, p)	__atomic_dec(p)
94 #define	atomic_compare_exchange(env, p, o, n)	\
95-	__atomic_compare_exchange((p), (o), (n))
96+	__atomic_compare_exchange_db((p), (o), (n))
97 static inline int __atomic_inc(db_atomic_t *p)
98 {
99 	int	temp;
100@@ -176,7 +176,7 @@ static inline int __atomic_dec(db_atomic_t *p)
101  * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
102  * which configure could be changed to use.
103  */
104-static inline int __atomic_compare_exchange(
105+static inline int __atomic_compare_exchange_db(
106 	db_atomic_t *p, atomic_value_t oldval, atomic_value_t newval)
107 {
108 	atomic_value_t was;
109@@ -206,7 +206,7 @@ static inline int __atomic_compare_exchange(
110 #define	atomic_dec(env, p)	(--(p)->value)
111 #define	atomic_compare_exchange(env, p, oldval, newval)		\
112 	(DB_ASSERT(env, atomic_read(p) == (oldval)),		\
113-	atomic_init(p, (newval)), 1)
114+	atomic_init_db(p, (newval)), 1)
115 #else
116 #define atomic_inc(env, p)	__atomic_inc(env, p)
117 #define atomic_dec(env, p)	__atomic_dec(env, p)
118diff --git a/mp/mp_fget.c b/mp/mp_fget.c
119index 5fdee5a..0b75f57 100644
120--- a/mp/mp_fget.c
121+++ b/mp/mp_fget.c
122@@ -617,7 +617,7 @@ alloc:		/* Allocate a new buffer header and data space. */
123
124 		/* Initialize enough so we can call __memp_bhfree. */
125 		alloc_bhp->flags = 0;
126-		atomic_init(&alloc_bhp->ref, 1);
127+		atomic_init_db(&alloc_bhp->ref, 1);
128 #ifdef DIAGNOSTIC
129 		if ((uintptr_t)alloc_bhp->buf & (sizeof(size_t) - 1)) {
130 			__db_errx(env,
131@@ -911,7 +911,7 @@ alloc:		/* Allocate a new buffer header and data space. */
132 			MVCC_MPROTECT(bhp->buf, mfp->stat.st_pagesize,
133 			    PROT_READ);
134
135-		atomic_init(&alloc_bhp->ref, 1);
136+		atomic_init_db(&alloc_bhp->ref, 1);
137 		MUTEX_LOCK(env, alloc_bhp->mtx_buf);
138 		alloc_bhp->priority = bhp->priority;
139 		alloc_bhp->pgno = bhp->pgno;
140diff --git a/mp/mp_mvcc.c b/mp/mp_mvcc.c
141index 34467d2..f05aa0c 100644
142--- a/mp/mp_mvcc.c
143+++ b/mp/mp_mvcc.c
144@@ -276,7 +276,7 @@ __memp_bh_freeze(dbmp, infop, hp, bhp, need_frozenp)
145 #else
146 	memcpy(frozen_bhp, bhp, SSZA(BH, buf));
147 #endif
148-	atomic_init(&frozen_bhp->ref, 0);
149+	atomic_init_db(&frozen_bhp->ref, 0);
150 	if (mutex != MUTEX_INVALID)
151 		frozen_bhp->mtx_buf = mutex;
152 	else if ((ret = __mutex_alloc(env, MTX_MPOOL_BH,
153@@ -428,7 +428,7 @@ __memp_bh_thaw(dbmp, infop, hp, frozen_bhp, alloc_bhp)
154 #endif
155 		alloc_bhp->mtx_buf = mutex;
156 		MUTEX_LOCK(env, alloc_bhp->mtx_buf);
157-		atomic_init(&alloc_bhp->ref, 1);
158+		atomic_init_db(&alloc_bhp->ref, 1);
159 		F_CLR(alloc_bhp, BH_FROZEN);
160 	}
161
162diff --git a/mp/mp_region.c b/mp/mp_region.c
163index e6cece9..ddbe906 100644
164--- a/mp/mp_region.c
165+++ b/mp/mp_region.c
166@@ -224,7 +224,7 @@ __memp_init(env, dbmp, reginfo_off, htab_buckets, max_nreg)
167 			     MTX_MPOOL_FILE_BUCKET, 0, &htab[i].mtx_hash)) != 0)
168 				return (ret);
169 			SH_TAILQ_INIT(&htab[i].hash_bucket);
170-			atomic_init(&htab[i].hash_page_dirty, 0);
171+			atomic_init_db(&htab[i].hash_page_dirty, 0);
172 		}
173
174 		/*
175@@ -269,7 +269,7 @@ __memp_init(env, dbmp, reginfo_off, htab_buckets, max_nreg)
176 		hp->mtx_hash = (mtx_base == MUTEX_INVALID) ? MUTEX_INVALID :
177 		    mtx_base + i;
178 		SH_TAILQ_INIT(&hp->hash_bucket);
179-		atomic_init(&hp->hash_page_dirty, 0);
180+		atomic_init_db(&hp->hash_page_dirty, 0);
181 #ifdef HAVE_STATISTICS
182 		hp->hash_io_wait = 0;
183 		hp->hash_frozen = hp->hash_thawed = hp->hash_frozen_freed = 0;
184diff --git a/mutex/mut_method.c b/mutex/mut_method.c
185index 2588763..5c6d516 100644
186--- a/mutex/mut_method.c
187+++ b/mutex/mut_method.c
188@@ -426,7 +426,7 @@ atomic_compare_exchange(env, v, oldval, newval)
189 	MUTEX_LOCK(env, mtx);
190 	ret = atomic_read(v) == oldval;
191 	if (ret)
192-		atomic_init(v, newval);
193+		atomic_init_db(v, newval);
194 	MUTEX_UNLOCK(env, mtx);
195
196 	return (ret);
197diff --git a/mutex/mut_tas.c b/mutex/mut_tas.c
198index f3922e0..e40fcdf 100644
199--- a/mutex/mut_tas.c
200+++ b/mutex/mut_tas.c
201@@ -46,7 +46,7 @@ __db_tas_mutex_init(env, mutex, flags)
202
203 #ifdef HAVE_SHARED_LATCHES
204 	if (F_ISSET(mutexp, DB_MUTEX_SHARED))
205-		atomic_init(&mutexp->sharecount, 0);
206+		atomic_init_db(&mutexp->sharecount, 0);
207 	else
208 #endif
209 	if (MUTEX_INIT(&mutexp->tas)) {
210@@ -486,7 +486,7 @@ __db_tas_mutex_unlock(env, mutex)
211 			F_CLR(mutexp, DB_MUTEX_LOCKED);
212 			/* Flush flag update before zeroing count */
213 			MEMBAR_EXIT();
214-			atomic_init(&mutexp->sharecount, 0);
215+			atomic_init_db(&mutexp->sharecount, 0);
216 		} else {
217 			DB_ASSERT(env, sharecount > 0);
218 			MEMBAR_EXIT();
219EOF
220
221# The packaged config.guess and config.sub are ancient (2009) and can cause build issues.
222# Replace them with modern versions.
223# See https://github.com/bitcoin/bitcoin/issues/16064
224CONFIG_GUESS_URL='https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=55eaf3e779455c4e5cc9f82efb5278be8f8f900b'
225CONFIG_GUESS_HASH='2d1ff7bca773d2ec3c6217118129220fa72d8adda67c7d2bf79994b3129232c1'
226CONFIG_SUB_URL='https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=55eaf3e779455c4e5cc9f82efb5278be8f8f900b'
227CONFIG_SUB_HASH='3a4befde9bcdf0fdb2763fc1bfa74e8696df94e1ad7aac8042d133c8ff1d2e32'
228
229rm -f "dist/config.guess"
230rm -f "dist/config.sub"
231
232http_get "${CONFIG_GUESS_URL}" dist/config.guess "${CONFIG_GUESS_HASH}"
233http_get "${CONFIG_SUB_URL}" dist/config.sub "${CONFIG_SUB_HASH}"
234
235cd build_unix/
236
237"${BDB_PREFIX}/${BDB_VERSION}/dist/configure" \
238  --enable-cxx --disable-shared --disable-replication --with-pic --prefix="${BDB_PREFIX}" \
239  "${@}"
240
241make install
242
243echo
244echo "db4 build complete."
245echo
246# shellcheck disable=SC2016
247echo 'When compiling bitcoind, run `./configure` in the following way:'
248echo
249echo "  export BDB_PREFIX='${BDB_PREFIX}'"
250# shellcheck disable=SC2016
251echo '  ./configure BDB_LIBS="-L${BDB_PREFIX}/lib -ldb_cxx-4.8" BDB_CFLAGS="-I${BDB_PREFIX}/include" ...'
252