Skip to content

Commit

Permalink
Zig update
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jun 9, 2023
1 parent 758a6a1 commit f68766f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 122 deletions.
26 changes: 21 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,37 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: goto-bus-stop/setup-zig@v1
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: build
run: echo -e "Rust build test" && cargo run --release && echo -e "Zig build test:" && zig build run -Drelease-safe
- name: Install features
run: |
sudo apt update && sudo apt install -y \
qemu-user-binfmt gcc-riscv64-linux-gnu \
&& rustup target add riscv64gc-unknown-linux-gnu
- name: build RISC-V64
run: |
echo -e "Rust build GNU test" \
&& cargo run --release --target riscv64gc-unknown-linux-gnu \
&& echo -e "Zig build Musl test" \
&& zig build run -Doptimize=ReleaseSafe -Dtarget=riscv64-linux-musl
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: goto-bus-stop/setup-zig@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
- run: rm -fr zig-cache && cargo fmt -- --check && zig fmt --check --ast-check */*.zig
- run: |
cargo fmt --check --all \
&& zig fmt --check --ast-check **.zig
100 changes: 0 additions & 100 deletions Cargo.lock

This file was deleted.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
riscv = "0.8.0"
rustix = "0.31.1"
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ cargo zigbuild --release
You will need [Zig](https://ziglang.org/)

```sh
# zig version v0.10 or higher (default self-hosting compiler [stage2 or stage3])
zig build -Drelease-safe|-Drelease-fast|-Drelease-small -Dtarget=arch-os-libc
# zig version v0.11 or higher (default self-hosting compiler [stage3])
zig build -Doptimize=<mode> -Dtarget=arch-os-libc
# mode = ReleaseSafe|ReleaseFast|ReleaseSmall

# specific target
zig build -Drelease-safe|-Drelease-fast|-Drelease-small -Dtarget=riscv64-linux-musl -Dcpu=baseline_rv64+v # Allwinner D1
zig build -Doptimize=<mode> -Dtarget=riscv64-linux -Dcpu=baseline_rv64+v # Allwinner D1

# more features RISC-V: https://github.com/lupyuen/zig-bl602-nuttx/issues/1

# execute
zig build run
# Execute (after builded)
./zig-out/bin/hello-zig
# or run directly (default: host target)
zig build run -Doptimize=<mode> -Dtarget=riscv64-linux

# all targets
zig targets | jq .libc
Expand Down
19 changes: 10 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
Expand All @@ -9,16 +9,17 @@ pub fn build(b: *std.build.Builder) void {

// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{});

b.prominent_compile_errors = true;
const exe = b.addExecutable(.{
.name = "hello-zig",
.target = target,
.optimize = optimize,
.root_source_file = .{ .path = "src/main.zig" },
});
b.installArtifact(exe);

const exe = b.addExecutable("hello-zig", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();

const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ fn main() {
let arch = "RISC-V";
println!("Let's have a look at your shiny {} system! :)\n", arch);

#[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
{
use riscv::register::{cycle, fcsr, time};
let f = fcsr::read();
let c = cycle::read();
let t = time::read();
println!("fcsr {:?} cycle {:?} time {:?}\n", f, c, t);
// let e = uepc::read();
// let s = ustatus::read();
// let t = utval::read();
// println!("uepc {:?} utval {:?} ustatus {:?} utval {:?}\n", e, s, t);
}

let mut f = "/proc/cpuinfo";
let cpuinfo = fs::read_to_string(f).expect("cpuinfo err");
println!("cpuinfo:\n{}", cpuinfo);
Expand Down
6 changes: 3 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const stdoutFile = std.io.getStdOut();
const stdout = std.io.getStdOut().writer();

pub fn main() !void {
try stdout.print("\nZig Info:\n\nVersion: {}\nStage: {}\n", .{ builtin.zig_backend, builtin.zig_version });
try stdout.print("\nZig Info:\n\nVersion: {}\nStage: {s}\n", .{ builtin.zig_version, @tagName(builtin.zig_backend) });

// .riscv32 - don't work:
// https://github.com/ziglang/zig/blob/c955379504d4866f9c474c50317b2a0da18ee631/lib/std/os/linux.zig#L35-L44
const arch = switch (builtin.cpu.arch) {
.i386 => "x86",
.x86 => "x86",
.x86_64 => "x86_64",
.aarch64 => "ARM64",
.riscv64 => "RISC-V",
.mipsel, .mips => "MIPS",
else => @compileError("Unsupported CPU Architecture"),
};
try stdout.print("\nLet's have a look at your shiny {s} system! :)\n\n", .{arch});
try stdout.print("\nLet's have a look at your shiny {s} - {s} system! :)\n\n", .{arch, builtin.cpu.model.name});

// Read HW linux info

Expand Down

0 comments on commit f68766f

Please sign in to comment.