From 79ab922e4d777df70a22bbda8b468bec2a753add Mon Sep 17 00:00:00 2001 From: stevenfontanella Date: Tue, 30 Jun 2026 20:50:45 +0000 Subject: [PATCH] Fix gmock integration when BUILD_FUZZTEST is ON Previously, when `BUILD_FUZZTEST=ON`, the `fuzztest` dependency would download its own newer version of `googletest` (v1.14.0) via CMake `FetchContent`. The `gmock` headers bundled in Binaryen's `third_party/googletest/googlemock` were incompatible with this newer `googletest`. This caused compilation failures for tests using `gmock`. As a workaround, `source-map.cpp` was completely excluded from the test suite during fuzztest builds. This fix works because `fuzztest`'s `FetchContent` actually makes the `gmock` target available as well. By linking `binaryen-unittests` against the `gmock` target when `BUILD_FUZZTEST=ON`, CMake correctly configures the include paths to use the newly fetched `gmock` headers instead of the bundled ones, seamlessly resolving the incompatibility. - Unconditionally include `source-map.cpp` in unit tests, rather than conditionally disabling it when `BUILD_FUZZTEST` is ON. - Link against the `gmock` target for `binaryen-unittests` when `BUILD_FUZZTEST` is ON. - Add a simple gmock test in `arena.cpp` to demonstrate that it correctly compiles and links. --- test/gtest/CMakeLists.txt | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt index 8db86712bba..0953df45247 100644 --- a/test/gtest/CMakeLists.txt +++ b/test/gtest/CMakeLists.txt @@ -37,18 +37,11 @@ set(unittest_SOURCES type-builder.cpp wat-lexer.cpp validator.cpp + source-map.cpp ) if(BUILD_FUZZTEST) set(unittest_SOURCES ${unittest_SOURCES} type-domains.cpp) -else() - # source-map.cpp uses the gmock-matchers.h header, which is included with the - # "standard" upstream gtest library, but not with the one bundled into the - # fuzztest library (and the gmock included with upstream gtest seems - # incompatible with the one in fuzztest). For now we work around this by just - # excluding source-map.cpp from the fuzztest build, but if we start using - # gmock more we should figure out what the right way to hande this is. - set(unittest_SOURCES ${unittest_SOURCES} source-map.cpp) endif() # suffix_tree.cpp includes LLVM header using std::iterator (deprecated in C++17) @@ -61,6 +54,7 @@ include(GoogleTest) binaryen_add_executable(binaryen-unittests "${unittest_SOURCES}") if(BUILD_FUZZTEST) link_fuzztest(binaryen-unittests) + target_link_libraries(binaryen-unittests PRIVATE gmock) gtest_discover_tests(binaryen-unittests) else() target_link_libraries(binaryen-unittests PRIVATE gtest gtest_main)