|
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 |
5 | 2 |
|
6 | 3 | set -o errexit
|
7 | 4 | set -o pipefail
|
8 | 5 |
|
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:?}" |
12 | 8 |
|
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 | +} |
15 | 15 |
|
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" |
19 | 18 |
|
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}")" |
22 | 25 | else
|
23 |
| - ENABLE_EXTRA_ALIGNMENT="OFF" |
| 26 | + mongoc_idir="${mongoc_dir}" |
| 27 | + mongoc_install_idir="${mongoc_dir}" |
24 | 28 | 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}" |
30 | 33 |
|
31 |
| -echo "About to install C driver ($VERSION) into $PREFIX" |
| 34 | +echo "Installing C Driver into ${mongoc_dir}..." |
32 | 35 |
|
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 |
38 | 41 |
|
| 42 | +# C Driver needs VERSION_CURRENT to compute BUILD_VERSION. |
39 | 43 | # 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" |
44 | 48 | 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. |
46 | 50 |
|
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": "(.+)"/') |
49 | 54 |
|
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}")" |
52 | 57 |
|
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}")")" |
55 | 60 |
|
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}")" |
58 | 63 |
|
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" |
61 | 66 | fi
|
62 | 67 |
|
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:?}" |
64 | 73 |
|
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}" |
67 | 76 |
|
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 | + } |
75 | 82 | fi
|
76 | 83 |
|
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 |
84 | 121 |
|
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 |
90 | 128 |
|
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[@]}" |
96 | 132 |
|
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." |
0 commit comments