-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from birth-software/backend-improvement
Backend improvement
- Loading branch information
Showing
26 changed files
with
8,952 additions
and
1,787 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,69 @@ | ||
const std = @import("std"); | ||
var all: bool = false; | ||
|
||
fn everythingForTargetAndOptimization(b: *std.Build, target: std.zig.CrossTarget, optimization: std.builtin.OptimizeMode, unit_tests: []const []const u8, test_step: *std.Build.Step) !void { | ||
const name = if (all) try std.mem.concat(b.allocator, u8, &.{ "nativity_", @tagName(optimization) }) else "nativity"; | ||
pub fn build(b: *std.Build) !void { | ||
all = b.option(bool, "all", "All") orelse false; | ||
const target = b.standardTargetOptions(.{}); | ||
const optimization = b.standardOptimizeOption(.{}); | ||
const exe = b.addExecutable(.{ | ||
.name = name, | ||
.name = "nativity", | ||
.root_source_file = .{ .path = "src/main.zig" }, | ||
.target = target, | ||
.optimize = optimization, | ||
.use_llvm = true, | ||
.use_lld = false, | ||
}); | ||
|
||
b.installArtifact(exe); | ||
b.installDirectory(.{ | ||
.source_dir = std.Build.LazyPath.relative("lib"), | ||
.install_dir = .bin, | ||
.install_subdir = "lib", | ||
}); | ||
|
||
const run_cmd = b.addRunArtifact(exe); | ||
|
||
run_cmd.step.dependOn(b.getInstallStep()); | ||
|
||
if (b.args) |args| { | ||
run_cmd.addArgs(args); | ||
} | ||
|
||
const run_step = b.step(if (all) try std.mem.concat(b.allocator, u8, &.{ "run_", @tagName(optimization) }) else "run", "Run the app"); | ||
run_step.dependOn(&run_cmd.step); | ||
|
||
const debug_command = addDebugCommand(b, exe); | ||
const debug_step = b.step(if (all) try std.mem.concat(b.allocator, u8, &.{ "debug_", @tagName(optimization) }) else "debug", "Debug the app"); | ||
debug_step.dependOn(&debug_command.step); | ||
|
||
const zig_tests = b.addTest(.{ | ||
.root_source_file = .{ .path = "src/main.zig" }, | ||
.target = target, | ||
.optimize = optimization, | ||
}); | ||
|
||
const run_zig_tests = b.addRunArtifact(zig_tests); | ||
run_zig_tests.has_side_effects = true; | ||
test_step.dependOn(&run_zig_tests.step); | ||
const run_command = b.addRunArtifact(exe); | ||
|
||
for (unit_tests) |unit_test_main_source_file| { | ||
const unit_test = b.addRunArtifact(exe); | ||
unit_test.has_side_effects = true; | ||
unit_test.addArg(unit_test_main_source_file); | ||
test_step.dependOn(&unit_test.step); | ||
} | ||
} | ||
|
||
fn addDebugCommand(b: *std.Build, artifact: *std.Build.Step.Compile) *std.Build.Step.Run { | ||
return switch (@import("builtin").os.tag) { | ||
const debug_command = switch (@import("builtin").os.tag) { | ||
.linux => blk: { | ||
const result = b.addSystemCommand(&.{"gf2"}); | ||
result.addArtifactArg(artifact); | ||
|
||
if (artifact.kind == .@"test") { | ||
result.addArgs(&.{ "-ex", "r" }); | ||
} | ||
|
||
result.addArg("--args"); | ||
result.addArtifactArg(exe); | ||
break :blk result; | ||
}, | ||
.windows => blk: { | ||
const result = b.addSystemCommand(&.{"remedybg"}); | ||
result.addArg("-g"); | ||
result.addArtifactArg(artifact); | ||
result.addArtifactArg(exe); | ||
|
||
break :blk result; | ||
}, | ||
.macos => blk: { | ||
// not tested | ||
const result = b.addSystemCommand(&.{"gdb"}); | ||
result.addArtifactArg(artifact); | ||
const result = b.addSystemCommand(&.{"lldb"}); | ||
result.addArg("--"); | ||
result.addArtifactArg(exe); | ||
break :blk result; | ||
}, | ||
else => @compileError("Operating system not supported"), | ||
else => @compileError("OS not supported"), | ||
}; | ||
} | ||
|
||
pub fn build(b: *std.Build) !void { | ||
all = b.option(bool, "all", "All") orelse false; | ||
|
||
var unit_test_list = std.ArrayList([]const u8).init(b.allocator); | ||
var test_dir = try std.fs.cwd().openIterableDir("test", .{ .access_sub_paths = true }); | ||
defer test_dir.close(); | ||
const test_command = b.addRunArtifact(zig_tests); | ||
|
||
var test_dir_iterator = test_dir.iterate(); | ||
|
||
while (try test_dir_iterator.next()) |entry| { | ||
switch (entry.kind) { | ||
.directory => { | ||
const dir_name = entry.name; | ||
const main_unit_test_source_file = try std.mem.concat(b.allocator, u8, &.{ "test/", dir_name, "/main.nat" }); | ||
try unit_test_list.append(main_unit_test_source_file); | ||
}, | ||
.file => {}, | ||
else => @panic("Don't put crap on test directory"), | ||
} | ||
if (b.args) |args| { | ||
run_command.addArgs(args); | ||
test_command.addArgs(args); | ||
debug_command.addArgs(args); | ||
} | ||
|
||
const target = b.standardTargetOptions(.{}); | ||
const unit_tests = unit_test_list.items; | ||
const run_step = b.step("run", "Test the Nativity compiler"); | ||
run_step.dependOn(&run_command.step); | ||
const test_step = b.step("test", "Test the Nativity compiler"); | ||
|
||
if (all) { | ||
inline for (@typeInfo(std.builtin.OptimizeMode).Enum.fields) |enum_field| { | ||
const optimization = @field(std.builtin.OptimizeMode, enum_field.name); | ||
try everythingForTargetAndOptimization(b, target, optimization, unit_tests, test_step); | ||
} | ||
} else { | ||
const optimization = b.standardOptimizeOption(.{}); | ||
_ = try everythingForTargetAndOptimization(b, target, optimization, unit_tests, test_step); | ||
} | ||
test_step.dependOn(&test_command.step); | ||
const debug_step = b.step("debug", "Debug the Nativity compiler"); | ||
debug_step.dependOn(&debug_command.step); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const Os = enum{ | ||
linux, | ||
macos, | ||
windows, | ||
}; | ||
|
||
const Cpu = enum{ | ||
aarch64, | ||
x86_64, | ||
}; | ||
|
||
const Abi = enum{ | ||
none, | ||
gnu, | ||
msvc, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const current = #import("builtin").os; | ||
const system = switch (current) { | ||
.linux => linux, | ||
.macos => macos, | ||
.windows => windows, | ||
}; | ||
|
||
const write = fn (file_descriptor: FileDescriptor, bytes_ptr: [@]const u8, bytes_len: usize) ssize { | ||
switch (current) { | ||
.linux => return #syscall(1, file_descriptor, bytes_ptr, bytes_len), | ||
.macos => return macos.write(file_descriptor, bytes_ptr, bytes_len), | ||
.windows => { | ||
var written_bytes: u32 = 0; | ||
if (windows.WriteFile(file_descriptor, bytes_ptr, bytes_len, @written_bytes, false) != 0) { | ||
return written_bytes; | ||
} else { | ||
unreachable; | ||
} | ||
}, | ||
} | ||
} | ||
|
||
const FileDescriptor = system.FileDescriptor; | ||
|
||
const print = fn(bytes_ptr: [@]const u8, bytes_len: usize) void { | ||
const file_descriptor = switch (current) { | ||
.linux, .macos => 2, | ||
.windows => windows.GetStdHandle(windows.STD_OUTPUT_HANDLE), | ||
}; | ||
|
||
_ = write(file_descriptor, bytes_ptr, bytes_len); | ||
} | ||
|
||
const exit = fn(exit_code: s32) noreturn { | ||
switch (current) { | ||
.linux => _ = #syscall(231, exit_code), | ||
.macos => macos.exit(exit_code), | ||
.windows => windows.ExitProcess(exit_code), | ||
} | ||
|
||
unreachable; | ||
} | ||
|
||
const linux = #import("os/linux.nat"); | ||
const macos = #import("os/macos.nat"); | ||
const windows = #import("os/windows.nat"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const FileDescriptor = s32; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const FileDescriptor = s32; | ||
const write = fn (file_descriptor: FileDescriptor, bytes_ptr: [@]const u8, bytes_len: usize) ssize extern; | ||
const exit = fn (exit_code: u32) noreturn extern; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const HANDLE = u64; | ||
const FileDescriptor = HANDLE; | ||
const GetStdHandle = fn(handle_descriptor: u32) HANDLE extern; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
const std = #import("std"); | ||
comptime { | ||
_ = _start; | ||
} | ||
|
||
const _start = fn () noreturn { | ||
const result = #import("main").main(); | ||
_ = #syscall(231, result); | ||
unreachable; | ||
}; | ||
std.os.exit(result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.