Skip to content

Update to zig 0.13.0 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
20 changes: 20 additions & 0 deletions .github/workflows/zig-checkers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: zig-checkers
on:
push:
permissions: {}

jobs:
run-zig-build:
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup zig
uses: mlugg/[email protected]
with:
version: 0.13.0
- name: zig-build
run: zig build
24 changes: 22 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
*.swp
zig-*
# This file is for zig-specific build artifacts.
# If you have OS-specific or editor-specific files to ignore,
# such as *.swp or .DS_Store, put those in your global
# ~/.gitignore and put this in your ~/.gitconfig:
#
# [core]
# excludesfile = ~/.gitignore
#
# Cheers!
# -andrewrk
.zig-cache/
zig-out/
/release/
/debug/
/build/
/build-*/
/docgen_tmp/
# Although this was renamed to .zig-cache, let's leave it here for a few
# releases to make it less annoying to work with multiple branches.
zig-cache/

# header-gen directory
headers/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Here are the following supported Zig language features:
- [x] Extern Unions
- [x] Extern Enums

## Zig version

zig 0.13.0

## Getting started

Given the following Zig code as the file `src/fancylib.zig` and using the C generator:
Expand Down
31 changes: 18 additions & 13 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
const Builder = @import("std").build.Builder;

const std = @import("std");
const warn = std.debug.print;

// This build.zig is only used as an example of using header_gen

pub fn build(b: *Builder) void {
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});

const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{});

