Skip to content

Conversation

dvdhrm
Copy link

@dvdhrm dvdhrm commented Sep 8, 2025

Meson sets 64-bit offsets as the default for all platforms but MSVC. Lets do the same for bindgen, to ensure we get compatible definitions.

Note that the libc crate does not expose 64-bit types as the default and there is no intention to do so. Instead, it exposes 32-bit default types, plus the 64-bit extended types with the *64 suffix. This is quite unfortunate, but it seems unlikely to change [1].

However, use of bindgen is usually not tied to the libc crate. Instead, it is tied to whatever other C code in the same project does. And Meson sets _FILE_OFFSET_BITS=64 unconditionally for all this C code. It thus seems much more plausible for Meson to also imply it for bindgen.

Given that Rust code that uses the libc crate very likely already uses the *64 suffixed variants, they are unaffected by whether _FILE_OFFSET_BITS=64 is set. If they use libc::off_t, they already explicitly state that they use the 32-bit variant on 32-bit platforms. Hence, it is inherently incompatible to C code that uses _FILE_OFFSET_BITS=64.

[1] rust-lang/libc#3223 (comment)

@dvdhrm dvdhrm marked this pull request as ready for review September 8, 2025 12:49
@dvdhrm dvdhrm requested a review from jpakkane as a code owner September 8, 2025 12:49
@bonzini bonzini added the module:rust Specific to the Rust module label Sep 8, 2025
@bonzini bonzini added this to the 1.9.1 milestone Sep 8, 2025
@bonzini
Copy link
Collaborator

bonzini commented Sep 8, 2025

Can you use get_largefile_args() on the C compiler object instead?

@dvdhrm
Copy link
Author

dvdhrm commented Sep 8, 2025

@bonzini No, I can't. Or, at least I need help to do that. bindgen is just a normal command invocation and it uses no compiler object. How would I go get access to the clang compiler object? And then, this would not necessarily match the compiler version used by bindgen, would it?

I am open to consolidate it with get_largefile_args(), but I have not found a suitable approach to do so. Hints appreciated!

@bonzini
Copy link
Collaborator

bonzini commented Sep 8, 2025

Or, at least I need help to do that

That's fair :) I was thinking of simply using the host C compiler so that if it is MSVC, which doesn't have automatic support for large files, you won't add the option to clang either.

You can get it with self.environment.coredata.compilers.host['c'].

This leaves out the case of wanting to use bindgen for the build machine, but that's not really supported at all by the rust module. Maybe that should be added to the documentation in fact, I'll add it to my TODO list.

@dvdhrm
Copy link
Author

dvdhrm commented Sep 8, 2025

Or, at least I need help to do that

That's fair :) I was thinking of simply using the host C compiler so that if it is MSVC, which doesn't have automatic support for large files, you won't add the option to clang either.

You can get it with self.environment.coredata.compilers.host['c'].

This leaves out the case of wanting to use bindgen for the build machine, but that's not really supported at all by the rust module. Maybe that should be added to the documentation in fact, I'll add it to my TODO list.

This will fail if no C-compiler is configured. IIRC, this could be the case when using meson for Rust with bindgen, but not adding 'c' as language to Meson. Maybe I am wrong about this? Should we force detection of a C compiler when using bindgen? Or does Meson already do this?

If we proceed with this: Should I use get_largefile_args() or get_always_args()? The latter is currently a wrapper of the former, but might include more args in the future. I lean towards the latter, but I am not sure.

@bonzini
Copy link
Collaborator

bonzini commented Sep 8, 2025

This will fail if no C-compiler is configured. IIRC, this could be the case when using meson for Rust with bindgen, but not adding 'c' as language to Meson.

Hmm, good catch. I would not add the flag (the chance is small anyway, even more so if you add the chance of having off_t change the output) and warn.

Should I use get_largefile_args() or get_always_args()? The latter is currently a wrapper of the former, but might include more args in the future. I lean towards the latter, but I am not sure.

I agree.

Meson sets 64-bit offsets as the default for all platforms but MSVC.
Lets do the same for bindgen, to ensure we get compatible definitions.
Do this by calling `get_always_args()` on the host compiler.

Note that the `libc` crate does not expose 64-bit types as the default
and there is no intention to do so. Instead, it exposes 32-bit default
types, plus the 64-bit extended types with the `*64` suffix. This is
quite unfortunate, but it seems unlikely to change [1].

However, use of `bindgen` is usually not tied to the `libc` crate.
Instead, it is tied to whatever other C code in the same project does.
And Meson sets `_FILE_OFFSET_BITS=64` unconditionally for all this C
code. It thus seems much more plausible for Meson to also imply it for
bindgen.

Given that Rust code that uses the `libc` crate very likely already uses
the `*64` suffixed variants, they are unaffected by whether
`_FILE_OFFSET_BITS=64` is set. If they use `libc::off_t`, they already
explicitly state that they use the 32-bit variant on 32-bit platforms.
Hence, it is inherently incompatible to C code that uses
`_FILE_OFFSET_BITS=64`.

And lastly, if a Meson project is Rust-only, but generates its internal
code from its public C headers, then it is better suited to actually
call `add_language('c')` and ensure that Meson knows what the compiler
configuration for the target platform actually is. Otherwise, bindgen
cannot know what platform options to enable. Hence, warn loudly if
`rust.bindgen()` is used without a configured C compiler (even if the
compiler used by bindgen does not necessarily match the configured one).

[1] rust-lang/libc#3223 (comment)
@dvdhrm
Copy link
Author

dvdhrm commented Sep 9, 2025

Thanks! I updated the code to check for the configured compiler and use get_always_args(), or warn about a lacking compiler.

@dcbaker
Copy link
Member

dcbaker commented Sep 16, 2025

Should we also check for c++, objc, and objc++ before giving up? It seems reasonable that someone could be building C++ + Rust and using C as an interface ABI.

@bonzini bonzini modified the milestones: 1.9.1, 1.9.2 Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:rust Specific to the Rust module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants