On 2/12/25 16:13, Kevin Wolf wrote:
Or if you have to define the constants anyway - you currently do this
only for Windows, but for into_negative_errno() you might need it on
Linux, too - and it wouldn't be a problem for the constants to be
signed (that they are unsigned is the main reason why it becomes so ugly
with the bindgen constants), you could just make it -errno::EINVAL
again.

Or just include the libc crate, see attachment.

Paolo
From 526ad76001e788eb9adb2624eb4bbedae50301f9 Mon Sep 17 00:00:00 2001
From: Paolo Bonzini <pbonz...@redhat.com>
Date: Mon, 16 Dec 2024 09:42:35 +0100
Subject: [PATCH] rust: subprojects: add libc crate

This allows access to errno values.

Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
---
 rust/Cargo.lock                               |  7 ++++
 rust/qemu-api/Cargo.toml                      |  1 +
 rust/qemu-api/src/errno.rs                    | 32 ++---------------
 scripts/archive-source.sh                     |  2 +-
 scripts/make-release                          |  4 +--
 subprojects/libc-0.2-rs.wrap                  |  7 ++++
 .../packagefiles/libc-0.2-rs/meson.build      | 36 +++++++++++++++++++
 7 files changed, 56 insertions(+), 33 deletions(-)
 create mode 100644 subprojects/libc-0.2-rs.wrap
 create mode 100644 subprojects/packagefiles/libc-0.2-rs/meson.build

diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index bd22cc39c16..ac9806a1b4f 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -56,4 +56,10 @@ dependencies = [
  "either",
 ]
 
+[[package]]
+name = "libc"
+version = "0.2.162"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
+
 [[package]]
