Skip to content

Commit

Permalink
Pass tests in Release mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Sep 1, 2023
1 parent 1dae891 commit a049821
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 48 deletions.
61 changes: 52 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ pub fn build(b: *std.build.Builder) !void {
tcc = tcc_lib.createModule(b);
mimalloc = mimalloc_lib.createModule(b);

const opts = getDefaultOptions(target, optimize);

{
const step = b.step("cli", "Build main cli.");

const opts = getDefaultOptions(target, optimize);

const exe = b.addExecutable(.{
.name = "cyber",
.root_source_file = .{ .path = "src/main.zig" },
Expand All @@ -61,8 +61,10 @@ pub fn build(b: *std.build.Builder) !void {

// exe.linkLibC();
exe.addModule("stdx", stdx);
mimalloc_lib.addModule(exe, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, exe, .{});
if (opts.useMimalloc) {
mimalloc_lib.addModule(exe, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, exe, .{});
}
tcc_lib.addModule(exe, "tcc", tcc);
tcc_lib.buildAndLink(b, exe, .{
.selinux = selinux,
Expand All @@ -73,6 +75,9 @@ pub fn build(b: *std.build.Builder) !void {

{
const step = b.step("lib", "Build as a library.");

const opts = getDefaultOptions(target, optimize);

const lib = b.addSharedLibrary(.{
.name = "cyber",
.root_source_file = .{ .path = "src/lib.zig" },
Expand All @@ -95,8 +100,10 @@ pub fn build(b: *std.build.Builder) !void {

// lib.linkLibC();
lib.addModule("stdx", stdx);
mimalloc_lib.addModule(lib, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, lib, .{});
if (opts.useMimalloc) {
mimalloc_lib.addModule(lib, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, lib, .{});
}

if (!target.getCpuArch().isWasm()) {
tcc_lib.addModule(lib, "tcc", tcc);
Expand All @@ -116,6 +123,7 @@ pub fn build(b: *std.build.Builder) !void {

{
const step = b.step("wasm-test", "Build the wasm test runner.");
const opts = getDefaultOptions(target, optimize);
const lib = b.addSharedLibrary(.{
.name = "test",
.root_source_file = .{ .path = "test/wasm_test.zig" },
Expand Down Expand Up @@ -144,8 +152,11 @@ pub fn build(b: *std.build.Builder) !void {

// lib.linkLibC();
lib.addModule("stdx", stdx);
mimalloc_lib.addModule(lib, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, lib, .{});

if (opts.useMimalloc) {
mimalloc_lib.addModule(lib, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, lib, .{});
}

if (!target.getCpuArch().isWasm()) {
tcc_lib.addModule(lib, "tcc", tcc);
Expand All @@ -166,6 +177,9 @@ pub fn build(b: *std.build.Builder) !void {
{
const mainStep = b.step("test", "Run tests.");

var opts = getDefaultOptions(target, optimize);
opts.trackGlobalRc = true;

var step = b.addTest(.{
.root_source_file = .{ .path = "./test/main_test.zig" },
.target = target,
Expand All @@ -179,6 +193,11 @@ pub fn build(b: *std.build.Builder) !void {
step.addModule("stdx", stdx);
step.rdynamic = true;

if (opts.useMimalloc) {
mimalloc_lib.addModule(step, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, step, .{});
}

if (vmEngine == .c) {
try buildCVM(b.allocator, step, opts);
}
Expand All @@ -195,6 +214,10 @@ pub fn build(b: *std.build.Builder) !void {

{
const mainStep = b.step("test-lib", "Run tests.");

var opts = getDefaultOptions(target, optimize);
opts.trackGlobalRc = true;

var step = b.addTest(.{
.root_source_file = .{ .path = "./test/lib_test.zig" },
.target = target,
Expand All @@ -208,6 +231,11 @@ pub fn build(b: *std.build.Builder) !void {
step.addModule("stdx", stdx);
step.rdynamic = true;

if (opts.useMimalloc) {
mimalloc_lib.addModule(step, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, step, .{});
}

tcc_lib.addModule(step, "tcc", tcc);
tcc_lib.buildAndLink(b, step, .{
.selinux = opts.selinux,
Expand All @@ -221,6 +249,8 @@ pub fn build(b: *std.build.Builder) !void {
{
// Just trace test.
const mainStep = b.step("test-trace", "Run trace tests.");
var opts = getDefaultOptions(target, optimize);
opts.trackGlobalRc = true;
const step = try addTraceTest(b, opts);
mainStep.dependOn(&b.addRunArtifact(step).step);
}
Expand All @@ -232,20 +262,25 @@ pub fn build(b: *std.build.Builder) !void {

const Options = struct {
selinux: bool,
linkMimalloc: bool = false,
trackGlobalRc: bool,
trace: bool,
traceObjects: bool,
target: std.zig.CrossTarget,
optimize: std.builtin.OptimizeMode,
useMimalloc: bool,
};

fn getDefaultOptions(target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) Options {
return .{
.selinux = selinux,
.trackGlobalRc = optimize != .ReleaseFast,
.trace = false,
.traceObjects = optimize == .Debug,
.target = target,
.optimize = optimize,

// Use mimalloc for fast builds.
.useMimalloc = optimize == .ReleaseFast and !target.getCpuArch().isWasm(),
};
}

Expand All @@ -269,8 +304,11 @@ fn addBuildOptions(b: *std.build.Builder, step: *std.build.LibExeObjStep, opts:
build_options.addOption([]const u8, "build", buildTag);
build_options.addOption([]const u8, "commit", commitTag);
build_options.addOption(bool, "useMalloc", useMalloc);
build_options.addOption(bool, "useMimalloc", opts.useMimalloc);

build_options.addOption(config.Engine, "vmEngine", vmEngine);
build_options.addOption(bool, "trace", opts.trace);
build_options.addOption(bool, "traceObjects", opts.traceObjects);
build_options.addOption(bool, "trackGlobalRC", opts.trackGlobalRc);
build_options.addOption([]const u8, "full_version", b.fmt("Cyber {s} build-{s}-{s}", .{Version, buildTag, commitTag}));
// build_options.addOption(bool, "trace", true);
Expand All @@ -293,6 +331,11 @@ fn addTraceTest(b: *std.build.Builder, opts: Options) !*std.build.LibExeObjStep
try addBuildOptions(b, step, newOpts);
step.addModule("stdx", stdx);

if (opts.useMimalloc) {
mimalloc_lib.addModule(step, "mimalloc", mimalloc);
mimalloc_lib.buildAndLink(b, step, .{});
}

tcc_lib.addModule(step, "tcc", tcc);
tcc_lib.buildAndLink(b, step, .{
.selinux = opts.selinux,
Expand Down
2 changes: 1 addition & 1 deletion src/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,6 @@ pub const UserVM = struct {
}
};

test "Internals." {
test "api internals." {
try t.eq(@alignOf(UserVM), UserVMAlign);
}
4 changes: 2 additions & 2 deletions src/arc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn release(vm: *cy.VM, val: cy.Value) linksection(cy.HotSection) void {
}
if (val.isPointer()) {
const obj = val.asHeapObject();
if (builtin.mode == .Debug) {
if (cy.TraceObjects) {
checkDoubleFree(vm, obj);
}
obj.head.rc -= 1;
Expand Down Expand Up @@ -64,7 +64,7 @@ fn checkDoubleFree(vm: *cy.VM, obj: *cy.HeapObject) void {
}

pub fn releaseObject(vm: *cy.VM, obj: *cy.HeapObject) linksection(cy.HotSection) void {
if (builtin.mode == .Debug or builtin.is_test) {
if (cy.TraceObjects) {
checkDoubleFree(vm, obj);
}
obj.head.rc -= 1;
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ pub const OpCode = enum(u8) {
end = vmc.CodeEnd,
};

test "Internals." {
test "bytecode internals." {
try t.eq(std.enums.values(OpCode).len, 99);
try t.eq(@sizeOf(Inst), 1);
try t.eq(@sizeOf(Const), 8);
Expand Down
2 changes: 1 addition & 1 deletion src/chunk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ const DataNode = extern struct {
next: u32,
};

test "Internals." {
test "chunk internals." {
try t.eq(@sizeOf(DataNode), 8);
}

Expand Down
2 changes: 2 additions & 0 deletions src/cyber.zig
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ pub const hasStdFiles = !isWasm;
const build_options = @import("build_options");
pub const TraceEnabled = build_options.trace;
pub const TrackGlobalRC = build_options.trackGlobalRC;
pub const TraceObjects = build_options.traceObjects;
pub const UseMimalloc = build_options.useMimalloc;

const std = @import("std");
pub const NullId = std.math.maxInt(u32);
Expand Down
2 changes: 1 addition & 1 deletion src/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub const StackFrame = struct {
chunkId: u32,
};

test "Internals." {
test "debug internals." {
try t.eq(@sizeOf(vmc.CompactFrame), 8);
}

Expand Down
2 changes: 1 addition & 1 deletion src/fiber.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub const PanicType = enum(u8) {
none = vmc.PANIC_NONE,
};

test "Internals" {
test "fiber internals." {
try t.eq(@sizeOf(vmc.Fiber), 88);
try t.eq(@sizeOf(vmc.TryFrame), 16);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ pub const KeyU64Context = struct {
}
};

test "Internals." {
test "hash internals." {
try t.eq(@sizeOf(KeyU64), 8);
}
11 changes: 4 additions & 7 deletions src/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const log = stdx.log.scoped(.heap);
const NullId = std.math.maxInt(u32);
const NullU8 = std.math.maxInt(u8);

/// Use mimalloc for fast builds.
const UseMimalloc = builtin.mode == .ReleaseFast and !cy.isWasm;

var gpa: std.heap.GeneralPurposeAllocator(.{
.enable_memory_limit = false,
.stack_trace_frames = if (builtin.mode == .Debug) 10 else 0,
Expand All @@ -32,7 +29,7 @@ fn initAllocator() void {
if (build_options.useMalloc) {
return;
} else {
if (UseMimalloc) {
if (cy.UseMimalloc) {
miAlloc.init();
} else {
return;
Expand All @@ -52,7 +49,7 @@ pub fn getAllocator() std.mem.Allocator {
if (build_options.useMalloc) {
return std.heap.c_allocator;
} else {
if (UseMimalloc) {
if (cy.UseMimalloc) {
return miAlloc.allocator();
} else {
if (cy.isWasm) {
Expand All @@ -69,7 +66,7 @@ pub fn deinitAllocator() void {
if (build_options.useMalloc) {
return;
} else {
if (UseMimalloc) {
if (cy.UseMimalloc) {
miAlloc.deinit();
} else {
if (cy.isWasm) {
Expand Down Expand Up @@ -1639,7 +1636,7 @@ test "Free object invalidation." {
}
}

test "Internals." {
test "heap internals." {
try t.eq(@sizeOf(MapInner), 32);
try t.eq(@sizeOf(HeapObject), 40);
try t.eq(@alignOf(HeapObject), 8);
Expand Down
2 changes: 1 addition & 1 deletion src/map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub const ValueMapEntry = struct {
value: cy.Value,
};

test "Internals." {
test "map internals." {
try t.eq(@sizeOf(ValueMapEntry), 16);
try t.eq(@alignOf(*ValueMapEntry), 8);
try t.eq(@sizeOf(Metadata), 1);
Expand Down
8 changes: 6 additions & 2 deletions src/module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,11 @@ pub fn appendModule(c: *cy.VMcompiler, name: []const u8) !ModuleId {
return id;
}

test "Internals" {
test "module internals" {
try t.eq(@sizeOf(ModuleFuncNode), 16);
try t.eq(@sizeOf(ModuleSym), 24);
if (builtin.mode == .Debug) {
try t.eq(@sizeOf(ModuleSym), 24);
} else {
try t.eq(@sizeOf(ModuleSym), 16);
}
}
8 changes: 6 additions & 2 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4600,10 +4600,14 @@ const StaticDecl = struct {
},
};

test "Internals." {
test "parser internals." {
try t.eq(@sizeOf(Token), 8);
try t.eq(@alignOf(Token), 4);
try t.eq(@sizeOf(Node), 28);
if (builtin.mode == .Debug) {
try t.eq(@sizeOf(Node), 28);
} else {
try t.eq(@sizeOf(Node), 24);
}
try t.eq(@sizeOf(TokenizeState), 4);

try t.eq(std.enums.values(TokenType).len, 59);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub const VarSym = struct {

pub const FieldSymbolMap = vmc.FieldSymbolMap;

test "Internals." {
test "runtime internals." {
try t.eq(@sizeOf(MethodData), 16);
try t.eq(@sizeOf(Method), 24);
try t.eq(@sizeOf(MethodExt), 4);
Expand Down
8 changes: 6 additions & 2 deletions src/sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4334,8 +4334,12 @@ pub fn getOrLoadModule(self: *cy.Chunk, spec: []const u8, nodeId: cy.NodeId) !cy
}
}

test "Internals." {
try t.eq(@sizeOf(LocalVar), 32);
test "sema internals." {
if (builtin.mode == .Debug) {
try t.eq(@sizeOf(LocalVar), 32);
} else {
try t.eq(@sizeOf(LocalVar), 16);
}
try t.eq(@sizeOf(ResolvedFuncSym), 24);
try t.eq(@sizeOf(ResolvedFuncSig), 16);

Expand Down
3 changes: 0 additions & 3 deletions src/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,4 @@ pub fn isRcCandidateType(c: *cy.VMcompiler, symId: TypeId) bool {
}
}
}
}

test "Internals." {
}
2 changes: 1 addition & 1 deletion src/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ test "asF64" {
try t.eq(val.asF64(), -std.math.inf(f64));
}

test "Internals." {
test "value internals." {
try t.eq(@sizeOf(Value), 8);
try t.eq(@alignOf(Value), 8);
try t.eq(StaticAstringMask, 0x7FFC000300000000);
Expand Down
Loading

0 comments on commit a049821

Please sign in to comment.