Skip to content

Commit

Permalink
update for zig 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Aug 4, 2023
1 parent f74d750 commit 7929bd7
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: "0.11.0-dev.1570+693b12f8e"
version: "0.11.0"

- run: zig version
- run: zig env
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ zigmod aq install 1/nektro/ziglint
```

## Built With
- Zig master `0.11.0-dev.1570+693b12f8e`
- Zig master `0.11.0`
- [Zigmod](https://github.com/nektro/zigmod) package manager
- See [`zig.mod`](./zig.mod)

Expand All @@ -46,7 +46,7 @@ jobs:
- name: Setup Zig
uses: goto-bus-stop/setup-zig@v1
with:
version: "0.11.0-dev.1570+693b12f8e"
version: "0.11.0"

- uses: nektro/actions-setup-zigmod@v1
- run: zigmod aq install 1/nektro/ziglint
Expand Down
9 changes: 4 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ const deps = @import("./deps.zig");

pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug;

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

const exe = b.addExecutable(.{ .name = "ziglint", .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize });
const exe = b.addExecutable(.{ .name = "ziglint", .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = mode });
deps.addAllTo(exe);
exe.install();
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);
Expand Down
64 changes: 38 additions & 26 deletions deps.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ pub const GitExactStep = struct {
pub fn create(b: *std.build.Builder, url: string, commit: string) *GitExactStep {
var result = b.allocator.create(GitExactStep) catch @panic("memory");
result.* = GitExactStep{
.step = std.build.Step.init(.custom, b.fmt("git clone {s} @ {s}", .{ url, commit }), b.allocator, make),
.step = std.build.Step.init(.{
.id = .custom,
.name = b.fmt("git clone {s} @ {s}", .{ url, commit }),
.owner = b,
.makeFn = make,
}),
.builder = b,
.url = url,
.commit = commit,
Expand All @@ -22,7 +27,7 @@ pub const GitExactStep = struct {
var urlpath = url;
urlpath = trimPrefix(u8, urlpath, "https://");
urlpath = trimPrefix(u8, urlpath, "git://");
const repopath = b.fmt("{s}/zigmod/deps/git/{s}/{s}", .{ b.cache_root, urlpath, commit });
const repopath = b.fmt("{s}/zigmod/deps/git/{s}/{s}", .{ b.cache_root.path.?, urlpath, commit });
flip(std.fs.cwd().access(repopath, .{})) catch return result;

var clonestep = std.build.RunStep.create(b, "clone");
Expand All @@ -32,24 +37,26 @@ pub const GitExactStep = struct {
var checkoutstep = std.build.RunStep.create(b, "checkout");
checkoutstep.addArgs(&.{ "git", "-C", repopath, "checkout", "-q", commit });
result.step.dependOn(&checkoutstep.step);
checkoutstep.step.dependOn(&clonestep.step);

return result;
}

fn make(step: *std.build.Step) !void {
fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
_ = step;
_ = prog_node;
}
};

pub fn fetch(exe: *std.build.LibExeObjStep) void {
const b = exe.builder;
const b = exe.step.owner;
inline for (comptime std.meta.declarations(package_data)) |decl| {
const path = &@field(package_data, decl.name).entry;
const root = if (@field(package_data, decl.name).store) |_| b.cache_root else ".";
const root = if (@field(package_data, decl.name).store) |_| b.cache_root.path.? else ".";
if (path.* != null) path.* = b.fmt("{s}/zigmod/deps{s}", .{ root, path.*.? });
}
exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-extras", "3fe700ebc8ff66966abe145166bfdf546b3a8422").step);
exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-flag", "07ea6a3daa950f7bbd8bbd60c0cc2251806fde95").step);
exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-extras", "8628846c832086d97f28c54eab157cda389c319a").step);
exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-flag", "4a03000239d5e05062cf9344a08e0dc43ad8ebd1").step);
exe.step.dependOn(&GitExactStep.create(b, "https://github.com/nektro/zig-range", "4b2f12808aa09be4b27a163efc424dd4e0415992").step);
}

Expand All @@ -68,7 +75,7 @@ fn flip(foo: anytype) !void {
pub fn addAllTo(exe: *std.build.LibExeObjStep) void {
checkMinZig(builtin.zig_version, exe);
fetch(exe);
const b = exe.builder;
const b = exe.step.owner;
@setEvalBranchQuota(1_000_000);
for (packages) |pkg| {
const moddep = pkg.zp(b);
Expand All @@ -78,22 +85,22 @@ pub fn addAllTo(exe: *std.build.LibExeObjStep) void {
var vcpkg = false;
inline for (comptime std.meta.declarations(package_data)) |decl| {
const pkg = @as(Package, @field(package_data, decl.name));
const root = if (pkg.store) |st| b.fmt("{s}/zigmod/deps/{s}", .{ b.cache_root, st }) else ".";
const root = if (pkg.store) |st| b.fmt("{s}/zigmod/deps/{s}", .{ b.cache_root.path.?, st }) else ".";
for (pkg.system_libs) |item| {
exe.linkSystemLibrary(item);
llc = true;
}
for (pkg.frameworks) |item| {
if (!builtin.target.isDarwin()) @panic(exe.builder.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item}));
if (!builtin.target.isDarwin()) @panic(exe.step.owner.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item}));
exe.linkFramework(item);
llc = true;
}
for (pkg.c_include_dirs) |item| {
exe.addIncludePath(b.fmt("{s}/{s}", .{ root, item }));
exe.addIncludePath(.{.path = b.fmt("{s}/{s}", .{ root, item })});
llc = true;
}
for (pkg.c_source_files) |item| {
exe.addCSourceFile(b.fmt("{s}/{s}", .{ root, item }), pkg.c_source_flags);
exe.addCSourceFile(.{ .file = .{ .path = b.fmt("{s}/{s}", .{ root, item }) }, .flags = pkg.c_source_flags });
llc = true;
}
vcpkg = vcpkg or pkg.vcpkg;
Expand All @@ -113,52 +120,57 @@ pub const Package = struct {
system_libs: []const string = &.{},
frameworks: []const string = &.{},
vcpkg: bool = false,
module: ?ModuleDependency = null,

pub fn zp(self: *const Package, b: *std.build.Builder) ModuleDependency {
pub fn zp(self: *Package, b: *std.build.Builder) ModuleDependency {
var temp: [100]ModuleDependency = undefined;
for (self.deps) |item, i| {
for (self.deps, 0..) |item, i| {
temp[i] = item.zp(b);
}
return .{
if (self.module) |mod| {
return mod;
}
const result = ModuleDependency{
.name = self.name,
.module = b.createModule(.{
.source_file = .{ .path = self.entry.? },
.dependencies = b.allocator.dupe(ModuleDependency, temp[0..self.deps.len]) catch @panic("oom"),
}),
};
self.module = result;
return result;
}
};

fn checkMinZig(current: std.SemanticVersion, exe: *std.build.LibExeObjStep) void {
const min = std.SemanticVersion.parse("0.11.0-dev.1570+693b12f8e") catch return;
if (current.order(min).compare(.lt)) @panic(exe.builder.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{current, min}));
const min = std.SemanticVersion.parse("0.11.0") catch return;
if (current.order(min).compare(.lt)) @panic(exe.step.owner.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{current, min}));
}

pub const package_data = struct {
pub var _8dglro8ootvr = Package{
};
pub var _f7dubzb7cyqe = Package{
.store = "/git/github.com/nektro/zig-extras/8628846c832086d97f28c54eab157cda389c319a",
.name = "extras",
.entry = "/git/github.com/nektro/zig-extras/8628846c832086d97f28c54eab157cda389c319a/src/lib.zig",
};
pub var _tnj3qf44tpeq = Package{
.store = "/git/github.com/nektro/zig-range/4b2f12808aa09be4b27a163efc424dd4e0415992",
.name = "range",
.entry = "/git/github.com/nektro/zig-range/4b2f12808aa09be4b27a163efc424dd4e0415992/src/lib.zig",
};
pub var _f7dubzb7cyqe = Package{
.store = "/git/github.com/nektro/zig-extras/3fe700ebc8ff66966abe145166bfdf546b3a8422",
.name = "extras",
.entry = "/git/github.com/nektro/zig-extras/3fe700ebc8ff66966abe145166bfdf546b3a8422/src/lib.zig",
.deps = &[_]*Package{ &_tnj3qf44tpeq },
};
pub var _pm68dn67ppvl = Package{
.store = "/git/github.com/nektro/zig-flag/07ea6a3daa950f7bbd8bbd60c0cc2251806fde95",
.store = "/git/github.com/nektro/zig-flag/4a03000239d5e05062cf9344a08e0dc43ad8ebd1",
.name = "flag",
.entry = "/git/github.com/nektro/zig-flag/07ea6a3daa950f7bbd8bbd60c0cc2251806fde95/src/lib.zig",
.entry = "/git/github.com/nektro/zig-flag/4a03000239d5e05062cf9344a08e0dc43ad8ebd1/src/lib.zig",
.deps = &[_]*Package{ &_f7dubzb7cyqe, &_tnj3qf44tpeq },
};
pub var _root = Package{
};
};

pub const packages = [_]*const Package{
pub const packages = [_]*Package{
&package_data._tnj3qf44tpeq,
&package_data._pm68dn67ppvl,
};
Expand Down
10 changes: 5 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Rule = enum {
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
defer std.debug.assert(!gpa.deinit());
defer std.debug.assert(gpa.deinit() == .ok);

//

Expand Down Expand Up @@ -79,7 +79,7 @@ pub fn main() !void {
var walker = try dir.walk(alloc);
defer walker.deinit();
while (try walker.next()) |item| {
if (item.kind != .File) continue;
if (item.kind != .file) continue;
try doFile(alloc, dir.dir, item.path, rulestorun.items, out);
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ fn doFile(alloc: std.mem.Allocator, dir: std.fs.Dir, path: string, rules: []cons
};

for (rules) |jtem| {
try linters[@enumToInt(jtem)](alloc2, path, &src, out);
try linters[@intFromEnum(jtem)](alloc2, path, &src, out);
}
}

Expand All @@ -131,7 +131,7 @@ pub const Loc = struct {
pub fn locToLoc(source: [:0]const u8, loc: std.zig.Token.Loc) Loc {
var line: usize = 1;
var pos: usize = 1;
for (range(loc.start)) |_, i| {
for (range(loc.start), 0..) |_, i| {
pos += 1;
if (source[i] != '\n') continue;
line += 1;
Expand Down Expand Up @@ -172,7 +172,7 @@ pub const Source = struct {
};

fn removeItem(comptime T: type, haystack: *std.ArrayList(T), needle: T) ?T {
for (haystack.items) |item, i| {
for (haystack.items, 0..) |item, i| {
if (item == needle) return haystack.orderedRemove(i);
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/rules/dupe_import.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn work(alloc: std.mem.Allocator, file_name: []const u8, src: *main.Source,
var map = std.StringHashMap(main.Loc).init(alloc);
defer map.deinit();

for (tokens) |tok, i| {
for (tokens, 0..) |tok, i| {
if (i + 4 >= tokens.len) break;

const a = tokens[i + 0].tag == .builtin;
Expand Down
11 changes: 6 additions & 5 deletions src/rules/unused.zig
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ fn checkValueForName(ast: std.zig.Ast, search_name: string, node: NodeIndex, wri
.add_sat,
.sub_sat,
.shl_sat,
.for_range,
=> {
if (try checkValueForName(ast, search_name, data.lhs, writer, file_name, node)) return true;
if (try checkValueForName(ast, search_name, data.rhs, writer, file_name, node)) return true;
Expand Down Expand Up @@ -299,11 +300,11 @@ fn checkValueForName(ast: std.zig.Ast, search_name: string, node: NodeIndex, wri
.sentinel,
.elem_type,
}),
.@"for" => try checkAstValuesForName(ast, search_name, writer, file_name, node, ast.forFull(node), &.{
.cond_expr,
.then_expr,
.else_expr,
}),
.@"for" => {
if (try checkValuesForName(ast, search_name, ast.forFull(node).ast.inputs, writer, file_name, node)) return true;
if (try checkAstValuesForName(ast, search_name, writer, file_name, node, ast.forFull(node), &.{ .then_expr, .else_expr })) return true;
return false;
},
.@"while" => try checkAstValuesForName(ast, search_name, writer, file_name, node, ast.whileFull(node), &.{
.cond_expr,
.cont_expr,
Expand Down
2 changes: 1 addition & 1 deletion zig.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ id: 8dglro8ootvrhtua84qcmx402ywi2dw0d1u8nfx3oc42z8dw
name: ziglint
license: MIT
description: A linting suite for Zig
min_zig_version: 0.11.0-dev.1570+693b12f8e
min_zig_version: 0.11.0
bin: True
provides: ["ziglint"]
root_dependencies:
Expand Down
4 changes: 2 additions & 2 deletions zigmod.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2
git https://github.com/nektro/zig-extras commit-3fe700ebc8ff66966abe145166bfdf546b3a8422
git https://github.com/nektro/zig-flag commit-07ea6a3daa950f7bbd8bbd60c0cc2251806fde95
git https://github.com/nektro/zig-extras commit-8628846c832086d97f28c54eab157cda389c319a
git https://github.com/nektro/zig-flag commit-4a03000239d5e05062cf9344a08e0dc43ad8ebd1
git https://github.com/nektro/zig-range commit-4b2f12808aa09be4b27a163efc424dd4e0415992

0 comments on commit 7929bd7

Please sign in to comment.