Skip to content

Commit 771e016

Browse files
committed
fix(sdk): add apple2 lazyDeps and missing save_basic to commodore/mega65
ref.: llvm-mos/llvm-mos-sdk#430
1 parent ad7899a commit 771e016

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub fn build(b: *std.Build) void {
4343
// llvm-mos-sdk git source — always required for headers, linker scripts, and platform libs.
4444
const sdk_dep = b.dependency("llvm-mos-sdk", .{});
4545
const sdk_src_raw = sdk_dep.path(".").getPath(b);
46-
const apple2_dep = b.dependency("apple2", .{});
4746
// Normalize separators for embedding in linker scripts and assembly (.incbin).
4847
const sdk_src = blk: {
4948
const buf = b.allocator.dupe(u8, sdk_src_raw) catch @panic("OOM");
@@ -717,7 +716,7 @@ pub fn build(b: *std.Build) void {
717716
}
718717

719718
// ---- Apple2 hello ----
720-
{
719+
if (b.lazyDependency("apple2", .{})) |apple2_dep| {
721720
const step = b.step("apple2-hello", "Build Apple IIe hello example");
722721
const exe = addApple2Exe(b, sdk_src, apple2_dep, optimize);
723722
const install = b.addInstallArtifact(exe, .{ .dest_sub_path = "hello.sys" });
@@ -1682,6 +1681,7 @@ fn addMega65Exe(
16821681
exe.root_module.linkLibrary(libs.c);
16831682
if (libs.crt0_obj) |obj| exe.root_module.addObject(obj);
16841683
if (libs.mem) |mem_obj| exe.root_module.addObject(mem_obj);
1684+
if (libs.save_basic) |sb_obj| exe.root_module.addObject(sb_obj);
16851685
exe.forceUndefinedSymbol("__zig_call_main_section");
16861686
exe.forceUndefinedSymbol("main");
16871687
exe.setLibCFile(libc_txt);

build.zig.zon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
.apple2 = .{
1515
.url = "git+https://github.com/TheHans255/apple-ii-port-work#31fde9997cfb4a5e98a9d0fc0efaf869fbedeb1f",
1616
.hash = "N-V-__8AAIzPAQDlio9aQBDM199SjX-qme9i0IIUuqp2IX0v",
17+
.lazy = true,
1718
},
1819
},
1920
.minimum_zig_version = "0.16.0",

sdk/build.zig

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ pub const Libs = struct {
3838
crt0_obj2: ?*std.Build.Step.Compile = null,
3939
// NES only: FamiTone2 music/sound engine + banked fixed-bank wrappers.
4040
famitone2: ?*std.Build.Step.Compile = null,
41+
// MEGA65/Commodore: save-basic.S saves/restores ZP and overrides _Exit to return
42+
// cleanly to BASIC. Must be a TRUE object (section-only .init.005/.fini.989).
43+
save_basic: ?*std.Build.Step.Compile = null,
4144
};
4245

4346
/// Build platform libraries for `pd` from the SDK source tree at `sdk_root`.
@@ -249,7 +252,25 @@ pub fn buildPlatform(b: *std.Build, sdk_root: []const u8, pd: Platform, opt: std
249252
.files = &.{"crt0.S"},
250253
});
251254
crt0_obj.lto = .none;
252-
return .{ .crt = libcrt, .crt0 = libcrt0, .c = libc, .printf = libprintf, .mem = mem_obj, .crt0_obj = crt0_obj };
255+
// save-basic.S: TRUE object for MEGA65/Commodore clean BASIC exit (PR #430).
256+
// Contains .init.005 (save ZP) and .fini.989 (restore ZP) section-only contributions
257+
// with no exported symbol → must be a TRUE object, not an archive member (Hard rule #5).
258+
var save_basic_obj: ?*std.Build.Step.Compile = null;
259+
if (std.mem.eql(u8, pd.name, "mega65")) {
260+
const sb_obj = b.addObject(.{
261+
.name = "save-basic",
262+
.root_module = b.createModule(.{ .target = target, .optimize = opt }),
263+
});
264+
sb_obj.root_module.addIncludePath(.{ .cwd_relative = com_asm });
265+
sb_obj.root_module.addIncludePath(.{ .cwd_relative = com_inc });
266+
sb_obj.root_module.addCSourceFiles(.{
267+
.root = .{ .cwd_relative = comm_dir },
268+
.files = &.{"save-basic.S"},
269+
});
270+
sb_obj.lto = .none;
271+
save_basic_obj = sb_obj;
272+
}
273+
return .{ .crt = libcrt, .crt0 = libcrt0, .c = libc, .printf = libprintf, .mem = mem_obj, .crt0_obj = crt0_obj, .save_basic = save_basic_obj };
253274
}
254275

255276
return .{ .crt = libcrt, .crt0 = libcrt0, .c = libc };

0 commit comments

Comments
 (0)