diff --git a/.gitignore b/.gitignore index 8974da9..9b1527f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -zig-cache/ +.zig-cache/ zig-out/ /dep/ diff --git a/GitRepoStep.zig b/GitRepoStep.zig index 838405b..5d2a407 100644 --- a/GitRepoStep.zig +++ b/GitRepoStep.zig @@ -15,13 +15,13 @@ pub const ShaCheck = enum { .warn => std.log.warn(fmt, args), .err => { std.log.err(fmt, args); - std.os.exit(0xff); + std.process.exit(0xff); }, } } }; -step: std.build.Step, +step: std.Build.Step, url: []const u8, name: []const u8, branch: ?[]const u8 = null, @@ -31,14 +31,14 @@ sha_check: ShaCheck = .warn, fetch_enabled: bool, var cached_default_fetch_option: ?bool = null; -pub fn defaultFetchOption(b: *std.build.Builder) bool { +pub fn defaultFetchOption(b: *std.Build) bool { if (cached_default_fetch_option) |_| {} else { cached_default_fetch_option = if (b.option(bool, "fetch", "automatically fetch network resources")) |o| o else false; } return cached_default_fetch_option.?; } -pub fn create(b: *std.build.Builder, opt: struct { +pub fn create(b: *std.Build, opt: struct { url: []const u8, branch: ?[]const u8 = null, sha: []const u8, @@ -47,10 +47,10 @@ pub fn create(b: *std.build.Builder, opt: struct { fetch_enabled: ?bool = null, first_ret_addr: ?usize = null, }) *GitRepoStep { - var result = b.allocator.create(GitRepoStep) catch @panic("memory"); + const result = b.allocator.create(GitRepoStep) catch @panic("memory"); const name = std.fs.path.basename(opt.url); result.* = GitRepoStep{ - .step = std.build.Step.init(.{ + .step = std.Build.Step.init(.{ .id = .custom, .name = b.fmt("clone git repository '{s}'", .{name}), .owner = b, @@ -69,8 +69,8 @@ pub fn create(b: *std.build.Builder, opt: struct { return result; } -// TODO: this should be included in std.build, it helps find bugs in build files -fn hasDependency(step: *const std.build.Step, dep_candidate: *const std.build.Step) bool { +// TODO: this should be included in std.Build, it helps find bugs in build files +fn hasDependency(step: *const std.Build.Step, dep_candidate: *const std.Build.Step) bool { for (step.dependencies.items) |dep| { // TODO: should probably use step.loop_flag to prevent infinite recursion // when a circular reference is encountered, or maybe keep track of @@ -81,9 +81,9 @@ fn hasDependency(step: *const std.build.Step, dep_candidate: *const std.build.St return false; } -fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { - _ = prog_node; - const self = @fieldParentPtr(GitRepoStep, "step", step); +fn make(step: *std.Build.Step, opts: std.Build.Step.MakeOptions) !void { + _ = opts; + const self: *GitRepoStep = @fieldParentPtr("step", step); std.fs.accessAbsolute(self.path, .{}) catch { const branch_args = if (self.branch) |b| &[2][]const u8{ " -b ", b } else &[2][]const u8{ "", "" }; @@ -97,7 +97,7 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { self.path, self.sha, }); - std.os.exit(1); + std.process.exit(1); } { @@ -134,7 +134,7 @@ fn checkSha(self: GitRepoStep) !void { return; const result: union(enum) { failed: anyerror, output: []const u8 } = blk: { - const result = std.ChildProcess.exec(.{ + const result = std.process.Child.run(.{ .allocator = self.step.owner.allocator, .argv = &[_][]const u8{ "git", @@ -144,7 +144,7 @@ fn checkSha(self: GitRepoStep) !void { "HEAD", }, .cwd = self.step.owner.build_root.path, - .env_map = self.step.owner.env_map, + .env_map = &self.step.owner.graph.env_map, }) catch |e| break :blk .{ .failed = e }; try std.io.getStdErr().writer().writeAll(result.stderr); switch (result.term) { @@ -169,7 +169,7 @@ fn checkSha(self: GitRepoStep) !void { } } -fn run(builder: *std.build.Builder, argv: []const []const u8) !void { +fn run(builder: *std.Build, argv: []const []const u8) !void { { var msg = std.ArrayList(u8).init(builder.allocator); defer msg.deinit(); @@ -182,30 +182,30 @@ fn run(builder: *std.build.Builder, argv: []const []const u8) !void { std.log.info("[RUN] {s}", .{msg.items}); } - var child = std.ChildProcess.init(argv, builder.allocator); + var child = std.process.Child.init(argv, builder.allocator); child.stdin_behavior = .Ignore; child.stdout_behavior = .Inherit; child.stderr_behavior = .Inherit; child.cwd = builder.build_root.path; - child.env_map = builder.env_map; + child.env_map = &builder.graph.env_map; try child.spawn(); const result = try child.wait(); switch (result) { .Exited => |code| if (code != 0) { std.log.err("git clone failed with exit code {}", .{code}); - std.os.exit(0xff); + std.process.exit(0xff); }, else => { std.log.err("git clone failed with: {}", .{result}); - std.os.exit(0xff); + std.process.exit(0xff); }, } } // Get's the repository path and also verifies that the step requesting the path // is dependent on this step. -pub fn getPath(self: *const GitRepoStep, who_wants_to_know: *const std.build.Step) []const u8 { +pub fn getPath(self: *const GitRepoStep, who_wants_to_know: *const std.Build.Step) []const u8 { if (!hasDependency(who_wants_to_know, &self.step)) @panic("a step called GitRepoStep.getPath but has not added it as a dependency"); return self.path; diff --git a/ProcessFileStep.zig b/ProcessFileStep.zig index fc4c58e..739b9a7 100644 --- a/ProcessFileStep.zig +++ b/ProcessFileStep.zig @@ -2,8 +2,8 @@ const std = @import("std"); const ProcessFileStep = @This(); const filecheck = @import("filecheck.zig"); -step: std.build.Step, -//builder: *std.build.Builder, +step: std.Build.Step, +//builder: *std.Build.Builder, in_filename: []const u8, out_filename: []const u8, subs: []const Sub, @@ -13,15 +13,15 @@ pub const Sub = struct { new: []const u8, }; -pub fn create(b: *std.build.Builder, opt: struct { +pub fn create(b: *std.Build, opt: struct { in_filename: []const u8, out_filename: []const u8, subs: []const Sub = &[_]Sub{}, }) *ProcessFileStep { - var result = b.allocator.create(ProcessFileStep) catch unreachable; + const result = b.allocator.create(ProcessFileStep) catch unreachable; const name = std.fmt.allocPrint(b.allocator, "process file '{s}'", .{std.fs.path.basename(opt.in_filename)}) catch unreachable; result.* = ProcessFileStep{ - .step = std.build.Step.init(.{ + .step = std.Build.Step.init(.{ .id = .custom, .name = name, .owner = b, @@ -34,9 +34,9 @@ pub fn create(b: *std.build.Builder, opt: struct { return result; } -fn make(step: *std.build.Step, progress: *std.Progress.Node) !void { - _ = progress; - const self = @fieldParentPtr(ProcessFileStep, "step", step); +fn make(step: *std.Build.Step, opts: std.Build.Step.MakeOptions) !void { + _ = opts; + const self: *ProcessFileStep = @fieldParentPtr("step", step); if (try filecheck.leftFileIsNewer(self.out_filename, self.in_filename)) { return; @@ -46,7 +46,7 @@ fn make(step: *std.build.Step, progress: *std.Progress.Node) !void { defer arena.deinit(); const content = std.fs.cwd().readFileAlloc(arena.allocator(), self.in_filename, std.math.maxInt(usize)) catch |err| { std.log.err("failed to read file '{s}' to process ({s})", .{ self.in_filename, @errorName(err) }); - std.os.exit(0xff); + std.process.exit(0xff); }; const tmp_filename = try std.fmt.allocPrint(arena.allocator(), "{s}.processing", .{self.out_filename}); { diff --git a/awkbuild.zig b/awkbuild.zig index c048237..f865104 100644 --- a/awkbuild.zig +++ b/awkbuild.zig @@ -2,13 +2,13 @@ const std = @import("std"); const GitRepoStep = @import("GitRepoStep.zig"); pub fn addAwk( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, +) *std.Build.Step.Compile { const repo = GitRepoStep.create(b, .{ .url = "https://github.com/onetrueawk/awk", .sha = "9e248c317b88470fc86aa7c988919dc49452c88c", @@ -41,18 +41,19 @@ pub fn addAwk( files.append(b.pathJoin(&.{ repo_path, "src", src })) catch unreachable; } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c11", + exe.addCSourceFiles(.{ + .files = files.toOwnedSlice() catch unreachable, + .flags = &[_][]const u8{"-std=c11"}, }); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.addIncludePath(.{ .path = "inc/gnu" }); + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.addIncludePath(b.path("inc/gnu")); exe.linkLibrary(libc_only_std_static); exe.linkLibrary(zig_start); exe.linkLibrary(zig_posix); // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { + if (target.result.os.tag == .windows) { exe.linkSystemLibrary("ntdll"); exe.linkSystemLibrary("kernel32"); } diff --git a/build.zig b/build.zig index d94ce47..c512536 100644 --- a/build.zig +++ b/build.zig @@ -5,13 +5,14 @@ const luabuild = @import("luabuild.zig"); const awkbuild = @import("awkbuild.zig"); const gnumakebuild = @import("gnumakebuild.zig"); -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.Build) void { const trace_enabled = b.option(bool, "trace", "enable libc tracing") orelse false; { const exe = b.addExecutable(.{ .name = "genheaders", - .root_source_file = .{ .path = "src" ++ std.fs.path.sep_str ++ "genheaders.zig" }, + .root_source_file = b.path("src/genheaders.zig"), + .target = b.host, }); const run = b.addRunArtifact(exe); run.addArg(b.pathFromRoot("capi.txt")); @@ -98,7 +99,7 @@ pub fn build(b: *std.build.Builder) void { const test_env_exe = b.addExecutable(.{ .name = "testenv", - .root_source_file = .{ .path = "test" ++ std.fs.path.sep_str ++ "testenv.zig" }, + .root_source_file = b.path("test/testenv.zig"), .target = target, .optimize = optimize, }); @@ -132,7 +133,7 @@ pub fn build(b: *std.build.Builder) void { { const exe = addTest("types", b, target, optimize, libc_only_std_static, zig_start); const run_step = b.addRunArtifact(exe); - run_step.addArg(b.fmt("{}", .{@divExact(target.toTarget().ptrBitWidth(), 8)})); + run_step.addArg(b.fmt("{}", .{@divExact(target.result.ptrBitWidth(), 8)})); run_step.addCheck(.{ .expect_stdout_exact = "Success!\n" }); test_step.dependOn(&run_step.step); } @@ -171,7 +172,7 @@ pub fn build(b: *std.build.Builder) void { } // this test only works on linux right now - if (target.getOsTag() == .linux) { + if (target.result.os.tag == .linux) { const exe = addTest("jmp", b, target, optimize, libc_only_std_static, zig_start); const run_step = b.addRunArtifact(exe); run_step.addCheck(.{ .expect_stdout_exact = "Success!\n" }); @@ -186,9 +187,9 @@ pub fn build(b: *std.build.Builder) void { _ = addYabfc(b, target, optimize, libc_only_std_static, zig_start, libc_only_posix, libc_only_gnu); _ = addSecretGame(b, target, optimize, libc_only_std_static, zig_start, libc_only_posix, libc_only_gnu); _ = awkbuild.addAwk(b, target, optimize, libc_only_std_static, libc_only_posix, zig_start); - _ = gnumakebuild.addGnuMake(b, target, optimize, libc_only_std_static, libc_only_posix, zig_start); + // _ = gnumakebuild.addGnuMake(b, target, optimize, libc_only_std_static, libc_only_posix, zig_start); - _ = @import("busybox/build.zig").add(b, target, optimize, libc_only_std_static, libc_only_posix); + // _ = @import("busybox/build.zig").add(b, target, optimize, libc_only_std_static, libc_only_posix); _ = @import("ncurses/build.zig").add(b, target, optimize, libc_only_std_static, libc_only_posix); } @@ -199,32 +200,34 @@ fn installArtifact(b: *std.Build, artifact: anytype) *std.Build.Step.InstallArti return install; } -fn addPosix(artifact: *std.build.LibExeObjStep, zig_posix: *std.build.LibExeObjStep) void { +fn addPosix(artifact: *std.Build.Step.Compile, zig_posix: *std.Build.Step.Compile) void { artifact.linkLibrary(zig_posix); - artifact.addIncludePath(.{ .path = "inc" ++ std.fs.path.sep_str ++ "posix" }); + artifact.addIncludePath(zig_posix.step.owner.path("inc/posix")); } fn addTest( comptime name: []const u8, - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, +) *std.Build.Step.Compile { const exe = b.addExecutable(.{ .name = name, - .root_source_file = .{ .path = "test" ++ std.fs.path.sep_str ++ name ++ ".c" }, .target = target, .optimize = optimize, }); - exe.addCSourceFiles(&.{"test" ++ std.fs.path.sep_str ++ "expect.c"}, &[_][]const u8{}); - exe.addIncludePath(.{ .path = "inc" ++ std.fs.path.sep_str ++ "libc" }); - exe.addIncludePath(.{ .path = "inc" ++ std.fs.path.sep_str ++ "posix" }); + exe.addCSourceFile(.{ .file = b.path("test/" ++ name ++ ".c") }); + exe.addCSourceFiles(.{ + .files = &.{"test/expect.c"}, + }); + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); exe.linkLibrary(libc_only_std_static); exe.linkLibrary(zig_start); // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { + if (target.result.os.tag == .windows) { exe.linkSystemLibrary("ntdll"); exe.linkSystemLibrary("kernel32"); } @@ -232,12 +235,12 @@ fn addTest( } fn addLibcTest( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - libc_only_posix: *std.build.LibExeObjStep, + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + libc_only_posix: *std.Build.Step.Compile, ) void { const libc_test_repo = GitRepoStep.create(b, .{ .url = "git://nsz.repo.hu:49100/repo/libc-test", @@ -252,17 +255,21 @@ fn addLibcTest( inline for (.{ "assert", "ctype", "errno", "main", "stdbool", "stddef", "string" }) |name| { const lib = b.addObject(.{ .name = "libc-test-api-" ++ name, - .root_source_file = .{ .path = b.pathJoin(&.{ libc_test_path, "src", "api", name ++ ".c" }) }, .target = target, .optimize = optimize, }); - lib.addIncludePath(.{ .path = "inc" ++ std.fs.path.sep_str ++ "libc" }); + lib.addCSourceFile(.{ .file = .{ .cwd_relative = b.pathJoin(&.{ libc_test_path, "src", "api", name ++ ".c" }) } }); + lib.addIncludePath(b.path("inc/libc")); lib.step.dependOn(&libc_test_repo.step); libc_test_step.dependOn(&lib.step); } const libc_inc_path = b.pathJoin(&.{ libc_test_path, "src", "common" }); const common_src = &[_][]const u8{ - b.pathJoin(&.{ libc_test_path, "src", "common", "print.c" }), + std.fs.path.relative( + b.allocator, + b.build_root.path orelse ".", + b.pathJoin(&.{ libc_test_path, "src", "common", "print.c" }), + ) catch @panic("OOM"), }; // strtol, it seems there might be some disagreement between libc-test/glibc @@ -270,20 +277,20 @@ fn addLibcTest( inline for (.{ "argv", "basename", "clock_gettime", "string" }) |name| { const exe = b.addExecutable(.{ .name = "libc-test-functional-" ++ name, - .root_source_file = .{ .path = b.pathJoin(&.{ libc_test_path, "src", "functional", name ++ ".c" }) }, .target = target, .optimize = optimize, }); - exe.addCSourceFiles(common_src, &[_][]const u8{}); + exe.addCSourceFile(.{ .file = .{ .cwd_relative = b.pathJoin(&.{ libc_test_path, "src", "functional", name ++ ".c" }) } }); + exe.addCSourceFiles(.{ .files = common_src }); exe.step.dependOn(&libc_test_repo.step); - exe.addIncludePath(.{ .path = libc_inc_path }); - exe.addIncludePath(.{ .path = "inc" ++ std.fs.path.sep_str ++ "libc" }); - exe.addIncludePath(.{ .path = "inc" ++ std.fs.path.sep_str ++ "posix" }); + exe.addIncludePath(.{ .cwd_relative = libc_inc_path }); + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); exe.linkLibrary(libc_only_std_static); exe.linkLibrary(zig_start); exe.linkLibrary(libc_only_posix); // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { + if (target.result.os.tag == .windows) { exe.linkSystemLibrary("ntdll"); exe.linkSystemLibrary("kernel32"); } @@ -292,375 +299,408 @@ fn addLibcTest( } fn addTinyRegexCTests( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, ) void { - const repo = GitRepoStep.create(b, .{ - .url = "https://github.com/marler8997/tiny-regex-c", - .sha = "95ef2ad35d36783d789b0ade3178b30a942f085c", - .branch = "nocompile", - .fetch_enabled = true, - }); + if (b.lazyDependency("tiny-regex-c", .{})) |tiny_regex_dep| { + const re_step = b.step("re-tests", "run the tiny-regex-c tests"); + inline for (&[_][]const u8{ "test1", "test3" }) |test_name| { + const exe = b.addExecutable(.{ + .name = "re" ++ test_name, + .target = target, + .optimize = optimize, + }); + //b.installArtifact(exe); + // exe.step.dependOn(&repo.step); + // const repo_path = repo.getPath(&exe.step); + // var files = std.ArrayList([]const u8).init(b.allocator); + // const sources = [_][]const u8{ + // "re.c", "tests/" ++ test_name ++ ".c", + // }; + // for (sources) |src| { + // files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; + // } + + exe.addCSourceFiles(.{ + .root = tiny_regex_dep.path(""), + .files = &.{ "re.c", "tests/" ++ test_name ++ ".c" }, + .flags = &[_][]const u8{"-std=c99"}, + }); + exe.addIncludePath(tiny_regex_dep.path("")); + + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.linkLibrary(libc_only_std_static); + exe.linkLibrary(zig_start); + exe.linkLibrary(zig_posix); + // TODO: should libc_only_std_static and zig_start be able to add library dependencies? + if (target.result.os.tag == .windows) { + exe.linkSystemLibrary("ntdll"); + exe.linkSystemLibrary("kernel32"); + } + + //const step = b.step("re", "build the re (tiny-regex-c) tool"); + //step.dependOn(&exe.install_step.?.step); + const run = b.addRunArtifact(exe); + re_step.dependOn(&run.step); + } + } +} - const re_step = b.step("re-tests", "run the tiny-regex-c tests"); - inline for (&[_][]const u8{ "test1", "test3" }) |test_name| { - const exe = b.addExecutable(.{ - .name = "re" ++ test_name, - .root_source_file = null, +fn addLua( + b: *std.Build, + target: anytype, + optimize: anytype, + libc_only_std_static: *std.Build.Step.Compile, + libc_only_posix: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, +) ?*std.Build.Step.Compile { + // const lua_repo = GitRepoStep.create(b, .{ + // .url = "https://github.com/lua/lua", + // .sha = "5d708c3f9cae12820e415d4f89c9eacbe2ab964b", + // .branch = "v5.4.4", + // .fetch_enabled = true, + // }); + if (b.lazyDependency("lua", .{})) |lua_dep| { + const lua_exe = b.addExecutable(.{ + .name = "lua", .target = target, .optimize = optimize, }); - //b.installArtifact(exe); - exe.step.dependOn(&repo.step); - const repo_path = repo.getPath(&exe.step); + // lua_exe.step.dependOn(&lua_repo.step); + const install = b.addInstallArtifact(lua_exe, .{}); + // doesn't compile for windows for some reason + if (target.result.os.tag != .windows) { + b.getInstallStep().dependOn(&install.step); + } + // const lua_repo_path = lua_repo.getPath(&lua_exe.step); var files = std.ArrayList([]const u8).init(b.allocator); - const sources = [_][]const u8{ - "re.c", "tests" ++ std.fs.path.sep_str ++ test_name ++ ".c", - }; - for (sources) |src| { - files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; + files.append("lua.c") catch unreachable; + inline for (luabuild.core_objects) |obj| { + files.append(obj ++ ".c") catch unreachable; + } + inline for (luabuild.aux_objects) |obj| { + files.append(obj ++ ".c") catch unreachable; + } + inline for (luabuild.lib_objects) |obj| { + files.append(obj ++ ".c") catch unreachable; } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c99", + lua_exe.addCSourceFiles(.{ + .root = lua_dep.path(""), + .files = files.toOwnedSlice() catch unreachable, + .flags = &[_][]const u8{ + "-nostdinc", + "-nostdlib", + "-std=c99", + }, }); - exe.addIncludePath(.{ .path = repo_path }); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.linkLibrary(libc_only_std_static); - exe.linkLibrary(zig_start); - exe.linkLibrary(zig_posix); + lua_exe.addIncludePath(b.path("inc/libc")); + lua_exe.linkLibrary(libc_only_std_static); + lua_exe.linkLibrary(libc_only_posix); + lua_exe.linkLibrary(zig_start); // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { - exe.linkSystemLibrary("ntdll"); - exe.linkSystemLibrary("kernel32"); + if (target.result.os.tag == .windows) { + lua_exe.addIncludePath(b.path("inc/win32")); + lua_exe.linkSystemLibrary("ntdll"); + lua_exe.linkSystemLibrary("kernel32"); } - //const step = b.step("re", "build the re (tiny-regex-c) tool"); - //step.dependOn(&exe.install_step.?.step); - const run = b.addRunArtifact(exe); - re_step.dependOn(&run.step); - } -} - -fn addLua( - b: *std.build.Builder, - target: anytype, - optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - libc_only_posix: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { - const lua_repo = GitRepoStep.create(b, .{ - .url = "https://github.com/lua/lua", - .sha = "5d708c3f9cae12820e415d4f89c9eacbe2ab964b", - .branch = "v5.4.4", - .fetch_enabled = true, - }); - const lua_exe = b.addExecutable(.{ - .name = "lua", - .target = target, - .optimize = optimize, - }); - lua_exe.step.dependOn(&lua_repo.step); - const install = b.addInstallArtifact(lua_exe, .{}); - // doesn't compile for windows for some reason - if (target.getOs().tag != .windows) { - b.getInstallStep().dependOn(&install.step); - } - const lua_repo_path = lua_repo.getPath(&lua_exe.step); - var files = std.ArrayList([]const u8).init(b.allocator); - files.append(b.pathJoin(&.{ lua_repo_path, "lua.c" })) catch unreachable; - inline for (luabuild.core_objects) |obj| { - files.append(b.pathJoin(&.{ lua_repo_path, obj ++ ".c" })) catch unreachable; - } - inline for (luabuild.aux_objects) |obj| { - files.append(b.pathJoin(&.{ lua_repo_path, obj ++ ".c" })) catch unreachable; - } - inline for (luabuild.lib_objects) |obj| { - files.append(b.pathJoin(&.{ lua_repo_path, obj ++ ".c" })) catch unreachable; - } - - lua_exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-nostdinc", - "-nostdlib", - "-std=c99", - }); + const step = b.step("lua", "build/install the LUA interpreter"); + step.dependOn(&install.step); - lua_exe.addIncludePath(.{ .path = "inc" ++ std.fs.path.sep_str ++ "libc" }); - lua_exe.linkLibrary(libc_only_std_static); - lua_exe.linkLibrary(libc_only_posix); - lua_exe.linkLibrary(zig_start); - // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { - lua_exe.addIncludePath(.{ .path = "inc/win32" }); - lua_exe.linkSystemLibrary("ntdll"); - lua_exe.linkSystemLibrary("kernel32"); - } - - const step = b.step("lua", "build/install the LUA interpreter"); - step.dependOn(&install.step); + const test_step = b.step("lua-test", "Run the lua tests"); - const test_step = b.step("lua-test", "Run the lua tests"); + const tests_path = lua_dep.path("testes"); + for ([_][]const u8{ "bwcoercion.lua", "tracegc.lua" }) |test_file| { + var run_test = b.addRunArtifact(lua_exe); + // run_test.addArg(b.pathJoin(&.{ lua_repo_path, "testes", test_file })); + run_test.addFileArg(tests_path.join(b.allocator, test_file) catch unreachable); + test_step.dependOn(&run_test.step); + } - for ([_][]const u8{ "bwcoercion.lua", "tracegc.lua" }) |test_file| { - var run_test = b.addRunArtifact(lua_exe); - run_test.addArg(b.pathJoin(&.{ lua_repo_path, "testes", test_file })); - test_step.dependOn(&run_test.step); + return lua_exe; } - - return lua_exe; + return null; } fn addCmph( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { - const repo = GitRepoStep.create(b, .{ - //.url = "https://git.code.sf.net/p/cmph/git", - .url = "https://github.com/bonitao/cmph", - .sha = "abd5e1e17e4d51b3e24459ab9089dc0522846d0d", - .branch = null, - .fetch_enabled = true, - }); + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, +) ?*std.Build.Step.Compile { + // const repo = GitRepoStep.create(b, .{ + // //.url = "https://git.code.sf.net/p/cmph/git", + // .url = "https://github.com/bonitao/cmph#abd5e1e17e4d51b3e24459ab9089dc0522846d0d", + // .branch = null, + // .fetch_enabled = true, + // }); + + if (b.lazyDependency("cmph", .{})) |cmph_dep| { + const config_h_path = cmph_dep.path("src/config.h"); + const config_step = b.addWriteFile( + config_h_path.getPath(b), + "#define VERSION \"1.0\"", + ); + // config_step.step.dependOn(&repo.step); - const config_step = b.addWriteFile( - b.pathJoin(&.{ repo.path, "src", "config.h" }), - "#define VERSION \"1.0\"", - ); - config_step.step.dependOn(&repo.step); + const exe = b.addExecutable(.{ + .name = "cmph", + .target = target, + .optimize = optimize, + }); + const install = installArtifact(b, exe); + // exe.step.dependOn(&repo.step); + exe.step.dependOn(&config_step.step); + // const repo_path = repo.getPath(&exe.step); + // var files = std.ArrayList([]const u8).init(b.allocator); + const sources = [_][]const u8{ + "main.c", "cmph.c", "hash.c", "chm.c", "bmz.c", "bmz8.c", "brz.c", "fch.c", + "bdz.c", "bdz_ph.c", "chd_ph.c", "chd.c", "jenkins_hash.c", "graph.c", "vqueue.c", "buffer_manager.c", + "fch_buckets.c", "miller_rabin.c", "compressed_seq.c", "compressed_rank.c", "buffer_entry.c", "select.c", "cmph_structs.c", + }; + // for (sources) |src| { + // files.append(b.pathJoin(&.{ repo_path, "src", src })) catch unreachable; + // } + + exe.addCSourceFiles(.{ + .root = cmph_dep.path(""), + .files = &sources, + .flags = &[_][]const u8{ + "-std=c11", + }, + }); - const exe = b.addExecutable(.{ - .name = "cmph", - .target = target, - .optimize = optimize, - }); - const install = installArtifact(b, exe); - exe.step.dependOn(&repo.step); - exe.step.dependOn(&config_step.step); - const repo_path = repo.getPath(&exe.step); - var files = std.ArrayList([]const u8).init(b.allocator); - const sources = [_][]const u8{ - "main.c", "cmph.c", "hash.c", "chm.c", "bmz.c", "bmz8.c", "brz.c", "fch.c", - "bdz.c", "bdz_ph.c", "chd_ph.c", "chd.c", "jenkins_hash.c", "graph.c", "vqueue.c", "buffer_manager.c", - "fch_buckets.c", "miller_rabin.c", "compressed_seq.c", "compressed_rank.c", "buffer_entry.c", "select.c", "cmph_structs.c", - }; - for (sources) |src| { - files.append(b.pathJoin(&.{ repo_path, "src", src })) catch unreachable; - } + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.addIncludePath(b.path("inc/gnu")); + exe.linkLibrary(libc_only_std_static); + exe.linkLibrary(zig_start); + exe.linkLibrary(zig_posix); + // TODO: should libc_only_std_static and zig_start be able to add library dependencies? + if (target.result.os.tag == .windows) { + exe.linkSystemLibrary("ntdll"); + exe.linkSystemLibrary("kernel32"); + } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c11", - }); + const step = b.step("cmph", "build the cmph tool"); + step.dependOn(&install.step); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.addIncludePath(.{ .path = "inc/gnu" }); - exe.linkLibrary(libc_only_std_static); - exe.linkLibrary(zig_start); - exe.linkLibrary(zig_posix); - // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { - exe.linkSystemLibrary("ntdll"); - exe.linkSystemLibrary("kernel32"); + return exe; } - - const step = b.step("cmph", "build the cmph tool"); - step.dependOn(&install.step); - - return exe; + return null; } fn addYacc( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { - const repo = GitRepoStep.create(b, .{ - .url = "https://github.com/ibara/yacc", - .sha = "1a4138ce2385ec676c6d374245fda5a9cd2fbee2", - .branch = null, - .fetch_enabled = true, - }); + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, +) ?*std.Build.Step.Compile { + // const repo = GitRepoStep.create(b, .{ + // .url = "https://github.com/ibara/yacc", + // .sha = "1a4138ce2385ec676c6d374245fda5a9cd2fbee2", + // .branch = null, + // .fetch_enabled = true, + // }); + + if (b.lazyDependency("yacc", .{})) |yacc_dep| { + const config_h_path = yacc_dep.path("config.h"); + const config_step = b.addWriteFile(config_h_path.getPath(b), + \\// for simplicity just don't supported __unused + \\#define __unused + \\// for simplicity we're just not supporting noreturn + \\#define __dead + \\//#define HAVE_PROGNAME + \\//#define HAVE_ASPRINTF + \\//#define HAVE_PLEDGE + \\//#define HAVE_REALLOCARRAY + \\#define HAVE_STRLCPY + \\ + ); + // config_step.step.dependOn(&repo.step); + const progname_path = yacc_dep.path("progname.c"); + const gen_progname_step = b.addWriteFile(progname_path.getPath(b), + \\// workaround __progname not defined, https://github.com/ibara/yacc/pull/1 + \\char *__progname; + \\ + ); + // gen_progname_step.step.dependOn(&repo.step); - const config_step = b.addWriteFile(b.pathJoin(&.{ repo.path, "config.h" }), - \\// for simplicity just don't supported __unused - \\#define __unused - \\// for simplicity we're just not supporting noreturn - \\#define __dead - \\//#define HAVE_PROGNAME - \\//#define HAVE_ASPRINTF - \\//#define HAVE_PLEDGE - \\//#define HAVE_REALLOCARRAY - \\#define HAVE_STRLCPY - \\ - ); - config_step.step.dependOn(&repo.step); - const gen_progname_step = b.addWriteFile(b.pathJoin(&.{ repo.path, "progname.c" }), - \\// workaround __progname not defined, https://github.com/ibara/yacc/pull/1 - \\char *__progname; - \\ - ); - gen_progname_step.step.dependOn(&repo.step); + const exe = b.addExecutable(.{ + .name = "yacc", + .target = target, + .optimize = optimize, + }); + const install = installArtifact(b, exe); + // exe.step.dependOn(&repo.step); + exe.step.dependOn(&config_step.step); + exe.step.dependOn(&gen_progname_step.step); + // const repo_path = repo.getPath(&exe.step); + // var files = std.ArrayList([]const u8).init(b.allocator); + const sources = [_][]const u8{ + "closure.c", "error.c", "lalr.c", "lr0.c", "main.c", "mkpar.c", "output.c", "reader.c", + "skeleton.c", "symtab.c", "verbose.c", "warshall.c", "portable.c", "progname.c", + }; + // for (sources) |src| { + // files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; + // } + + exe.addCSourceFiles(.{ + .root = yacc_dep.path(""), + .files = &sources, + .flags = &[_][]const u8{ + "-std=c90", + }, + }); - const exe = b.addExecutable(.{ - .name = "yacc", - .target = target, - .optimize = optimize, - }); - const install = installArtifact(b, exe); - exe.step.dependOn(&repo.step); - exe.step.dependOn(&config_step.step); - exe.step.dependOn(&gen_progname_step.step); - const repo_path = repo.getPath(&exe.step); - var files = std.ArrayList([]const u8).init(b.allocator); - const sources = [_][]const u8{ - "closure.c", "error.c", "lalr.c", "lr0.c", "main.c", "mkpar.c", "output.c", "reader.c", - "skeleton.c", "symtab.c", "verbose.c", "warshall.c", "portable.c", "progname.c", - }; - for (sources) |src| { - files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; - } + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.linkLibrary(libc_only_std_static); + exe.linkLibrary(zig_start); + exe.linkLibrary(zig_posix); + // TODO: should libc_only_std_static and zig_start be able to add library dependencies? + if (target.result.os.tag == .windows) { + exe.linkSystemLibrary("ntdll"); + exe.linkSystemLibrary("kernel32"); + } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c90", - }); + const step = b.step("yacc", "build the yacc tool"); + step.dependOn(&install.step); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.linkLibrary(libc_only_std_static); - exe.linkLibrary(zig_start); - exe.linkLibrary(zig_posix); - // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { - exe.linkSystemLibrary("ntdll"); - exe.linkSystemLibrary("kernel32"); + return exe; } - - const step = b.step("yacc", "build the yacc tool"); - step.dependOn(&install.step); - - return exe; + return null; } fn addYabfc( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, - zig_gnu: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { - const repo = GitRepoStep.create(b, .{ - .url = "https://github.com/julianneswinoga/yabfc", - .sha = "a789be25a0918d330b7a4de12db0d33e0785f244", - .branch = null, - .fetch_enabled = true, - }); - - const exe = b.addExecutable(.{ - .name = "yabfc", - .target = target, - .optimize = optimize, - }); - const install = installArtifact(b, exe); - exe.step.dependOn(&repo.step); - const repo_path = repo.getPath(&exe.step); - var files = std.ArrayList([]const u8).init(b.allocator); - const sources = [_][]const u8{ - "assembly.c", "elfHelper.c", "helpers.c", "optimize.c", "yabfc.c", - }; - for (sources) |src| { - files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; - } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c99", - }); + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, + zig_gnu: *std.Build.Step.Compile, +) ?*std.Build.Step.Compile { + // const repo = GitRepoStep.create(b, .{ + // .url = "https://github.com/julianneswinoga/yabfc#a789be25a0918d330b7a4de12db0d33e0785f244", + // .branch = null, + // .fetch_enabled = true, + // }); + + if (b.lazyDependency("yabfc", .{})) |yabfc_dep| { + const exe = b.addExecutable(.{ + .name = "yabfc", + .target = target, + .optimize = optimize, + }); + const install = installArtifact(b, exe); + // exe.step.dependOn(&repo.step); + // const repo_path = repo.getPath(&exe.step); + // var files = std.ArrayList([]const u8).init(b.allocator); + const sources = [_][]const u8{ + "assembly.c", "elfHelper.c", "helpers.c", "optimize.c", "yabfc.c", + }; + // for (sources) |src| { + // files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; + // } + exe.addCSourceFiles(.{ + .root = yabfc_dep.path(""), + .files = &sources, + .flags = &[_][]const u8{ + "-std=c99", + }, + }); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.addIncludePath(.{ .path = "inc/linux" }); - exe.addIncludePath(.{ .path = "inc/gnu" }); - exe.linkLibrary(libc_only_std_static); - exe.linkLibrary(zig_start); - exe.linkLibrary(zig_posix); - exe.linkLibrary(zig_gnu); - // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { - exe.linkSystemLibrary("ntdll"); - exe.linkSystemLibrary("kernel32"); - } + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.addIncludePath(b.path("inc/linux")); + exe.addIncludePath(b.path("inc/gnu")); + exe.linkLibrary(libc_only_std_static); + exe.linkLibrary(zig_start); + exe.linkLibrary(zig_posix); + exe.linkLibrary(zig_gnu); + // TODO: should libc_only_std_static and zig_start be able to add library dependencies? + if (target.result.os.tag == .windows) { + exe.linkSystemLibrary("ntdll"); + exe.linkSystemLibrary("kernel32"); + } - const step = b.step("yabfc", "build/install the yabfc tool (Yet Another BrainFuck Compiler)"); - step.dependOn(&install.step); + const step = b.step("yabfc", "build/install the yabfc tool (Yet Another BrainFuck Compiler)"); + step.dependOn(&install.step); - return exe; + return exe; + } + return null; } fn addSecretGame( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, - zig_gnu: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { - const repo = GitRepoStep.create(b, .{ - .url = "https://github.com/ethinethin/Secret", - .sha = "8ec8442f84f8bed2cb3985455e7af4d1ce605401", - .branch = null, - .fetch_enabled = true, - }); - - const exe = b.addExecutable(.{ - .name = "secret", - .target = target, - .optimize = optimize, - }); - const install = b.addInstallArtifact(exe, .{}); - exe.step.dependOn(&repo.step); - const repo_path = repo.getPath(&exe.step); - var files = std.ArrayList([]const u8).init(b.allocator); - const sources = [_][]const u8{ - "main.c", "inter.c", "input.c", "items.c", "rooms.c", "linenoise/linenoise.c", - }; - for (sources) |src| { - files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; - } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c90", - }); + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, + zig_gnu: *std.Build.Step.Compile, +) ?*std.Build.Step.Compile { + // const repo = GitRepoStep.create(b, .{ + // .url = "https://github.com/ethinethin/Secret#8ec8442f84f8bed2cb3985455e7af4d1ce605401", + // .branch = null, + // .fetch_enabled = true, + // }); + + if (b.lazyDependency("secret-game", .{})) |secret_dep| { + const exe = b.addExecutable(.{ + .name = "secret", + .target = target, + .optimize = optimize, + }); + const install = b.addInstallArtifact(exe, .{}); + // exe.step.dependOn(&repo.step); + // const repo_path = repo.getPath(&exe.step); + // var files = std.ArrayList([]const u8).init(b.allocator); + const sources = [_][]const u8{ + "main.c", "inter.c", "input.c", "items.c", "rooms.c", "linenoise/linenoise.c", + }; + // for (sources) |src| { + // files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; + // } + exe.addCSourceFiles(.{ + .root = secret_dep.path(""), + .files = &sources, + .flags = &[_][]const u8{ + "-std=c90", + }, + }); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.addIncludePath(.{ .path = "inc/linux" }); - exe.addIncludePath(.{ .path = "inc/gnu" }); - exe.linkLibrary(libc_only_std_static); - exe.linkLibrary(zig_start); - exe.linkLibrary(zig_posix); - exe.linkLibrary(zig_gnu); - // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { - exe.linkSystemLibrary("ntdll"); - exe.linkSystemLibrary("kernel32"); - } + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.addIncludePath(b.path("inc/linux")); + exe.addIncludePath(b.path("inc/gnu")); + exe.linkLibrary(libc_only_std_static); + exe.linkLibrary(zig_start); + exe.linkLibrary(zig_posix); + exe.linkLibrary(zig_gnu); + // TODO: should libc_only_std_static and zig_start be able to add library dependencies? + if (target.result.os.tag == .windows) { + exe.linkSystemLibrary("ntdll"); + exe.linkSystemLibrary("kernel32"); + } - const step = b.step("secret", "build/install the secret game"); - step.dependOn(&install.step); + const step = b.step("secret", "build/install the secret game"); + step.dependOn(&install.step); - return exe; + return exe; + } + return null; } diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..e7a005c --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,37 @@ +.{ + .name = "ziglibc", + .version = "0.0.1", + .paths = .{""}, + .dependencies = .{ + .@"tiny-regex-c" = .{ + .url = "git+https://github.com/marler8997/tiny-regex-c#95ef2ad35d36783d789b0ade3178b30a942f085c", + .hash = "12200f37e89b53ddb28c5afc564a6092f4f62cc15cdec602532ff7671841d86a1a02", + .lazy = true, + }, + .lua = .{ + .url = "git+https://github.com/lua/lua#5d708c3f9cae12820e415d4f89c9eacbe2ab964b", + .hash = "122094d2699fd09a9d8d46414e3aa5ded6b39713ad54215da2125821092137342b28", + .lazy = true, + }, + .cmph = .{ + .url = "git+https://github.com/bonitao/cmph#abd5e1e17e4d51b3e24459ab9089dc0522846d0d", + .hash = "12203b28550f41bb268eab26103c43c6bc4cb32b2939e5420573ac0c1ac4a3593fdf", + .lazy = true, + }, + .yacc = .{ + .url = "git+https://github.com/ibara/yacc#1a4138ce2385ec676c6d374245fda5a9cd2fbee2", + .hash = "1220bb39eb5bfade87582a2acf5265c4f37f7b93d0c0b57015287dfd7701f9145ea0", + .lazy = true, + }, + .yabfc = .{ + .url = "git+https://github.com/julianneswinoga/yabfc#a789be25a0918d330b7a4de12db0d33e0785f244", + .hash = "122081a373be058cd09307a60e80edec1721ca13447e0c81749e7a0b0cd1b05dfb00", + .lazy = true, + }, + .@"secret-game" = .{ + .url = "git+https://github.com/ethinethin/Secret#8ec8442f84f8bed2cb3985455e7af4d1ce605401", + .hash = "1220be423b75a267f50a8c2e1046f0acffaf7cd7b6a6e16af20c6fb8ec8f41fb27b6", + .lazy = true, + }, + }, +} diff --git a/busybox/build.zig b/busybox/build.zig index 6f8311f..89c7013 100644 --- a/busybox/build.zig +++ b/busybox/build.zig @@ -2,13 +2,13 @@ const std = @import("std"); const GitRepoStep = @import("../GitRepoStep.zig"); const BusyboxPrepStep = struct { - step: std.build.Step, - builder: *std.build.Builder, + step: std.Build.Step, + builder: *std.Build, repo_path: []const u8, - pub fn create(b: *std.build.Builder, repo: *GitRepoStep) *BusyboxPrepStep { - var result = b.allocator.create(BusyboxPrepStep) catch unreachable; + pub fn create(b: *std.Build, repo: *GitRepoStep) *BusyboxPrepStep { + const result = b.allocator.create(BusyboxPrepStep) catch unreachable; result.* = BusyboxPrepStep{ - .step = std.build.Step.init(.{ + .step = std.Build.Step.init(.{ .id = .custom, .name = "busybox prep", .owner = b, @@ -20,13 +20,14 @@ const BusyboxPrepStep = struct { result.*.step.dependOn(&repo.step); return result; } - fn make(step: *std.build.Step, progress: *std.Progress.Node) !void { - _ = progress; - const self = @fieldParentPtr(BusyboxPrepStep, "step", step); + fn make(step: *std.Build.Step, opts: std.Build.Step.MakeOptions) !void { + _ = opts; + const self: *BusyboxPrepStep = @fieldParentPtr("step", step); const b = self.builder; std.log.warn("TODO: check config file timestamp to prevent unnecessary copy", .{}); - var src_dir = try std.fs.cwd().openDir(b.pathJoin(&.{ b.build_root.path.?, "busybox" }), .{}); + // var src_dir = try std.fs.cwd().openDir(b.pathJoin(&.{ b.build_root.path.?, "busybox" }), .{}); + var src_dir = try b.build_root.handle.openDir("busybox", .{}); defer src_dir.close(); var dst_dir = try std.fs.cwd().openDir(self.repo_path, .{}); defer dst_dir.close(); @@ -35,12 +36,12 @@ const BusyboxPrepStep = struct { }; pub fn add( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { + libc_only_std_static: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, +) *std.Build.Step.Compile { const repo = GitRepoStep.create(b, .{ .url = "https://git.busybox.net/busybox", .sha = "e512aeb0fb3c585948ae6517cfdf4a53cf99774d", @@ -64,19 +65,22 @@ pub fn add( for (sources) |src| { files.append(b.pathJoin(&.{ repo_path, src })) catch unreachable; } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c99", + exe.addCSourceFiles(.{ + .files = files.toOwnedSlice() catch unreachable, + .flags = &[_][]const u8{ + "-std=c99", + }, }); - exe.addIncludePath(.{ .path = b.pathJoin(&.{ repo_path, "include" }) }); + exe.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ repo_path, "include" }) }); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.addIncludePath(.{ .path = "inc/linux" }); + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.addIncludePath(b.path("inc/linux")); exe.linkLibrary(libc_only_std_static); //exe.linkLibrary(zig_start); exe.linkLibrary(zig_posix); // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { + if (target.result.os.tag == .windows) { exe.linkSystemLibrary("ntdll"); exe.linkSystemLibrary("kernel32"); } diff --git a/gnumakebuild.zig b/gnumakebuild.zig index a1f1203..8ca196d 100644 --- a/gnumakebuild.zig +++ b/gnumakebuild.zig @@ -2,21 +2,21 @@ const std = @import("std"); const GitRepoStep = @import("GitRepoStep.zig"); pub fn addGnuMake( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_start: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { + libc_only_std_static: *std.Build.Step.Compile, + zig_start: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, +) *std.Build.Step.Compile { const repo = GitRepoStep.create(b, .{ .url = "https://git.savannah.gnu.org/git/make.git", .sha = "ed493f6c9116cc217b99c2cfa6a95f15803235a2", .branch = "4.4", }); - const config_step = b.allocator.create(std.build.Step) catch unreachable; - config_step.* = std.build.Step.init(.{ + const config_step = b.allocator.create(std.Build.Step) catch unreachable; + config_step.* = std.Build.Step.init(.{ .id = .custom, .name = "configure GNU Make", .owner = b, @@ -82,37 +82,40 @@ pub fn addGnuMake( files.append(b.pathJoin(&.{ repo_path, "src", src })) catch unreachable; } - exe.addIncludePath(.{ .path = b.pathJoin(&.{ repo_path, "src" }) }); - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c99", - "-DHAVE_CONFIG_H", - "-Wall", - "-Wextra", - "-Werror", - "-Wwrite-strings", - "-Wshadow", - "-Wdeclaration-after-statement", - "-Wbad-function-cast", - "-Wformat-security", - "-Wtype-limits", - "-Wunused-but-set-parameter", - "-Wpointer-arith", - "-Wignored-qualifiers", - // ignore unused parameter errors because the ATTRIBUTE define isn't working in makeint.h - "-Wno-unused-parameter", - "-Wno-dangling-else", - //"-Wlogical-op", "-Wformat-signedness", "-Wduplicated-cond", + exe.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ repo_path, "src" }) }); + exe.addCSourceFiles(.{ + .files = files.toOwnedSlice() catch unreachable, + .flags = &[_][]const u8{ + "-std=c99", + "-DHAVE_CONFIG_H", + "-Wall", + "-Wextra", + "-Werror", + "-Wwrite-strings", + "-Wshadow", + "-Wdeclaration-after-statement", + "-Wbad-function-cast", + "-Wformat-security", + "-Wtype-limits", + "-Wunused-but-set-parameter", + "-Wpointer-arith", + "-Wignored-qualifiers", + // ignore unused parameter errors because the ATTRIBUTE define isn't working in makeint.h + "-Wno-unused-parameter", + "-Wno-dangling-else", + //"-Wlogical-op", "-Wformat-signedness", "-Wduplicated-cond", + }, }); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.addIncludePath(.{ .path = "inc/gnu" }); - exe.addIncludePath(.{ .path = "inc/alloca" }); + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.addIncludePath(b.path("inc/gnu")); + exe.addIncludePath(b.path("inc/alloca")); exe.linkLibrary(libc_only_std_static); exe.linkLibrary(zig_start); exe.linkLibrary(zig_posix); // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { + if (target.result.os.tag == .windows) { exe.linkSystemLibrary("ntdll"); exe.linkSystemLibrary("kernel32"); } diff --git a/ncurses/build.zig b/ncurses/build.zig index 9c2ac85..597e1ef 100644 --- a/ncurses/build.zig +++ b/ncurses/build.zig @@ -4,18 +4,18 @@ const ProcessFileStep = @import("../ProcessFileStep.zig"); const filecheck = @import("../filecheck.zig"); const NcursesPrepStep = struct { - step: std.build.Step, - builder: *std.build.Builder, + step: std.Build.Step, + builder: *std.Build, repo_path: []const u8, defs_h_src: []const u8, defs_h_dst: []const u8, //curses_h: []const u8, - pub fn create(b: *std.build.Builder, repo: *GitRepoStep) *NcursesPrepStep { - var result = b.allocator.create(NcursesPrepStep) catch unreachable; + pub fn create(b: *std.Build, repo: *GitRepoStep) *NcursesPrepStep { + const result = b.allocator.create(NcursesPrepStep) catch unreachable; result.* = NcursesPrepStep{ - .step = std.build.Step.init(.{ + .step = std.Build.Step.init(.{ .id = .custom, .name = "ncurses prep", .owner = b, @@ -30,9 +30,9 @@ const NcursesPrepStep = struct { result.*.step.dependOn(&repo.step); return result; } - fn make(step: *std.build.Step, progress: *std.Progress.Node) !void { - _ = progress; - const self = @fieldParentPtr(NcursesPrepStep, "step", step); + fn make(step: *std.Build.Step, opts: std.Build.Step.MakeOptions) !void { + _ = opts; + const self: *NcursesPrepStep = @fieldParentPtr("step", step); //try self.generateCuresesH(); try self.generateNcursesDefH(); } @@ -51,7 +51,7 @@ const NcursesPrepStep = struct { defer arena.deinit(); const content = std.fs.cwd().readFileAlloc(arena.allocator(), self.defs_h_src, std.math.maxInt(usize)) catch |err| { std.log.err("failed to read file '{s}' to process ({s})", .{ self.defs_h_src, @errorName(err) }); - std.os.exit(0xff); + std.process.exit(0xff); }; const tmp_filename = try std.fmt.allocPrint(arena.allocator(), "{s}.processing", .{self.defs_h_dst}); @@ -64,11 +64,11 @@ const NcursesPrepStep = struct { try writer.writeAll("#ifndef NC_DEFINE_H\n"); try writer.writeAll("#define NC_DEFINE_H\n"); - var it = std.mem.split(u8, content, "\n"); + var it = std.mem.splitScalar(u8, content, '\n'); while (it.next()) |line_untrimmed| { const line = std.mem.trim(u8, line_untrimmed, " \t\r"); if (line.len == 0 or line[0] == '#') continue; - var field_it = std.mem.tokenize(u8, line, " \t"); + var field_it = std.mem.tokenizeAny(u8, line, " \t"); const name = field_it.next().?; const optional_value = field_it.next(); try writer.print("\n#ifndef {s}\n", .{name}); @@ -87,9 +87,9 @@ const NcursesPrepStep = struct { }; fn addProcessFile( - b: *std.build.Builder, + b: *std.Build, repo: *GitRepoStep, - exe: *std.build.LibExeObjStep, + exe: *std.Build.Step.Compile, in_sub_path: []const u8, out_sub_path: []const u8, opt: struct { @@ -106,12 +106,12 @@ fn addProcessFile( } pub fn add( - b: *std.build.Builder, + b: *std.Build, target: anytype, optimize: anytype, - libc_only_std_static: *std.build.LibExeObjStep, - zig_posix: *std.build.LibExeObjStep, -) *std.build.LibExeObjStep { + libc_only_std_static: *std.Build.Step.Compile, + zig_posix: *std.Build.Step.Compile, +) *std.Build.Step.Compile { const repo = GitRepoStep.create(b, .{ .url = "https://github.com/mirror/ncurses", .sha = "deb0d07e8eb4803b9e9653359eab17a30d04369d", @@ -154,26 +154,30 @@ pub fn add( const install = b.addInstallArtifact(exe, .{}); exe.step.dependOn(&prep.step); const repo_path = repo.getPath(&exe.step); - var files = std.ArrayList([]const u8).init(b.allocator); - for ([_][]const u8{"lib_initscr.c"}) |src| { - files.append(b.pathJoin(&.{ repo_path, "ncurses", "base", src })) catch unreachable; - } - exe.addCSourceFiles(files.toOwnedSlice() catch unreachable, &[_][]const u8{ - "-std=c99", + // var files = std.ArrayList([]const u8).init(b.allocator); + // for ([_][]const u8{"lib_initscr.c"}) |src| { + // files.append(b.pathJoin(&.{ repo_path, "ncurses", "base", src })) catch unreachable; + // } + exe.addCSourceFiles(.{ + .root = .{ .cwd_relative = repo_path }, + .files = &.{"ncurses/base/lib_initscr.c"}, + .flags = &[_][]const u8{ + "-std=c99", + }, }); - exe.addIncludePath(.{ .path = b.pathJoin(&.{ repo_path, "include" }) }); - exe.addIncludePath(.{ .path = b.pathJoin(&.{ repo_path, "ncurses" }) }); + exe.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ repo_path, "include" }) }); + exe.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ repo_path, "ncurses" }) }); - exe.addIncludePath(.{ .path = "inc/libc" }); - exe.addIncludePath(.{ .path = "inc/posix" }); - exe.addIncludePath(.{ .path = "inc/linux" }); + exe.addIncludePath(b.path("inc/libc")); + exe.addIncludePath(b.path("inc/posix")); + exe.addIncludePath(b.path("inc/linux")); //exe.addIncludePath(.{.path = "inc/gnu"}); exe.linkLibrary(libc_only_std_static); //exe.linkLibrary(zig_start); exe.linkLibrary(zig_posix); //exe.linkLibrary(zig_gnu); // TODO: should libc_only_std_static and zig_start be able to add library dependencies? - if (target.getOs().tag == .windows) { + if (target.result.os.tag == .windows) { exe.linkSystemLibrary("ntdll"); exe.linkSystemLibrary("kernel32"); } diff --git a/src/cstd.zig b/src/cstd.zig index e7cd213..11e6965 100644 --- a/src/cstd.zig +++ b/src/cstd.zig @@ -26,7 +26,7 @@ fn __main() callconv(.C) void { // TODO: call constructors } comptime { - if (builtin.os.tag == .windows) @export(__main, .{ .name = "__main" }); + if (builtin.os.tag == .windows) @export(&__main, .{ .name = "__main" }); } const windows = struct { @@ -80,7 +80,7 @@ export fn exit(status: c_int) callconv(.C) noreturn { global.atexit_funcs.items[i - 1](); } } - std.os.exit(@intCast(status)); + std.process.exit(@intCast(status)); } const ExitFunc = switch (builtin.zig_backend) { @@ -457,7 +457,7 @@ fn strto(comptime T: type, str: [*:0]const u8, optional_endptr: ?*[*:0]const u8, break :blk 10; }; - var digit_start = next; + const digit_start = next; var x: T = 0; while (true) : (next += 1) { @@ -590,7 +590,7 @@ export fn signal(sig: c_int, func: SignalFn) callconv(.C) ?SignalFn { // stdio // -------------------------------------------------------------------------------- const global = struct { - var rand: std.rand.DefaultPrng = undefined; + var rand: std.Random.DefaultPrng = undefined; var gpa = std.heap.GeneralPurposeAllocator(.{ .MutexType = std.Thread.Mutex, @@ -613,7 +613,7 @@ const global = struct { fn reserveFile() *c.FILE { var i: usize = 0; while (i < files_reserved.len) : (i += 1) { - if (!@atomicRmw(bool, &files_reserved[i], .Xchg, true, .SeqCst)) { + if (!@atomicRmw(bool, &files_reserved[i], .Xchg, true, .seq_cst)) { return &files[i]; } } @@ -621,7 +621,7 @@ const global = struct { } fn releaseFile(file: *c.FILE) void { const i = (@intFromPtr(file) - @intFromPtr(&files[0])) / @sizeOf(usize); - if (!@atomicRmw(bool, &files_reserved[i], .Xchg, false, .SeqCst)) { + if (!@atomicRmw(bool, &files_reserved[i], .Xchg, false, .seq_cst)) { std.debug.panic("released FILE (i={} ptr={*}) that was not reserved", .{ i, file }); } } @@ -861,9 +861,9 @@ export fn freopen(filename: [*:0]const u8, mode: [*:0]const u8, stream: *c.FILE) export fn fclose(stream: *c.FILE) callconv(.C) c_int { trace.log("fclose {*}", .{stream}); if (builtin.os.tag == .windows) { - std.os.close(stream.fd.?); + std.posix.close(stream.fd.?); } else { - std.os.close(stream.fd); + std.posix.close(stream.fd); } global.releaseFile(stream); return 0; @@ -908,7 +908,7 @@ export fn rewind(stream: *c.FILE) callconv(.C) void { // TODO: why is there a putc and an fputc function? They seem to be equivalent // so what's the history? comptime { - @export(fputc, .{ .name = "putc" }); + @export(&fputc, .{ .name = "putc" }); } export fn fputc(character: c_int, stream: *c.FILE) callconv(.C) c_int { @@ -1293,7 +1293,7 @@ fn longjmp(env: c.jmp_buf, val: c_int) callconv(.C) noreturn { comptime { // temporary to get windows to link for now if (builtin.os.tag == .windows) { - @export(setjmp, .{ .name = "setjmp" }); - @export(longjmp, .{ .name = "longjmp" }); + @export(&setjmp, .{ .name = "setjmp" }); + @export(&longjmp, .{ .name = "longjmp" }); } } diff --git a/src/posix.zig b/src/posix.zig index 630e677..e255cc2 100644 --- a/src/posix.zig +++ b/src/posix.zig @@ -1,6 +1,7 @@ const builtin = @import("builtin"); const std = @import("std"); const os = std.os; +const posix = std.posix; const c = @cImport({ @cInclude("errno.h"); @@ -81,7 +82,7 @@ export fn write(fd: c_int, buf: [*]const u8, nbyte: usize) callconv(.C) isize { @panic("write not implemented on windows"); } const rc = os.system.write(fd, buf, nbyte); - switch (os.errno(rc)) { + switch (posix.errno(rc)) { .SUCCESS => return @as(isize, @intCast(rc)), else => |e| { c.errno = @intFromEnum(e); @@ -93,7 +94,7 @@ export fn write(fd: c_int, buf: [*]const u8, nbyte: usize) callconv(.C) isize { export fn read(fd: c_int, buf: [*]u8, len: usize) callconv(.C) isize { trace.log("read fd={} buf={*} len={}", .{ fd, buf, len }); const rc = os.linux.read(fd, buf, len); - switch (os.errno(rc)) { + switch (posix.errno(rc)) { .SUCCESS => return @as(isize, @intCast(rc)), else => |e| { c.errno = @intFromEnum(e); @@ -148,7 +149,7 @@ export fn mkostemp(template: [*:0]u8, suffixlen: c_int, flags: c_int) callconv(. while (true) : (attempt += 1) { randomizeTempFilename(rand_part); const fd = os.system.open(template, @as(u32, @intCast(flags | os.O.RDWR | os.O.CREAT | os.O.EXCL)), 0o600); - switch (os.errno(fd)) { + switch (posix.errno(fd)) { .SUCCESS => return @as(c_int, @intCast(fd)), else => |e| { if (attempt >= max_attempts) { @@ -235,7 +236,7 @@ export fn unlink(path: [*:0]const u8) callconv(.C) c_int { if (builtin.os.tag == .windows) @panic("windows unlink not implemented"); - switch (os.errno(os.system.unlink(path))) { + switch (posix.errno(os.system.unlink(path))) { .SUCCESS => return 0, else => |e| { c.errno = @intFromEnum(e); @@ -263,7 +264,7 @@ export fn isatty(fd: c_int) callconv(.C) c_int { @panic("isatty not supported on windows (yet?)"); var size: c.winsize = undefined; - switch (os.errno(os.system.ioctl(fd, c.TIOCGWINSZ, @intFromPtr(&size)))) { + switch (posix.errno(os.system.ioctl(fd, c.TIOCGWINSZ, @intFromPtr(&size)))) { .SUCCESS => return 1, .BADF => { c.errno = c.ENOTTY; @@ -277,13 +278,13 @@ export fn isatty(fd: c_int) callconv(.C) c_int { // sys/time // -------------------------------------------------------------------------------- comptime { - std.debug.assert(@sizeOf(c.timespec) == @sizeOf(os.timespec)); + std.debug.assert(@sizeOf(c.timespec) == @sizeOf(posix.timespec)); if (builtin.os.tag != .windows) { - std.debug.assert(c.CLOCK_REALTIME == os.CLOCK.REALTIME); + std.debug.assert(c.CLOCK_REALTIME == posix.CLOCK.REALTIME); } } -export fn clock_gettime(clk_id: c.clockid_t, tp: *os.timespec) callconv(.C) c_int { +export fn clock_gettime(clk_id: c.clockid_t, tp: *posix.timespec) callconv(.C) c_int { if (builtin.os.tag == .windows) { if (clk_id == c.CLOCK_REALTIME) { var ft: os.windows.FILETIME = undefined; @@ -292,8 +293,8 @@ export fn clock_gettime(clk_id: c.clockid_t, tp: *os.timespec) callconv(.C) c_in const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime; const ft_per_s = std.time.ns_per_s / 100; tp.* = .{ - .tv_sec = @as(i64, @intCast(ft64 / ft_per_s)) + std.time.epoch.windows, - .tv_nsec = @as(c_long, @intCast(ft64 % ft_per_s)) * 100, + .sec = @as(i64, @intCast(ft64 / ft_per_s)) + std.time.epoch.windows, + .nsec = @as(c_long, @intCast(ft64 % ft_per_s)) * 100, }; return 0; } @@ -301,7 +302,7 @@ export fn clock_gettime(clk_id: c.clockid_t, tp: *os.timespec) callconv(.C) c_in std.debug.panic("clk_id {} not implemented on Windows", .{clk_id}); } - switch (os.errno(os.system.clock_gettime(clk_id, tp))) { + switch (posix.errno(os.system.clock_gettime(clk_id, tp))) { .SUCCESS => return 0, else => |e| { c.errno = @intFromEnum(e); @@ -349,7 +350,7 @@ export fn fstat(fd: c_int, buf: *c.struct_stat) c_int { export fn umask(mode: c.mode_t) callconv(.C) c.mode_t { trace.log("umask 0x{x}", .{mode}); const old_mode = os.linux.syscall1(.umask, @as(usize, @intCast(mode))); - switch (os.errno(old_mode)) { + switch (posix.errno(old_mode)) { .SUCCESS => {}, else => |e| std.debug.panic("umask syscall should never fail but got '{s}'", .{@tagName(e)}), } @@ -379,7 +380,7 @@ export fn basename(path: ?[*:0]u8) callconv(.C) [*:0]u8 { // termios // -------------------------------------------------------------------------------- export fn tcgetattr(fd: c_int, ios: *os.linux.termios) callconv(.C) c_int { - switch (os.errno(os.linux.tcgetattr(fd, ios))) { + switch (posix.errno(os.linux.tcgetattr(fd, ios))) { .SUCCESS => return 0, else => |errno| { c.errno = @intFromEnum(errno); @@ -393,7 +394,7 @@ export fn tcsetattr( optional_actions: c_int, ios: *const os.linux.termios, ) callconv(.C) c_int { - switch (os.errno(os.linux.tcsetattr(fd, @as(os.linux.TCSA, @enumFromInt(optional_actions)), ios))) { + switch (posix.errno(os.linux.tcsetattr(fd, @as(os.linux.TCSA, @enumFromInt(optional_actions)), ios))) { .SUCCESS => return 0, else => |errno| { c.errno = @intFromEnum(errno); @@ -425,7 +426,7 @@ export fn strcasecmp(a: [*:0]const u8, b: [*:0]const u8) callconv(.C) c_int { export fn _ioctlArgPtr(fd: c_int, request: c_ulong, arg_ptr: *anyopaque) c_int { trace.log("ioctl fd={} request=0x{x} arg={*}", .{ fd, request, arg_ptr }); const rc = os.linux.ioctl(fd, @as(u32, @intCast(request)), @intFromPtr(arg_ptr)); - switch (os.errno(rc)) { + switch (posix.errno(rc)) { .SUCCESS => return @as(c_int, @intCast(rc)), else => |errno| { c.errno = @intFromEnum(errno); @@ -457,9 +458,9 @@ export fn select( // -------------------------------------------------------------------------------- comptime { if (builtin.os.tag == .windows) { - @export(fileno, .{ .name = "_fileno" }); - @export(isatty, .{ .name = "_isatty" }); - @export(popen, .{ .name = "_popen" }); - @export(pclose, .{ .name = "_pclose" }); + @export(&fileno, .{ .name = "_fileno" }); + @export(&isatty, .{ .name = "_isatty" }); + @export(&popen, .{ .name = "_popen" }); + @export(&pclose, .{ .name = "_pclose" }); } } diff --git a/src/start.zig b/src/start.zig index c9ad92e..886c31f 100644 --- a/src/start.zig +++ b/src/start.zig @@ -36,7 +36,7 @@ fn windowsArgsAlloc() [:null]?[*:0]u8 { var it = std.process.argsWithAllocator(tmp_arena.allocator()) catch |err| switch (err) { error.OutOfMemory => @panic(out_of_memory_msg), // TODO: would be nice to get the actual utf16 decode error name - error.InvalidCmdLine => @panic("Failed to decode command line"), + // error.InvalidCmdLine => @panic("Failed to decode command line"), }; defer it.deinit(); while (it.next()) |tmp_arg| { diff --git a/src/trace.zig b/src/trace.zig index c7b9091..148cd6e 100644 --- a/src/trace.zig +++ b/src/trace.zig @@ -9,14 +9,14 @@ pub fn log(comptime fmt: []const u8, args: anytype) void { pub fn fmtStr(s: anytype) FmtStr { switch (@typeInfo(@TypeOf(s))) { - .Pointer => |info| switch (info.size) { + .pointer => |info| switch (info.size) { .Slice => return FmtStr.initSlice(s), .Many => if (info.sentinel) |_| { return FmtStr.initSentinel(s); }, else => {}, }, - .Optional => return fmtStr(s orelse return FmtStr.initNull()), + .optional => return fmtStr(s orelse return FmtStr.initNull()), else => {}, } @compileError("fmtStr for type " ++ @typeName(@TypeOf(s)) ++ " is not implemented"); diff --git a/test/testenv.zig b/test/testenv.zig index 66ec009..4ede587 100644 --- a/test/testenv.zig +++ b/test/testenv.zig @@ -15,7 +15,7 @@ pub fn main() !u8 { const dirname = try std.fmt.allocPrint(arena.allocator(), "{s}.test.tmp", .{std.fs.path.basename(args[0])}); try std.fs.cwd().deleteTree(dirname); try std.fs.cwd().makeDir(dirname); - var child = std.ChildProcess.init(args, arena.allocator()); + var child = std.process.Child.init(args, arena.allocator()); child.cwd = dirname; try child.spawn(); const result = try child.wait(); diff --git a/ziglibcbuild.zig b/ziglibcbuild.zig index 960b567..a52e172 100644 --- a/ziglibcbuild.zig +++ b/ziglibcbuild.zig @@ -1,6 +1,6 @@ const std = @import("std"); -const build = std.Build; -const CompileStep = build.Step.Compile; +const Build = std.Build; +const CompileStep = Build.Step.Compile; pub const LinkKind = enum { static, shared }; pub const LibVariant = enum { @@ -19,37 +19,37 @@ pub const ZigLibcOptions = struct { link: LinkKind, start: Start, trace: bool, - target: std.zig.CrossTarget, + target: std.Build.ResolvedTarget, optimize: std.builtin.Mode, }; -fn relpath(comptime src_path: []const u8) std.Build.LazyPath { - if (comptime std.fs.path.dirname(@src().file)) |dir| - return .{ .path = dir ++ std.fs.path.sep_str ++ src_path }; - return .{ .path = src_path }; -} +// fn relpath(comptime src_path: []const u8) std.Build.LazyPath { +// if (comptime std.fs.path.dirname(@src().file)) |dir| +// return .{ .path = dir ++ std.fs.path.sep_str ++ src_path }; +// return .{ .path = src_path }; +// } /// Provides a _start symbol that will call C main pub fn addZigStart( - builder: *build, - target: std.zig.CrossTarget, + b: *Build, + target: std.Build.ResolvedTarget, optimize: anytype, ) *CompileStep { - const lib = builder.addStaticLibrary(.{ + const lib = b.addStaticLibrary(.{ .name = "start", - .root_source_file = relpath("src" ++ std.fs.path.sep_str ++ "start.zig"), + .root_source_file = b.path("src/start.zig"), .target = target, .optimize = optimize, }); // TODO: not sure if this is reallly needed or not, but it shouldn't hurt // anything except performance to enable it - lib.force_pic = true; + lib.root_module.pic = true; return lib; } // Returns ziglibc as a CompileStep // Caller will also need to add the include path to get the C headers -pub fn addLibc(builder: *std.build.Builder, opt: ZigLibcOptions) *CompileStep { +pub fn addLibc(b: *Build, opt: ZigLibcOptions) *CompileStep { const name = switch (opt.variant) { .only_std => "c-only-std", .only_posix => "c-only-posix", @@ -59,23 +59,23 @@ pub fn addLibc(builder: *std.build.Builder, opt: ZigLibcOptions) *CompileStep { .full => "cguana", // use cguana to avoid passing in '-lc' to zig which will // cause it to add the system libc headers }; - const trace_options = builder.addOptions(); + const trace_options = b.addOptions(); trace_options.addOption(bool, "enabled", opt.trace); - const modules_options = builder.addOptions(); + const modules_options = b.addOptions(); modules_options.addOption(bool, "glibcstart", switch (opt.start) { .glibc => true, else => false, }); - const index = relpath("src" ++ std.fs.path.sep_str ++ "lib.zig"); + const index = b.path("src/lib.zig"); const lib = switch (opt.link) { - .static => builder.addStaticLibrary(.{ + .static => b.addStaticLibrary(.{ .name = name, .root_source_file = index, .target = opt.target, .optimize = opt.optimize, }), - .shared => builder.addSharedLibrary(.{ + .shared => b.addSharedLibrary(.{ .name = name, .root_source_file = index, .target = opt.target, @@ -88,9 +88,9 @@ pub fn addLibc(builder: *std.build.Builder, opt: ZigLibcOptions) *CompileStep { }; // TODO: not sure if this is reallly needed or not, but it shouldn't hurt // anything except performance to enable it - lib.force_pic = true; - lib.addOptions("modules", modules_options); - lib.addOptions("trace_options", trace_options); + lib.root_module.pic = true; + lib.root_module.addOptions("modules", modules_options); + lib.root_module.addOptions("trace_options", trace_options); const c_flags = [_][]const u8{ "-std=c11", }; @@ -100,10 +100,10 @@ pub fn addLibc(builder: *std.build.Builder, opt: ZigLibcOptions) *CompileStep { }; modules_options.addOption(bool, "cstd", include_cstd); if (include_cstd) { - lib.addCSourceFile(.{ .file = relpath("src" ++ std.fs.path.sep_str ++ "printf.c"), .flags = &c_flags }); - lib.addCSourceFile(.{ .file = relpath("src" ++ std.fs.path.sep_str ++ "scanf.c"), .flags = &c_flags }); - if (opt.target.getOsTag() == .linux) { - lib.addAssemblyFile(relpath("src/linux/jmp.s")); + lib.addCSourceFile(.{ .file = b.path("src" ++ std.fs.path.sep_str ++ "printf.c"), .flags = &c_flags }); + lib.addCSourceFile(.{ .file = b.path("src" ++ std.fs.path.sep_str ++ "scanf.c"), .flags = &c_flags }); + if (opt.target.result.os.tag == .linux) { + lib.addAssemblyFile(b.path("src/linux/jmp.s")); } } const include_posix = switch (opt.variant) { @@ -112,7 +112,7 @@ pub fn addLibc(builder: *std.build.Builder, opt: ZigLibcOptions) *CompileStep { }; modules_options.addOption(bool, "posix", include_posix); if (include_posix) { - lib.addCSourceFile(.{ .file = relpath("src" ++ std.fs.path.sep_str ++ "posix.c"), .flags = &c_flags }); + lib.addCSourceFile(.{ .file = b.path("src" ++ std.fs.path.sep_str ++ "posix.c"), .flags = &c_flags }); } const include_linux = switch (opt.variant) { .only_linux, .full => true, @@ -120,8 +120,8 @@ pub fn addLibc(builder: *std.build.Builder, opt: ZigLibcOptions) *CompileStep { }; modules_options.addOption(bool, "linux", include_linux); if (include_cstd or include_posix) { - lib.addIncludePath(relpath("inc" ++ std.fs.path.sep_str ++ "libc")); - lib.addIncludePath(relpath("inc" ++ std.fs.path.sep_str ++ "posix")); + lib.addIncludePath(b.path("inc" ++ std.fs.path.sep_str ++ "libc")); + lib.addIncludePath(b.path("inc" ++ std.fs.path.sep_str ++ "posix")); } const include_gnu = switch (opt.variant) { .only_gnu, .full => true, @@ -129,7 +129,7 @@ pub fn addLibc(builder: *std.build.Builder, opt: ZigLibcOptions) *CompileStep { }; modules_options.addOption(bool, "gnu", include_gnu); if (include_gnu) { - lib.addIncludePath(relpath("inc" ++ std.fs.path.sep_str ++ "gnu")); + lib.addIncludePath(b.path("inc" ++ std.fs.path.sep_str ++ "gnu")); } return lib; }