This is an automated email from Gerrit. "Jan Matyas <[email protected]>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9506
-- gerrit commit 4ce3fc1c994e0ff074597b269ad3e7d93b54730c Author: Jan Matyas <[email protected]> Date: Thu Mar 12 14:07:32 2026 +0100 helper/replacements: Fix Windows build using clang (-Wsign-compare) OpenOCD currently won't build on Windows using clang for these reasons: - "-Wextra", which is enabled for OpenOCD builds by default, causes on clang that "-Wsign-compare" gets also enabled. - Socket descriptors on Windows are signed, causing -Wsign-compare warnings on each invocation of FD_SET. Possible workaround: ./configure [...] --disable-werror Fix the issue by creating OCD_FD_* wrapper macros over FD_* that add the missing cast on Windows, silencing those compiler warnings. Also make sure that -Wsign-compare gets enabled on GCC. Steps to reproduce the issue: 1) Use a Windows machine 2) Download and install MSYS2 from: https://www.msys2.org 3) In the MSYS2 shell, install the following packages: pacman -S autotools automake base-devel mingw-w64-x86_64-clang git tcl 4) Clone libjaylink source git clone https://github.com/openocd-org/openocd.git --recursive cd openocd 5) Configure and build openocd export CC=/mingw64/bin/clang ./bootstrap ./configure \ --enable-jtag_vpi \ --enable-remote-bitbang \ --prefix=`pwd`/install \ --enable-internal-jimtcl make -j$(nproc) 6) Observe the sign-compare error message(s) produced by clang, for example: src/helper/replacements.c:178:6: error: comparison of integers of different signs: 'SOCKET' (aka 'unsigned long long') and 'int' [-Werror,-Wsign-compare] 178 | FD_SET(i, &sock_read); | ^~~~~~~~~~~~~~~~~~~~~ C:/msys64/mingw64/include/psdk_inc/_fd_types.h:77:40: note: expanded from macro 'FD_SET' 77 | if (((fd_set *)(set))->fd_array[__i] == (fd)) { \ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~ $ clang --version clang version 21.1.7 Target: x86_64-w64-windows-gnu Thread model: posix InstalledDir: C:/msys64/mingw64/bin Change-Id: I164da7165c77f232ae83f6d60305360a87c7604a Signed-off-by: Jan Matyas <[email protected]> diff --git a/configure.ac b/configure.ac index 39f5e074ed..2be0958eea 100644 --- a/configure.ac +++ b/configure.ac @@ -754,6 +754,7 @@ AS_IF([test "x${gcc_wextra}" = "xyes"], [ GCC_WARNINGS="${GCC_WARNINGS} -Wredundant-decls" GCC_WARNINGS="${GCC_WARNINGS} -Wpointer-arith" GCC_WARNINGS="${GCC_WARNINGS} -Wundef" + GCC_WARNINGS="${GCC_WARNINGS} -Wsign-compare" ]) AS_IF([test "x${gcc_werror}" = "xyes"], [ GCC_WARNINGS="${GCC_WARNINGS} -Werror" diff --git a/src/helper/replacements.c b/src/helper/replacements.c index 9fb7b4a35b..583ecba0c5 100644 --- a/src/helper/replacements.c +++ b/src/helper/replacements.c @@ -138,7 +138,7 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time struct timeval tvslice; int retcode; -#define SAFE_FD_ISSET(fd, set) (set && FD_ISSET(fd, set)) +#define SAFE_FD_ISSET(fd, set) (set && OCD_FD_ISSET(fd, set)) /* calculate how long we need to wait in milliseconds */ if (!tv) @@ -175,11 +175,11 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time if (handles[n_handles] == INVALID_HANDLE_VALUE) { /* socket */ if (SAFE_FD_ISSET(i, rfds)) - FD_SET(i, &sock_read); + OCD_FD_SET(i, &sock_read); if (SAFE_FD_ISSET(i, wfds)) - FD_SET(i, &sock_write); + OCD_FD_SET(i, &sock_write); if (SAFE_FD_ISSET(i, efds)) - FD_SET(i, &sock_except); + OCD_FD_SET(i, &sock_except); if (i > sock_max_fd) sock_max_fd = i; } else { @@ -251,20 +251,20 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time NULL, &bytes, NULL)) { /* check to see if gdb pipe has data available */ if (bytes) { - FD_SET(handle_slot_to_fd[i], &aread); + OCD_FD_SET(handle_slot_to_fd[i], &aread); retcode++; } } else { - FD_SET(handle_slot_to_fd[i], &aread); + OCD_FD_SET(handle_slot_to_fd[i], &aread); retcode++; } } if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) { - FD_SET(handle_slot_to_fd[i], &awrite); + OCD_FD_SET(handle_slot_to_fd[i], &awrite); retcode++; } if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) { - FD_SET(handle_slot_to_fd[i], &aexcept); + OCD_FD_SET(handle_slot_to_fd[i], &aexcept); retcode++; } } diff --git a/src/helper/replacements.h b/src/helper/replacements.h index 747c2ae09a..015f6eed4a 100644 --- a/src/helper/replacements.h +++ b/src/helper/replacements.h @@ -162,6 +162,24 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time #endif /* _WIN32 */ +/* Socket descriptor is unsigned on Windows. + * Add a cast to prevent sign mismatch warnings (-Wsign-compare). */ +#ifdef _WIN32 +#define OCD_FD_SET(fd, set) do { \ + assert(fd > 0); \ + FD_SET((SOCKET)fd, set); \ + } while (0) +#define OCD_FD_CLR(fd, set) do { \ + assert(fd > 0); \ + FD_CLR((SOCKET)fd, set); \ + } while (0) +#define OCD_FD_ISSET(fd, set) FD_ISSET((SOCKET)fd, set) +#else +#define OCD_FD_SET(fd, set) FD_SET(fd, set) +#define OCD_FD_CLR(fd, set) FD_CLR(fd, set) +#define OCD_FD_ISSET(fd, set) FD_ISSET(fd, set) +#endif + /* generic socket functions for Windows and Posix */ static inline int write_socket(int handle, const void *buffer, unsigned int count) { diff --git a/src/helper/system.h b/src/helper/system.h index 60308abcd6..909edc5163 100644 --- a/src/helper/system.h +++ b/src/helper/system.h @@ -43,7 +43,7 @@ #include <netinet/in.h> #endif #ifdef HAVE_SYS_SELECT_H -#include <sys/select.h> /* select, FD_SET and friends (POSIX.1-2001) */ +#include <sys/select.h> /* select, OCD_FD_SET and friends (POSIX.1-2001) */ #endif #ifdef HAVE_SYS_PARAM_H #include <sys/param.h> /* for MIN/MAX macros */ diff --git a/src/jtag/drivers/cmsis_dap_tcp.c b/src/jtag/drivers/cmsis_dap_tcp.c index 7894550fe2..54c1adb905 100644 --- a/src/jtag/drivers/cmsis_dap_tcp.c +++ b/src/jtag/drivers/cmsis_dap_tcp.c @@ -283,7 +283,7 @@ static int peekall_socket(int handle, void *buffer, unsigned int count, // Blocking wait. fd_set rfds; FD_ZERO(&rfds); - FD_SET(handle, &rfds); + OCD_FD_SET(handle, &rfds); struct timeval tv; tv.tv_sec = timeout_ms / 1000; diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index f59eb5029e..6bbf01cc98 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -220,7 +220,7 @@ static int check_pending(struct connection *connection, } FD_ZERO(&read_fds); - FD_SET(connection->fd, &read_fds); + OCD_FD_SET(connection->fd, &read_fds); tv.tv_sec = timeout_s; tv.tv_usec = 0; @@ -233,7 +233,7 @@ static int check_pending(struct connection *connection, else return ERROR_OK; } - *got_data = FD_ISSET(connection->fd, &read_fds) != 0; + *got_data = OCD_FD_ISSET(connection->fd, &read_fds) != 0; return ERROR_OK; } diff --git a/src/server/server.c b/src/server/server.c index a48c0dba97..9e40d4d002 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -457,7 +457,7 @@ int server_loop(struct command_context *command_context) for (service = services; service; service = service->next) { if (service->fd != -1) { /* listen for new connections */ - FD_SET(service->fd, &read_fds); + OCD_FD_SET(service->fd, &read_fds); if (service->fd > fd_max) fd_max = service->fd; @@ -468,7 +468,7 @@ int server_loop(struct command_context *command_context) for (c = service->connections; c; c = c->next) { /* check for activity on the connection */ - FD_SET(c->fd, &read_fds); + OCD_FD_SET(c->fd, &read_fds); if (c->fd > fd_max) fd_max = c->fd; } @@ -546,7 +546,7 @@ int server_loop(struct command_context *command_context) for (service = services; service; service = service->next) { /* handle new connections on listeners */ if ((service->fd != -1) - && (FD_ISSET(service->fd, &read_fds))) { + && (OCD_FD_ISSET(service->fd, &read_fds))) { if (service->max_connections != 0) add_connection(service, command_context); else { @@ -570,7 +570,7 @@ int server_loop(struct command_context *command_context) struct connection *c; for (c = service->connections; c; ) { - if ((c->fd >= 0 && FD_ISSET(c->fd, &read_fds)) || c->input_pending) { + if ((c->fd >= 0 && OCD_FD_ISSET(c->fd, &read_fds)) || c->input_pending) { retval = service->input(c); if (retval != ERROR_OK) { struct connection *next = c->next; --
