Skip to content

Commit 52c5dca

Browse files
committed
1. Build luajit without tests
2. Add cross build for lua51 and luajit 3. Add editorconfig
1 parent 561b60c commit 52c5dca

File tree

7 files changed

+136
-29
lines changed

7 files changed

+136
-29
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[makefile]
2+
indent_style = tab
3+
4+
[*.yml]
5+
indent_style = space
6+
indent_size = 2
7+
tab_width = 2

.github/workflows/tests.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,23 @@ jobs:
2626

2727
- name: Run tests
2828
run: make test
29+
30+
test_cross:
31+
strategy:
32+
matrix:
33+
os: [ubuntu-latest, macos-latest, windows-latest]
34+
zig: ["0.14.0", "0.15.0-dev.441+c1649d586"]
35+
36+
runs-on: ${{matrix.os}}
37+
38+
steps:
39+
- name: Clone Ziglua
40+
uses: actions/checkout@v3
41+
42+
- name: Setup Zig
43+
uses: mlugg/setup-zig@v1
44+
with:
45+
version: ${{ matrix.zig }}
46+
47+
- name: Run tests
48+
run: make test_cross

build/lua.zig

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const std = @import("std");
33
const Build = std.Build;
44
const Step = std.Build.Step;
55

6+
const applyPatchToFile = @import("utils.zig").applyPatchToFile;
7+
68
pub const Language = enum {
79
lua51,
810
lua52,
@@ -98,8 +100,11 @@ pub fn configure(
98100

99101
// Patch ldo.c for Lua 5.1
100102
if (lang == .lua51) {
101-
const patched = patchFile(b, target, lib, upstream.path("src/ldo.c"), b.path("build/lua-5.1.patch"));
102-
lib.addCSourceFile(.{ .file = patched, .flags = &flags });
103+
const patched = applyPatchToFile(b, b.graph.host, upstream.path("src/ldo.c"), b.path("build/lua-5.1.patch"), "ldo.c");
104+
105+
lib.step.dependOn(&patched.run.step);
106+
107+
lib.addCSourceFile(.{ .file = patched.output, .flags = &flags });
103108
}
104109

105110
lib.linkLibC();
@@ -117,29 +122,6 @@ pub fn configure(
117122
return lib;
118123
}
119124

120-
fn patchFile(
121-
b: *Build,
122-
target: Build.ResolvedTarget,
123-
lib: *Step.Compile,
124-
file: Build.LazyPath,
125-
patch_file: Build.LazyPath,
126-
) Build.LazyPath {
127-
const patch = b.addExecutable(.{
128-
.name = "patch",
129-
.root_source_file = b.path("build/patch.zig"),
130-
.target = target,
131-
});
132-
133-
const patch_run = b.addRunArtifact(patch);
134-
patch_run.addFileArg(file);
135-
patch_run.addFileArg(patch_file);
136-
const out = patch_run.addOutputFileArg("ldo.c");
137-
138-
lib.step.dependOn(&patch_run.step);
139-
140-
return out;
141-
}
142-
143125
const lua_base_source_files = [_][]const u8{
144126
"src/lapi.c",
145127
"src/lcode.c",

build/luajit.patch

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Patch for cross build on windows
2+
# C:\Users\runneradmin\AppData\Local\zig\o\2f9fc57e5a283abd5bd9f55a5d990f42/buildvm_arch.h:8:12: error: \U used with no following hex digits
3+
# #line 1 "C:\Users\runneradmin\AppData\Local\zig\p\N-V-__8AACcgQgCuLYTPzCp6pnBmFJHyG77RAtM13hjOfTaG\src\vm_arm64.dasc"
4+
# ^~~
5+
# 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:
6+
# #include "buildvm_arch.h"
7+
--- a/dynasm/dynasm.lua
8+
+++ b/dynasm/dynasm.lua
9+
@@ -85,7 +85,8 @@ end
10+
-- Resync CPP line numbers.
11+
local function wsync()
12+
if g_synclineno ~= g_lineno and g_opt.cpp then
13+
- wline("#line "..g_lineno..' "'..g_fname..'"')
14+
+ local fname = gsub(g_fname, "\\", "/")
15+
+ wline("#line "..g_lineno..' "'..fname..'"')
16+
g_synclineno = g_lineno
17+
end
18+
end

build/luajit.zig

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const std = @import("std");
33
const Build = std.Build;
44
const Step = std.Build.Step;
55

6+
const applyPatchToFile = @import("utils.zig").applyPatchToFile;
7+
68
pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, upstream: *Build.Dependency, shared: bool) *Step.Compile {
79
// TODO: extract this to the main build function because it is shared between all specialized build functions
810

@@ -24,7 +26,7 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
2426
// Compile minilua interpreter used at build time to generate files
2527
const minilua = b.addExecutable(.{
2628
.name = "minilua",
27-
.target = target, // TODO ensure this is the host
29+
.target = b.graph.host, // Use host target for cross build
2830
.optimize = .ReleaseSafe,
2931
});
3032
minilua.linkLibC();
@@ -39,7 +41,28 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
3941

4042
// Generate the buildvm_arch.h file using minilua
4143
const dynasm_run = b.addRunArtifact(minilua);
42-
dynasm_run.addFileArg(upstream.path("dynasm/dynasm.lua"));
44+
45+
if (b.graph.host.result.os.tag == .windows) {
46+
// Patch windows cross build for LuaJIT
47+
const sourceDynasmFile = upstream.path("dynasm/dynasm.lua");
48+
const destDynasmFile = upstream.path("dynasm/dynasm-patched.lua");
49+
const patchDynasm = applyPatchToFile(b, b.graph.host, sourceDynasmFile, b.path("build/luajit.patch"), "dynasm-patched.lua");
50+
51+
const copyPatchedDynasm = b.addSystemCommand(&[_][]const u8{
52+
"cmd",
53+
});
54+
copyPatchedDynasm.addArgs(&.{ "/q", "/c", "copy" });
55+
copyPatchedDynasm.step.dependOn(&patchDynasm.run.step);
56+
copyPatchedDynasm.addFileArg(patchDynasm.output);
57+
copyPatchedDynasm.addFileArg(destDynasmFile);
58+
59+
dynasm_run.step.dependOn(&patchDynasm.run.step);
60+
dynasm_run.step.dependOn(&copyPatchedDynasm.step);
61+
62+
dynasm_run.addFileArg(destDynasmFile);
63+
} else {
64+
dynasm_run.addFileArg(upstream.path("dynasm/dynasm.lua"));
65+
}
4366

4467
// TODO: Many more flags to figure out
4568
if (target.result.cpu.arch.endian() == .little) {
@@ -55,6 +78,10 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
5578
dynasm_run.addArgs(&.{ "-D", "FPU", "-D", "HFABI" });
5679
}
5780

81+
if (target.result.cpu.arch == .aarch64 or target.result.cpu.arch == .aarch64_be) {
82+
dynasm_run.addArgs(&.{ "-D", "DUALNUM" });
83+
}
84+
5885
if (target.result.os.tag == .windows) dynasm_run.addArgs(&.{ "-D", "WIN" });
5986

6087
dynasm_run.addArg("-o");
@@ -81,7 +108,7 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
81108
// Compile the buildvm executable used to generate other files
82109
const buildvm = b.addExecutable(.{
83110
.name = "buildvm",
84-
.target = target, // TODO ensure this is the host
111+
.target = b.graph.host, // Use host target for cross build
85112
.optimize = .ReleaseSafe,
86113
});
87114
buildvm.linkLibC();
@@ -96,12 +123,18 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
96123
buildvm.step.dependOn(&dynasm_run.step);
97124
buildvm.step.dependOn(&genversion_run.step);
98125

126+
const buildvm_c_flags = switch (target.result.cpu.arch) {
127+
.aarch64, .aarch64_be => &[_][]const u8{ "-DLUAJIT_TARGET=LUAJIT_ARCH_arm64", "-DLJ_ARCH_HASFPU=1", "-DLJ_ABI_SOFTFP=0" },
128+
else => &[_][]const u8{},
129+
};
130+
99131
buildvm.addCSourceFiles(.{
100132
.root = .{ .dependency = .{
101133
.dependency = upstream,
102134
.sub_path = "",
103135
} },
104136
.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" },
137+
.flags = buildvm_c_flags,
105138
});
106139

107140
buildvm.addIncludePath(upstream.path("src"));
@@ -156,7 +189,7 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
156189

157190
buildvm_ljvm.addArg("-o");
158191
if (target.result.os.tag == .windows) {
159-
const ljvm_ob = buildvm_ljvm.addOutputFileArg("lj_vm. o");
192+
const ljvm_ob = buildvm_ljvm.addOutputFileArg("lj_vm.o");
160193
lib.addObjectFile(ljvm_ob);
161194
} else {
162195
const ljvm_asm = buildvm_ljvm.addOutputFileArg("lj_vm.S");
@@ -210,6 +243,11 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
210243
return lib;
211244
}
212245

246+
fn getPath(lazy_path: Build.LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 {
247+
const p = lazy_path.getPath3(src_builder, asking_step);
248+
return src_builder.pathResolve(&.{ p.root_dir.path orelse ".", p.sub_path });
249+
}
250+
213251
const luajit_lib = [_][]const u8{
214252
"src/lib_base.c",
215253
"src/lib_math.c",

build/utils.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const std = @import("std");
2+
3+
const Build = std.Build;
4+
const Step = std.Build.Step;
5+
6+
const PatchFile = struct {
7+
run: *Step.Run,
8+
output: Build.LazyPath,
9+
};
10+
11+
pub fn applyPatchToFile(
12+
b: *Build,
13+
target: Build.ResolvedTarget,
14+
file: Build.LazyPath,
15+
patch_file: Build.LazyPath,
16+
output_file: []const u8,
17+
) PatchFile {
18+
const patch = b.addExecutable(.{
19+
.name = "patch",
20+
.root_source_file = b.path("build/patch.zig"),
21+
.target = target,
22+
});
23+
24+
const patch_run = b.addRunArtifact(patch);
25+
patch_run.addFileArg(file);
26+
patch_run.addFileArg(patch_file);
27+
28+
const out = patch_run.addOutputFileArg(output_file);
29+
30+
return .{
31+
.run = patch_run,
32+
.output = out,
33+
};
34+
}

makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@ test:
1111
zig build install-example-zig-function
1212
zig build -Dlang=luau install-example-luau-bytecode
1313

14+
zig build -Dlang=luajit
15+
16+
test_cross:
17+
zig build -Dlang=lua51 -Dtarget=aarch64-linux
18+
zig build -Dlang=lua51 -Dtarget=aarch64-linux-gnu
19+
zig build -Dlang=luajit -Dtarget=aarch64-linux
20+
zig build -Dlang=luajit -Dtarget=aarch64-linux-gnu
21+
1422
docs:
1523
zig build docs

0 commit comments

Comments
 (0)