From 52c5dca3336a615ad018457881a2a6c61ce4fd38 Mon Sep 17 00:00:00 2001 From: Fallenwood Date: Fri, 27 Jun 2025 17:32:23 +0800 Subject: [PATCH 1/5] 1. Build luajit without tests 2. Add cross build for lua51 and luajit 3. Add editorconfig --- .editorconfig | 7 ++++++ .github/workflows/tests.yml | 20 ++++++++++++++++ build/lua.zig | 32 ++++++-------------------- build/luajit.patch | 18 +++++++++++++++ build/luajit.zig | 46 +++++++++++++++++++++++++++++++++---- build/utils.zig | 34 +++++++++++++++++++++++++++ makefile | 8 +++++++ 7 files changed, 136 insertions(+), 29 deletions(-) create mode 100644 .editorconfig create mode 100644 build/luajit.patch create mode 100644 build/utils.zig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7b206dd --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +[makefile] +indent_style = tab + +[*.yml] +indent_style = space +indent_size = 2 +tab_width = 2 \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0e7dce5..ee63b42 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,3 +26,23 @@ jobs: - name: Run tests run: make test + + test_cross: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + zig: ["0.14.0", "0.15.0-dev.441+c1649d586"] + + runs-on: ${{matrix.os}} + + steps: + - name: Clone Ziglua + uses: actions/checkout@v3 + + - name: Setup Zig + uses: mlugg/setup-zig@v1 + with: + version: ${{ matrix.zig }} + + - name: Run tests + run: make test_cross \ No newline at end of file diff --git a/build/lua.zig b/build/lua.zig index 59c4594..d7a2b61 100644 --- a/build/lua.zig +++ b/build/lua.zig @@ -3,6 +3,8 @@ const std = @import("std"); const Build = std.Build; const Step = std.Build.Step; +const applyPatchToFile = @import("utils.zig").applyPatchToFile; + pub const Language = enum { lua51, lua52, @@ -98,8 +100,11 @@ pub fn configure( // Patch ldo.c for Lua 5.1 if (lang == .lua51) { - const patched = patchFile(b, target, lib, upstream.path("src/ldo.c"), b.path("build/lua-5.1.patch")); - lib.addCSourceFile(.{ .file = patched, .flags = &flags }); + const patched = applyPatchToFile(b, b.graph.host, upstream.path("src/ldo.c"), b.path("build/lua-5.1.patch"), "ldo.c"); + + lib.step.dependOn(&patched.run.step); + + lib.addCSourceFile(.{ .file = patched.output, .flags = &flags }); } lib.linkLibC(); @@ -117,29 +122,6 @@ pub fn configure( return lib; } -fn patchFile( - b: *Build, - target: Build.ResolvedTarget, - lib: *Step.Compile, - file: Build.LazyPath, - patch_file: Build.LazyPath, -) Build.LazyPath { - const patch = b.addExecutable(.{ - .name = "patch", - .root_source_file = b.path("build/patch.zig"), - .target = target, - }); - - const patch_run = b.addRunArtifact(patch); - patch_run.addFileArg(file); - patch_run.addFileArg(patch_file); - const out = patch_run.addOutputFileArg("ldo.c"); - - lib.step.dependOn(&patch_run.step); - - return out; -} - const lua_base_source_files = [_][]const u8{ "src/lapi.c", "src/lcode.c", diff --git a/build/luajit.patch b/build/luajit.patch new file mode 100644 index 0000000..dfd2748 --- /dev/null +++ b/build/luajit.patch @@ -0,0 +1,18 @@ +# Patch for cross build on windows +# C:\Users\runneradmin\AppData\Local\zig\o\2f9fc57e5a283abd5bd9f55a5d990f42/buildvm_arch.h:8:12: error: \U used with no following hex digits +# #line 1 "C:\Users\runneradmin\AppData\Local\zig\p\N-V-__8AACcgQgCuLYTPzCp6pnBmFJHyG77RAtM13hjOfTaG\src\vm_arm64.dasc" +# ^~~ +# C:\Users\runneradmin\AppData\Local\zig\p\N-V-__8AACcgQgCuLYTPzCp6pnBmFJHyG77RAtM13hjOfTaG\src/host/buildvm.c:75:10: note: in file included from C:\Users\runneradmin\AppData\Local\zig\p\N-V-__8AACcgQgCuLYTPzCp6pnBmFJHyG77RAtM13hjOfTaG\src/host/buildvm.c:75: +# #include "buildvm_arch.h" +--- a/dynasm/dynasm.lua ++++ b/dynasm/dynasm.lua +@@ -85,7 +85,8 @@ end + -- Resync CPP line numbers. + local function wsync() + if g_synclineno ~= g_lineno and g_opt.cpp then +- wline("#line "..g_lineno..' "'..g_fname..'"') ++ local fname = gsub(g_fname, "\\", "/") ++ wline("#line "..g_lineno..' "'..fname..'"') + g_synclineno = g_lineno + end + end \ No newline at end of file diff --git a/build/luajit.zig b/build/luajit.zig index 636113e..b370ee8 100644 --- a/build/luajit.zig +++ b/build/luajit.zig @@ -3,6 +3,8 @@ const std = @import("std"); const Build = std.Build; const Step = std.Build.Step; +const applyPatchToFile = @import("utils.zig").applyPatchToFile; + pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, upstream: *Build.Dependency, shared: bool) *Step.Compile { // TODO: extract this to the main build function because it is shared between all specialized build functions @@ -24,7 +26,7 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. // Compile minilua interpreter used at build time to generate files const minilua = b.addExecutable(.{ .name = "minilua", - .target = target, // TODO ensure this is the host + .target = b.graph.host, // Use host target for cross build .optimize = .ReleaseSafe, }); minilua.linkLibC(); @@ -39,7 +41,28 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. // Generate the buildvm_arch.h file using minilua const dynasm_run = b.addRunArtifact(minilua); - dynasm_run.addFileArg(upstream.path("dynasm/dynasm.lua")); + + if (b.graph.host.result.os.tag == .windows) { + // Patch windows cross build for LuaJIT + const sourceDynasmFile = upstream.path("dynasm/dynasm.lua"); + const destDynasmFile = upstream.path("dynasm/dynasm-patched.lua"); + const patchDynasm = applyPatchToFile(b, b.graph.host, sourceDynasmFile, b.path("build/luajit.patch"), "dynasm-patched.lua"); + + const copyPatchedDynasm = b.addSystemCommand(&[_][]const u8{ + "cmd", + }); + copyPatchedDynasm.addArgs(&.{ "/q", "/c", "copy" }); + copyPatchedDynasm.step.dependOn(&patchDynasm.run.step); + copyPatchedDynasm.addFileArg(patchDynasm.output); + copyPatchedDynasm.addFileArg(destDynasmFile); + + dynasm_run.step.dependOn(&patchDynasm.run.step); + dynasm_run.step.dependOn(©PatchedDynasm.step); + + dynasm_run.addFileArg(destDynasmFile); + } else { + dynasm_run.addFileArg(upstream.path("dynasm/dynasm.lua")); + } // TODO: Many more flags to figure out if (target.result.cpu.arch.endian() == .little) { @@ -55,6 +78,10 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. dynasm_run.addArgs(&.{ "-D", "FPU", "-D", "HFABI" }); } + if (target.result.cpu.arch == .aarch64 or target.result.cpu.arch == .aarch64_be) { + dynasm_run.addArgs(&.{ "-D", "DUALNUM" }); + } + if (target.result.os.tag == .windows) dynasm_run.addArgs(&.{ "-D", "WIN" }); dynasm_run.addArg("-o"); @@ -81,7 +108,7 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. // Compile the buildvm executable used to generate other files const buildvm = b.addExecutable(.{ .name = "buildvm", - .target = target, // TODO ensure this is the host + .target = b.graph.host, // Use host target for cross build .optimize = .ReleaseSafe, }); buildvm.linkLibC(); @@ -96,12 +123,18 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. buildvm.step.dependOn(&dynasm_run.step); buildvm.step.dependOn(&genversion_run.step); + const buildvm_c_flags = switch (target.result.cpu.arch) { + .aarch64, .aarch64_be => &[_][]const u8{ "-DLUAJIT_TARGET=LUAJIT_ARCH_arm64", "-DLJ_ARCH_HASFPU=1", "-DLJ_ABI_SOFTFP=0" }, + else => &[_][]const u8{}, + }; + buildvm.addCSourceFiles(.{ .root = .{ .dependency = .{ .dependency = upstream, .sub_path = "", } }, .files = &.{ "src/host/buildvm_asm.c", "src/host/buildvm_fold.c", "src/host/buildvm_lib.c", "src/host/buildvm_peobj.c", "src/host/buildvm.c" }, + .flags = buildvm_c_flags, }); buildvm.addIncludePath(upstream.path("src")); @@ -156,7 +189,7 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. buildvm_ljvm.addArg("-o"); if (target.result.os.tag == .windows) { - const ljvm_ob = buildvm_ljvm.addOutputFileArg("lj_vm. o"); + const ljvm_ob = buildvm_ljvm.addOutputFileArg("lj_vm.o"); lib.addObjectFile(ljvm_ob); } else { const ljvm_asm = buildvm_ljvm.addOutputFileArg("lj_vm.S"); @@ -210,6 +243,11 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. return lib; } +fn getPath(lazy_path: Build.LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 { + const p = lazy_path.getPath3(src_builder, asking_step); + return src_builder.pathResolve(&.{ p.root_dir.path orelse ".", p.sub_path }); +} + const luajit_lib = [_][]const u8{ "src/lib_base.c", "src/lib_math.c", diff --git a/build/utils.zig b/build/utils.zig new file mode 100644 index 0000000..eddf5ce --- /dev/null +++ b/build/utils.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +const Build = std.Build; +const Step = std.Build.Step; + +const PatchFile = struct { + run: *Step.Run, + output: Build.LazyPath, +}; + +pub fn applyPatchToFile( + b: *Build, + target: Build.ResolvedTarget, + file: Build.LazyPath, + patch_file: Build.LazyPath, + output_file: []const u8, +) PatchFile { + const patch = b.addExecutable(.{ + .name = "patch", + .root_source_file = b.path("build/patch.zig"), + .target = target, + }); + + const patch_run = b.addRunArtifact(patch); + patch_run.addFileArg(file); + patch_run.addFileArg(patch_file); + + const out = patch_run.addOutputFileArg(output_file); + + return .{ + .run = patch_run, + .output = out, + }; +} diff --git a/makefile b/makefile index 0cc9bd9..b892db5 100644 --- a/makefile +++ b/makefile @@ -11,5 +11,13 @@ test: zig build install-example-zig-function zig build -Dlang=luau install-example-luau-bytecode + zig build -Dlang=luajit + +test_cross: + zig build -Dlang=lua51 -Dtarget=aarch64-linux + zig build -Dlang=lua51 -Dtarget=aarch64-linux-gnu + zig build -Dlang=luajit -Dtarget=aarch64-linux + zig build -Dlang=luajit -Dtarget=aarch64-linux-gnu + docs: zig build docs From 426b34476a249f0b8467837bc253117f04d0912f Mon Sep 17 00:00:00 2001 From: Fallenwood Date: Mon, 7 Jul 2025 11:26:26 +0800 Subject: [PATCH 2/5] Remove editorconfig --- .editorconfig | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 7b206dd..0000000 --- a/.editorconfig +++ /dev/null @@ -1,7 +0,0 @@ -[makefile] -indent_style = tab - -[*.yml] -indent_style = space -indent_size = 2 -tab_width = 2 \ No newline at end of file From 732af43ea048b1f34da3eec9f1cf6a8a9ab3baa1 Mon Sep 17 00:00:00 2001 From: Fallenwood Date: Mon, 7 Jul 2025 11:39:59 +0800 Subject: [PATCH 3/5] 1. Remove unused function "getPath" 2. Use zig-style variable for buildvm_c_flags --- build/luajit.zig | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/build/luajit.zig b/build/luajit.zig index b370ee8..f355ebd 100644 --- a/build/luajit.zig +++ b/build/luajit.zig @@ -123,9 +123,9 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. buildvm.step.dependOn(&dynasm_run.step); buildvm.step.dependOn(&genversion_run.step); - const buildvm_c_flags = switch (target.result.cpu.arch) { - .aarch64, .aarch64_be => &[_][]const u8{ "-DLUAJIT_TARGET=LUAJIT_ARCH_arm64", "-DLJ_ARCH_HASFPU=1", "-DLJ_ABI_SOFTFP=0" }, - else => &[_][]const u8{}, + const buildvm_c_flags: []const []const u8 = switch (target.result.cpu.arch) { + .aarch64, .aarch64_be => &.{ "-DLUAJIT_TARGET=LUAJIT_ARCH_arm64", "-DLJ_ARCH_HASFPU=1", "-DLJ_ABI_SOFTFP=0" }, + else => &.{}, }; buildvm.addCSourceFiles(.{ @@ -243,11 +243,6 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin. return lib; } -fn getPath(lazy_path: Build.LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 { - const p = lazy_path.getPath3(src_builder, asking_step); - return src_builder.pathResolve(&.{ p.root_dir.path orelse ".", p.sub_path }); -} - const luajit_lib = [_][]const u8{ "src/lib_base.c", "src/lib_math.c", From 3a2df94d3ca774d12a9bc53b59626f770a32c8f7 Mon Sep 17 00:00:00 2001 From: Fallenwood Date: Tue, 8 Jul 2025 09:13:00 +0800 Subject: [PATCH 4/5] Update makefile to cross build macos targets --- makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/makefile b/makefile index b892db5..a3da751 100644 --- a/makefile +++ b/makefile @@ -19,5 +19,8 @@ test_cross: zig build -Dlang=luajit -Dtarget=aarch64-linux zig build -Dlang=luajit -Dtarget=aarch64-linux-gnu + zig build -Dlang=luajit -Dtarget=aarch64-macos + zig build -Dlang=luajit -Dtarget=x86_64-macos + docs: zig build docs From 41d5caad3893b2d3c012fd8c6331c7c57d78da44 Mon Sep 17 00:00:00 2001 From: Fallenwood Date: Tue, 8 Jul 2025 11:46:24 +0800 Subject: [PATCH 5/5] Remove cross build from macos arm64 to macos x86-64 which fails --- makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/makefile b/makefile index a3da751..d796925 100644 --- a/makefile +++ b/makefile @@ -20,7 +20,6 @@ test_cross: zig build -Dlang=luajit -Dtarget=aarch64-linux-gnu zig build -Dlang=luajit -Dtarget=aarch64-macos - zig build -Dlang=luajit -Dtarget=x86_64-macos docs: zig build docs