Skip to content

Commit

Permalink
update build support for Zig 0.12, remove build support for Zig 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed May 17, 2024
1 parent 8da0c6b commit 462ee1a
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ jobs:
compile:
strategy:
matrix:
zig_version: [0.11.0, master]
os: [ubuntu-latest]
zig_version: [0.11.0, 0.12.0]
experimental: [false]
os: [ubuntu-latest]
include:
- zig_version: 0.11.0
os: windows-latest
experimental: true
- zig_version: master
- zig_version: 0.12.0
os: windows-latest
experimental: true

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Zig
uses: goto-bus-stop/[email protected]
with:
version: 0.11.0
version: 0.12.0

- name: Check out repository
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
zig 0.11
zig 0.12
2 changes: 1 addition & 1 deletion aqua.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ registries:
- type: standard
ref: v4.63.0 # renovate: depName=aquaproj/aqua-registry
packages:
- name: ziglang/zig@0.11.0
- name: ziglang/zig@0.12.0
25 changes: 15 additions & 10 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const std = @import("std");
const LazyPath = if (@hasDecl(std.Build, "LazyPath")) std.Build.LazyPath else std.Build.FileSource;
const Build = if (@hasDecl(std, "Build")) std.Build else std.build.Builder;

pub fn build(b: *std.Build) !void {
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const root_source_file = LazyPath.relative("src/main.zig");
const optimize = if (comptime @hasDecl(Build, "standardOptimizeOption")) b.standardOptimizeOption(.{}) else b.standardReleaseOptions();
const root_source_file = if (@hasDecl(Build, "path")) b.path("src/main.zig") else Build.LazyPath.relative("src/main.zig");

// Dt executable
const dt_step = b.step("dt", "Install dt executable");
Expand All @@ -16,7 +16,9 @@ pub fn build(b: *std.Build) !void {
.target = target,
});

const dt_install = b.addInstallArtifact(dt, .{});
const dt_install =
b.addInstallArtifact(dt, .{});

dt_step.dependOn(&dt_install.step);
b.default_step.dependOn(dt_step);

