Skip to content

Commit 790320d

Browse files
[MONGOCRYPT-874] Refactor build scripts to use more standard environment variable controls for CMake (#1110)
* Drop irrelevant complications since we use Ninja on Windows * Use OSX architectures env var instead * Set the CMake generator by using an environment variable * More robust env handling in build_all.sh * Use CC and CXX to set compilers * Drop LIBMONGOCRYPT_EXTRA_CMAKE_FLAGS * Drop ADDITIONAL_CMAKE_FLAGS * Misc cleanup, remove DEFAULT_BUILD_ONLY * Don't set exception handling flags on Windows * Set CMake env for controlling parallelism, not Makeflags
1 parent 150ed16 commit 790320d

7 files changed

Lines changed: 105 additions & 134 deletions

File tree

.evergreen/build_all.sh

Lines changed: 59 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,90 @@
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

76
echo "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
2925
export 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

3146
if [ "$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)
3950
fi
4051

4152
for 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
4657
done
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"
7064
fi
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

7778
if [ "$CONFIGURE_ONLY" ]; then
7879
echo "Only running cmake";
7980
exit 0;
8081
fi
8182
echo "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;
102101
fi
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

.evergreen/config.yml

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ functions:
106106
export LSAN_OPTIONS="suppressions=$LIBMONGOCRYPT_DIR/.lsan-suppressions"
107107
export VS_VERSION=${vs_version|}
108108
export VS_TARGET_ARCH=${vs_target_arch|amd64}
109-
export USE_NINJA=${use_ninja|ON}
109+
export CMAKE_GENERATOR=${CMAKE_GENERATOR|Ninja}
110110
env ${compile_env|} \
111111
bash "$EVG_DIR/env-run.sh" \
112112
bash "$EVG_DIR/build_all.sh"
@@ -151,7 +151,7 @@ functions:
151151
- command: "shell.exec"
152152
params:
153153
script: |-
154-
if test "$OS_NAME" != "windows"; then export USE_NINJA=${use_ninja|ON}; fi
154+
if test "$OS_NAME" != "windows"; then export CMAKE_GENERATOR=${CMAKE_GENERATOR|Ninja}; fi
155155
env ${compile_env|} CONFIGURE_ONLY=ON ${clang_env|CC=clang CXX=clang++} \
156156
bash libmongocrypt/.evergreen/build_all.sh
157157
./libmongocrypt/.evergreen/clang-tidy.sh
@@ -483,7 +483,9 @@ tasks:
483483
- func: "fetch source"
484484
- func: "build and test"
485485
vars:
486-
compile_env: ${compile_env|} LIBMONGOCRYPT_EXTRA_CMAKE_FLAGS="-DUSE_SHARED_LIBBSON=ON"
486+
compile_env: >-
487+
${compile_env|}
488+
USE_SHARED_LIBBSON=true
487489
488490
- name: build-and-test-asan
489491
commands:
@@ -493,7 +495,7 @@ tasks:
493495
# Add detect_odr_violation=0 to ASAN_OPTIONS to ignore odr-violation in IntelDFP symbol: __dpml_bid_globals_table
494496
compile_env: >-
495497
${compile_env|}
496-
LIBMONGOCRYPT_EXTRA_CFLAGS="-fsanitize=address -pthread"
498+
LIBMONGOCRYPT_COMPILE_FLAGS="-fsanitize=address -pthread"
497499
ASAN_OPTIONS="detect_leaks=1 detect_odr_violation=0"
498500
499501
- name: build-and-test-ubsan
@@ -503,17 +505,19 @@ tasks:
503505
vars:
504506
compile_env: >-
505507
${compile_env|}
506-
LIBMONGOCRYPT_EXTRA_CFLAGS="-fsanitize=undefined -fno-omit-frame-pointer"
508+
LIBMONGOCRYPT_COMPILE_FLAGS="-fsanitize=undefined -fno-omit-frame-pointer"
507509
UBSAN_OPTIONS="halt_on_error=1,print_stacktrace=1"
508-
510+
509511
510512
- name: build-and-test-asan-mac
511513
commands:
512514
- func: "fetch source"
513515
- func: "build and test"
514516
# Exclude leak detection. clang on macos-11-amd64 reports: "detect_leaks is not supported on this platform"
515517
vars:
516-
compile_env: ${compile_env|} LIBMONGOCRYPT_EXTRA_CFLAGS="-fsanitize=address"
518+
compile_env: >-
519+
${compile_env|}
520+
LIBMONGOCRYPT_COMPILE_FLAGS="-fsanitize=address"
517521
518522
- name: test-python
519523
depends_on:
@@ -1354,7 +1358,9 @@ buildvariants:
13541358
display_name: "RHEL 6.2 64-bit"
13551359
run_on: rhel62-small
13561360
expansions:
1357-
compile_env: CFLAGS="-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS" ADDITIONAL_CMAKE_FLAGS="-DENABLE_TESTS=OFF -DBUILD_TESTING=OFF"
1361+
compile_env: >-
1362+
LIBMONGOCRYPT_COMPILE_FLAGS="-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS"
1363+
BUILD_TESTING=OFF
13581364
tasks:
13591365
- build-and-test-and-upload
13601366
- build-and-test-shared-bson
@@ -1524,7 +1530,9 @@ buildvariants:
15241530
display_name: "Ubuntu 18.04 64-bit clang7"
15251531
run_on: ubuntu1804-test
15261532
expansions:
1527-
compile_env: LIBMONGOCRYPT_EXTRA_CMAKE_FLAGS="-DCMAKE_C_COMPILER=/opt/mongodbtoolchain/v3/bin/clang -DCMAKE_CXX_COMPILER=/opt/mongodbtoolchain/v3/bin/clang++"
1533+
compile_env: >-
1534+
CC=/opt/mongodbtoolchain/v3/bin/clang
1535+
CXX=/opt/mongodbtoolchain/v3/bin/clang++
15281536
tasks:
15291537
- build-and-test-and-upload
15301538
- name: ubuntu1804-arm64
@@ -1642,9 +1650,11 @@ buildvariants:
16421650
display_name: macOS m1 (Apple LLVM)
16431651
run_on: macos-1100-arm64
16441652
expansions:
1645-
compile_env: MACOS_UNIVERSAL=ON CMAKE=/opt/homebrew/bin/cmake
1653+
compile_env: >-
1654+
CMAKE_OSX_ARCHITECTURES="arm64;x86_64"
1655+
CMAKE=/opt/homebrew/bin/cmake
16461656
# Disable Ninja to work around error "Bad CPU type in executable"
1647-
use_ninja: OFF
1657+
CMAKE_GENERATOR: ''
16481658
tasks:
16491659
- build-and-test-and-upload
16501660
- name: test-python

.evergreen/ensure-ninja.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ _build_ninja() {
6969
tar -x -f "$src_tgz" -C "$extract_dir" --strip-components=1
7070

7171
log "Building Ninja from source"
72-
run_chdir "$build_out_dir" run_python "$extract_dir/configure.py" --bootstrap
72+
# Clear any CFLAGS or CXXFLAGS values from the parent environment, as Ninja doesn't handle them
73+
# correctly.
74+
CFLAGS="" CXXFLAGS="" run_chdir "$build_out_dir" run_python "$extract_dir/configure.py" --bootstrap
7375
test -f "$expect_exe" || fail "Bootstrap did not generate the expected executable [$expect_exe]"
7476

7577
# Recursive invocation will find our build and copy it

.evergreen/linker-tests.sh

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,19 @@ echo "Make libbson1 ..."
2929
run_chdir "$linker_tests_root" bash "$EVG_DIR/prep_c_driver_source.sh"
3030
MONGOC_DIR="$linker_tests_root/mongo-c-driver"
3131

32-
if test "$OS_NAME" = "windows" && is_false WINDOWS_32BIT && is_false USE_NINJA; then
33-
# These options are only needed for VS CMake generators to force it to
34-
# generate a 64-bit build. Default is 32-bit. Ninja inherits settings
35-
# from the build environment variables.
36-
ADDITIONAL_CMAKE_FLAGS="-Thost=x64 -A x64"
37-
fi
38-
39-
if [ "${MACOS_UNIVERSAL-}" = "ON" ]; then
40-
ADDITIONAL_CMAKE_FLAGS="$ADDITIONAL_CMAKE_FLAGS -DCMAKE_OSX_ARCHITECTURES='arm64;x86_64'"
41-
fi
42-
4332
# Disable extra alignment in libbson and libmongocrypt to ensure agreement.
4433
# libmongocrypt disables by default, but may enable if a system install of libbson is detected with extra alignment.
4534
common_cmake_args=(
46-
$ADDITIONAL_CMAKE_FLAGS
4735
-DENABLE_EXTRA_ALIGNMENT=OFF
4836
-DCMAKE_BUILD_TYPE=RelWithDebInfo
37+
# Toggle building of tests
38+
-D BUILD_TESTING="${BUILD_TESTING-TRUE}"
4939
)
5040

51-
if is_true USE_NINJA; then
41+
if test "${CMAKE_GENERATOR-}" = Ninja; then
5242
export NINJA_EXE
5343
: "${NINJA_EXE:="$linker_tests_root/ninja$EXE_SUFFIX"}"
54-
common_cmake_args+=(
55-
-GNinja
56-
-DCMAKE_MAKE_PROGRAM="$NINJA_EXE"
57-
)
44+
common_cmake_args+=(-DCMAKE_MAKE_PROGRAM="$NINJA_EXE")
5845
bash "$EVG_DIR/ensure-ninja.sh"
5946
fi
6047

@@ -111,11 +98,7 @@ run_cmake \
11198
run_cmake --build "$BUILD_DIR" --target app --config RelWithDebInfo
11299

113100
export PATH="$PATH:$BSON1_INSTALL_PATH/bin:$LMC_INSTALL_PATH/bin"
114-
if is_true IS_MULTICONF; then
115-
APP_CMD="$BUILD_DIR/RelWithDebInfo/app.exe"
116-
else
117-
APP_CMD="$BUILD_DIR/app"
118-
fi
101+
APP_CMD="$BUILD_DIR/app"
119102

120103
check_output () {
121104
output="$($APP_CMD)"

0 commit comments

Comments
 (0)