Skip to content

Commit 6a49989

Browse files
authored
CXX-2517 Use C Driver scripts to install libmongoc and libmongocrypt (#950)
* Format install_c_driver.sh * Move expansions for install_c_driver EVG function into script * Bump minimum libmongoc version from 1.22.1 to latest commit * Use C Driver find-cmake-latest.sh and compile-libmongocrypt.sh scripts
1 parent 46b86f4 commit 6a49989

File tree

2 files changed

+109
-128
lines changed

2 files changed

+109
-128
lines changed

.evergreen/install_c_driver.sh

Lines changed: 105 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,133 @@
1-
#!/bin/bash
2-
# Usage examples:
3-
# MONGOC_VERSION=1.22.1 MONGOCRYPT_VERSION=1.5.2 ./install.sh
4-
# PREFIX=/tmp/installdir MONGOC_VERSION=1.22.1 MONGOCRYPT_VERSION=1.5.2 ./install.sh
1+
#!/usr/bin/env bash
52

63
set -o errexit
74
set -o pipefail
85

9-
print_usage() {
10-
echo "usage: MONGOC_VERSION=<version> MONGOCRYPT_VERSION=<version> ./install.sh"
11-
}
6+
declare -r mongoc_version="${mongoc_version:-"${mongoc_version_default:?"missing mongoc version"}"}"
7+
: "${mongoc_version:?}"
128

13-
if [[ -z $MONGOC_VERSION ]]; then print_usage; exit 2; fi
14-
if [[ -z $MONGOCRYPT_VERSION ]]; then print_usage; exit 2; fi
9+
# Usage:
10+
# to_windows_path "./some/unix/style/path"
11+
# to_windows_path "/some/unix/style/path"
12+
to_windows_path() {
13+
cygpath -aw "${1:?"to_windows_path requires a path to convert"}"
14+
}
1515

16-
VERSION=$MONGOC_VERSION
17-
PREFIX=${PREFIX:-$(pwd)"/../mongoc/"}
18-
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
16+
declare mongoc_dir
17+
mongoc_dir="$(pwd)/mongoc"
1918

20-
if [ "$BSON_EXTRA_ALIGNMENT" = "1" ]; then
21-
ENABLE_EXTRA_ALIGNMENT="ON"
19+
# "i" for "(platform-)independent".
20+
declare mongoc_idir mongoc_install_idir
21+
if [[ "${OSTYPE:?}" == "cygwin" ]]; then
22+
# CMake requires Windows paths for configuration variables on Windows.
23+
mongoc_idir="$(to_windows_path "${mongoc_dir}")"
24+
mongoc_install_idir="$(to_windows_path "${mongoc_dir}")"
2225
else
23-
ENABLE_EXTRA_ALIGNMENT="OFF"
26+
mongoc_idir="${mongoc_dir}"
27+
mongoc_install_idir="${mongoc_dir}"
2428
fi
25-
CMAKE_ARGS="
26-
-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DENABLE_SHM_COUNTERS=OFF
27-
-DENABLE_TESTS=OFF -DENABLE_EXAMPLES=OFF -DENABLE_STATIC=ON
28-
-DENABLE_EXTRA_ALIGNMENT=${ENABLE_EXTRA_ALIGNMENT} -DCMAKE_MACOSX_RPATH=ON
29-
-DCMAKE_INSTALL_PREFIX=$PREFIX -DCMAKE_PREFIX_PATH=$PREFIX"
29+
: "${mongoc_idir:?}"
30+
: "${mongoc_install_idir:?}"
31+
32+
echo "libmongoc version: ${mongoc_version}"
3033

31-
echo "About to install C driver ($VERSION) into $PREFIX"
34+
echo "Installing C Driver into ${mongoc_dir}..."
3235

33-
LIB=mongo-c-driver
34-
rm -rf $(echo $LIB*)
35-
curl -sS -o $LIB.tar.gz -L https://api.github.com/repos/mongodb/$LIB/tarball/$VERSION
36-
tar xzf $LIB.tar.gz
37-
DIR=$(echo mongodb-$LIB-*)
36+
# Download tarball from GitHub and extract into ${mongoc_dir}.
37+
rm -rf "${mongoc_dir}"
38+
mkdir "${mongoc_dir}"
39+
curl -sS -o mongo-c-driver.tar.gz -L "https://api.github.com/repos/mongodb/mongo-c-driver/tarball/${mongoc_version}"
40+
tar xzf mongo-c-driver.tar.gz --directory "${mongoc_dir}" --strip-components=1
3841

42+
# C Driver needs VERSION_CURRENT to compute BUILD_VERSION.
3943
# RegEx pattern to match SemVer strings. See https://semver.org/.
40-
SEMVER_REGEX="^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
41-
if [ $(echo "$VERSION" | perl -ne "$(printf 'exit 1 unless /%s/' $SEMVER_REGEX)") ]; then
42-
# If $VERSION is already SemVer compliant, use as-is.
43-
CMAKE_ARGS="$CMAKE_ARGS -DBUILD_VERSION=$BUILD_VERSION"
44+
declare -r semver_regex="^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
45+
if echo "${mongoc_version}" | perl -ne "$(printf 'exit 1 unless /%s/' "${semver_regex}")"; then
46+
# If $VERSION is already SemVer compliant, use as-is.
47+
echo "${mongoc_version}" >|"${mongoc_dir}/VERSION_CURRENT"
4448
else
45-
# Otherwise, use the tag name of the latest release to construct a prerelease version string.
49+
# Otherwise, use the tag name of the latest release to construct a prerelease version string.
4650

47-
# Extract "tag_name" from latest Github release.
48-
BUILD_VERSION=$(curl -sS -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/mongodb/mongo-c-driver/releases/latest | perl -ne 'print for /"tag_name": "(.+)"/')
51+
# Extract "tag_name" from latest Github release.
52+
declare build_version
53+
build_version=$(curl -sS -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/mongodb/mongo-c-driver/releases/latest | perl -ne 'print for /"tag_name": "(.+)"/')
4954

50-
# Assert the tag name is a SemVer string via errexit.
51-
echo $BUILD_VERSION | perl -ne "$(printf 'exit 1 unless /%s/' $SEMVER_REGEX)"
55+
# Assert the tag name is a SemVer string via errexit.
56+
echo "${build_version}" | perl -ne "$(printf 'exit 1 unless /%s/' "${semver_regex}")"
5257

53-
# Bump to the next minor version, e.g. 1.0.1 -> 1.1.0.
54-
BUILD_VERSION=$(echo $BUILD_VERSION | perl -ne "$(printf '/%s/; print $+{major} . "." . ($+{minor}+1) . ".0"' $SEMVER_REGEX)")
58+
# Bump to the next minor version, e.g. 1.0.1 -> 1.1.0.
59+
build_version="$(echo "${build_version}" | perl -ne "$(printf '/%s/; print $+{major} . "." . ($+{minor}+1) . ".0"' "${semver_regex}")")"
5560

56-
# Append a prerelease tag, e.g. 1.1.0-pre+<version>.
57-
BUILD_VERSION=$(printf "%s-pre+%s" $BUILD_VERSION $VERSION)
61+
# Append a prerelease tag, e.g. 1.1.0-pre+<version>.
62+
build_version="$(printf "%s-pre+%s" "${build_version}" "${mongoc_version}")"
5863

59-
# Use the constructed prerelease build version when building the C driver.
60-
CMAKE_ARGS="$CMAKE_ARGS -DBUILD_VERSION=$BUILD_VERSION"
64+
# Use the constructed prerelease build version when building the C driver.
65+
echo "${build_version}" >|"${mongoc_dir}/VERSION_CURRENT"
6166
fi
6267

63-
. .evergreen/find_cmake.sh
68+
# shellcheck source=/dev/null
69+
. "${mongoc_dir}/.evergreen/scripts/find-cmake-latest.sh"
70+
declare cmake_binary
71+
cmake_binary="$(find_cmake_latest)"
72+
command -v "${cmake_binary:?}"
6473

65-
cd $DIR
66-
MONGOC_DIR="$(pwd)"
74+
# Install libmongocrypt.
75+
"${mongoc_dir}/.evergreen/scripts/compile-libmongocrypt.sh" "${cmake_binary}" "${mongoc_idir}" "${mongoc_install_idir}"
6776

68-
if [ -f /proc/cpuinfo ]; then
69-
CONCURRENCY=$(grep -c ^processor /proc/cpuinfo)
70-
elif which sysctl; then
71-
CONCURRENCY=$(sysctl -n hw.logicalcpu)
72-
else
73-
echo "$0: can't figure out what value of -j to pass to 'make'" >&2
74-
exit 1
77+
if [[ "${OSTYPE}" == darwin* ]]; then
78+
# MacOS does not have nproc.
79+
nproc() {
80+
sysctl -n hw.logicalcpu
81+
}
7582
fi
7683

77-
export CFLAGS="-fPIC"
78-
79-
case "$OS" in
80-
darwin|linux)
81-
GENERATOR=${GENERATOR:-"Unix Makefiles"}
82-
CMAKE_BUILD_OPTS="-j $CONCURRENCY"
83-
;;
84+
# Default CMake generator to use if not already provided.
85+
declare cmake_generator
86+
if [[ "${OSTYPE:?}" == "cygwin" ]]; then
87+
cmake_generator=${generator:-"Visual Studio 14 2015 Win64"}
88+
else
89+
cmake_generator=${generator:-"Unix Makefiles"}
90+
fi
91+
: "${cmake_generator:?}"
92+
93+
declare -a configure_flags=(
94+
"-DCMAKE_BUILD_TYPE=Debug"
95+
"-DCMAKE_INSTALL_PREFIX=${mongoc_install_idir}"
96+
"-DCMAKE_PREFIX_PATH=${mongoc_idir}"
97+
"-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"
98+
"-DENABLE_CLIENT_SIDE_ENCRYPTION=ON"
99+
"-DENABLE_EXAMPLES=OFF"
100+
"-DENABLE_SHM_COUNTERS=OFF"
101+
"-DENABLE_STATIC=ON"
102+
"-DENABLE_TESTS=OFF"
103+
)
104+
105+
declare -a compile_flags
106+
107+
case "${OSTYPE:?}" in
108+
cygwin)
109+
compile_flags+=("/maxcpucount:$(nproc)")
110+
;;
111+
darwin*)
112+
configure_flags+=("-DCMAKE_C_FLAGS=-fPIC")
113+
configure_flags+=("-DCMAKE_MACOSX_RPATH=ON")
114+
compile_flags+=("-j" "$(nproc)")
115+
;;
116+
*)
117+
configure_flags+=("-DCMAKE_C_FLAGS=-fPIC")
118+
compile_flags+=("-j" "$(nproc)")
119+
;;
120+
esac
84121

