Skip to content

Commit f83c1db

Browse files
authored
Remove @cImport in favor of translateC (#96)
1 parent baba73e commit f83c1db

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

build.zig

+19-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ pub fn build(b: *Build) void {
5151
else => buildLua(b, target, optimize, upstream, lang, shared),
5252
};
5353

54-
// Expose the Lua artifact
55-
b.installArtifact(lib);
54+
// Expose the Lua artifact, and get an install step that header translation can refer to
55+
const install_lib = b.addInstallArtifact(lib, .{});
56+
b.getInstallStep().dependOn(&install_lib.step);
5657

5758
switch (lang) {
5859
.luau => {
@@ -66,6 +67,21 @@ pub fn build(b: *Build) void {
6667

6768
ziglua.linkLibrary(lib);
6869

70+
// lib must expose all headers included by these root headers
71+
const c_header_path = switch (lang) {
72+
.luajit => b.path("include/luajit_all.h"),
73+
.luau => b.path("include/luau_all.h"),
74+
else => b.path("include/lua_all.h"),
75+
};
76+
const c_headers = b.addTranslateC(.{
77+
.root_source_file = c_header_path,
78+
.target = target,
79+
.optimize = optimize,
80+
});
81+
c_headers.addIncludeDir(b.getInstallPath(install_lib.h_dir.?, ""));
82+
c_headers.step.dependOn(&install_lib.step);
83+
ziglua.addImport("c", c_headers.createModule());
84+
6985
// Tests
7086
const tests = b.addTest(.{
7187
.root_source_file = b.path("src/tests.zig"),
@@ -235,10 +251,10 @@ fn buildLuau(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Opti
235251
lib.addCSourceFile(.{ .file = b.path("src/luau.cpp"), .flags = &flags });
236252
lib.linkLibCpp();
237253

238-
// It may not be as likely that other software links against Luau, but might as well expose these anyway
239254
lib.installHeader(upstream.path("VM/include/lua.h"), "lua.h");
240255
lib.installHeader(upstream.path("VM/include/lualib.h"), "lualib.h");
241256
lib.installHeader(upstream.path("VM/include/luaconf.h"), "luaconf.h");
257+
lib.installHeader(upstream.path("Compiler/include/luacode.h"), "luacode.h");
242258

243259
return lib;
244260
}

include/lua_all.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef lua_all_h
2+
#define lua_all_h
3+
4+
#include "lua.h"
5+
#include "lualib.h"
6+
#include "lauxlib.h"
7+
#include "luaconf.h"
8+
9+
#endif // lua_all_h

include/luajit_all.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef luajit_all_h
2+
#define luajit_all_h
3+
4+
#include "lua.h"
5+
#include "lualib.h"
6+
#include "lauxlib.h"
7+
#include "luaconf.h"
8+
#include "luajit.h"
9+
10+
#endif // luajit_all_h

include/luau_all.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef luau_all_h
2+
#define luau_all_h
3+
4+
#include "lua.h"
5+
#include "lualib.h"
6+
#include "luaconf.h"
7+
#include "luacode.h"
8+
9+
#endif // luau_all_h

src/lib.zig

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ const std = @import("std");
33
pub const def = @import("define.zig");
44
pub const define = def.define;
55

6-
const c = @cImport({
7-
@cInclude("luaconf.h");
8-
@cInclude("lua.h");
9-
@cInclude("lualib.h");
10-
11-
if (lang != .luau) @cInclude("lauxlib.h");
12-
if (lang == .luau) @cInclude("luacode.h");
13-
if (lang == .luajit) @cInclude("luajit.h");
14-
});
6+
const c = @import("c");
157

168
const config = @import("config");
179

0 commit comments

Comments
 (0)