Skip to content

Remove @cImport in favor of translateC #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ pub fn build(b: *Build) void {
else => buildLua(b, target, optimize, upstream, lang, shared),
};

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

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

ziglua.linkLibrary(lib);

// lib must expose all headers included by these root headers
const c_header_path = switch (lang) {
.luajit => b.path("include/luajit_all.h"),
.luau => b.path("include/luau_all.h"),
else => b.path("include/lua_all.h"),
};
const c_headers = b.addTranslateC(.{
.root_source_file = c_header_path,
.target = target,
.optimize = optimize,
});
c_headers.addIncludeDir(b.getInstallPath(install_lib.h_dir.?, ""));
c_headers.step.dependOn(&install_lib.step);
ziglua.addImport("c", c_headers.createModule());

// Tests
const tests = b.addTest(.{
.root_source_file = b.path("src/tests.zig"),
Expand Down Expand Up @@ -235,10 +251,10 @@ fn buildLuau(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Opti
lib.addCSourceFile(.{ .file = b.path("src/luau.cpp"), .flags = &flags });
lib.linkLibCpp();

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

return lib;
}
Expand Down
9 changes: 9 additions & 0 deletions include/lua_all.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef lua_all_h
#define lua_all_h

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luaconf.h"

#endif // lua_all_h
10 changes: 10 additions & 0 deletions include/luajit_all.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef luajit_all_h
#define luajit_all_h

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luaconf.h"
#include "luajit.h"

#endif // luajit_all_h
9 changes: 9 additions & 0 deletions include/luau_all.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef luau_all_h
#define luau_all_h

#include "lua.h"
#include "lualib.h"
#include "luaconf.h"
#include "luacode.h"

#endif // luau_all_h
10 changes: 1 addition & 9 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ const std = @import("std");
pub const def = @import("define.zig");
pub const define = def.define;

const c = @cImport({
@cInclude("luaconf.h");
@cInclude("lua.h");
@cInclude("lualib.h");

if (lang != .luau) @cInclude("lauxlib.h");
if (lang == .luau) @cInclude("luacode.h");
if (lang == .luajit) @cInclude("luajit.h");
});
const c = @import("c");

const config = @import("config");

Expand Down
Loading