Skip to content

Commit d73f614

Browse files
committed
CXX-3092 Enable /Gv when building test suite with MSVC
1 parent 2850a7f commit d73f614

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

cmake/FetchCatch2.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ function(fetch_catch2)
3030
string(REPLACE " -Werror" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
3131
string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
3232

33+
# Ensure consistent default calling convention with test executables.
34+
if(MSVC)
35+
string(APPEND CMAKE_CXX_FLAGS " /Gv")
36+
endif()
37+
3338
FetchContent_MakeAvailable(EP_Catch2)
3439

3540
# Avoid building unnecessary targets. Use FetchContent_Declare(EXCLUDE_FROM_ALL) in CMake 3.28 and newer.

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function(add_examples_executable source)
7575
RUNTIME_OUTPUT_DIRECTORY ${subdir}
7676
)
7777

78-
# Deliberately use `__vectorcall` by default with MSVC to catch missing `__cdecl`.
78+
# Use `__vectorcall` by default with MSVC to catch missing `__cdecl`.
7979
target_compile_options(${target_name} PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/Gv>")
8080

8181
# Keep build and run targets completely separate.

src/bsoncxx/test/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ add_executable(test_bson
3636
bson_util_itoa.cpp
3737
bson_validate.cpp
3838
bson_value.cpp
39+
catch.cpp
3940
exception_guard.cpp
4041
json.cpp
4142
oid.cpp
@@ -62,11 +63,14 @@ target_include_directories(bsoncxx_test_properties INTERFACE
6263
${CMAKE_CURRENT_SOURCE_DIR}/../..
6364
)
6465
target_compile_definitions(bsoncxx_test_properties INTERFACE ${libbson_definitions})
66+
# Use `__vectorcall` by default with MSVC to catch missing `__cdecl`.
67+
target_compile_options(bsoncxx_test_properties INTERFACE "$<$<CXX_COMPILER_ID:MSVC>:/Gv>")
6568
add_library(bsoncxx::test_properties ALIAS bsoncxx_test_properties)
6669

6770
# Avoid redundant recompilation of catch/main.cpp.
6871
add_library(bsoncxx_test_properties_with_main INTERFACE)
69-
target_link_libraries(bsoncxx_test_properties_with_main INTERFACE bsoncxx::test_properties Catch2::Catch2WithMain)
72+
target_link_libraries(bsoncxx_test_properties_with_main INTERFACE bsoncxx::test_properties)
73+
target_sources(bsoncxx_test_properties INTERFACE catch.cpp)
7074
add_library(bsoncxx::test_properties_with_main ALIAS bsoncxx_test_properties_with_main)
7175

7276
target_link_libraries (test_bson PRIVATE bsoncxx::test_properties_with_main)
@@ -158,6 +162,7 @@ set_dist_list(src_bsoncxx_test_DIST
158162
bson_util_itoa.cpp
159163
bson_validate.cpp
160164
bson_value.cpp
165+
catch.cpp
161166
catch.hh
162167
exception_guard.cpp
163168
exception_guard.hh

src/bsoncxx/test/catch.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2009-present MongoDB, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <bsoncxx/config/prelude.hpp>
16+
17+
#include <catch2/catch_session.hpp>
18+
19+
int BSONCXX_ABI_CDECL main(int argc, char* argv[]) {
20+
return Catch::Session().run(argc, argv);
21+
}

src/mongocxx/test/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,13 @@ target_include_directories(mongocxx_test_properties INTERFACE
190190
# Allow `#include <mongocxx/test/...>`.
191191
${CMAKE_CURRENT_SOURCE_DIR}/../..
192192
)
193+
# Use `__vectorcall` by default with MSVC to catch missing `__cdecl`.
194+
target_compile_options(mongocxx_test_properties INTERFACE "$<$<CXX_COMPILER_ID:MSVC>:/Gv>")
193195
add_library(mongocxx::test_properties ALIAS mongocxx_test_properties)
194196

195197
add_library(mongocxx_test_properties_with_main INTERFACE)
196-
target_link_libraries(mongocxx_test_properties_with_main INTERFACE mongocxx::test_properties Catch2::Catch2WithMain)
198+
target_link_libraries(mongocxx_test_properties_with_main INTERFACE mongocxx::test_properties)
199+
target_sources(mongocxx_test_properties_with_main INTERFACE catch.cpp)
197200
add_library(mongocxx::test_properties_with_main ALIAS mongocxx_test_properties_with_main)
198201

199202
set_property(
@@ -360,6 +363,7 @@ set_dist_list(src_mongocxx_test_DIST
360363
bulk_write.cpp
361364
catch_helpers.cpp
362365
catch_helpers.hh
366+
catch.cpp
363367
change_streams.cpp
364368
client_helpers.cpp
365369
client_helpers.hh

src/mongocxx/test/catch.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2009-present MongoDB, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <mongocxx/config/prelude.hpp>
16+
17+
#include <catch2/catch_session.hpp>
18+
19+
int MONGOCXX_ABI_CDECL main(int argc, char* argv[]) {
20+
return Catch::Session().run(argc, argv);
21+
}

0 commit comments

Comments
 (0)