85-
cygwin*)
86-
GENERATOR=${GENERATOR:-"Visual Studio 14 2015 Win64"}
87-
CMAKE_BUILD_OPTS="/maxcpucount:$CONCURRENCY"
88-
MONGOC_DIR=$(cygpath -m "$MONGOC_DIR")
89-
;;
122+
if [[ "${BSON_EXTRA_ALIGNMENT}" == "1" ]]; then
123+
echo "Building C Driver with ENABLE_EXTRA_ALIGNMENT=ON"
124+
configure_flags+=("-DENABLE_EXTRA_ALIGNMENT=ON")
125+
else
126+
configure_flags+=("-DENABLE_EXTRA_ALIGNMENT=OFF")
127+
fi
90128

91-
*)
92-
echo "$0: unsupported platform '$OS'" >&2
93-
exit 2
94-
;;
95-
esac
129+
# Install libmongoc.
130+
"${cmake_binary}" -S "${mongoc_idir}" -B "${mongoc_idir}" -G "${cmake_generator}" "${configure_flags[@]}"
131+
"${cmake_binary}" --build "${mongoc_idir}" --config Debug --target install -- "${compile_flags[@]}"
96132

97-
# build libbson
98-
mkdir cmake_build
99-
cd cmake_build
100-
"$CMAKE" -G "$GENERATOR" -DCMAKE_BUILD_TYPE="Debug" -DENABLE_MONGOC=OFF $CMAKE_ARGS ..
101-
"$CMAKE" --build . --config Debug -- $CMAKE_BUILD_OPTS
102-
"$CMAKE" --build . --config Debug --target install
103-
cd ../../
104-
105-
# fetch and build libmongocrypt
106-
git clone https://github.com/mongodb/libmongocrypt
107-
mkdir libmongocrypt/cmake_build
108-
cd libmongocrypt/cmake_build
109-
git checkout $MONGOCRYPT_VERSION
110-
"$CMAKE" -G "$GENERATOR" -DENABLE_SHARED_BSON=ON -DCMAKE_INSTALL_PREFIX="$PREFIX" \
111-
-DCMAKE_PREFIX_PATH="$PREFIX" -DCMAKE_BUILD_TYPE="Debug" \
112-
-DMONGOCRYPT_MONGOC_DIR="$MONGOC_DIR" -DENABLE_CLIENT_SIDE_ENCRYPTION=OFF ..
113-
"$CMAKE" --build . --config Debug -- $CMAKE_BUILD_OPTS
114-
"$CMAKE" --build . --config Debug --target install
115-
cd ../../$DIR
116-
117-
# build libmongoc
118-
cd cmake_build
119-
"$CMAKE" -G "$GENERATOR" -DCMAKE_BUILD_TYPE="Debug" -DENABLE_MONGOC=ON -DENABLE_CLIENT_SIDE_ENCRYPTION=ON $CMAKE_ARGS ..
120-
"$CMAKE" --build . --config Debug -- $CMAKE_BUILD_OPTS
121-
"$CMAKE" --build . --config Debug --target install
122-
cd ../
123-
124-
echo "Done installing"
125-
126-
cd ..
127-
ls -l ..
133+
echo "Installing C Driver into ${mongoc_dir}... done."

