11#! /bin/bash
22# Compiles libmongocrypt dependencies and targets.
33#
4- # Set extra cflags for libmongocrypt variables by setting LIBMONGOCRYPT_EXTRA_CFLAGS.
5- #
4+ # Set extra compilation for libmongocrypt variables by setting CFLAGS and CXXFLAGS.
65
76echo " Begin compile process"
87
98. " $( dirname " ${BASH_SOURCE[0]} " ) /setup-env.sh"
109
11- # We may need some more C++ flags
12- _cxxflags=" "
10+ set -eu
1311
14- : " ${CONFIGURE_ONLY:= } "
15- : " ${LIBMONGOCRYPT_BUILD_TYPE:= RelWithDebInfo} "
16-
17- if [ " $OS_NAME " = " windows" ]; then
18- # Enable exception handling for MSVC
19- _cxxflags=" -EHsc"
20- if is_false WINDOWS_32BIT && is_false USE_NINJA; then
21- # These options are only needed for VS CMake generators to force it to
22- # generate a 64-bit build. Default is 32-bit. Ninja inherits settings
23- # from the build environment variables.
24- ADDITIONAL_CMAKE_FLAGS=" -Thost=x64 -A x64"
25- fi
26- fi
12+ # Directory where build files will be stored
13+ : " ${BINARY_DIR:= " $LIBMONGOCRYPT_DIR /cmake-build" } "
14+ # Additional compilation flags that apply only to the libmongocrypt build
15+ : " ${LIBMONGOCRYPT_COMPILE_FLAGS:= } "
16+
17+ # Control the build configuration that is generated.
18+ export CMAKE_BUILD_TYPE=" ${CMAKE_BUILD_TYPE:- RelWithDebInfo} "
19+ # Sets the default config for --build and CTest
20+ export CMAKE_CONFIG_TYPE=" $CMAKE_BUILD_TYPE "
21+ # Control the install prefix
22+ export CMAKE_INSTALL_PREFIX=" ${MONGOCRYPT_INSTALL_PREFIX-} "
2723
2824# Have CTest print test failure info to stderr
2925export CTEST_OUTPUT_ON_FAILURE=1
26+ # Generate a compilation database for use by other tools
27+ export CMAKE_EXPORT_COMPILE_COMMANDS=1
28+
29+ # Accumulate arguments that are passed to CMake
30+ cmake_args=(
31+ --fresh
32+ # Set the build type. CMake 3.22 recognizes this via environment variable
33+ -D CMAKE_BUILD_TYPE=" ${CMAKE_BUILD_TYPE} "
34+ # Set the install prefix. CMake 3.29 recognizes this via environment variable
35+ -D CMAKE_INSTALL_PREFIX=" $CMAKE_INSTALL_PREFIX "
36+ # Toggle compiling with shared BSON
37+ -D USE_SHARED_LIBBSON=" ${USE_SHARED_LIBBSON-FALSE} "
38+ # Toggle building of tests
39+ -D BUILD_TESTING=" ${BUILD_TESTING-TRUE} "
40+ # Enable additional warnings-as-errors
41+ -D ENABLE_MORE_WARNINGS_AS_ERRORS=TRUE
42+ )
43+
44+ : " ${CONFIGURE_ONLY:= } "
3045
3146if [ " $PPA_BUILD_ONLY " ]; then
3247 # Clean-up from previous build iteration
33- rm -rf -- " $LIBMONGOCRYPT_DIR " /cmake-build* " $MONGOCRYPT_INSTALL_PREFIX "
34- ADDITIONAL_CMAKE_FLAGS=" $ADDITIONAL_CMAKE_FLAGS -DENABLE_BUILD_FOR_PPA=ON"
35- fi
36-
37- if [ " $MACOS_UNIVERSAL " = " ON" ]; then
38- ADDITIONAL_CMAKE_FLAGS=" $ADDITIONAL_CMAKE_FLAGS -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64'"
48+ rm -rf -- " $LIBMONGOCRYPT_DIR " /cmake-build* " $CMAKE_INSTALL_PREFIX "
49+ cmake_args+=(-DENABLE_BUILD_FOR_PPA=ON)
3950fi
4051
4152for suffix in " dll" " dylib" " so" ; do
4253 cand=" $( abspath " $LIBMONGOCRYPT_DIR /../mongocrypt_v1.$suffix " ) "
4354 if test -f " $cand " ; then
44- ADDITIONAL_CMAKE_FLAGS= " $ADDITIONAL_CMAKE_FLAGS -DMONGOCRYPT_TESTING_CRYPT_SHARED_FILE=$cand "
55+ cmake_args+=( " -DMONGOCRYPT_TESTING_CRYPT_SHARED_FILE=$cand " )
4556 fi
4657done
4758
48- ADDITIONAL_CMAKE_FLAGS=" $ADDITIONAL_CMAKE_FLAGS -DENABLE_MORE_WARNINGS_AS_ERRORS=ON"
49-
50- build_dir=" $LIBMONGOCRYPT_DIR /cmake-build"
51- common_cmake_args=(
52- $ADDITIONAL_CMAKE_FLAGS
53- $LIBMONGOCRYPT_EXTRA_CMAKE_FLAGS
54- -DCMAKE_C_FLAGS=" $LIBMONGOCRYPT_EXTRA_CFLAGS "
55- -DCMAKE_CXX_FLAGS=" $LIBMONGOCRYPT_EXTRA_CFLAGS $_cxxflags "
56- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
57- -DCMAKE_BUILD_TYPE=" $LIBMONGOCRYPT_BUILD_TYPE "
58- -H" $LIBMONGOCRYPT_DIR "
59- -B" $build_dir "
60- )
61-
62- if is_true USE_NINJA; then
59+ if test " ${CMAKE_GENERATOR-} " = Ninja; then
6360 export NINJA_EXE
64- : " ${NINJA_EXE:= " $build_dir /ninja$EXE_SUFFIX " } "
65- common_cmake_args+=(
66- -GNinja
67- -DCMAKE_MAKE_PROGRAM=" $NINJA_EXE "
68- )
61+ : " ${NINJA_EXE:= " $BINARY_DIR /ninja$EXE_SUFFIX " } "
62+ cmake_args+=(-DCMAKE_MAKE_PROGRAM=" $NINJA_EXE " )
6963 bash " $EVG_DIR /ensure-ninja.sh"
7064fi
7165
66+ # A command that prepends our custom compile flags for any CMake execution
67+ _cmake_with_env () {
68+ # Prepend our custom C and CXX flags for any possible CMake builds
69+ CFLAGS=" $LIBMONGOCRYPT_COMPILE_FLAGS ${CFLAGS-} " \
70+ CXXFLAGS=" $LIBMONGOCRYPT_COMPILE_FLAGS ${CXXFLAGS-} " \
71+ run_cmake " $@ "
72+ }
73+
7274# Build and install libmongocrypt.
73- run_cmake \
74- -DCMAKE_INSTALL_PREFIX=" $MONGOCRYPT_INSTALL_PREFIX " \
75- " ${common_cmake_args[@]} "
75+ _cmake_with_env " ${cmake_args[@]} " \
76+ -B " $BINARY_DIR " -S " $LIBMONGOCRYPT_DIR "
7677
7778if [ " $CONFIGURE_ONLY " ]; then
7879 echo " Only running cmake" ;
7980 exit 0;
8081fi
8182echo " Installing libmongocrypt"
82- run_cmake --build " $build_dir " --target install --config " $LIBMONGOCRYPT_BUILD_TYPE "
83- run_cmake --build " $build_dir " --target test-mongocrypt --config " $LIBMONGOCRYPT_BUILD_TYPE "
84- run_cmake --build " $build_dir " --target test_kms_request --config " $LIBMONGOCRYPT_BUILD_TYPE "
85- run_chdir " $build_dir " run_ctest -C " $LIBMONGOCRYPT_BUILD_TYPE "
83+ _cmake_with_env --build " $BINARY_DIR " --target install test-mongocrypt test_kms_request
84+ run_chdir " $BINARY_DIR " run_ctest
8685
8786# MONGOCRYPT-372, ensure macOS universal builds contain both x86_64 and arm64 architectures.
88- if [ " $MACOS_UNIVERSAL " = " ON " ] ; then
87+ if test " ${CMAKE_OSX_ARCHITECTURES-} " ! = ' ' ; then
8988 echo " Checking if libmongocrypt.dylib contains both x86_64 and arm64 architectures..."
9089 ARCHS=$( lipo -archs $MONGOCRYPT_INSTALL_PREFIX /lib/libmongocrypt.dylib)
9190 if [[ " $ARCHS " == * " x86_64" * && " $ARCHS " == * " arm64" * ]]; then
@@ -101,27 +100,18 @@ if [ "$PPA_BUILD_ONLY" ]; then
101100 exit 0;
102101fi
103102
104- if " ${DEFAULT_BUILD_ONLY:- false} " ; then
105- echo " Skipping nocrypto+sharedbson builds"
106- exit 0
107- fi
108-
109103# Build and install libmongocrypt with no native crypto.
110- run_cmake \
104+ _cmake_with_env " ${cmake_args[@]} " \
111105 -DDISABLE_NATIVE_CRYPTO=ON \
112106 -DCMAKE_INSTALL_PREFIX=" $MONGOCRYPT_INSTALL_PREFIX /nocrypto" \
113- " ${common_cmake_args[@]} "
114-
115- run_cmake --build " $build_dir " --target install --config " $LIBMONGOCRYPT_BUILD_TYPE "
116- run_cmake --build " $build_dir " --target test-mongocrypt --config " $LIBMONGOCRYPT_BUILD_TYPE "
117- run_chdir " $build_dir " run_ctest -C " $LIBMONGOCRYPT_BUILD_TYPE "
107+ -B " $BINARY_DIR " -S " $LIBMONGOCRYPT_DIR "
108+ _cmake_with_env --build " $BINARY_DIR " --target install test-mongocrypt
109+ run_chdir " $BINARY_DIR " run_ctest
118110
119111# Build and install libmongocrypt without statically linking libbson
120- run_cmake \
121- -UDISABLE_NATIVE_CRYPTO \
112+ _cmake_with_env " ${cmake_args[@]} " \
122113 -DUSE_SHARED_LIBBSON=ON \
123114 -DCMAKE_INSTALL_PREFIX=" $MONGOCRYPT_INSTALL_PREFIX /sharedbson" \
124- " ${common_cmake_args[@]} "
125-
126- run_cmake --build " $build_dir " --target install --config " $LIBMONGOCRYPT_BUILD_TYPE "
127- run_chdir " $build_dir " run_ctest -C " $LIBMONGOCRYPT_BUILD_TYPE "
115+ -B " $BINARY_DIR " -S " $LIBMONGOCRYPT_DIR "
116+ _cmake_with_env --build " $BINARY_DIR " --target install test-mongocrypt
117+ run_chdir " $BINARY_DIR " run_ctest
0 commit comments