Skip to content

Commit

Permalink
cbindgen: Allow generating multiple headers on one Rust target
Browse files Browse the repository at this point in the history
User may want to generate both a C and a separate C++ header file
  • Loading branch information
jschwe committed Apr 23, 2024
1 parent 3bf5f1c commit d60944e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
- 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. [#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

Expand Down
16 changes: 12 additions & 4 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1681,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)
Expand Down

0 comments on commit d60944e

Please sign in to comment.