Skip to content

Commit a6e14ee

Browse files
committed
Remove @cImport
See ziglang/zig#20630
1 parent 7479680 commit a6e14ee

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"),
@@ -222,10 +238,10 @@ fn buildLuau(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Opti
222238
lib.addCSourceFile(.{ .file = b.path("src/luau.cpp"), .flags = &flags });
223239
lib.linkLibCpp();
224240

225-
// It may not be as likely that other software links against Luau, but might as well expose these anyway
226241
lib.installHeader(upstream.path("VM/include/lua.h"), "lua.h");
227242
lib.installHeader(upstream.path("VM/include/lualib.h"), "lualib.h");
228243
lib.installHeader(upstream.path("VM/include/luaconf.h"), "luaconf.h");
244+
lib.installHeader(upstream.path("Compiler/include/luacode.h"), "luacode.h");
229245

230246
return lib;
231247
}

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
@@ -1,14 +1,6 @@
11
const std = @import("std");
22

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

135
const config = @import("config");
146

0 commit comments

Comments
 (0)