Expand All @@ -28,7 +30,7 @@ pub fn build(b: *std.Build) !void {

const query = try std.zig.CrossTarget.parse(.{ .arch_os_abi = TRIPLE });

const cross: *std.Build.Step.Compile = b.addExecutable(.{
const cross = b.addExecutable(.{
.name = exe,
.root_source_file = root_source_file,
.optimize = optimize,
Expand All @@ -40,9 +42,11 @@ pub fn build(b: *std.Build) !void {
query,
});

const cross_install = b.addInstallArtifact(cross, .{});
const cross_install =
b.addInstallArtifact(cross, .{});

const exe_filename = if (query.cpu_arch == .wasm32) exe ++ ".wasm" else if (query.os_tag == .windows) exe ++ ".exe" else exe;
const is_wasm = if (query.cpu_arch) |arch| arch.isWasm() else false;
const exe_filename = if (is_wasm) exe ++ ".wasm" else if (query.os_tag == .windows) exe ++ ".exe" else exe;

const cross_tar = b.addSystemCommand(&.{
"tar", "--transform", "s|" ++ exe ++ "|dt|", "-czvf", exe ++ ".tgz", exe_filename,
Expand All @@ -52,7 +56,7 @@ pub fn build(b: *std.Build) !void {
// Zig 0.12.0
cross_tar.setCwd(.{ .path = "./zig-out/bin/" });
} else {
// Zig 0.11.0
// Zig 0.11
cross_tar.cwd = "./zig-out/bin/";
}

Expand All @@ -63,7 +67,8 @@ pub fn build(b: *std.Build) !void {
// Tests
const test_step = b.step("test", "Run tests");

const test_exe = b.addTest(.{
const test_exe =
b.addTest(.{
.root_source_file = root_source_file,
.optimize = optimize,
.target = target,
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.name = "dt",
.version = "1.3.1", // Update in main.zig as well
.paths = .{ "" },
.version = "2.0.0-rc", // Update in main.zig as well
.paths = .{""},
}
4 changes: 2 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ lib
, stdenv
, testers
, zig_0_11
, zig_0_12
}:

stdenv.mkDerivation (finalAttrs: {
Expand All @@ -10,7 +10,7 @@ stdenv.mkDerivation (finalAttrs: {

src = lib.cleanSource ./.;

nativeBuildInputs = [ zig_0_11.hook ];
nativeBuildInputs = [ zig_0_12.hook ];

passthru.tests.version = testers.testVersion { package = finalAttrs.finalPackage; };

Expand Down
2 changes: 1 addition & 1 deletion dev/dt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [[ $DT_ROOT != dev/.. ]]; then
>&2 echo "ERROR: $0 is intended to be run when developing dt."
>&2 echo "If you're just trying to run a dt built from source:"
>&2 echo "- Navigate to $DT_ROOT"
>&2 echo "- Run './build release' to compile a release executable (Requires Zig 0.11.*)"
>&2 echo "- Run './build release' to compile a release executable (Expects Zig 0.12)"
>&2 echo "- Use the result at: $DT"
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
in {
packages.default = dt;
devShells.default = pkgs.mkShell {
packages = with pkgs; [ zig_0_11 ];
packages = with pkgs; [ zig_0_12 ];
};
});
}
8 changes: 5 additions & 3 deletions src/builtins.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const std = @import("std");
const ArrayList = std.ArrayList;
const osExit = if (@hasDecl(std, "posix")) std.posix.exit else std.os.exit;
const osChdir = if (@hasDecl(std, "posix")) std.posix.chdir else std.os.chdir;

const interpret = @import("interpret.zig");
const Command = interpret.Command;
Expand Down Expand Up @@ -139,7 +141,7 @@ pub fn quit(dt: *DtMachine) !void {
try dt.norm();
}

std.os.exit(0);
osExit(0);
}

test "drop quit" {
Expand Down Expand Up @@ -172,7 +174,7 @@ pub fn exit(dt: *DtMachine) !void {
}

const code: u8 = @intCast(i);
std.os.exit(code);
osExit(code);
}

test "7 exit" {
Expand Down Expand Up @@ -225,7 +227,7 @@ pub fn cd(dt: *DtMachine) !void {
path = try std.process.getEnvVarOwned(dt.alloc, "HOME");
}

std.os.chdir(path) catch |e| return dt.rewind(log, val, e);
osChdir(path) catch |e| return dt.rewind(log, val, e);
}

test "\".\" cd" {
Expand Down
38 changes: 7 additions & 31 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const osExit = if (@hasDecl(std, "posix")) std.posix.exit else std.os.exit;

const builtins = @import("builtins.zig");

const interpret = @import("interpret.zig");
const DtMachine = interpret.DtMachine;

// TODO: Change to @import when it's supported for zon
pub const version = "1.3.1"; // Update in build.zig.zon as well.
pub const version = "2.0.0-rc"; // Update in build.zig.zon as well.

const stdlib = @embedFile("stdlib.dt");
const dtlib = @embedFile("dt.dt");
Expand Down Expand Up @@ -35,17 +36,15 @@ pub fn main() !void {
if (try readShebangFile(arena.allocator(), firstArg)) |fileContents| {
return dt.loadFile(fileContents) catch |e| return doneOrDie(&dt, e);
} else if ((std.mem.eql(u8, firstArg, "--stream") or std.mem.startsWith(u8, firstArg, "--stream ")) and (stdinPiped or stdoutPiped)) {
return handlePipedStdoutOnly(&dt);
dt.handleCmd("dt/run-args") catch |e| return doneOrDie(&dt, e);
}
}

if (stdinPiped) {
return handlePipedStdin(&dt);
} else if (stdoutPiped) {
return handlePipedStdoutOnly(&dt);
dt.handleCmd("dt/pipe-thru-args") catch |e| return doneOrDie(&dt, e);
} else {
dt.handleCmd("dt/run-args") catch |e| return doneOrDie(&dt, e);
}

return readEvalPrintLoop(&dt);
}

fn readFirstArg(allocator: Allocator) !?[]const u8 {
Expand All @@ -54,29 +53,6 @@ fn readFirstArg(allocator: Allocator) !?[]const u8 {
return if (args.next()) |arg| try allocator.dupe(u8, arg) else null;
}

fn handlePipedStdin(dt: *DtMachine) !void {
dt.handleCmd("dt/pipe-thru-args") catch |e| return doneOrDie(dt, e);
}

fn handlePipedStdoutOnly(dt: *DtMachine) !void {
dt.handleCmd("dt/run-args") catch |e| return doneOrDie(dt, e);
}

fn readEvalPrintLoop(dt: *DtMachine) !void {
dt.handleCmd("dt/run-args") catch |e| return doneOrDie(dt, e);

// TODO: Can this catch be done in the stdlib? Other people need to catch errors too!
while (true) dt.handleCmd("dt/main-repl") catch |e| switch (e) {
error.EndOfStream => return,
else => {
const stderr = std.io.getStdErr().writer();
try dt.red();
try stderr.print("\nRestarting REPL after error: {s}\n\n", .{@errorName(e)});
try dt.norm();
},
};
}

fn doneOrDie(dt: *DtMachine, reason: anyerror) !void {
const stderr = std.io.getStdErr().writer();
try stderr.print("\n", .{});
Expand All @@ -88,7 +64,7 @@ fn doneOrDie(dt: *DtMachine, reason: anyerror) !void {
try stderr.print("\nRIP: {any}\n", .{reason});
try dt.norm();

std.os.exit(1);
osExit(1);
},
}
}
Expand Down

0 comments on commit 462ee1a

Please sign in to comment.