Skip to content

upgrade to 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 2 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
zig-cache
.zig-cache
zig-out
.gdb_history
*.bfbs
Expand Down
38 changes: 21 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 {
Expand All @@ -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);
Comment on lines +72 to +73
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just delete this.

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());
Expand All @@ -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");
Expand Down
30 changes: 18 additions & 12 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you bump the version?

.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",
},
}
2 changes: 1 addition & 1 deletion codegen/bfbs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
Expand Down
2 changes: 1 addition & 1 deletion codegen/examples/monster/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions codegen/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -51,8 +48,12 @@ pub fn main() !void {
\\-f, --function-case <str> 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, &params, clap.parsers.default, .{
.allocator = allocator,
.diagnostic = &diag,
}) catch |err| {
diag.report(std.io.getStdErr().writer(), err) catch {};
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions codegen/writer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions lib/table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
};
}
Expand Down
Loading