From f3eab3096b26544fe2f4658fb9149982d4d7830a Mon Sep 17 00:00:00 2001 From: Jay Satiro Date: Sun, 3 Dec 2023 03:15:36 -0500 Subject: [PATCH] squashme: fix for cmake builds and openssl forks - For cmake only build dllmain.c for SHARED builds. - For DllMain do not call OPENSSL_thread_stop for OpenSSL forks. --- lib/CMakeLists.txt | 19 +++++++++++++------ lib/dllmain.c | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 982ed179f97768..39b744d8954d0e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -31,9 +31,12 @@ configure_file(curl_config.h.cmake transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") include(${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake) -list(APPEND HHEADERS - ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h - ) +# DllMain is added later for DLL builds only. +list(REMOVE_ITEM CSOURCES dllmain.c) +set_source_files_properties(dllmain.c PROPERTIES + COMPILE_FLAGS -DCURL_CMAKE_DLLMAIN) + +list(APPEND HHEADERS ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h) # The rest of the build @@ -64,9 +67,10 @@ if(ENABLE_CURLDEBUG) endif() if(CYGWIN) - # for cygwin compile dllmain.c separately since it includes windows.h, which - # shouldn't be included for other units. - set_source_files_properties(dllmain.c PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) + # For cygwin always compile dllmain.c as a separate unit since it includes + # windows.h, which shouldn't be included in other units. + set_source_files_properties(dllmain.c PROPERTIES + SKIP_UNITY_BUILD_INCLUSION ON) endif() if(BUILD_TESTING) @@ -187,6 +191,9 @@ if(BUILD_SHARED_LIBS) list(APPEND libcurl_export ${LIB_SHARED}) add_library(${LIB_SHARED} SHARED ${LIB_SOURCE}) add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED}) + if(WIN32 OR CYGWIN) + set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES dllmain.c) + endif() if(WIN32) set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES libcurl.rc) if(HIDES_CURL_PRIVATE_SYMBOLS) diff --git a/lib/dllmain.c b/lib/dllmain.c index 48d79f734741ec..6d9753e43e47ac 100644 --- a/lib/dllmain.c +++ b/lib/dllmain.c @@ -24,22 +24,37 @@ #include "curl_setup.h" -#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(CURL_STATICLIB) - -/* WARNING: Including Cygwin windows.h may define _WIN32 in old versions or - may have unexpected behavior in unity builds (where all source files are - bundled into a single unit). For that reason, this source file must be - compiled separately for Cygwin unity builds. */ +#ifdef USE_OPENSSL +#include +#endif +/* The fourth-to-last include */ #ifdef __CYGWIN__ #define WIN32_LEAN_AND_MEAN #include +#ifdef _WIN32 +#undef _WIN32 #endif - -#ifdef USE_OPENSSL -#include #endif +/* The last 3 #include files should be in this order */ +#include "curl_printf.h" +#include "curl_memory.h" +#include "memdebug.h" + +/* + * DllMain() must only be defined for Windows and Cygwin DLL builds. For most + * build systems that means CURL_STATICLIB is not defined. However, CMake + * defines CURL_STATICLIB always for Windows builds (discussion in #12408). + * CMake builds dllmain.c only for Windows and Cygwin DLL builds and defines + * CURL_CMAKE_DLLMAIN. + */ + +#if (defined(_WIN32) || defined(__CYGWIN__)) && \ + (!defined(CURL_STATICLIB) || defined(CURL_CMAKE_DLLMAIN)) + +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); + BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { (void)hinstDLL; @@ -53,7 +68,12 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: -#if defined(USE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10100000L) + MessageBoxA(NULL, "test", "", 0); +#if defined(USE_OPENSSL) && \ + !defined(OPENSSL_IS_AWSLC) && \ + !defined(OPENSSL_IS_BORINGSSL) && \ + !defined(LIBRESSL_VERSION_NUMBER) && \ + (OPENSSL_VERSION_NUMBER >= 0x10100000L) /* Call OPENSSL_thread_stop to prevent a memory leak in case OpenSSL is linked statically. https://github.com/curl/curl/issues/12327#issuecomment-1826405944 */ @@ -64,4 +84,4 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) return TRUE; } -#endif /* (_WIN32 || __CYGWIN__) && !CURL_STATICLIB */ +#endif /* DLL build */