Skip to content

Commit

Permalink
Remove @cImport
Browse files Browse the repository at this point in the history
  • Loading branch information
luchak committed Sep 22, 2024
1 parent 7479680 commit a6e14ee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
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 @@ -222,10 +238,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
@@ -1,14 +1,6 @@
const std = @import("std");

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

0 comments on commit a6e14ee

Please sign in to comment.