From b11a3383d349d9e11dd5cd7326ae7b42735e7830 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Thu, 2 May 2024 19:35:54 +0200 Subject: [PATCH] cbindgen: Automatically detect package name. 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). --- RELEASES.md | 3 +++ cmake/Corrosion.cmake | 18 ++++++++++++------ cmake/CorrosionGenerator.cmake | 2 ++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 7e22041f..71571270 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -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 diff --git a/cmake/Corrosion.cmake b/cmake/Corrosion.cmake index 00d3d47b..e4fb52ed 100644 --- a/cmake/Corrosion.cmake +++ b/cmake/Corrosion.cmake @@ -1498,8 +1498,8 @@ ANCHOR: corrosion_cbindgen ```cmake corrosion_cbindgen( TARGET - CARGO_PACKAGE HEADER_NAME + [CARGO_PACKAGE ] [MANIFEST_DIRECTORY ] [CBINDGEN_VERSION ] [FLAGS ... ] @@ -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 @@ -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}") diff --git a/cmake/CorrosionGenerator.cmake b/cmake/CorrosionGenerator.cmake index 594645af..d29744fb 100644 --- a/cmake/CorrosionGenerator.cmake +++ b/cmake/CorrosionGenerator.cmake @@ -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 "") @@ -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()