-
Notifications
You must be signed in to change notification settings - Fork 24
RSDK-1975 - autogen header file #156
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
Changes from all commits
5328e2d
2c19c29
2ca8592
4b0ed04
cc48a24
6a38358
9672ff9
d9ff509
a77fea2
45399df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,5 @@ bin/ | |
|
|
||
| # goutils | ||
| tests/goutils/ | ||
|
|
||
| viam_rust_utils.h | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| extern crate cbindgen; | ||
|
|
||
| use std::{collections::HashMap, env}; | ||
|
|
||
| use cbindgen::{Config, ExportConfig, FunctionConfig, ParseConfig, RenameRule, StructConfig}; | ||
|
|
||
| fn main() { | ||
| let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
|
|
||
| // mangled nalgebra type outputs, mapped to a prefixed snake case variant | ||
| let nalgebra_types = vec![ | ||
| ("ArrayStorage_f64__3__1", "nalgebra_array_storage_f64__3__1"), | ||
| ( | ||
| "Matrix_f64__U3__U1__ArrayStorage_f64__3__1", | ||
| "nalgebra_matrix_f64_3_1", | ||
| ), | ||
| ("Vector3_f64", "nalgebra_vector3_f64"), | ||
| ("ArrayStorage_f64__4__1", "nalgebra_array_storage_f64__4__1"), | ||
| ( | ||
| "Matrix_f64__U4__U1__ArrayStorage_f64__4__1", | ||
| "nalgebra_matrix_f64_4_1", | ||
| ), | ||
| ("Vector4_f64", "nalgebra_vector4_f64"), | ||
| ("Quaternion_f64", "nalgebra_quaternion_f64"), | ||
| ( | ||
| "Unit_Matrix_f64__U3__U1__ArrayStorage_f64__3__1", | ||
| "nalgebra_unit_matrix_f64_3_1", | ||
| ), | ||
| ("UnitVector3_f64", "nalgebra_unit_vector3_f64"), | ||
| ("Rotation_f64__3", "nalgebra_rotation_f64_3"), | ||
| ( | ||
| "Matrix_f64__Const_3_____Const_3_____ArrayStorage_f64__3__3", | ||
| "nalgebra_matrix_f64_3_3", | ||
| ), | ||
| ("ArrayStorage_f64__3__3", "nalgebra_array_storage_f64__3__3"), | ||
| ("SMatrix_f64__3__3", "nalgebra_smatrix_f64_3_3"), | ||
| ("Rotation_f64__3", "nalgebra_rotation_f64_3"), | ||
| ("Rotation3_f64", "nalgebra_rotation3_f64"), | ||
| ]; | ||
|
|
||
| // mangled viam type outputs, mapped to a prefixed snake case variant | ||
| let viam_types = vec![ | ||
| ("DialFfi", "viam_dial_ffi"), | ||
| ("AxisAngle", "viam_axis_angle"), | ||
| ("EulerAngles", "viam_euler_angles"), | ||
| ("OrientationVector", "viam_orientation_vector"), | ||
| ]; | ||
|
|
||
| let rename = vec![nalgebra_types, viam_types] | ||
| .into_iter() | ||
| .flatten() | ||
| .map(|(a, b)| (a.to_string(), b.to_string())) | ||
| .collect::<HashMap<String, String>>(); | ||
|
|
||
| cbindgen::Builder::new() | ||
| .with_crate(crate_dir) | ||
| .with_config(Config { | ||
| parse: ParseConfig { | ||
| parse_deps: true, | ||
| include: Some(vec!["nalgebra".to_string()]), | ||
| ..Default::default() | ||
| }, | ||
| export: ExportConfig { | ||
| rename, | ||
| ..Default::default() | ||
| }, | ||
| structure: StructConfig { | ||
| rename_associated_constant: RenameRule::SnakeCase, | ||
| rename_fields: RenameRule::SnakeCase, | ||
| ..Default::default() | ||
| }, | ||
| function: FunctionConfig { | ||
| deprecated: Some( | ||
| "/// @deprecated please use `viam_`-prefixed function instead".to_string(), | ||
| ), | ||
| ..Default::default() | ||
| }, | ||
| ..Default::default() | ||
| }) | ||
| .with_cpp_compat(true) | ||
| .with_language(cbindgen::Language::C) | ||
| .with_autogen_warning( | ||
| "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */", | ||
| ) | ||
| .generate() | ||
| .expect("Unable to generate bindings") | ||
| .write_to_file("viam_rust_utils.h"); // Output to target directory | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,3 +65,9 @@ | |
| :why: | ||
| :versions: [] | ||
| :when: 2025-04-14 14:25:17.811988000 Z | ||
| - - :permit | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (flyby) license finder was failing because of a |
||
| - BSD-zero-clause | ||
| - :who: | ||
| :why: | ||
| :versions: [] | ||
| :when: 2025-11-04 16:20:20.790232000 Z | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possibly overly defensive but I opted for unique header files for each architecture in case they differ in includes or otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe cc @acmorrow but I think we're fine just using one header for all of them. i'm not sure the level of control you have over the generated code, but if there are indeed differing includes this is something you'd want to do with a single header file that has
#ifdefblocksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, client needs to be able to name one include and get the right things. If there are architecture dependencies, there should be a per-arch header and then they should be unified with a top-level header.
A suggestion if you are worried: generate for each platform, then checksum or diff them. If they compare identical, just ship the first one. If they differ, fail the build.
Then you don't need a wrapper header at all, unless and until a divergence emerges.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm unfortunately it's a bit awkward to force in more complicated code blocks to the generated code, not impossible but might be awkward.
I do think though that having multiple headers isn't too bad (at least for the SDK), since we can just enforce that the right one is downloaded in CMakeLists.txt
edit: this comment was made before seeing Drew's reply above!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, a question on that front just for my own understanding. Are we planning on checking in the header file to the repo? If so, what happens if someone has their own copy of the
rust_utilsbinary that they want to use (a thing we currently support), but it differs in some way from the checked in header? And if we're not checking it in but rather downloading it when we download therust_utilsbinary (or expecting a user to provide their own along with their ownrust_utilsbinary) then what's the danger with having that download seek out the architecture-specific header and then save it as to the generic.../rust_utils.hpath?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my perspective, there really shouldn't be any architecture dependent code in this header. It should be identical across all. If architecture variation slipped in, I'd consider that an error.
Shifting the burden to the consumer to select the right header seems somewhat unfair.
Regarding whether we will check this in, my expectation is that no, we wouldn't. I'd instead expect to see the one header published along with the release.
That's why my suggestion was to verify at build time that the header is bit identical on every platform where we generate it. That way, we will notice if they ever diverge, and can publish any one of them as "the header".