From 964bd6948111d2058f90004afcc36b294223bb24 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 30 Mar 2023 13:18:48 +0100 Subject: [PATCH] MinGW: Initial compile environment support --- .github/workflows/main.yml | 16 ++++++++++++ BUILDING | 45 +++++++++++++++++++++++++++++++- CMakeLists.txt | 20 +++++++++----- configure.ac | 8 +++++- examples/coap-client.c | 7 ++--- examples/coap-server.c | 7 ++--- examples/getopt.c | 3 +-- examples/oscore-interop-server.c | 2 +- include/coap3/libcoap.h | 10 ++++++- man/examples-code-check.c | 10 +++++++ scripts/mingw-cmake.sh | 6 +++++ src/coap_address.c | 16 ++++++++++++ src/coap_debug.c | 4 +-- src/coap_io.c | 24 ++++++++++------- src/coap_mem.c | 4 +-- src/coap_time.c | 4 +-- src/coap_ws.c | 8 +++--- 17 files changed, 156 insertions(+), 38 deletions(-) create mode 100755 scripts/mingw-cmake.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8d51c28e..710f63bc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -156,6 +156,22 @@ jobs: run: | cd build_test cmake --build . + mingw-cmake: + runs-on: windows-latest + defaults: + run: + shell: msys2 {0} + steps: + - uses: actions/checkout@v3 + - uses: msys2/setup-msys2@v2 + with: + msystem: UCRT64 + update: true + install: git mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-openssl + - name: MinGW build + run: | + echo 'Running in MSYS2!' + scripts/mingw-cmake.sh additional-tests: runs-on: ubuntu-latest diff --git a/BUILDING b/BUILDING index 6c7f087c..ce458e20 100644 --- a/BUILDING +++ b/BUILDING @@ -56,7 +56,7 @@ General Building with cmake for linux/windows/macos/android (not for LwIP or Con Note: For Windows, this is supported by Visual Studio Code with CMake extension Note: You must use cmake version >=3.10. - Note: you can use cmake's find package after installation: find_package(libcoap-2 REQUIRED), + Note: you can use cmake's find package after installation: find_package(libcoap-3 REQUIRED), and target_link_libraries(myTarget PRIVATE libcoap::coap-2) Note: Shared Library support is not currently available for Windows. @@ -155,3 +155,46 @@ is installed so that the include, lib etc. directories are correctly set up. Note: Make sure that you include a trailing \ in the variable definitions. Alternatively you can build everything in Visual Studio with CMake. + +MinGW +===== + +As there are many ways to install MinGW, depending on the different +installed packages, random failures can occur (usually because of conflicts +with cygwin based packages). Below is a way known to work with libcoap +on a Windows host. + +Remove any old copy of MSYS2 using Windows program remove. + +Download https://repo.msys2.org/distrib/x86_64/msys2-x86_64-20230318.exe and +run the executable, accepting the defaults. + +In a UCRT64 window, add in the following packages + + pacman -S git + pacman -S vim + pacman -S mingw-w64-ucrt-x86_64-cmake + pacman -S mingw-w64-ucrt-x86_64-gcc + pacman -S mingw-w64-ucrt-x86_64-openssl + +Alternatively, in a MINGW64 window, add in the following packages + + pacman -S git + pacman -S vim + pacman -S mingw-w64-x86_64-cmake + pacman -S mingw-w64-x86_64-gcc + pacman -S mingw-w64-x86_64-openssl + +Then clone a copy of the github libcoap repository in a UCRT64 or MINGW64 window + + git clone https://github.com/obgm/libcoap.git + cd libcoap + +Then build the libcoap library and example executables (which will be in the +build directory) + + cmake -E remove_directory build + cmake -E make_directory build + cd build + cmake .. -DENABLE_DOCS=OFF -DDTLS_BACKEND=openssl + cmake --build . diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a0f8917..d0208748 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ option( add_library(${COAP_LIBRARY_NAME}) set_property(TARGET ${COAP_LIBRARY_NAME} PROPERTY C_STANDARD 99) -set(CMAKE_POLICY_DEFAULT_CMP0115 OLD) # Supresses libcoap configuration warning +cmake_policy(SET CMP0115 OLD) # Supresses libcoap configuration warning # # options to tweak the library @@ -151,6 +151,9 @@ include(CheckTypeSize) include(TestBigEndian) # check for headers +check_include_file(assert.h HAVE_ASSERT_H) +check_include_file(string.h HAVE_STRING_H) +check_include_file(strings.h HAVE_STRINGS_H) check_include_file(byteswap.h HAVE_BYTESWAP_H) check_include_file(inttypes.h HAVE_INTTYPES_H) check_include_file(errno.h HAVE_ERRNO_H) @@ -197,8 +200,9 @@ check_function_exists(random HAVE_RANDOM) check_function_exists(if_nametoindex HAVE_IF_NAMETOINDEX) # check for symbols -if(WIN32) +if(WIN32 AND NOT MINGW) set(HAVE_STRUCT_CMSGHDR 1) + message(STATUS "setting HAVE_STRUCT_CMSGHDR") else() check_symbol_exists( CMSG_FIRSTHDR @@ -235,8 +239,8 @@ else() endif() if(${WITH_EPOLL} - AND ${HAVE_EPOLL_H} - AND ${HAVE_TIMERFD_H}) + AND HAVE_EPOLL_H + AND HAVE_TIMERFD_H) set(COAP_EPOLL_SUPPORT "1") message(STATUS "compiling with epoll support") else() @@ -482,6 +486,7 @@ message(STATUS "BUILD_SHARED_LIBS:...............${BUILD_SHARED_LIBS}") message(STATUS "CMAKE_BUILD_TYPE:................${CMAKE_BUILD_TYPE}") message(STATUS "CMAKE_SYSTEM_PROCESSOR:..........${CMAKE_SYSTEM_PROCESSOR}") message(STATUS "MAX_LOGGING_LEVEL:...............${MAX_LOGGING_LEVEL}") +message(STATUS "CMAKE_HOST_SYSTEM_NAME:..........${CMAKE_HOST_SYSTEM_NAME}") message(STATUS "WARNING_TO_ERROR:................${WARNING_TO_ERROR}") set(top_srcdir "${CMAKE_CURRENT_LIST_DIR}") @@ -588,7 +593,8 @@ target_link_libraries( $<$:tinydtls> $<$:${MBEDTLS_LIBRARY}> $<$:${MBEDX509_LIBRARY}> - $<$:${MBEDCRYPTO_LIBRARY}>) + $<$:${MBEDCRYPTO_LIBRARY}> + $<$:ws2_32>) target_compile_options( ${COAP_LIBRARY_NAME} @@ -688,7 +694,7 @@ if(ENABLE_EXAMPLES) target_link_libraries(coap-server PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME}) - if(NOT WIN32) + if(NOT WIN32 AND NOT MINGW) add_executable(etsi_iot_01 ${CMAKE_CURRENT_LIST_DIR}/examples/etsi_iot_01.c) target_link_libraries(etsi_iot_01 PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME}) @@ -800,7 +806,7 @@ if(ENABLE_EXAMPLES) TARGETS coap-server coap-client coap-rd DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT dev) - if(NOT WIN32) + if(NOT WIN32 AND NOT MINGW) install( TARGETS etsi_iot_01 tiny oscore-interop-server DESTINATION ${CMAKE_INSTALL_BINDIR} diff --git a/configure.ac b/configure.ac index 655bcc1f..88f68f3b 100644 --- a/configure.ac +++ b/configure.ac @@ -836,7 +836,7 @@ fi # Checks for header files. AC_CHECK_HEADERS([assert.h arpa/inet.h limits.h netdb.h netinet/in.h \ - pthread.h errno.h \ + pthread.h errno.h winsock2.h ws2tcpip.h \ stdlib.h string.h strings.h sys/socket.h sys/time.h \ time.h unistd.h sys/unistd.h sys/ioctl.h net/if.h]) @@ -990,6 +990,12 @@ case $host in ADDITIONAL_CFLAGS="-D_GNU_SOURCE" ;; + *mingw*) + AC_MSG_RESULT([MinGW]) + ADDITIONAL_CFLAGS="-D_GNU_SOURCE -Wno-format -Wno-format-security" + LIBS="${LIBS} -lws2_32" + ;; + *) AC_MSG_WARN([==> Currently unsupported operating system '${host}' !]) AC_MSG_ERROR([==> If you can provide patches to support your operating system please write to 'libcoap-developers@lists.sourceforge.net'.]) diff --git a/examples/coap-client.c b/examples/coap-client.c index 4419a7d1..d87fac81 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -27,7 +27,8 @@ #if !defined(S_ISDIR) #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) #endif -static char * +char * strndup(const char* s1, size_t n); +char * strndup(const char* s1, size_t n) { char* copy = (char*)malloc(n + 1); if (copy) { @@ -35,7 +36,7 @@ strndup(const char* s1, size_t n) { copy[n] = 0; } return copy; -}; +} #else #include #include @@ -1555,7 +1556,7 @@ get_session(coap_context_t *ctx, /* Need a unique address */ snprintf(buf, COAP_UNIX_PATH_MAX, - "/tmp/coap-client.%d", getpid()); + "/tmp/coap-client.%d", (int)getpid()); coap_address_set_unix_domain(&bind_addr, (const uint8_t *)buf, strlen(buf)); remove(bind_addr.addr.cun.sun_path); } diff --git a/examples/coap-server.c b/examples/coap-server.c index 7b758dd5..8638e12e 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -30,7 +30,8 @@ #ifndef R_OK #define R_OK 4 #endif -static char * +char * strndup(const char* s1, size_t n); +char * strndup(const char* s1, size_t n) { char* copy = (char*)malloc(n + 1); if (copy) { @@ -38,7 +39,7 @@ strndup(const char* s1, size_t n) { copy[n] = 0; } return copy; -}; +} #include #define access _access #define fileno _fileno @@ -3160,7 +3161,7 @@ finish: free(proxy_list); proxy_list = NULL; proxy_list_count = 0; -#ifdef _WIN32 +#if defined(_WIN32) && !defined(__MINGW32__) #pragma warning( disable : 4090 ) #endif coap_free(proxy_host_name_list); diff --git a/examples/getopt.c b/examples/getopt.c index 8a266c2b..b3865d63 100644 --- a/examples/getopt.c +++ b/examples/getopt.c @@ -15,13 +15,12 @@ #include #include -static int opterr = 1; static int optind = 1; static int optopt; static char *optarg; static int -getopt(int argc, char *argv[], char *opts) +getopt(int argc, char *argv[], const char *opts) { static int sp = 1; int c; diff --git a/examples/oscore-interop-server.c b/examples/oscore-interop-server.c index dfa544f0..2fb900c8 100644 --- a/examples/oscore-interop-server.c +++ b/examples/oscore-interop-server.c @@ -40,7 +40,7 @@ strndup(const char* s1, size_t n) { copy[n] = 0; } return copy; -}; +} #include #define access _access #define fileno _fileno diff --git a/include/coap3/libcoap.h b/include/coap3/libcoap.h index b2a4f520..bf9ff531 100644 --- a/include/coap3/libcoap.h +++ b/include/coap3/libcoap.h @@ -26,10 +26,18 @@ * The CONTIKI variable is within the Contiki-NG build environment! */ #if defined(_WIN32) -#pragma comment(lib,"Ws2_32.lib") #include +#if !defined(__MINGW32__) +#pragma comment(lib,"Ws2_32.lib") +#ifndef _SSIZE_T_DECLARED typedef SSIZE_T ssize_t; +#define _SSIZE_T_DECLARED +#endif +#ifndef _IN_PORT_T_DECLARED typedef USHORT in_port_t; +#define _IN_PORT_T_DECLARED +#endif +#endif /* !defined(__MINGW32__) */ #elif !defined (CONTIKI) && !defined (WITH_LWIP) #include #include diff --git a/man/examples-code-check.c b/man/examples-code-check.c index 4e9d3880..ac6f2ae0 100644 --- a/man/examples-code-check.c +++ b/man/examples-code-check.c @@ -22,6 +22,9 @@ #ifdef _WIN32 #define GCC_OPTIONS "-I../include" +#ifndef WEXITSTATUS +#define WEXITSTATUS(v) (v & 0xff) +#endif /* WEXITSTATUS */ #else /* ! _WIN32 */ #define GCC_OPTIONS "\ -I../include \ @@ -154,10 +157,17 @@ int main(int argc, char* argv[]) fprintf(stderr, "chdir: %s: %s (%d)\n", argv[1], strerror(errno), errno); exit(1); } +#if defined(WIN32) || defined(__MINGW32__) + if (mkdir("tmp") == -1 && errno != EEXIST) { + fprintf(stderr, "mkdir: %s: %s (%d)\n", "tmp", strerror(errno), errno); + exit(1); + } +#else /* ! WIN32 && ! __MINGW32__ */ if (mkdir("tmp", 0777) == -1 && errno != EEXIST) { fprintf(stderr, "mkdir: %s: %s (%d)\n", "tmp", strerror(errno), errno); exit(1); } +#endif /* ! WIN32 && ! __MINGW32__ */ while ((pdir_ent = readdir (pdir)) != NULL) { if (!strncmp(pdir_ent->d_name, "coap_", sizeof ("coap_")-1) && diff --git a/scripts/mingw-cmake.sh b/scripts/mingw-cmake.sh new file mode 100755 index 00000000..e2e70b01 --- /dev/null +++ b/scripts/mingw-cmake.sh @@ -0,0 +1,6 @@ +cmake -E remove_directory build +cmake -E make_directory build +cd build +cmake .. -DENABLE_DOCS=OFF -DDTLS_BACKEND=openssl -DWARNING_TO_ERROR=ON +cmake --build . +cmake --build . -- install diff --git a/src/coap_address.c b/src/coap_address.c index a67b6c55..d8a9ffcf 100644 --- a/src/coap_address.c +++ b/src/coap_address.c @@ -241,50 +241,66 @@ coap_resolve_address_info(const coap_str_const_t *server, if (scheme_hint_bits & (1 << scheme)) { switch (scheme) { case COAP_URI_SCHEME_COAP: +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_DGRAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_COAPS: if (!coap_dtls_is_supported()) continue; +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_DGRAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_COAP_TCP: if (!coap_tcp_is_supported()) continue; +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_COAPS_TCP: if (!coap_tls_is_supported()) continue; +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_HTTP: if (!coap_tcp_is_supported()) continue; +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_HTTPS: if (!coap_tls_is_supported()) continue; +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_COAP_WS: if (!coap_ws_is_supported()) continue; +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_COAPS_WS: if (!coap_wss_is_supported()) continue; +#if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; +#endif /* ! __MINGW32__ */ break; case COAP_URI_SCHEME_LAST: default: diff --git a/src/coap_debug.c b/src/coap_debug.c index e53b3371..45380f14 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -145,7 +145,7 @@ print_timestamp(char *s, size_t len, coap_tick_t t) { #endif /* HAVE_TIME_H */ -#ifndef HAVE_STRNLEN +#if !defined(HAVE_STRNLEN) && !defined(__MINGW32__) /** * A length-safe strlen() fake. * @@ -161,7 +161,7 @@ strnlen(const char *s, size_t maxlen) { ++n; return n; } -#endif /* HAVE_STRNLEN */ +#endif /* HAVE_STRNLEN && !__MINGW32__ */ static size_t print_readable( const uint8_t *data, size_t len, diff --git a/src/coap_io.c b/src/coap_io.c index 9ec3c793..9de873b5 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -716,6 +716,7 @@ coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) { /* define struct in6_pktinfo and struct in_pktinfo if not available FIXME: check with configure */ +#if !defined(__MINGW32__) struct in6_pktinfo { struct in6_addr ipi6_addr; /* src/dst IPv6 address */ unsigned int ipi6_ifindex; /* send/recv interface index */ @@ -726,6 +727,7 @@ struct in_pktinfo { struct in_addr ipi_spec_dst; struct in_addr ipi_addr; }; +#endif /* ! __MINGW32__ */ #endif #endif /* ! WITH_LWIP */ @@ -736,7 +738,9 @@ struct in_pktinfo { #if defined(_WIN32) #include +#if !defined(__MINGW32__) static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL; +#endif /* ! __MINGW32__ */ /* Map struct WSABUF fields to their posix counterpart */ #define msghdr _WSAMSG #define msg_name name @@ -752,7 +756,9 @@ static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL; #undef CMSG_DATA #define CMSG_DATA WSA_CMSG_DATA #define ipi_spec_dst ipi_addr +#if !defined(__MINGW32__) #pragma warning( disable : 4116 ) +#endif /* ! __MINGW32__ */ #else #define iov_len_t size_t #endif @@ -780,10 +786,10 @@ coap_socket_send(coap_socket_t *sock, const coap_session_t *session, const uint8 bytes_written = send(sock->fd, data, datalen, 0); #endif } else { -#ifdef _WIN32 +#if defined(_WIN32) && !defined(__MINGW32__) DWORD dwNumberOfBytesSent = 0; int r; -#endif +#endif /* _WIN32 && !__MINGW32__ */ #ifdef HAVE_STRUCT_CMSGHDR /* a buffer large enough to hold all packet info types, ipv6 is the largest */ char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; @@ -906,21 +912,21 @@ coap_socket_send(coap_socket_t *sock, const coap_session_t *session, const uint8 } #endif /* HAVE_STRUCT_CMSGHDR */ -#ifdef _WIN32 +#if defined(_WIN32) && !defined(__MINGW32__) r = WSASendMsg(sock->fd, &mhdr, 0 /*dwFlags*/, &dwNumberOfBytesSent, NULL /*lpOverlapped*/, NULL /*lpCompletionRoutine*/); if (r == 0) bytes_written = (ssize_t)dwNumberOfBytesSent; else bytes_written = -1; -#else +#else /* !_WIN32 || __MINGW32__ */ #ifdef HAVE_STRUCT_CMSGHDR bytes_written = sendmsg(sock->fd, &mhdr, 0); #else /* ! HAVE_STRUCT_CMSGHDR */ - bytes_written = sendto(sock->fd, data, datalen, 0, + bytes_written = sendto(sock->fd, (const void*)data, datalen, 0, &session->addr_info.remote.addr.sa, session->addr_info.remote.size); #endif /* ! HAVE_STRUCT_CMSGHDR */ -#endif +#endif /* !_WIN32 || __MINGW32__ */ } if (bytes_written < 0) @@ -989,10 +995,10 @@ coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) { packet->length = (size_t)len; } } else { -#if defined(_WIN32) +#if defined(_WIN32) && !defined(__MINGW32__) DWORD dwNumberOfBytesRecvd = 0; int r; -#endif +#endif /* _WIN32 && !__MINGW32__ */ #ifdef HAVE_STRUCT_CMSGHDR /* a buffer large enough to hold all packet info types, ipv6 is the largest */ char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; @@ -1037,7 +1043,7 @@ coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) { #endif #else /* ! HAVE_STRUCT_CMSGHDR */ - len = recvfrom(sock->fd, packet->payload, COAP_RXBUFFER_SIZE, 0, + len = recvfrom(sock->fd, (void *)packet->payload, COAP_RXBUFFER_SIZE, 0, &packet->addr_info.remote.addr.sa, &packet->addr_info.remote.size); #endif /* ! HAVE_STRUCT_CMSGHDR */ diff --git a/src/coap_mem.c b/src/coap_mem.c index 9d5a2295..dbb97195 100644 --- a/src/coap_mem.c +++ b/src/coap_mem.c @@ -355,7 +355,7 @@ coap_realloc_type(coap_memory_tag_t type, void *p, size_t size) { } #else /* ! RIOT_VERSION */ -#ifdef HAVE_MALLOC +#if defined(HAVE_MALLOC) || defined(__MINGW32__) #include void @@ -380,7 +380,7 @@ coap_free_type(coap_memory_tag_t type, void *p) { free(p); } -#else /* ! HAVE_MALLOC */ +#else /* ! HAVE_MALLOC && !__MINGW32__ */ #ifdef WITH_CONTIKI #include "lib/heapmem.h" diff --git a/src/coap_time.c b/src/coap_time.c index b798ea4a..241f7e05 100644 --- a/src/coap_time.c +++ b/src/coap_time.c @@ -37,7 +37,7 @@ static coap_tick_t coap_clock_offset = 0; #define COAP_CLOCK CLOCK_REALTIME #endif -#ifdef HAVE_WINSOCK2_H +#if defined(HAVE_WINSOCK2_H) && !defined(__MINGW32__) static int gettimeofday(struct timeval *tp, TIME_ZONE_INFORMATION *tzp) { (void)tzp; @@ -56,7 +56,7 @@ gettimeofday(struct timeval *tp, TIME_ZONE_INFORMATION *tzp) { tp->tv_usec = (long)(tUsSinceUnicEpoch % 1000000); return 0; } -#endif +#endif /* HAVE_WINSOCK2_H && !__MINGW32__ */ void coap_clock_init(void) { diff --git a/src/coap_ws.c b/src/coap_ws.c index de2cad71..2881f088 100644 --- a/src/coap_ws.c +++ b/src/coap_ws.c @@ -234,10 +234,10 @@ coap_ws_write(coap_session_t *session, const uint8_t *data, size_t datalen) { hdr_len += 2; } else { ws_header[1] = 127; - ws_header[2] = (datalen >> 56) & 0xff; - ws_header[3] = (datalen >> 48) & 0xff; - ws_header[4] = (datalen >> 40) & 0xff; - ws_header[5] = (datalen >> 32) & 0xff; + ws_header[2] = ((uint64_t)datalen >> 56) & 0xff; + ws_header[3] = ((uint64_t)datalen >> 48) & 0xff; + ws_header[4] = ((uint64_t)datalen >> 40) & 0xff; + ws_header[5] = ((uint64_t)datalen >> 32) & 0xff; ws_header[6] = (datalen >> 24) & 0xff; ws_header[7] = (datalen >> 16) & 0xff; ws_header[8] = (datalen >> 8) & 0xff;