Skip to content

Commit

Permalink
squashme: fix for cmake builds and openssl forks
Browse files Browse the repository at this point in the history
- For cmake only build dllmain.c for SHARED builds.

- For DllMain do not call OPENSSL_thread_stop for OpenSSL forks.
  • Loading branch information
jay committed Dec 3, 2023
1 parent 3b2522f commit f3eab30
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
19 changes: 13 additions & 6 deletions lib/CMakeLists.txt
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 31 additions & 11 deletions lib/dllmain.c
Expand Up @@ -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 <openssl/crypto.h>
#endif

/* The fourth-to-last include */
#ifdef __CYGWIN__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef _WIN32
#undef _WIN32
#endif

#ifdef USE_OPENSSL
#include <openssl/crypto.h>
#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;
Expand All @@ -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 */
Expand All @@ -64,4 +84,4 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
return TRUE;
}

#endif /* (_WIN32 || __CYGWIN__) && !CURL_STATICLIB */
#endif /* DLL build */

0 comments on commit f3eab30

Please sign in to comment.