eclipse-mosquitto / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
9.1k stars 2.4k forks source link

[QUESTION] MinGW Support #1707

Open mabuze opened 4 years ago

mabuze commented 4 years ago

since mosquitto (as of 1.6.10) does not build out-of-the-box with the mingw toolchain (currently using i686-w64-mingw32-gcc (GCC) 9.2.1 20190827 (Fedora MinGW 9.2.1-6.fc32)) i'm working on fixing these issues.

is there any interest in upstream for this?

Sumatt92 commented 4 years ago

Are you using cmake ?

mabuze commented 4 years ago

yes.

mkdir build && cd build
mingw32-cmake -DCMAKE_BUILD_TYPE=Debug ..
mingw32-make
ralight commented 4 years ago

I'm a bit snowed under with other things, so sorry for not responding earlier. Yes, fixes for mingw32 would be appreciated.

mabuze commented 4 years ago

tested with:

steps to reproduce:

git clone https://github.com/eclipse/mosquitto.git
mkdir mosquitto/build && cd mosquitto/build
mingw32-cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS=-Wno-deprecated-declarations ..
mingw32-make -k

build error 1:

-snip-
...
[ 33%] Linking C shared library libmosquitto.dll
/usr/lib/gcc/i686-w64-mingw32/9.2.1/../../../../i686-w64-mingw32/bin/ld: cannot find -lC:pthreadsPre-built.2libx86pthreadVC2.lib
...
-snip-

proposed fix 1:

the mingw toolchain comes with a pthreads library:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d326ea2b..dbe15f0d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -57,14 +60,14 @@ option(WITH_PIC "Build the static library with PIC (Position Independent Code) e
 option(WITH_THREADING "Include client library threading support?" ON)
 if (WITH_THREADING)
        add_definitions("-DWITH_THREADING")
-       if (WIN32)
+       if (WIN32 AND NOT MINGW)
                if (CMAKE_CL_64)
                        set (PTHREAD_LIBRARIES C:\\pthreads\\Pre-built.2\\lib\\x64\\pthreadVC2.lib)
                else (CMAKE_CL_64)
                        set (PTHREAD_LIBRARIES C:\\pthreads\\Pre-built.2\\lib\\x86\\pthreadVC2.lib)
                endif (CMAKE_CL_64)
                set (PTHREAD_INCLUDE_DIR C:\\pthreads\\Pre-built.2\\include)
-       else (WIN32)
+       else (WIN32 AND NOT MINGW)
                find_library(LIBPTHREAD pthread)
                if (LIBPTHREAD)
                        set (PTHREAD_LIBRARIES pthread)
@@ -72,7 +75,7 @@ if (WITH_THREADING)
                        set (PTHREAD_LIBRARIES "")
                endif()
                set (PTHREAD_INCLUDE_DIR "")
-       endif (WIN32)
+       endif (WIN32 AND NOT MINGW)
 else (WITH_THREADING)
        set (PTHREAD_LIBRARIES "")
        set (PTHREAD_INCLUDE_DIR "")

build warnings/errors 2:

-snip-
...
/home/mabuze/work/mosquitto/lib/mosquitto.c:55:9: warning: implicit declaration of function 'GetTickCount64'; did you mean 'GetTickCount'? [-Wimplicit-function-declaration]
/home/mabuze/work/mosquitto/lib/socks_mosq.c:158:22: warning: implicit declaration of function 'inet_pton'; did you mean 'inet_ntoa'? [-Wimplicit-function-declaration]
/home/mabuze/work/mosquitto/lib/tls_mosq.c:126:12: warning: implicit declaration of function 'InetPton' [-Wimplicit-function-declaration]
/home/mabuze/work/mosquitto/src/loop.c:479:13: warning: implicit declaration of function 'WSAPoll' [-Wimplicit-function-declaration]
/home/mabuze/work/mosquitto/src/net.c:743:7: warning: implicit declaration of function 'inet_ntop'; did you mean 'inet_ntoa'? [-Wimplicit-function-declaration]
/home/mabuze/work/mosquitto/lib/tls_mosq.c:126:12: warning: implicit declaration of function 'InetPton' [-Wimplicit-function-declaration]
...
/usr/lib/gcc/i686-w64-mingw32/9.2.1/../../../../i686-w64-mingw32/bin/ld: warning: resolving _GetTickCount64 by linking to _GetTickCount64@0
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups
`_GetTickCount64' referenced in section `.text' of CMakeFiles/libmosquitto.dir/objects.a(mosquitto.c.obj): defined in discarded section `.text' of /usr/i686-w64-mingw32/sys-root/mingw/l
ib/../lib/libkernel32.a(dslsucs00665.o)
/usr/lib/gcc/i686-w64-mingw32/9.2.1/../../../../i686-w64-mingw32/bin/ld: CMakeFiles/libmosquitto.dir/objects.a(socks_mosq.c.obj): in function `socks5__send':
/home/mabuze/work/mosquitto/lib/socks_mosq.c:158: undefined reference to `inet_pton'
/usr/lib/gcc/i686-w64-mingw32/9.2.1/../../../../i686-w64-mingw32/bin/ld: /home/mabuze/work/mosquitto/lib/socks_mosq.c:159: undefined reference to `inet_pton'
/usr/lib/gcc/i686-w64-mingw32/9.2.1/../../../../i686-w64-mingw32/bin/ld: CMakeFiles/libmosquitto.dir/objects.a(tls_mosq.c.obj): in function `mosquitto__verify_certificate_hostname':
/home/mabuze/work/mosquitto/lib/tls_mosq.c:126: undefined reference to `InetPton'
/usr/lib/gcc/i686-w64-mingw32/9.2.1/../../../../i686-w64-mingw32/bin/ld: /home/mabuze/work/mosquitto/lib/tls_mosq.c:127: undefined reference to `InetPton'
...
/home/mabuze/work/mosquitto/src/loop.c:69:70: warning: 'struct pollfd' declared inside parameter list will not be visible outside of this definition or declaration
   69 | static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds);
