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

Improve cbindgen by regenerating bindings only when necessary #435

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
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
30 changes: 22 additions & 8 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,15 @@ between multiple invocations of this function.

[cbindgen]: https://github.com/eqrion/cbindgen

### Current limitations

- Cbindgens (optional) macro expansion feature internally actually builds the crate / runs the build script.
For this to work as expected in all cases, we probably need to set all the same environment variables
as when corrosion builds the crate. However the crate is a **library**, so we would need to figure out which
target builds it - and if there are multiple, potentially generate bindings per-target?
Alternatively we could add support of setting some environment variables on rlibs, and pulling that
information in when building the actual corrosion targets
Alternatively we could restrict corrosions support of this feature to actual imported staticlib/cdylib targets.
ANCHOR_END: corrosion_cbindgen
#]=======================================================================]
function(corrosion_experimental_cbindgen)
Expand Down Expand Up @@ -1672,6 +1681,8 @@ function(corrosion_experimental_cbindgen)
set(corrosion_generated_dir "${CMAKE_CURRENT_BINARY_DIR}/corrosion_generated")
set(generated_dir "${corrosion_generated_dir}/cbindgen/${rust_target}")
set(header_placement_dir "${generated_dir}/include/")
set(depfile_placement_dir "${generated_dir}/depfile")
set(generated_depfile "${depfile_placement_dir}/${output_header_name}.d")
set(generated_header "${header_placement_dir}/${output_header_name}")
message(STATUS "rust target is ${rust_target}")
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.23")
Expand All @@ -1693,29 +1704,32 @@ function(corrosion_experimental_cbindgen)
# This may be different from $header_placement_dir since the user specified HEADER_NAME may contain
# relative directories.
get_filename_component(generated_header_dir "${generated_header}" DIRECTORY)
file(MAKE_DIRECTORY ${generated_header_dir})
file(MAKE_DIRECTORY "${generated_header_dir}")

unset(depfile_cbindgen_arg)
unset(depfile_cmake_arg)
get_filename_component(generated_depfile_dir "${generated_depfile}" DIRECTORY)
file(MAKE_DIRECTORY "${generated_depfile_dir}")
set(depfile_cbindgen_arg "--depfile=${generated_depfile}")

# Todo: Add dependencies on the source Rust files here to get proper dependency rules (hard).
# For now we just specify a dummy file we won't generate as an additional output, so that the rule
# always runs.
# Future Options: a) upstream depfile creation into cbindgen, b) spider our way through the source-code ourselves.
# c) ask the user to specify the dependencies (in order to relax the rules).
add_custom_command(
OUTPUT
"${generated_header}" "${CMAKE_CURRENT_BINARY_DIR}/corrosion/non-existing-file.h"
"${generated_header}"
COMMAND
"${cbindgen}"
--output "${generated_header}"
--crate "${rust_target}"
${depfile_cbindgen_arg}
${CCN_FLAGS}
COMMENT "Generate cbindgen bindings for crate ${rust_target}"
DEPFILE "${generated_depfile}"
COMMAND_EXPAND_LISTS
WORKING_DIRECTORY "${package_manifest_dir}"
)

if(NOT installed_cbindgen)
add_custom_command(
OUTPUT "${generated_header}" "${CMAKE_CURRENT_BINARY_DIR}/corrosion/non-existing-file.h"
OUTPUT "${generated_header}"
APPEND
DEPENDS _corrosion_cbindgen
)
Expand Down
2 changes: 2 additions & 0 deletions test/cbindgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ set_tests_properties(cbindgen_rust2cpp_run_cpp-exe PROPERTIES PASS_REGULAR_EXPRE
# - A rust lib that is used by a rust executable
# - cbindgen creates bindings for the rust-lib
# - c++ code uses the rust lib and is used in turn by the rust bin.

# todo: add a test for the DEPFILE and correct regenerating if the sources are touched.
3 changes: 3 additions & 0 deletions test/cbindgen/rust2cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ int main(int argc, char **argv) {
add_point(&p1, NULL);
assert(p1.x == 100);
assert(p1.y == 100);

assert(OTHER_MOD_MAGIC_NUMBER == 192312312);
assert(FFI_MAGIC_NUMBER == 0xFDA00184);
}
9 changes: 9 additions & 0 deletions test/cbindgen/rust2cpp/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/cbindgen/rust2cpp/rust/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
language = "C++"
include_version = true


3 changes: 3 additions & 0 deletions test/cbindgen/rust2cpp/rust/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Just a module that contains some entries that should be parsed by cbindgen.

pub const FFI_MAGIC_NUMBER: u64 = 0xFDA0_0184;
3 changes: 3 additions & 0 deletions test/cbindgen/rust2cpp/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub const MAGIC_NUMBER: u64 = 0xABCD_EFAB;

pub mod ffi;
pub mod other_mod;

#[derive(Debug)]
#[repr(C)]
pub struct Point {
Expand Down
1 change: 1 addition & 0 deletions test/cbindgen/rust2cpp/rust/src/other_mod/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const OTHER_MOD_MAGIC_NUMBER: u32 = 192312312;