diff --git a/.gitignore b/.gitignore index af6693a..48bd5d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -zig-cache +.zig-cache zig-out .gdb_history *.bfbs diff --git a/build.zig b/build.zig index 709bb47..465fcad 100644 --- a/build.zig +++ b/build.zig @@ -5,34 +5,35 @@ const path = "./lib/lib.zig"; fn buildLib( b: *std.Build, - target: std.zig.CrossTarget, + target: std.Build.ResolvedTarget, optimize: std.builtin.Mode, -) *std.build.Module { - const module = b.addModule(name, .{ .source_file = .{ .path = path } }); +) *std.Build.Module { + const module = b.addModule(name, .{ .root_source_file = b.path(path) }); const lib = b.addSharedLibrary(.{ .name = name, - .root_source_file = .{ .path = path }, + .root_source_file = b.path(path), .target = target, .optimize = optimize, }); b.installArtifact(lib); const tests = b.addTest(.{ - .root_source_file = .{ .path = path }, + .root_source_file = b.path(path), .target = target, .optimize = optimize, }); const run_main_tests = b.addRunArtifact(tests); const example_tests = b.addTest(.{ - .root_source_file = .{ .path = "./codegen/examples/lib.zig" }, + .root_source_file = b.path("./codegen/examples/lib.zig"), .target = target, .optimize = optimize, }); - example_tests.addModule(name, module); - example_tests.addCSourceFile(.{ .file = .{ .path = "./codegen/examples/arrow-cpp/verify.cpp" }, .flags = &.{} }); - example_tests.addIncludePath(.{ .path = "./codegen/examples/arrow-cpp" }); + // example_tests.addModule(name, module); + example_tests.root_module.addImport(name, module); + example_tests.addCSourceFile(.{ .file = b.path("./codegen/examples/arrow-cpp/verify.cpp"), .flags = &.{} }); + example_tests.addIncludePath(b.path("./codegen/examples/arrow-cpp")); example_tests.linkLibCpp(); const test_step = b.step("test", "Run library and example tests"); @@ -45,7 +46,7 @@ fn buildLib( fn buildExe( b: *std.Build, - target: std.zig.CrossTarget, + target: std.Build.ResolvedTarget, optimize: std.builtin.Mode, module: *std.Build.Module, ) void { @@ -63,19 +64,21 @@ fn buildExe( const exe = b.addExecutable(.{ .name = "flatc-zig", - .root_source_file = .{ .path = "./codegen/main.zig" }, + .root_source_file = b.path("./codegen/main.zig"), .target = target, .optimize = optimize, }); exe.step.dependOn(&flatc.step); - exe.addModule("clap", clap); - exe.addModule(name, module); + // exe.addModule("clap", clap); + // exe.addModule(name, module); + exe.root_module.addImport("clap", clap); + exe.root_module.addImport(name, module); b.installArtifact(exe); const build_options = b.addOptions(); build_options.addOptionArtifact("flatc_exe_path", flatc); build_options.addOption([]const u8, "module_name", name); - exe.addOptions("build_options", build_options); + exe.root_module.addOptions("build_options", build_options); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); @@ -84,12 +87,13 @@ fn buildExe( run_step.dependOn(&run_cmd.step); const codegen_tests = b.addTest(.{ - .root_source_file = .{ .path = "./codegen/main.zig" }, + .root_source_file = b.path("./codegen/main.zig"), .target = target, .optimize = optimize, }); - codegen_tests.addModule(name, module); - codegen_tests.addOptions("build_options", build_options); + // codegen_tests.addModule(name, module); + codegen_tests.root_module.addImport(name, module); + codegen_tests.root_module.addOptions("build_options", build_options); const run_codegen_tests = b.addRunArtifact(codegen_tests); const test_step = b.step("test-exe", "Run codegen tests"); diff --git a/build.zig.zon b/build.zig.zon index f4739ff..12a1484 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,14 +1,20 @@ .{ - .name = "flatbuffers", - .version = "0.0.3", - .dependencies = .{ - .clap = .{ - .url = "https://github.com/Hejsil/zig-clap/archive/f49b94700e0761b7514abdca0e4f0e7f3f938a93.tar.gz", - .hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2" - }, - .flatbuffers = .{ - .url = "https://github.com/clickingbuttons/flatbuffers/archive/refs/tags/0.0.4.tar.gz", - .hash = "122094e2b1ae9ef4b8bc50ecde2ee886aab1d4bf2e8b648927b53b48f7810b60e243" - }, - } + .name = "flatbuffers", + .version = "0.0.3", + .dependencies = .{ + .clap = .{ + .url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz", + .hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b", + }, + .flatbuffers = .{ + .url = "https://github.com/flowerinthenight/flatbuffers/archive/refs/tags/v0.0.2.tar.gz", + .hash = "1220ae1b7185cfa8b1c53ab62eae1efb3b2270d0e0ff5672ff45ed7c46786ea97b61", + }, + }, + .paths = .{ + "build.zig", + "build.zig.zon", + "lib", + "codegen", + }, } diff --git a/codegen/bfbs.zig b/codegen/bfbs.zig index 7ace9b0..ffea10d 100644 --- a/codegen/bfbs.zig +++ b/codegen/bfbs.zig @@ -28,7 +28,7 @@ pub fn bfbs(allocator: Allocator, include: []const u8, fname: []const u8) ![]con } try argv.append(fname); - const exec_res = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv.items }); + const exec_res = try std.process.Child.run(.{ .allocator = allocator, .argv = argv.items }); if (exec_res.term != .Exited or exec_res.term.Exited != 0) { for (argv.items) |it| std.debug.print("{s} ", .{it}); std.debug.print("\nerror: flatc command failure:\n", .{}); diff --git a/codegen/examples/monster/test.zig b/codegen/examples/monster/test.zig index 76aa3dc..9768031 100644 --- a/codegen/examples/monster/test.zig +++ b/codegen/examples/monster/test.zig @@ -112,7 +112,7 @@ test "build monster and unpack" { test "build monster and pack" { var builder = Builder.init(testing.allocator); const root = try example_monster.pack(&builder); - var bytes = try builder.finish(root); + const bytes = try builder.finish(root); defer testing.allocator.free(bytes); const packed_monster = try PackedMonster.init(bytes); diff --git a/codegen/main.zig b/codegen/main.zig index e2970b1..872be08 100644 --- a/codegen/main.zig +++ b/codegen/main.zig @@ -9,15 +9,12 @@ const Case = @import("./util.zig").Case; fn fatal(comptime T: type, msg: []const u8) T { std.debug.print("{s}\n", .{msg}); - std.os.exit(1); + std.posix.exit(1); unreachable; } -fn walk(opts: Options) !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const allocator = gpa.allocator(); - - var dir = try std.fs.cwd().openIterableDir(opts.input_dir, .{}); +fn walk(allocator: std.mem.Allocator, opts: Options) !void { + var dir = try std.fs.cwd().openDir(opts.input_dir, .{ .iterate = true }); defer dir.close(); var walker = try dir.walk(allocator); @@ -51,8 +48,12 @@ pub fn main() !void { \\-f, --function-case Casing for function names (camel, snake, title) (default camel) \\ ); + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); var diag = clap.Diagnostic{}; var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ + .allocator = allocator, .diagnostic = &diag, }) catch |err| { diag.report(std.io.getStdErr().writer(), err) catch {}; @@ -71,7 +72,7 @@ pub fn main() !void { const documentation = res.args.@"no-documentation" == 0; const function_case = Case.fromString(res.args.@"function-case" orelse "camel") orelse fatal(Case, "invalid function case"); - try walk(Options{ + try walk(allocator, Options{ .extension = extension, .input_dir = input_dir, .output_dir = output_dir, diff --git a/codegen/writer.zig b/codegen/writer.zig index 66b5d7b..27fcafa 100644 --- a/codegen/writer.zig +++ b/codegen/writer.zig @@ -111,7 +111,7 @@ pub const CodeWriter = struct { // This struct owns returned string fn getPrefixedIdentifier(self: *Self, ident: []const u8, prefix: []const u8) ![]const u8 { - var prefixed = try self.allocPrint("{s} {s}", .{ prefix, ident }); + const prefixed = try self.allocPrint("{s} {s}", .{ prefix, ident }); defer self.allocator.free(prefixed); return try getIdentifier(prefixed); @@ -133,7 +133,7 @@ pub const CodeWriter = struct { // This struct owns returned string fn getPrefixedFunctionName(self: *Self, prefix: []const u8, name: []const u8) ![]const u8 { - var prefixed = try self.allocPrint("{s} {s}", .{ prefix, name }); + const prefixed = try self.allocPrint("{s} {s}", .{ prefix, name }); defer self.allocator.free(prefixed); return try self.getFunctionName(prefixed); @@ -159,7 +159,7 @@ pub const CodeWriter = struct { // This struct owns returned string fn getPrefixedTypeName(self: *Self, prefix: []const u8, name: []const u8) ![]const u8 { - var tmp = try self.allocPrint("{s}{s}", .{ prefix, name }); + const tmp = try self.allocPrint("{s}{s}", .{ prefix, name }); defer self.allocator.free(tmp); var res = std.ArrayList(u8).init(self.allocator); @@ -252,7 +252,7 @@ pub const CodeWriter = struct { // This struct owns returned string. fn getPrefixedTmpName(self: *Self, prefix: []const u8, name: []const u8) ![]const u8 { - var prefixed = try self.allocPrint("{s} {s}", .{ prefix, name }); + const prefixed = try self.allocPrint("{s} {s}", .{ prefix, name }); defer self.allocator.free(prefixed); return try self.getTmpName(prefixed); diff --git a/lib/table.zig b/lib/table.zig index 13d461b..a2856bb 100644 --- a/lib/table.zig +++ b/lib/table.zig @@ -27,12 +27,12 @@ pub const Table = struct { fn readVOffset(bytes: []u8) Error!VOffset { if (bytes.len < @sizeOf(VOffset)) return Error.PrematureEnd; - return std.mem.readIntLittle(VOffset, bytes[0..@sizeOf(VOffset)]); + return std.mem.readInt(VOffset, bytes[0..@sizeOf(VOffset)], std.builtin.Endian.little); } fn readOffset(bytes: []u8) Error!Offset { if (bytes.len < @sizeOf(Offset)) return Error.PrematureEnd; - return std.mem.readIntLittle(Offset, bytes[0..@sizeOf(Offset)]); + return std.mem.readInt(Offset, bytes[0..@sizeOf(Offset)], std.builtin.Endian.little); } pub fn init(size_prefixed_bytes: []u8) Error!Self { @@ -135,7 +135,7 @@ pub const Table = struct { pub fn isScalar(comptime T: type) bool { return switch (@typeInfo(T)) { .Void, .Bool, .Int, .Float, .Array, .Enum => true, - .Struct => |s| s.layout == .Extern or s.layout == .Packed, + .Struct => |s| s.layout == .@"extern" or s.layout == .@"packed", else => false, }; }