From a0c027efe03a171e1ba871f107528f9bdf91c285 Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Thu, 12 Mar 2020 10:54:49 +0000 Subject: [PATCH] Allow static library to work on Windows and run static lib tests --- appveyor.yml | 10 +++++++++- src/MQTTAsync.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++- src/MQTTClient.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index f8f3634f..4bf7c26b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -4,8 +4,16 @@ environment: matrix: - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013 PAHO_WINDOWS_BUILD_BIT: x86 + PAHO_BUILD_STATIC: FALSE + PAHO_BUILD_SHARED: TRUE - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 PAHO_WINDOWS_BUILD_BIT: x64 + PAHO_BUILD_STATIC: FALSE + PAHO_BUILD_SHARED: TRUE + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + PAHO_WINDOWS_BUILD_BIT: x64 + PAHO_BUILD_STATIC: TRUE + PAHO_BUILD_SHARED: FALSE configuration: Debug install: @@ -32,7 +40,7 @@ build_script: if "%APPVEYOR_BUILD_WORKER_IMAGE%" == "Visual Studio 2013" call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" %PAHO_WINDOWS_BUILD_BIT% - cmake -G "NMake Makefiles" -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_DOCUMENTATION=FALSE -DPAHO_BUILD_SAMPLES=TRUE -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=TRUE .. + cmake -G "NMake Makefiles" -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_DOCUMENTATION=FALSE -DPAHO_BUILD_SAMPLES=TRUE -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=TRUE -DPAHO_BUILD_STATIC=%PAHO_BUILD_STATIC% -DPAHO_BUILD_SHARED=%PAHO_BUILD_SHARED% .. nmake diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index 094cc9b1..195f109e 100644 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -116,6 +116,8 @@ static sem_type send_sem = NULL; extern mutex_type stack_mutex; extern mutex_type heap_mutex; extern mutex_type log_mutex; + +/* BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) @@ -137,7 +139,7 @@ BOOL APIENTRY DllMain(HANDLE hModule, break; } return TRUE; -} +}*/ void MQTTAsync_init(void) { @@ -162,6 +164,48 @@ void MQTTAsync_init(void) } } +// Global variable for one-time initialization structure +INIT_ONCE g_InitOnce = INIT_ONCE_STATIC_INIT; // Static initialization + +// Initialization callback function +BOOL CALLBACK InitHandleFunction ( + PINIT_ONCE InitOnce, + PVOID Parameter, + PVOID *lpContext); + +// Returns a handle to an event object that is created only once +HANDLE OpenEventHandleSync() +{ + PVOID lpContext; + BOOL bStatus; + + // Execute the initialization callback function + bStatus = InitOnceExecuteOnce(&g_InitOnce, // One-time initialization structure + InitHandleFunction, // Pointer to initialization callback function + NULL, // Optional parameter to callback function (not used) + &lpContext); // Receives pointer to event object stored in g_InitOnce + + // InitOnceExecuteOnce function succeeded. Return event object. + if (bStatus) + { + return (HANDLE)lpContext; + } + else + { + return (INVALID_HANDLE_VALUE); + } +} + +// Initialization callback function that creates the event object +BOOL CALLBACK InitHandleFunction ( + PINIT_ONCE InitOnce, // Pointer to one-time initialization structure + PVOID Parameter, // Optional parameter passed by InitOnceExecuteOnce + PVOID *lpContext) // Receives pointer to event object +{ + MQTTAsync_init(); + return TRUE; +} + #else static pthread_mutex_t mqttasync_mutex_store = PTHREAD_MUTEX_INITIALIZER; @@ -562,6 +606,9 @@ int MQTTAsync_createWithOptions(MQTTAsync* handle, const char* serverURI, const MQTTAsyncs *m = NULL; FUNC_ENTRY; +#if defined(_WIN32) || defined(_WIN64) + OpenEventHandleSync(); +#endif MQTTAsync_lock_mutex(mqttasync_mutex); if (serverURI == NULL || clientId == NULL) diff --git a/src/MQTTClient.c b/src/MQTTClient.c index 44f4b8a2..0134b225 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -111,6 +111,8 @@ static mutex_type connect_mutex = NULL; extern mutex_type stack_mutex; extern mutex_type heap_mutex; extern mutex_type log_mutex; + +/* BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) @@ -132,7 +134,7 @@ BOOL APIENTRY DllMain(HANDLE hModule, break; } return TRUE; -} +}*/ void MQTTClient_init(void) { @@ -149,6 +151,49 @@ void MQTTClient_init(void) } } +// Global variable for one-time initialization structure +INIT_ONCE g_InitOnce = INIT_ONCE_STATIC_INIT; // Static initialization + +// Initialization callback function +BOOL CALLBACK InitHandleFunction ( + PINIT_ONCE InitOnce, + PVOID Parameter, + PVOID *lpContext); + +// Returns a handle to an event object that is created only once +HANDLE OpenEventHandleSync() +{ + PVOID lpContext; + BOOL bStatus; + + // Execute the initialization callback function + bStatus = InitOnceExecuteOnce(&g_InitOnce, // One-time initialization structure + InitHandleFunction, // Pointer to initialization callback function + NULL, // Optional parameter to callback function (not used) + &lpContext); // Receives pointer to event object stored in g_InitOnce + + // InitOnceExecuteOnce function succeeded. Return event object. + if (bStatus) + { + return (HANDLE)lpContext; + } + else + { + return (INVALID_HANDLE_VALUE); + } +} + +// Initialization callback function that creates the event object +BOOL CALLBACK InitHandleFunction ( + PINIT_ONCE InitOnce, // Pointer to one-time initialization structure + PVOID Parameter, // Optional parameter passed by InitOnceExecuteOnce + PVOID *lpContext) // Receives pointer to event object +{ + MQTTClient_init(); + return TRUE; +} + + #else static pthread_mutex_t mqttclient_mutex_store = PTHREAD_MUTEX_INITIALIZER; static mutex_type mqttclient_mutex = &mqttclient_mutex_store; @@ -352,6 +397,7 @@ int MQTTClient_createWithOptions(MQTTClient* handle, const char* serverURI, cons MQTTClients *m = NULL; FUNC_ENTRY; + OpenEventHandleSync(); rc = Thread_lock_mutex(mqttclient_mutex); if (serverURI == NULL || clientId == NULL)