5 releases
| 0.0.3 | Feb 2, 2023 |
|---|---|
| 0.0.2 | Feb 1, 2023 |
| 0.0.1 | Jan 31, 2023 |
| 0.0.0-alpha0.2 | Jan 31, 2023 |
| 0.0.0-alpha0.1 | Jan 30, 2023 |
#31 in #compilation
27 downloads per month
Used in zsling
12KB
252 lines
Zigc aims to provide the basic functionality for compiling and linking Zig libraries into your Rust projects.
Disclaimer
zig is a requirement to compile .zig files with this crate.
Usage
Given the following function definition as an example:
// main.zig
const std = @import("std");
export fn add(a: c_int, b: c_int) callconv(.C) c_int {
return a + b;
}
- Import the
zigcandlibccrates:
[dependencies]
libc = "*"
[build-dependencies]
zigc = "*"
- Specify the
.zigsource file in your build script and zigc automatically compiles it into the right directory and links the artifacts into your rust binary.
/* build.rs */
fn main() {
zigc::Build::new()
.file("./src/main.zig")
.finish();
}
- Import the functions in your Rust source code.
/* 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}");
}
- Build/run your crate.
$ cargo run
4
Roadmap
- Basic
.zigcompilation - MVP linking of
.sofiles to cargo projects. - Add logging.
- Automatic target specification using cargo's
TARGETflag. - Add more options to
Build- Additional flags (
-cflags,-target,-mcpu, etc) - Name output library file.
- Specify additional
includelibraries
- Additional flags (
- Allow compilation and linking of
staticZig libraries. - Ability to compile and link multiple
.zigfiles.
Contribute
Any discovered issues, feature requests, and pull request are highly encouraged and appreciated! :)
Dependencies
~500–790KB
~14K SLoC