Skip to content

Windows: Win32 threads should be used when compiling with clang #2362

@ognevny

Description

@ognevny

Problem:

currently for MinGW environment pthreads are assumed. mostly clang based on MinGW is built with Win32 threading model because of better LLVM support. winpthread is just a wrapper around Win32 threads, but such wrapping is slower anyway.

this causes a build issue when compiling aws-lc-rs based packages with clang, because aws-lc links pthread functions, but resulting binaries are compiled without linking pthread

Relevant details

AWS-LC commit: recent

currently in MSYS2 we use this patch, but this a workaround and not an actual fix:

workaround to make clang based builds work
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fcb8c928e..d283a6ed4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -399,7 +399,7 @@ if(GCC OR CLANG)
       set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
     else()
       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
-      set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
+      set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common -fms-extensions")
       set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wno-newline-eof")
     endif()
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow")
diff --git a/crypto/internal.h b/crypto/internal.h
index 4e42bb5b5..cb74fac23 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -141,7 +141,7 @@
 #endif
 
 #if defined(OPENSSL_THREADS) && \
-    (!defined(OPENSSL_WINDOWS) || defined(__MINGW32__))
+    (!defined(OPENSSL_WINDOWS) || defined(__MINGW32__) && !defined(__clang__))
 #include <pthread.h>
 #define OPENSSL_PTHREADS
 #endif
diff --git a/crypto/thread_win.c b/crypto/thread_win.c
index 9a1f8045e..0606e6db0 100644
--- a/crypto/thread_win.c
+++ b/crypto/thread_win.c
@@ -146,6 +146,7 @@ static void NTAPI thread_local_destructor(PVOID module, DWORD reason,
 // optimization from discarding the variable.
 //
 // Note, in the prefixed build, |p_thread_callback_boringssl| may be a macro.
+#ifdef _MSC_VER
 #define STRINGIFY(x) #x
 #define EXPAND_AND_STRINGIFY(x) STRINGIFY(x)
 #ifdef _WIN64
@@ -157,6 +158,7 @@ __pragma(comment(linker, "/INCLUDE:__tls_used"))
 __pragma(comment(
     linker, "/INCLUDE:_" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl)))
 #endif
+#endif
 
 // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
 // called automatically by the OS loader code (not the CRT) when the module is

how to make workaround be a fix: apply -fms-extensions compile flag only if a C compiler is Clang and we're under Windows; ungate code with #define STRINGIFY(x) and so on and make it work for clang compiler (because current code is MSVC/link.exe specific)

Build log:

simple errors of pthread_* symbols being undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions