Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emilHof/zigc
A tool for compiling and linking Zig libraries to Rust projects.
https://github.com/emilHof/zigc
Last synced: about 1 month ago
JSON representation
A tool for compiling and linking Zig libraries to Rust projects.
- Host: GitHub
- URL: https://github.com/emilHof/zigc
- Owner: emilHof
- License: mit
- Created: 2023-01-30T21:44:50.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-02T18:42:10.000Z (about 2 years ago)
- Last Synced: 2025-01-10T06:08:53.484Z (about 1 month ago)
- Language: Rust
- Size: 23.4 KB
- Stars: 14
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rust-list - emilHof/zigc - lang.org/) projects. (FFI Bindings)
- awesome-rust-list - emilHof/zigc - lang.org/) projects. (FFI Bindings)
README
Zigc aims to provide the basic functionality for compiling and linking [Zig](https://ziglang.org/)
libraries into your [Rust](https://www.rust-lang.org/) projects.### Disclaimer
[zig](https://ziglang.org/download/) is a requirement to compile `.zig` files with this crate.
### Usage
Given the following function definition as an example:
```zig
// main.zig
const std = @import("std");export fn add(a: c_int, b: c_int) callconv(.C) c_int {
return a + b;
}
```1. Import the `zigc` and `libc` crates:
```toml
[dependencies]
libc = "*"[build-dependencies]
zigc = "*"
```2. Specify the `.zig` source file in your build script and zigc automatically compiles it into the right
directory and links the artifacts into your rust binary.```rust
/* build.rs */
fn main() {
zigc::Build::new()
.file("./src/main.zig")
.finish();
}
```3. Import the functions in your Rust source code.
```rust
/* main.rs */
extern crate libc;
use libc::c_int;#[link(name = "main", kind = "dylib")]
extern "C" {
fn add(a: c_int, b: c_int) -> c_int;
}fn main() {
let res = unsafe { add(2, 2) };
println!("{res}");
}
```4. Build/run your crate.
```
$ cargo run
4
```### Roadmap
- [x] Basic `.zig` compilation
- [x] MVP linking of `.so` files to cargo projects.
- [x] Add logging.
- [x] Automatic target specification using cargo's `TARGET` flag.
- [x] Allow compilation and linking of `static` Zig libraries.
- [ ] Add more options to `Build`
- [ ] Additional flags (`-cflags`, `-target`, `-mcpu`, etc)
- [x] Name output library file.
- [ ] Specify additional `include` libraries
- [ ] Ability to compile and link multiple `.zig` files.### Contribute
Any discovered issues, feature requests, and pull request are highly encouraged and appreciated! :)