/home/mabuze/work/mosquitto/src/loop.c:163:37: error: invalid application of 'sizeof' to incomplete type 'struct pollfd'
  163 |  pollfds = mosquitto__malloc(sizeof(struct pollfd)*pollfd_max);
/home/mabuze/work/mosquitto/src/loop.c:214:30: error: invalid application of 'sizeof' to incomplete type 'struct pollfd'
  214 |   memset(pollfds, -1, sizeof(struct pollfd)*pollfd_max);
/home/mabuze/work/mosquitto/src/loop.c:218:11: error: invalid use of undefined type 'struct pollfd'
  218 |    pollfds[pollfd_index].fd = listensock[i];
/home/mabuze/work/mosquitto/src/loop.c:218:11: error: dereferencing pointer to incomplete type 'struct pollfd'
/home/mabuze/work/mosquitto/src/loop.c:219:11: error: invalid use of undefined type 'struct pollfd'
-snip-
...

proposed fix 2:

above functions/macro/struct are guarded in the respective mingw header files with

#if (_WIN32_WINNT >= 0x0600)
...
#endif /*(_WIN32_WINNT >= 0x0600)*/

aka _WIN32_WINNT_VISTA (see sdkddkver.h)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d326ea2b..dbe15f0d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,9 @@ add_definitions (-DCMAKE -DVERSION=\"${VERSION}\")
 if (WIN32)
        add_definitions("-D_CRT_SECURE_NO_WARNINGS")
        add_definitions("-D_CRT_NONSTDC_NO_DEPRECATE")
+       if (MINGW)
+              add_definitions("-D_WIN32_WINNT=_WIN32_WINNT_VISTA")
+       endif (MINGW)
 endif (WIN32)
fenwick67 commented 4 years ago

Just wanted to note that @mabuze 's proposed fixes worked for me with gcc 10.2.0 and cmake 3.15.2, and regular MinGW, thank you ❤️

EDIT: Windows 10 Pro, version 2004, OS build 19041.508