.mci.yml

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
#######################################
88
variables:
99

10-
mongoc_version_default: &mongoc_version_default "2966c67d"
11-
mongocrypt_version_default: &mongocrypt_version_default "1.5.2"
10+
mongoc_version_default: &mongoc_version_default "9ef1cc99" # TODO: update to 1.24.0 once released.
1211

1312
# If updating mongoc_version_minimum, also update:
1413
# - the default value of --c-driver-build-ref in etc/make_release.py
1514
# - LIBMONGOC_REQUIRED_VERSION in src/mongocxx/CMakeLists.txt
16-
mongoc_version_minimum: &mongoc_version_minimum "1.22.1"
17-
mongocrypt_version_minimum: &mongocrypt_version_minimum "1.5.2"
15+
mongoc_version_minimum: &mongoc_version_minimum "9ef1cc99" # TODO: update to 1.24.0 once released.
1816

1917
mongodb_version:
2018
version_latest: &version_latest "latest"
@@ -223,33 +221,11 @@ functions:
223221
updates:
224222
- key: mongoc_version_default
225223
value: *mongoc_version_default
226-
- key: mongocrypt_version_default
227-
value: *mongocrypt_version_default
228224
- command: shell.exec
229225
params:
230226
shell: bash
231-
working_dir: "mongo-cxx-driver"
232-
script: |
233-
set -o errexit
234-
set -o pipefail
235-
export PREFIX=$(pwd)/../mongoc/
236-
if [ "Windows_NT" == "$OS" ]; then
237-
PREFIX=$(cygpath -m "$PREFIX")
238-
fi
239-
export CMAKE=${cmake}
240-
export GENERATOR="${generator}"
241-
if [ "1" == "${BSON_EXTRA_ALIGNMENT}" ]; then
242-
echo "Building C driver with BSON extra alignment"
243-
fi
244-
MONGOC_VERSION=${mongoc_version}
245-
MONGOCRYPT_VERSION=${mongocrypt_version}
246-
if [[ -z $MONGOC_VERSION ]]; then
247-
MONGOC_VERSION=${mongoc_version_default}
248-
fi
249-
if [[ -z $MONGOCRYPT_VERSION ]]; then
250-
MONGOCRYPT_VERSION=${mongocrypt_version_default}
251-
fi
252-
BSON_EXTRA_ALIGNMENT=${BSON_EXTRA_ALIGNMENT} MONGOC_VERSION=$MONGOC_VERSION MONGOCRYPT_VERSION=$MONGOCRYPT_VERSION ./.evergreen/install_c_driver.sh
227+
add_expansions_to_env: true
228+
script: mongo-cxx-driver/.evergreen/install_c_driver.sh
253229

254230
"lint":
255231
- command: shell.exec
@@ -1611,7 +1587,6 @@ buildvariants:
16111587
cmake_flags: *linux_cmake_flags
16121588
mongodb_version: *version_latest
16131589
mongoc_version: *mongoc_version_minimum
1614-
mongocrypt_version: *mongocrypt_version_minimum
16151590
run_on:
16161591
- ubuntu1804-build
16171592
tasks:

0 commit comments

Comments
 (0)