@@ -126,4 +132,5 @@ dependencies = [
 name = "qemu_api"
 version = "0.1.0"
 dependencies = [
+ "libc",
  "qemu_api_macros",
diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build
index d273e5d903f..3708d97ae75 100644
--- a/rust/qemu-api/meson.build
+++ b/rust/qemu-api/meson.build
@@ -1,6 +1,8 @@
 _qemu_api_cfg = run_command(rustc_args,
   '--config-headers', config_host_h, '--features', files('Cargo.toml'),
   capture: true, check: true).stdout().strip().splitlines()
+
+libc_dep = dependency('libc-0.2-rs')
 
 if get_option('debug_mutex')
   _qemu_api_cfg += ['--cfg', 'feature="debug_cell"']
@@ -38,6 +38,7 @@ _qemu_api_rs = static_library(
   override_options: ['rust_std=2021', 'build.rust_std=2021'],
   rust_abi: 'rust',
   rust_args: _qemu_api_cfg,
+  link_with: libc_dep,
 )
 
 rust.test('rust-qemu-api-tests', _qemu_api_rs,
diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml
index c96e87f592e..7255e9ee596 100644
--- a/rust/qemu-api/Cargo.toml
+++ b/rust/qemu-api/Cargo.toml
@@ -18,6 +18,7 @@ rust-version = "1.63.0"

 [dependencies]
 qemu_api_macros = { path = "../qemu-api-macros" }
+libc = "0.2.162"
 
 [build-dependencies]
 version_check = "~0.9"
diff --git a/rust/qemu-api/src/errno.rs b/rust/qemu-api/src/errno.rs
index c697f9bef05..58d46abc21c 100644
--- a/rust/qemu-api/src/errno.rs
+++ b/rust/qemu-api/src/errno.rs
@@ -16,32 +16,6 @@
 // into io::Error by hand.  For simplicity use ErrorKind and use
 // the standard library's simple-minded mapping of ErrorKind to Error
 // (`impl From<ErrorKind> for io::Error`).
-//
-// Since this is just for Windows, do not bother with using the libc
-// crate or generating the constants from C.  Just list here the
-// constants that map to stable error kinds.
-#[cfg(windows)]
-mod libc {
-    pub const EPERM: u16 = 1;
-    pub const ENOENT: u16 = 2;
-    pub const EINTR: u16 = 4;
-    pub const EAGAIN: u16 = 11;
-    pub const ENOMEM: u16 = 12;
-    pub const EACCES: u16 = 13;
-    pub const EEXIST: u16 = 17;
-    pub const EINVAL: u16 = 22;
-    pub const EPIPE: u16 = 32;
-    pub const EADDRINUSE: u16 = 100;
-    pub const EADDRNOTAVAIL: u16 = 101;
-    pub const ECONNABORTED: u16 = 106;
-    pub const ECONNREFUSED: u16 = 107;
-    pub const ECONNRESET: u16 = 108;
-    pub const ENOTCONN: u16 = 126;
-    pub const ENOTSUP: u16 = 129;
-    pub const ETIMEDOUT: u16 = 138;
-    pub const EWOULDBLOCK: u16 = 140;
-}
-
 impl From<Errno> for io::Error {
     #[cfg(unix)]
     fn from(value: Errno) -> io::Error {
@@ -52,13 +26,11 @@ fn from(value: Errno) -> io::Error {
     #[cfg(windows)]
     fn from(value: Errno) -> io::Error {
         let Errno(errno) = value;
-        let error_kind = match errno {
+        let error_kind = match errno as i32 {
             libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
             libc::ENOENT => ErrorKind::NotFound,
             libc::EINTR => ErrorKind::Interrupted,
-            // Note that on Windows we know these two are distinct.  In general,
-            // it would not be possible to use "|".
-            libc::EAGAIN | libc::EWOULDBLOCK => ErrorKind::WouldBlock,
+            x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock,
             libc::ENOMEM => ErrorKind::OutOfMemory,
             libc::EEXIST => ErrorKind::AlreadyExists,
             libc::EINVAL => ErrorKind::InvalidInput,
diff --git a/scripts/archive-source.sh b/scripts/archive-source.sh
index e72bc7aa61e..00a1254ce8b 100755
--- a/scripts/archive-source.sh
+++ b/scripts/archive-source.sh
@@ -29,7 +29,7 @@ sub_file="${sub_tdir}/submodule.tar"
 # different to the host OS.
 subprojects="keycodemapdb libvfio-user berkeley-softfloat-3
   berkeley-testfloat-3 arbitrary-int-1-rs bilge-0.2-rs
-  bilge-impl-0.2-rs either-1-rs itertools-0.11-rs proc-macro2-1-rs
+  bilge-impl-0.2-rs either-1-rs itertools-0.11-rs libc-0.2-rs proc-macro2-1-rs
   proc-macro-error-1-rs proc-macro-error-attr-1-rs quote-1-rs
   syn-2-rs unicode-ident-1-rs"
 sub_deinit=""
diff --git a/scripts/make-release b/scripts/make-release
index 2acbfed8f2d..a53aebd1f1e 100755
--- a/scripts/make-release
+++ b/scripts/make-release
@@ -40,7 +40,7 @@ fi
 # Only include wraps that are invoked with subproject()
 SUBPROJECTS="libvfio-user keycodemapdb berkeley-softfloat-3
   berkeley-testfloat-3 arbitrary-int-1-rs bilge-0.2-rs
-  bilge-impl-0.2-rs either-1-rs itertools-0.11-rs
+  bilge-impl-0.2-rs either-1-rs itertools-0.11-rs libc-0.2-rs
   proc-macro-error-1-rs proc-macro-error-attr-1-rs quote-1-rs
   syn-2-rs unicode-ident-1-rs"
 
diff --git a/subprojects/libc-0.2-rs.wrap b/subprojects/libc-0.2-rs.wrap
new file mode 100644
index 00000000000..0ce0e75291a
--- /dev/null
+++ b/subprojects/libc-0.2-rs.wrap
@@ -0,0 +1,7 @@
+[wrap-file]
+directory = libc-0.2
+source_url = https://crates.io/api/v1/crates/libc/0.2.162/download
+source_filename = libc-0.2.162.tar.gz
+source_hash = 18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398
+#method = cargo
+patch_directory = libc-0.2-rs
diff --git a/subprojects/packagefiles/libc-0.2-rs/meson.build b/subprojects/packagefiles/libc-0.2-rs/meson.build
new file mode 100644
index 00000000000..11c4ada33a5
--- /dev/null
+++ b/subprojects/packagefiles/libc-0.2-rs/meson.build
@@ -0,0 +1,36 @@
+project('libc-0.2-rs', 'rust',
+  meson_version: '>=1.5.0',
+  version: '0.2.162',
+  license: 'MIT OR Apache-2.0',
+  default_options: [])
+
+_libc_rs = static_library(
+  'libc',
+  files('src/lib.rs'),
+  gnu_symbol_visibility: 'hidden',
+  override_options: ['rust_std=2015', 'build.rust_std=2015'],
+  rust_abi: 'rust',
+  rust_args: [
+    '--cfg', 'freebsd11',
+    '--cfg', 'libc_priv_mod_use',
+    '--cfg', 'libc_union',
+    '--cfg', 'libc_const_size_of',
+    '--cfg', 'libc_align',
+    '--cfg', 'libc_int128',
+    '--cfg', 'libc_core_cvoid',
+    '--cfg', 'libc_packedN',
+    '--cfg', 'libc_cfg_target_vendor',
+    '--cfg', 'libc_non_exhaustive',
+    '--cfg', 'libc_long_array',
+    '--cfg', 'libc_ptr_addr_of',
+    '--cfg', 'libc_underscore_const_names',
+    '--cfg', 'libc_const_extern_fn',
+  ],
+  dependencies: [],
+)
+
+libc_dep = declare_dependency(
+  link_with: _libc_rs,
+)
+
+meson.override_dependency('libc-0.2-rs', libc_dep)
-- 
2.48.1

Reply via email to