Skip to content

Commit

Permalink
cbindgen: Automatically detect package name.
Browse files Browse the repository at this point in the history
Store the package name of the rust target as a property,
so that the user does not need to specify it.
If I'm not missing anything, then this should also
allow us to remove the `CARGO_PACKAGE` parameter
completely (in a future update).
  • Loading branch information
jschwe committed May 10, 2024
1 parent f85b242 commit b11a338
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
- `corrosion_experimental_cbindgen()` now forwards the Rust target-triple (e.g. `aarch64-unknown-linux-gnu`)
to cbindgen via the `TARGET` environment variable. The `hostbuild` property is considered. [#507]
- Detect msvc linker flags coming from `--print=native-static-libs` and put them into `INTERFACE_LINK_OPTIONS` instead of `INTERFACE_LINK_LIBRARIES` [#511]
- `corrosion_experimental_cbindgen()` will now correctly use the package name, instead of assuming that
the package and crate name are identical.


[issue #501]: https://github.com/corrosion-rs/corrosion/issues/501
[#459]: https://github.com/corrosion-rs/corrosion/pull/459
Expand Down
18 changes: 12 additions & 6 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1498,8 +1498,8 @@ ANCHOR: corrosion_cbindgen
```cmake
corrosion_cbindgen(
TARGET <imported_target_name>
CARGO_PACKAGE <cargo_package_name>
HEADER_NAME <output_header_name>
[CARGO_PACKAGE <cargo_package_name>]
[MANIFEST_DIRECTORY <package_manifest_directory>]
[CBINDGEN_VERSION <version>]
[FLAGS <flag1> ... <flagN>]
Expand All @@ -1517,8 +1517,8 @@ between multiple invocations of this function.
`rlib`, you must additionally specify `MANIFEST_DIRECTORY`.
* **CARGO_PACKAGE**: The name of the Rust cargo package that contains the library target. Note, that `cbindgen`
expects this package name using the `--crate` parameter. If not specified, the **TARGET**
will be used instead.
expects this package name using the `--crate` parameter. If not specified, the **package** name
of the imported **TARGET** will be used, which should be correct.
* **MANIFEST_DIRECTORY**: Directory of the package defining the library crate bindings should be generated for.
If you want to avoid specifying `MANIFEST_DIRECTORY` you could add a `staticlib` target to your package
Expand Down Expand Up @@ -1587,12 +1587,18 @@ function(corrosion_experimental_cbindgen)

unset(rust_cargo_package)
if(NOT DEFINED CCN_CARGO_PACKAGE)
message(STATUS "Using rust_target ${rust_target} as crate for cbindgen")
set(rust_cargo_package "${rust_target}")
get_target_property(rust_cargo_package "${rust_target}" COR_CARGO_PACKAGE_NAME )
if(NOT rust_cargo_package)
message(FATAL_ERROR "Could not determine cargo package name for cbindgen. "
"You may want to explicitly pass the package name via the "
"CARGO_PACKAGE argument to corrosion_experimental_cbindgen."
)
endif()
else()
message(STATUS "Using crate ${CCN_CARGO_PACKAGE} as crate for cbindgen")
set(rust_cargo_package "${CCN_CARGO_PACKAGE}")
endif()
message(STATUS "Using package ${rust_cargo_package} as crate for cbindgen")


set(output_header_name "${CCN_HEADER_NAME}")

Expand Down
2 changes: 2 additions & 0 deletions cmake/CorrosionGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ function(_generator_add_package_targets)
)
endif()
list(APPEND corrosion_targets ${target_name})
set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
# Note: "bin" is mutually exclusive with "staticlib/cdylib", since `bin`s are seperate crates from libraries.
elseif("bin" IN_LIST kinds)
set(bin_byproduct "")
Expand Down Expand Up @@ -188,6 +189,7 @@ function(_generator_add_package_targets)
)
endif()
list(APPEND corrosion_targets ${target_name})
set_property(TARGET "${target_name}" PROPERTY COR_CARGO_PACKAGE_NAME "${package_name}" )
else()
# ignore other kinds (like examples, tests, build scripts, ...)
endif()
Expand Down

0 comments on commit b11a338

Please sign in to comment.