Skip to content

Commit

Permalink
ziggy
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyBanks committed Feb 29, 2020
0 parents commit 0b8a989
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
/target
*.so
*.so.*
*.a
*.h
*.o

5 changes: 5 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions Cargo.toml
@@ -0,0 +1,8 @@
[package]
name = "ziggy"
version = "0.1.0"
authors = ["Jeremy Banks <_@jeremy.ca>"]
edition = "2018"
links = "ziggy"

[dependencies]
34 changes: 34 additions & 0 deletions build.rs
@@ -0,0 +1,34 @@
fn main() {
let zig_bin = "./zig";
let project_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let out_dir = std::env::var("OUT_DIR").unwrap();
let lib_dir = out_dir + "/zig-lib";
let lib_name = "ziggy";
let src_path = project_dir + "/src/ziggy.zig";

let output = std::process::Command::new(zig_bin)
.args(&[
"build-lib",
"-fPIC",
"--bundle-compiler-rt",
"--output-dir",
&lib_dir,
&src_path,
])
.output();

match output {
Err(error) => {
panic!("unable to execute zig: {:?}", error);
}
Ok(output) => {
if !output.status.success() {
panic!("zig compilation failed: {:?}", output.stderr);
}
}
}

println!("cargo:rerun-if-changed={}", src_path);
println!("cargo:rustc-link-search=native={}", lib_dir);
println!("cargo:rustc-link-lib=static={}", lib_name);
}
13 changes: 13 additions & 0 deletions src/main.rs
@@ -0,0 +1,13 @@
pub mod ziggy {
#[link(name = "ziggy")]
extern "C" {
pub fn ziggy() -> ();
}
}

fn main() {
println!("Hello, world from Rust!");
unsafe {
ziggy::ziggy();
}
}
6 changes: 6 additions & 0 deletions src/ziggy.zig
@@ -0,0 +1,6 @@
const std = @import("std");

export fn ziggy() void {
const stdout = &std.io.getStdOut().outStream().stream;
stdout.print("Hello, {} from Zig!\n", .{"world"}) catch unreachable;
}
1 change: 1 addition & 0 deletions zig

0 comments on commit 0b8a989

Please sign in to comment.