zigc

Crates.iozigc
lib.rszigc
version0.0.3
sourcesrc
created_at2023-01-30 22:49:33.674632
updated_at2023-02-02 18:41:22.732912
descriptionA tool for compiling and linking Zig libraries to Rust projects.
homepagehttps://docs.rs/zigc
repositoryhttps://github.com/emilHof/zigc
max_upload_size
id772224
size12,964
(emilHof)

documentation

https://docs.rs/zigc

README

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;
}
  1. Import the zigc and libc crates:
[dependencies]
libc = "*"

[build-dependencies]
zigc = "*"
  1. 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.
/* build.rs */
fn main() {
    zigc::Build::new()
        .file("./src/main.zig")
        .finish();
}
  1. 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}");
}
  1. Build/run your crate.
$ cargo run
4

Roadmap

  • Basic .zig compilation
  • MVP linking of .so files to cargo projects.
  • Add logging.
  • Automatic target specification using cargo's TARGET flag.
  • Add more options to Build
    • Additional flags (-cflags, -target, -mcpu, etc)
    • Name output library file.
    • Specify additional include libraries
  • Allow compilation and linking of static Zig libraries.
  • Ability to compile and link multiple .zig files.

Contribute

Any discovered issues, feature requests, and pull request are highly encouraged and appreciated! :)

Commit count: 19

cargo fmt