Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using mingw-std-threads in gtest_zlib #1573

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,39 @@ if(WITH_GTEST)
target_link_libraries(gtest_zlib GTest::GTest)

find_package(Threads)
if(Threads_FOUND AND NOT BASEARCH_WASM32_FOUND)
# TODO: Should regular C++11 threads be used when MinGW-w64 is built with pthreads?
if(Threads_FOUND AND NOT BASEARCH_WASM32_FOUND AND NOT MINGW)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake sets CMAKE_USE_PTHREADS_INIT if pthreads are available and developer can set CMAKE_THREAD_PREFER_PTHREAD before calling find_package(Threads) to prefer pthreads over any other thread implementation.

It's also possible to explicitly check if libpthread.a or libpthread.dll.a exists to see if MinGW is built with pthreads.

target_sources(gtest_zlib PRIVATE test_deflate_concurrency.cc)
if(UNIX AND NOT APPLE)
# On Linux, use a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590
target_link_libraries(gtest_zlib -Wl,--whole-archive -lpthread -Wl,--no-whole-archive)
endif()
target_link_libraries(gtest_zlib Threads::Threads)
elseif(WIN32)
if(NOT TARGET mingw_stdthreads)
# Allow specifying alternative mingw-std-threads repository
if(NOT DEFINED MINGW_STDTHREADS_REPOSITORY)
set(MINGW_STDTHREADS_REPOSITORY https://github.com/meganz/mingw-std-threads.git)
endif()
if(NOT DEFINED MINGW_STDTHREADS_TAG)
set(MINGW_STDTHREADS_TAG 6c2061b)
endif()

# Fetch Google test source code from official repository
FetchContent_Declare(mingw_stdthreads
GIT_REPOSITORY ${MINGW_STDTHREADS_REPOSITORY}
GIT_TAG ${MINGW_STDTHREADS_TAG})

FetchContent_GetProperties(mingw_stdthreads)
if(NOT mingw_stdthreads_POPULATED)
FetchContent_Populate(mingw_stdthreads)
add_subdirectory(${mingw_stdthreads_SOURCE_DIR} ${mingw_stdthreads_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()

target_sources(gtest_zlib PRIVATE test_deflate_concurrency.cc)
target_compile_definitions(gtest_zlib PRIVATE USE_MINGW_STDTHREAD _WIN32_WINNT=0x501)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change USE_MINGW_STDTHREAD to HAVE_MINGW_STDTHREAD?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make better sense to add -D_WIN32_WINNT=0x501 in cmake/toolchain-mingw-i686.cmake and cmake/toolchain-mingw-x86_64.cmake instead?

target_link_libraries(gtest_zlib mingw_stdthreads)
endif()

add_test(NAME gtest_zlib
Expand Down
5 changes: 5 additions & 0 deletions test/test_deflate_concurrency.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
#include <algorithm>
#include <atomic>
#include <cstring>

#ifdef USE_MINGW_STDTHREAD
#include "mingw.thread.h"
#else
#include <thread>
#endif

static uint8_t buf[8 * 1024];
static uint8_t zbuf[4 * 1024];
Expand Down
Loading