// HEADER GEN BUILD STEP
const exe = b.addExecutable("example", "src/example/exports.zig");
exe.main_pkg_path = "src/";
exe.setTarget(target);
exe.setBuildMode(mode);
exe.addPackagePath("header_gen", "src/header_gen.zig");
exe.install();
const header_gen = b.addModule("header_gen", .{
.root_source_file = b.path("src/header_gen.zig"),
});
const exe = b.addExecutable(.{
.name = "example",
.root_source_file = b.path("src/example/exports.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("header_gen", header_gen);
b.installArtifact(exe);

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);
}

const run_step = b.step("headergen", "Run the app");
run_step.dependOn(&run_cmd.step);
}
13 changes: 13 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.{
.name = "header_gen",
.version = "0.0.0",
.dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"example",
"LICENSE",
"README.md",
},
}
14 changes: 7 additions & 7 deletions src/deps_graph.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn DepsGraph(comptime T: type) type {
pub const BeginSymbolError = error{ DuplicateSymbol, OutOfMemory };

pub fn beginSymbol(self: *Self, name: []const u8, payload: T) BeginSymbolError!void {
var result = try self.symbols.getOrPut(name);
const result = try self.symbols.getOrPut(name);

if (result.found_existing) {
return error.DuplicateSymbol;
Expand Down Expand Up @@ -148,13 +148,13 @@ pub fn DepsGraph(comptime T: type) type {
pub const EndSymbolError = error{OutOfMemory};

pub fn createNode(comptime V: type, data: V, allocator: Allocator) !*TailQueue(V).Node {
var node = try allocator.create(TailQueue(V).Node);
const node = try allocator.create(TailQueue(V).Node);
node.* = .{ .data = data };
return node;
}

pub fn endSymbol(self: *Self) EndSymbolError!void {
var current_symbol = self.current_symbol orelse return;
const current_symbol = self.current_symbol orelse return;

var unblock_queue = std.TailQueue(EmittedSymbol){};

Expand Down Expand Up @@ -199,9 +199,9 @@ pub fn DepsGraph(comptime T: type) type {
}

pub fn readEmitted(self: *Self) ?EmittedSymbol {
var symbol_node = self.emitted.popFirst() orelse return null;
const symbol_node = self.emitted.popFirst() orelse return null;

var symbol = symbol_node.data;
const symbol = symbol_node.data;

self.allocator.destroy(symbol_node);

Expand Down Expand Up @@ -277,7 +277,7 @@ pub fn DepsGraph(comptime T: type) type {
_ = dependency_name;
var maybe_dep_index: ?usize = null;

for (self.dependencies.items) |dependency, i| {
for (self.dependencies.items, 0..) |dependency, i| {
if (dependency.eqlName(dependency)) {
maybe_dep_index = i;
break;
Expand Down Expand Up @@ -430,7 +430,7 @@ test "Blocked symbols iterator" {
try expect(deps.readEmitted() == null);

var iter = deps.blockedIterator();
var symbol = iter.next();
const symbol = iter.next();

try expect(symbol != null);
try expectEqualStrings(symbol.?.name, "TextPosition");
Expand Down
6 changes: 3 additions & 3 deletions src/generators/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub const C_Generator = struct {

pub fn init(comptime src_file: []const u8, dst_dir: *Dir) Self {

var file = dst_dir.createFile(comptime filebase(src_file) ++ ".h", .{}) catch
const file = dst_dir.createFile(comptime filebase(src_file) ++ ".h", .{}) catch
@panic("Failed to create header file for source: " ++ src_file);

var res = Self{ .file = file };
Expand Down Expand Up @@ -48,7 +48,7 @@ pub const C_Generator = struct {
self.writeType(meta.return_type.?);
self.write(" " ++ name ++ "(");

inline for (meta.params) |arg, i| {
inline for (meta.params, 0..) |arg, i| {
self.writeType(arg.type.?);
//TODO: Figure out how to get arg names; for now just do arg0..argN
_ = self.file.writer().print(" arg{}", .{i}) catch unreachable;
Expand Down Expand Up @@ -93,7 +93,7 @@ pub const C_Generator = struct {
self.write("enum " ++ name ++ " {\n");

comptime var last = 0;
inline for (meta.fields) |field, i| {
inline for (meta.fields, 0..) |field, i| {
self.write(" " ++ field.name);

// if field value is unexpected/custom, manually define it
Expand Down
6 changes: 3 additions & 3 deletions src/generators/ordered.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn Ordered_Generator(comptime Generator: type) type {
const Self = @This();

pub fn init(comptime src_file: []const u8, dst_dir: *Dir) Self {
var allocator = std.heap.page_allocator;
const allocator = std.heap.page_allocator;

return Self{
.inner_gen = Generator.init(src_file, dst_dir),
Expand All @@ -82,7 +82,7 @@ pub fn Ordered_Generator(comptime Generator: type) type {
}

fn getNextPhaseFor(self: *Self, symbol_name: []const u8, partial: bool) !?SymbolPhase {
var result = try self.emitted_phase.getOrPut(symbol_name);
const result = try self.emitted_phase.getOrPut(symbol_name);

if (!result.found_existing) {
result.value_ptr.* = if (partial) .Signature else .Full;
Expand All @@ -106,7 +106,7 @@ pub fn Ordered_Generator(comptime Generator: type) type {
const partial = if (emitted.symbol.payload == .Fn) false else emitted.partial;
_ = partial;

var phase = self.getNextPhaseFor(emitted.symbol.name, emitted.partial) catch unreachable orelse continue;
const phase = self.getNextPhaseFor(emitted.symbol.name, emitted.partial) catch unreachable orelse continue;

switch (emitted.symbol.payload) {
.Struct => |meta| self.inner_gen.gen_struct(emitted.symbol.name, meta, phase),
Expand Down
8 changes: 4 additions & 4 deletions src/generators/python.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const Python_Generator = struct {
const Self = @This();

pub fn init(comptime src_file: []const u8, dst_dir: *Dir) Self {
var file = dst_dir.createFile(comptime filebase(src_file) ++ ".py", .{}) catch
const file = dst_dir.createFile(comptime filebase(src_file) ++ ".py", .{}) catch
@panic("Failed to create header file for source: " ++ src_file);

var res = Self{ .file = file };
Expand Down Expand Up @@ -45,7 +45,7 @@ pub const Python_Generator = struct {
pub fn gen_func(self: *Self, name: []const u8, meta: FnMeta) void {
self.print("lib.{s}.argtypes = [", .{name});

for (meta.params) |arg, i| {
for (meta.params, 0..) |arg, i| {
if (arg.type) |t| {
self.writeType(t.*);
} else {
Expand Down Expand Up @@ -77,7 +77,7 @@ pub const Python_Generator = struct {
self.write("\t_fields_ = [");
}

for (fields) |field, i| {
for (fields, 0..) |field, i| {
if (i > 0) {
self.write(prefix);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ pub const Python_Generator = struct {
var was_lower: bool = false;
var is_upper: bool = undefined;

for (str) |char, i| {
for (str, 0..) |char, i| {
is_upper = std.ascii.isUpper(char);

if (char == '_' and i > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/header_gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn validateGenerator(comptime Generator: type) void {
}

pub fn HeaderGen(comptime S: type, comptime libname: []const u8) type {
comptime var all_decls: []const Declaration = @typeInfo(S).Struct.decls;
const all_decls: []const Declaration = @typeInfo(S).Struct.decls;

return struct {
decls: @TypeOf(all_decls) = all_decls,
Expand Down
Loading