Skip to content

Conversation

@HunkBenny
Copy link
Contributor

Hi Mo!
Sorry in advance for also including a deprecation-warning fix in this PR, figured I would tackle that one as well.

The reason why I need this is because of SCIP_INVALID. (See scipopt/russcip#269)

This constant is defined as a C-macro over here; https://scipopt.org/doc/html/def_8h_source.php#l00178. It would be nice if we had access to this macro in Rust as well. By letting bindgen generate these constants, we do not need to manually define everything ourselves. Therefore, I added 'def.h' to bindgen.

There was one problem though, bindgen has a hard time when dealing with type-casts in macros. Therefore it skipped generating bindings for SCIP_INVALID. The solution for this was defining a callback by implementing this trait. This callback looks for a pattern like: '( KEYWORD )', where I believe the KEYWORD to be a clang-keyword. I did not dare to dive further in the clang-rabbithole, but it seemed to work for standard C-types (double) and not for other types like SCIP_Real. Which is precisely what I think we should want.

As an extra safety I made sure that the macros we want to target should be added to the DeriveCastedConstant struct. I think that bindgen must have a good reason why it ignores type-casted macros, so I think it is best to not let this callback blindly noscope every single type-casted macro that crosses its path!

I tested this in my local version of russcip and was able to access ffi::SCIP_INVALID. 🎉

… callback

The new callback is needed because bindgen cannot infer type-casted macros. For example the macro '#define SCIP_INVALID (double)1e99' is too complicated. This is because of the type-cast to double. If we simply remove the (double), then bindgen CAN generate a binding for it. It also defaults to an `f64` in Rust, which is what the C-representation would look like too. The callback should only be used for types of which we know that Rust can infer the datatype. Therefore, I made sure that it only goes off on;
1. macros that the user deliberately targets (added via `target`-method)
2. only looks at casts to clang-keywords. So no weird custom mumbo-jumbo casts
@mmghannam
Copy link
Member

This is great! thanks a lot @HunkBenny.

Just one thing as a sanity check, please add a test that just accesses something from def.h. Just to test that it works on all setup methods.

@HunkBenny
Copy link
Contributor Author

This is great! thanks a lot @HunkBenny.

Just one thing as a sanity check, please add a test that just accesses something from def.h. Just to test that it works on all setup methods.

Do you think something as simple as this would work?

@mmghannam
Copy link
Member

This is great! thanks a lot @HunkBenny.
Just one thing as a sanity check, please add a test that just accesses something from def.h. Just to test that it works on all setup methods.

Do you think something as simple as this would work?

Yes that was exactly what I had in mind. thank you!

@mmghannam mmghannam merged commit cda52bb into scipopt:main Dec 16, 2025
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants