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

cbindgen: Pass Rust target-triple to cbindgen #507

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
@@ -12,16 +12,23 @@
- Support using the `$<CONFIG>` generator expression in `OUTPUT_DIRECTORY`. [#459]
- If `corrosion_link_libraries()` is called on a Rust static library target, then
`target_link_libraries()` is called to propogate the dependencies to C/C++ consumers.
Previously a warning was emitted in this case and the arguments ignored.
Previously a warning was emitted in this case and the arguments ignored. [#506]
- `corrosion_experimental_cbindgen()` can now be called multiple times on the same Rust target,
as long as the output header name differs. This may be useful to generate separate C and C++
bindings. [#507]

### Fixes

- Combine `-framework` flags on macos to avoid linker deduplication errors [#455]
- Set the `AR_<triple>` variable for `cc-rs` (except for msvc targets) [#456]
- `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]

[#459]: https://github.com/corrosion-rs/corrosion/pull/459
[#456]: https://github.com/corrosion-rs/corrosion/pull/456
[#455]: https://github.com/corrosion-rs/corrosion/pull/455
[#506]: https://github.com/corrosion-rs/corrosion/pull/506
[#507]: https://github.com/corrosion-rs/corrosion/pull/507

# v0.4.7 (2024-01-19)

33 changes: 25 additions & 8 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
@@ -1539,10 +1539,13 @@ between multiple invocations of this function.
ANCHOR_END: corrosion_cbindgen
#]=======================================================================]
function(corrosion_experimental_cbindgen)
# Todo:
# - set the target-triple via the TARGET env variable based on the target triple for the rust crate.
set(OPTIONS "")
set(ONE_VALUE_KEYWORDS TARGET CARGO_PACKAGE MANIFEST_DIRECTORY HEADER_NAME CBINDGEN_VERSION)
set(ONE_VALUE_KEYWORDS
TARGET
CARGO_PACKAGE
MANIFEST_DIRECTORY
HEADER_NAME
CBINDGEN_VERSION)
set(MULTI_VALUE_KEYWORDS "FLAGS")
cmake_parse_arguments(PARSE_ARGV 0 CCN "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")

@@ -1557,6 +1560,10 @@ function(corrosion_experimental_cbindgen)
set(rust_target "${CCN_TARGET}")
unset(package_manifest_dir)


set(hostbuild_override "$<BOOL:$<TARGET_PROPERTY:${rust_target},${_CORR_PROP_HOST_BUILD}>>")
set(cbindgen_target_triple "$<IF:${hostbuild_override},${_CORROSION_RUST_CARGO_HOST_TARGET},${_CORROSION_RUST_CARGO_TARGET}>")

if(TARGET "${rust_target}")
get_target_property(package_manifest_path "${rust_target}" INTERFACE_COR_PACKAGE_MANIFEST_PATH)
if(NOT EXISTS "${package_manifest_path}")
@@ -1653,7 +1660,9 @@ function(corrosion_experimental_cbindgen)
OUTPUT
"${generated_header}"
COMMAND
"${cbindgen}"
"${CMAKE_COMMAND}" -E env
TARGET="${cbindgen_target_triple}"
"${cbindgen}"
--output "${generated_header}"
--crate "${rust_cargo_package}"
${depfile_cbindgen_arg}
@@ -1672,11 +1681,19 @@ function(corrosion_experimental_cbindgen)
)
endif()

add_custom_target(_corrosion_cbindgen_${rust_target}_bindings
DEPENDS "${generated_header}"
COMMENT "Generate cbindgen bindings for package ${rust_cargo_package}"
if(NOT TARGET "_corrosion_cbindgen_${rust_target}_bindings")
add_custom_target(_corrosion_cbindgen_${rust_target}_bindings
COMMENT "Generate cbindgen bindings for package ${rust_cargo_package}"
)
endif()
# Users might want to call cbindgen multiple times, e.g. to generate separate C++ and C header files.
string(MAKE_C_IDENTIFIER "${output_header_name}" header_identifier )
add_custom_target("_corrosion_cbindgen_${rust_target}_bindings_${header_identifier}"
DEPENDS "${generated_header}"
COMMENT "Generate ${generated_header} for ${rust_target}"
)
add_dependencies(${rust_target} _corrosion_cbindgen_${rust_target}_bindings)
add_dependencies("_corrosion_cbindgen_${rust_target}_bindings" "_corrosion_cbindgen_${rust_target}_bindings_${header_identifier}")
add_dependencies(${rust_target} "_corrosion_cbindgen_${rust_target}_bindings")
endfunction()

# Parse the version of a Rust package from it's package manifest (Cargo.toml)