Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for a GCC-based backend #1949

Open
ojeda opened this issue Dec 20, 2020 · 7 comments
Open

Support for a GCC-based backend #1949

ojeda opened this issue Dec 20, 2020 · 7 comments
Labels
enhancement rust-for-linux Issues relevant to the Rust for Linux project

Comments

@ojeda
Copy link

ojeda commented Dec 20, 2020

Currently, bindgen uses libclang to understand C/C++ code. #1740 considers moving to a lower-level interface for LLVM, with discussion of whether keeping support for several interfaces is feasible.

It would be great to have support for non-LLVM backends as well. In particular, a GCC-based one. Clang is very compatible with GCC nowadays (in both the Clang driver and the support for GNU C language extensions), but there are still differences here and there, which means one needs to be careful when applying bindgen in complex projects compiled with GCC.

It would require compiling a GCC plugin and using callbacks like PLUGIN_PRE_GENERICIZE ("Allows to see low level AST in C and C++ frontends.") and/or others like PLUGIN_FINISH_PARSE_FUNCTION, PLUGIN_FINISH_TYPE, PLUGIN_FINISH_DECL, etc.; or perhaps another way to use GCC's parser/tooling/etc.; or possibly using other approaches that fetch the information in another way (e.g. querying the compiler via generated C files or extracting it from debug information).

Of course, tackling this would be non-trivial, and it might not be worth the maintenance cost, but it might be an interesting project to expand bindgen's capabilities, as well as relaxing the dependency on LLVM.

fengguang pushed a commit to 0day-ci/linux that referenced this issue Apr 14, 2021
To support Rust under GCC-built kernels, we need to save the flags that
would have been passed if the kernel was being compiled with Clang.

The reason is that bindgen -- the tool we use to generate Rust bindings
to the C side of the kernel -- relies on libclang to parse C. Ideally:

  - bindgen would support a GCC backend (requested at [1]),

  - or the Clang driver would be perfectly compatible with GCC,
    including plugins. Unlikely, of course, but perhaps a big
    subset of configs may be possible to guarantee to be kept
    compatible nevertheless.

This is also the reason why GCC builds are very experimental and some
configurations may not work (e.g. GCC_PLUGIN_RANDSTRUCT). However,
we keep GCC builds working (for some example configs) in the CI
to avoid diverging/regressing further, so that we are better prepared
for the future when a solution might become available.

[1] rust-lang/rust-bindgen#1949

Link: Rust-for-Linux#167

Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Geoffrey Thomas <geofft@ldpreload.com>
Signed-off-by: Geoffrey Thomas <geofft@ldpreload.com>
Co-developed-by: Finn Behrens <me@kloenk.de>
Signed-off-by: Finn Behrens <me@kloenk.de>
Co-developed-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
ojeda added a commit to ojeda/linux that referenced this issue Jul 4, 2021
To support Rust under GCC-built kernels, we need to save the flags that
would have been passed if the kernel was being compiled with Clang.

The reason is that `bindgen` -- the tool we use to generate Rust
bindings to the C side of the kernel -- relies on `libclang` to
parse C. Ideally:

  - `bindgen` would support a GCC backend (requested at [1]),

  - or the Clang driver would be perfectly compatible with GCC,
    including plugins. Unlikely, of course, but perhaps a big
    subset of configs may be possible to guarantee to be kept
    compatible nevertheless.

This is also the reason why GCC builds are very experimental and some
configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`). However,
we keep GCC builds working (for some example configs) in the CI
to avoid diverging/regressing further, so that we are better prepared
for the future when a solution might become available.

[1] rust-lang/rust-bindgen#1949

Link: Rust-for-Linux#167
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Geoffrey Thomas <geofft@ldpreload.com>
Signed-off-by: Geoffrey Thomas <geofft@ldpreload.com>
Co-developed-by: Finn Behrens <me@kloenk.de>
Signed-off-by: Finn Behrens <me@kloenk.de>
Co-developed-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
ojeda added a commit to ojeda/linux that referenced this issue Jul 4, 2021
To support Rust under GCC-built kernels, we need to save the flags that
would have been passed if the kernel was being compiled with Clang.

The reason is that `bindgen` -- the tool we use to generate Rust
bindings to the C side of the kernel -- relies on `libclang` to
parse C. Ideally:

  - `bindgen` would support a GCC backend (requested at [1]),

  - or the Clang driver would be perfectly compatible with GCC,
    including plugins. Unlikely, of course, but perhaps a big
    subset of configs may be possible to guarantee to be kept
    compatible nevertheless.

This is also the reason why GCC builds are very experimental and some
configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`). However,
we keep GCC builds working (for some example configs) in the CI
to avoid diverging/regressing further, so that we are better prepared
for the future when a solution might become available.

[1] rust-lang/rust-bindgen#1949

