Mix C, Rust and Zig code in a Raspberry Pico SDK (CMake) project
Go to file
Triss d1a0fdc9dd initial 2025-01-09 23:57:00 +01:00
cmake initial 2025-01-09 23:57:00 +01:00
inc initial 2025-01-09 23:57:00 +01:00
src initial 2025-01-09 23:57:00 +01:00
.gitignore initial 2025-01-09 23:57:00 +01:00
CMakeLists.txt initial 2025-01-09 23:57:00 +01:00
README.md initial 2025-01-09 23:57:00 +01:00

README.md

rpsdk-mix-c-rs-zig

Mix C, Rust and Zig in a Raspberry Pico SDK (CMake) project.

Yeah.

Usage

Don't.

Other than that,

Add Rust code to your project

set(RUST_TARGET thumbv6m-none-eabi)
set(RUST_CPU cortex-m0plus)
list(APPEND BINDGEN_CFLAGS "--use-core" "--rust-edition=2021")
list(APPEND RUST_CFLAGS "--edition=2021")
include(cmake/rust-obj.cmake)
add_rust_object(${PROJECT}
	# rust top level module
	${CMAKE_CURRENT_SOURCE_DIR}/src/rustcode.rs
	# optional: path to header file to use for c2rs bindgen stuff (currently limited to one, sorry)
	${CMAKE_CURRENT_SOURCE_DIR}/inc/clib.h
)

Bindgen and CBindgen are used automatically to generate C headers from the Rust code, and vice versa.

Add Zig code to your project

set(ZIG_TARGET thumb-freestanding-gnueabi)
set(ZIG_CPU cortex_m0plus)
list(APPEND ZIG_CFLAGS "-ffunction-sections" "-fdata-sections")
include(cmake/zig-obj.cmake)
add_zig_object(${PROJECT}
	# zig top level module
	${CMAKE_CURRENT_SOURCE_DIR}/src/zigcode.zig
)

Known issues and limitations

  • Sometimes, compilation fails with rustc failing to copy an object file to the destination path. Reason for this is unknown, just retrying the build seems to work.
  • References between Zig and Rust code aren't really possible automatically (can't generate Rust via Bindgen from Zig-generated headers, or include Bindgen-generated C headers in Zig code). This is because Zig can't generate these headers without also compiling the code.
  • Only one C header can be Bindgen'ed per Rust "root" module/crate (one crate gets compiled into an object file and included in the main link).

License

be gay, do crimes, death to america