From 2230482ca9a766b09e5a96cb92ac484c924f739e Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Thu, 22 May 2025 15:46:30 +0200 Subject: [PATCH] Fix missing declarations of "POSIX" `read()`/`write()` on Windows with Clang When (cross?) compiling this crate to Windows (in a Rust project) using Clang 16 or newer, we're seeing these declaration errors: > cargo b --target x86_64-pc-windows-msvc warning: metis-sys@0.3.1: /usr/local/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/metis-sys-0.3.1/vendor/GKlib/io.c(63,18): error: call to undeclared function 'read'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] warning: metis-sys@0.3.1: 63 | if ((rsize = read(fd, buf, tsize)) == -1) warning: metis-sys@0.3.1: | ^ ... warning: metis-sys@0.3.1: /usr/local/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/metis-sys-0.3.1/vendor/GKlib/io.c(84,17): error: call to undeclared function 'write'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] warning: metis-sys@0.3.1: 84 | if ((size = write(fd, buf, tsize)) == -1) warning: metis-sys@0.3.1: | ^ (https://github.com/LIHPC-Computational-Geometry/metis-rs/pull/43#issuecomment-2901280518) Now it's yet unknown (we haven't checked) if older Clang had these declarations in a header, or didn't treat this warning as error yet, but the functions are POSIX which Windows isn't required to implement. Fortunately they provide it but with a deprecation warning and a recommended replacement of `_read()` (and `_write()`), but for this `` needs to be included: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-read https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/read The same is true for `getpid()` that commit a8e7e25 ("port to vs2017") used to provide a `win32/adapt.c/h` workaround for, but this can instead be pulled from `` (equally with a deprecation warning, but we should probably address these all in a consistent manner instead of defining a workaround). Let's take this as a starting point and go from there. --- CMakeLists.txt | 5 +---- include/gk_arch.h | 4 +++- include/win32/adapt.h | 14 -------------- src/win32/adapt.c | 11 ----------- 4 files changed, 4 insertions(+), 30 deletions(-) delete mode 100644 include/win32/adapt.h delete mode 100644 src/win32/adapt.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a9a694..c949c4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,10 +60,7 @@ target_sources(${PROJECT_NAME} include/gk_mkpqueue2.h include/gk_mkrandom.h include/gk_mksort.h include/gk_mkutils.h include/gk_proto.h include/gk_struct.h include/gk_types.h include/gkregex.h include/gk_ms_inttypes.h - include/gk_ms_stat.h include/gk_ms_stdint.h - # the following are shims for win32 systems - $<$:src/win32/adapt.c - include/win32/adapt.h>) + include/gk_ms_stat.h include/gk_ms_stdint.h) target_compile_definitions(${PROJECT_NAME} PUBLIC $<$:LINUX>) diff --git a/include/gk_arch.h b/include/gk_arch.h index b82fb6a..26fa312 100644 --- a/include/gk_arch.h +++ b/include/gk_arch.h @@ -35,7 +35,9 @@ #include "gk_ms_stdint.h" #include "gk_ms_inttypes.h" #include "gk_ms_stat.h" - #include "win32/adapt.h" + #include + #include + typedef int pid_t; #else #ifndef SUNOS #include diff --git a/include/win32/adapt.h b/include/win32/adapt.h deleted file mode 100644 index 35e60ed..0000000 --- a/include/win32/adapt.h +++ /dev/null @@ -1,14 +0,0 @@ -/* -\file win32/adapt.h -\brief Declaration of Win32 adaptation of POSIX functions and types -*/ -#ifndef _WIN32_ADAPT_H_ -#define _WIN32_ADAPT_H_ - -#include - -typedef DWORD pid_t; - -pid_t getpid(void); - -#endif /* _WIN32_ADAPT_H_ */ diff --git a/src/win32/adapt.c b/src/win32/adapt.c deleted file mode 100644 index 546857c..0000000 --- a/src/win32/adapt.c +++ /dev/null @@ -1,11 +0,0 @@ -/* -\file win32/adapt.c -\brief Implementation of Win32 adaptation of libc functions -*/ - -#include "adapt.h" - -pid_t getpid(void) -{ - return GetCurrentProcessId(); -}