Link: Rust-for-Linux#167
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Geoffrey Thomas <geofft@ldpreload.com>
Signed-off-by: Geoffrey Thomas <geofft@ldpreload.com>
Co-developed-by: Finn Behrens <me@kloenk.de>
Signed-off-by: Finn Behrens <me@kloenk.de>
Co-developed-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
fengguang pushed a commit to 0day-ci/linux that referenced this issue Jul 4, 2021
To support Rust under GCC-built kernels, we need to save the flags that
would have been passed if the kernel was being compiled with Clang.

The reason is that `bindgen` -- the tool we use to generate Rust
bindings to the C side of the kernel -- relies on `libclang` to
parse C. Ideally:

  - `bindgen` would support a GCC backend (requested at [1]),

  - or the Clang driver would be perfectly compatible with GCC,
    including plugins. Unlikely, of course, but perhaps a big
    subset of configs may be possible to guarantee to be kept
    compatible nevertheless.

This is also the reason why GCC builds are very experimental and some
configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`). However,
we keep GCC builds working (for some example configs) in the CI
to avoid diverging/regressing further, so that we are better prepared
for the future when a solution might become available.

[1] rust-lang/rust-bindgen#1949

Link: Rust-for-Linux#167
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Geoffrey Thomas <geofft@ldpreload.com>
Signed-off-by: Geoffrey Thomas <geofft@ldpreload.com>
Co-developed-by: Finn Behrens <me@kloenk.de>
Signed-off-by: Finn Behrens <me@kloenk.de>
Co-developed-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
@bobbbay
Copy link

bobbbay commented Sep 6, 2022

+1 for this (albeit two years later). I have a library that is incompatible with clang and must be compiled using gcc, but can't run bindgen on it. It would very nice to be able to simply set the backend to gcc and compile things as they should.

@emmatebibyte
Copy link

I'm trying to write some cross-platform tools and it would be nice to be able to use bindgen with them on any system.

@StellarHS
Copy link

In LLVM, some standard headers like wchar.h only find in model libcxx. That may cause some trouble when a code uses gcc. If it support for a GCC-based backend, that trouble can be easily solved.

@pvdrz
Copy link
Contributor

pvdrz commented Oct 17, 2023

This is unlikely to be implemented anytime soon unless someone else shows up and decides to give it a go

BuptOs pushed a commit to BUPT-OS/RROS that referenced this issue Dec 5, 2023
To support Rust under GCC-built kernels, we need to save the flags that
would have been passed if the kernel was being compiled with Clang.

The reason is that `bindgen` -- the tool we use to generate Rust
bindings to the C side of the kernel -- relies on `libclang` to
parse C. Ideally:

  - `bindgen` would support a GCC backend (requested at [1]),

  - or the Clang driver would be perfectly compatible with GCC,
    including plugins. Unlikely, of course, but perhaps a big
    subset of configs may be possible to guarantee to be kept
    compatible nevertheless.

This is also the reason why GCC builds are very experimental and some
configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`). However,
we keep GCC builds working (for some example configs) in the CI
to avoid diverging/regressing further, so that we are better prepared
for the future when a solution might become available.

[1] rust-lang/rust-bindgen#1949

Link: Rust-for-Linux/linux#167
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Geoffrey Thomas <geofft@ldpreload.com>
Signed-off-by: Geoffrey Thomas <geofft@ldpreload.com>
Co-developed-by: Finn Behrens <me@kloenk.de>
Signed-off-by: Finn Behrens <me@kloenk.de>
Co-developed-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
@Shinyzenith
Copy link

Shinyzenith commented Jan 11, 2024

Hi, I'm not too familiar with bindgen internals but I want to support this! Is this a feature the bindgen team would like to see upstream? I'm willing to try my hands at making a PR. I'd rather not invest time blindly hence a stance on this proposal would be helpful.

My main motivation for this is to further the development of rust for linux. @ojeda sorry for the ping but I just wanna confirm if this is still a "good to have"/requirement for rust for linux since it's been 4 years.

@tgross35
Copy link
Contributor

tgross35 commented Jan 12, 2024

@Shinyzenith I can answer for RFL - yes, this is very much still wanted :) Especially with both rustc_codegen_gcc and gccrs probably going to be usable sometime this year I think demand for GCC-produced bindings will only go up from here, even outside of linux.

Just be aware that this is definitely quite a bit of effort.

@Shinyzenith
Copy link

@Shinyzenith I can answer for RFL - yes, this is very much still wanted :) Especially with both rustc_codegen_gcc and gccrs probably going to be usable sometime this year I think demand for GCC-produced bindings will only go up from here, even outside of linux.

Just be aware that this is definitely quite a bit of effort.

Hi, thanks for the response. I understand that this is a lot of effort -- maybe getting a draft PR up at the very least even if I'm unable to finish it in the future would help attract possible contributors to pick up the slack -- but that's probably a discussion for later 😅.

If anyone from the bindgen team could confirm if this proposal is accepted then I'd love to try my hands at it.

@emilio emilio added the rust-for-linux Issues relevant to the Rust for Linux project label Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement rust-for-linux Issues relevant to the Rust for Linux project
Projects
None yet
Development

No branches or pull requests

8 participants