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

HAL headers are not C compatible #476

Closed
XavilPergis opened this issue Feb 6, 2017 · 5 comments · Fixed by #1177
Closed

HAL headers are not C compatible #476

XavilPergis opened this issue Feb 6, 2017 · 5 comments · Fixed by #1177
Labels
type: feature Brand new functionality, features, pages, workflows, endpoints, etc.

Comments

@XavilPergis
Copy link

XavilPergis commented Feb 6, 2017

Hello, I am writing bindings for the HAL in Rust, and using rust-bindgen to generate the raw wrappers over the HAL headers.

The problem I'm having is that the headers have C++ specific code that is not behind an #ifdef __cplusplus guard, so I need to patch the headers before I can run bindgen on them.

Some of the problems are:

  • types on enums (aka enum foo : int32_t {} needs to be enum foo {})
  • namespaces
  • missing struct/enum keyword (aka HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) needs to be HAL_GetJoystickAxes(int32_t joystickNum, struct HAL_JoystickAxes* axes))
  • include of stddef

This change would not break any C++ code.

@ThadHouse
Copy link
Member

We had originally started making these entirely C based, but then backed off in the middle to clean up the headers. We had figured most people wouldn't be directly compiling the headers with a C compiler, but would just be using language interop to hook into the C symbols. If you are willing to submit a PR to fix these issues we would look at this. Or we can see if someone can get to this over the summer, but it is unlikely anyone on the WPILib team will be able to do this during the season.

Most of it should just be adding the C++ guards however, which shouldn't be too bad. The only thing we are going to want special is the types on the enums should be ifdef'd out, and not just removed. There are reasons to force that for interop purposes.

@nightpool
Copy link

@XavilPergis my understanding is that bindgen supports C++ code. What's the problem with running bindgen in c++ mode? Do you get errors?

@333fred 333fred added type: feature Brand new functionality, features, pages, workflows, endpoints, etc. up for grabs labels May 7, 2017
@JaciBrunning
Copy link
Member

Correct me if I'm wrong, but there shouldn't be any issues de-typing HAL enums. The HAL and its dependencies are all compiled for the same system, so the internal size for the enum should be the same on both ends. If it does get implicitly cast to an int32_t at some point, surely just (int32_t)(enum) should do just fine, since enums are by C spec some form of integer.

Would it be worth switching struct foo {} to typedef struct {} foo in the HAL, rather than adding struct to every parameter/variable of type foo?

@ThadHouse
Copy link
Member

The reasoning for the explicit sizing is the use case for the HAL being C is not for interfacing with a C compiler. It's more for dealing with interop between other languages that can only directly hook into C API's. Those don't normally directly use the headers, and having explicit sizes on everything helps make that interop work easier. Explicit sizing just makes sure that those other languages don't get unexpected changes to variable sizing, which could happen without explicit sizing.

@ThadHouse
Copy link
Member

ThadHouse commented May 7, 2017

We could IfDef around all of those things to enable the headers to be parsable in pure C, but when actually compiling the HAL itself, or linking to C++, those explicit sizing definitions definitely should be there.
Note this is very much down my list of stuff to fix, so this is definitely an up for grabs issue, and tbh unless someone else picks it up, I probably won't get to it.

PeterJohnson pushed a commit that referenced this issue Jul 8, 2018
Also fix the return type of HAL_IsNewControlData() and HAL_MatchType's type.

Since UsageReporting is intended to be namespaced, it is hidden when this is being used in C.

Fixes: #476
Closes: #535
Ref: #1122
jwhite66 pushed a commit to jwhite66/allwpilib that referenced this issue Jul 9, 2018
Also fix the return type of HAL_IsNewControlData() and HAL_MatchType's type.

Since UsageReporting is intended to be namespaced, it is hidden when this is being used in C.

Fixes: wpilibsuite#476
Closes: wpilibsuite#535
Ref: wpilibsuite#1122
pjbuterbaugh pushed a commit to pjbuterbaugh/allwpilib that referenced this issue Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Brand new functionality, features, pages, workflows, endpoints, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants