Skip to content

Commit 3dc208a

Browse files
committed
Add lua52compat build option instead of feature flag
1 parent 86a32d1 commit 3dc208a

File tree

6 files changed

+199
-8
lines changed

6 files changed

+199
-8
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ description = """
1111
Sources of LuaJIT 2.1 (OpenResty's branch) and logic to build it.
1212
"""
1313

14-
[features]
15-
lua52compat = []
16-
1714
[dependencies]
1815
cc = "1.0"

extras/msvcbuild.bat

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
@rem Script to build LuaJIT with MSVC.
2+
@rem Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
3+
@rem
4+
@rem Open a "Visual Studio Command Prompt" (either x86 or x64).
5+
@rem Then cd to this directory and run this script. Use the following
6+
@rem options (in order), if needed. The default is a dynamic release build.
7+
@rem
8+
@rem nogc64 disable LJ_GC64 mode for x64
9+
@rem lua52c enable lua52 compat mode
10+
@rem debug emit debug symbols
11+
@rem amalg amalgamated build
12+
@rem static static linkage
13+
14+
@if not defined INCLUDE goto :FAIL
15+
16+
@setlocal
17+
@rem Add more debug flags here, e.g. DEBUGCFLAGS=/DLUA_USE_APICHECK
18+
@set DEBUGCFLAGS=
19+
@set LJCOMPILE=cl /nologo /c /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_STDIO_INLINE=__declspec(dllexport)__inline
20+
@set LJLINK=link /nologo
21+
@set LJMT=mt /nologo
22+
@set LJLIB=lib /nologo /nodefaultlib
23+
@set DASMDIR=..\dynasm
24+
@set DASM=%DASMDIR%\dynasm.lua
25+
@set DASC=vm_x64.dasc
26+
@set LJDLLNAME=lua51.dll
27+
@set LJLIBNAME=lua51.lib
28+
@set BUILDTYPE=release
29+
@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c
30+
31+
%LJCOMPILE% host\minilua.c
32+
@if errorlevel 1 goto :BAD
33+
%LJLINK% /out:minilua.exe minilua.obj
34+
@if errorlevel 1 goto :BAD
35+
if exist minilua.exe.manifest^
36+
%LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe
37+
38+
@set DASMFLAGS=-D WIN -D JIT -D FFI -D P64
39+
@set LJARCH=x64
40+
@minilua
41+
@if errorlevel 8 goto :X64
42+
@set DASC=vm_x86.dasc
43+
@set DASMFLAGS=-D WIN -D JIT -D FFI
44+
@set LJARCH=x86
45+
@set LJCOMPILE=%LJCOMPILE% /arch:SSE2
46+
:X64
47+
@if "%1" neq "nogc64" goto :GC64
48+
@shift
49+
@set DASC=vm_x86.dasc
50+
@set LJCOMPILE=%LJCOMPILE% /DLUAJIT_DISABLE_GC64
51+
:GC64
52+
minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h %DASC%
53+
@if errorlevel 1 goto :BAD
54+
55+
@if "%1" neq "lua52c" goto :NOLUA52C
56+
@shift
57+
@set LJCOMPILE=%LJCOMPILE% /DLUAJIT_ENABLE_LUA52COMPAT
58+
:NOLUA52C
59+
60+
%LJCOMPILE% /I "." /I %DASMDIR% host\buildvm*.c
61+
@if errorlevel 1 goto :BAD
62+
%LJLINK% /out:buildvm.exe buildvm*.obj
63+
@if errorlevel 1 goto :BAD
64+
if exist buildvm.exe.manifest^
65+
%LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe
66+
67+
buildvm -m peobj -o lj_vm.obj
68+
@if errorlevel 1 goto :BAD
69+
buildvm -m bcdef -o lj_bcdef.h %ALL_LIB%
70+
@if errorlevel 1 goto :BAD
71+
buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%
72+
@if errorlevel 1 goto :BAD
73+
buildvm -m libdef -o lj_libdef.h %ALL_LIB%
74+
@if errorlevel 1 goto :BAD
75+
buildvm -m recdef -o lj_recdef.h %ALL_LIB%
76+
@if errorlevel 1 goto :BAD
77+
buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB%
78+
@if errorlevel 1 goto :BAD
79+
buildvm -m folddef -o lj_folddef.h lj_opt_fold.c
80+
@if errorlevel 1 goto :BAD
81+
82+
@if "%1" neq "debug" goto :NODEBUG
83+
@shift
84+
@set BUILDTYPE=debug
85+
@set LJCOMPILE=%LJCOMPILE% /Zi %DEBUGCFLAGS%
86+
@set LJLINK=%LJLINK% /opt:ref /opt:icf /incremental:no
87+
:NODEBUG
88+
@set LJLINK=%LJLINK% /%BUILDTYPE%
89+
@if "%1"=="amalg" goto :AMALGDLL
90+
@if "%1"=="static" goto :STATIC
91+
%LJCOMPILE% /MD /DLUA_BUILD_AS_DLL lj_*.c lib_*.c
92+
@if errorlevel 1 goto :BAD
93+
%LJLINK% /DLL /out:%LJDLLNAME% lj_*.obj lib_*.obj
94+
@if errorlevel 1 goto :BAD
95+
@goto :MTDLL
96+
:STATIC
97+
%LJCOMPILE% lj_*.c lib_*.c
98+
@if errorlevel 1 goto :BAD
99+
%LJLIB% /OUT:%LJLIBNAME% lj_*.obj lib_*.obj
100+
@if errorlevel 1 goto :BAD
101+
@goto :MTDLL
102+
:AMALGDLL
103+
%LJCOMPILE% /MD /DLUA_BUILD_AS_DLL ljamalg.c
104+
@if errorlevel 1 goto :BAD
105+
%LJLINK% /DLL /out:%LJDLLNAME% ljamalg.obj lj_vm.obj
106+
@if errorlevel 1 goto :BAD
107+
:MTDLL
108+
if exist %LJDLLNAME%.manifest^
109+
%LJMT% -manifest %LJDLLNAME%.manifest -outputresource:%LJDLLNAME%;2
110+
111+
%LJCOMPILE% luajit.c
112+
@if errorlevel 1 goto :BAD
113+
%LJLINK% /out:luajit.exe luajit.obj %LJLIBNAME%
114+
@if errorlevel 1 goto :BAD
115+
if exist luajit.exe.manifest^
116+
%LJMT% -manifest luajit.exe.manifest -outputresource:luajit.exe
117+
118+
@del *.obj *.manifest minilua.exe buildvm.exe
119+
@del host\buildvm_arch.h
120+
@del lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h
121+
@echo.
122+
@echo === Successfully built LuaJIT for Windows/%LJARCH% ===
123+
124+
@goto :END
125+
:BAD
126+
@echo.
127+
@echo *******************************************************
128+
@echo *** Build FAILED -- Please check the error messages ***
129+
@echo *******************************************************
130+
@goto :END
131+
:FAIL
132+
@echo You must open a "Visual Studio Command Prompt" to run this script
133+
:END

src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct Build {
77
out_dir: Option<PathBuf>,
88
target: Option<String>,
99
host: Option<String>,
10+
options: Options,
1011
}
1112

1213
pub struct Artifacts {
@@ -15,13 +16,19 @@ pub struct Artifacts {
1516
libs: Vec<String>,
1617
}
1718

19+
#[derive(Default, Clone, Copy)]
20+
struct Options {
21+
lua52compat: bool,
22+
}
23+
1824
impl Build {
1925
#[allow(clippy::new_without_default)]
2026
pub fn new() -> Build {
2127
Build {
2228
out_dir: env::var_os("OUT_DIR").map(|s| PathBuf::from(s).join("luajit-build")),
2329
target: env::var("TARGET").ok(),
2430
host: env::var("HOST").ok(),
31+
options: Options::default(),
2532
}
2633
}
2734

@@ -40,6 +47,11 @@ impl Build {
4047
self
4148
}
4249

50+
pub fn lua52compat(&mut self, status: bool) -> &mut Build {
51+
self.options.lua52compat = status;
52+
self
53+
}
54+
4355
fn cmd_make(&self) -> Command {
4456
match &self.host.as_ref().expect("HOST dir not set")[..] {
4557
"x86_64-unknown-dragonfly" => Command::new("gmake"),
@@ -106,7 +118,7 @@ impl Build {
106118
}
107119

108120
let mut xcflags = vec!["-fPIC"];
109-
if cfg!(feature = "lua52compat") {
121+
if self.options.lua52compat {
110122
xcflags.push("-DLUAJIT_ENABLE_LUA52COMPAT");
111123
}
112124

@@ -134,6 +146,7 @@ impl Build {
134146
let target = &self.target.as_ref().expect("TARGET not set")[..];
135147
let out_dir = self.out_dir.as_ref().expect("OUT_DIR not set");
136148
let source_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("luajit2");
149+
let extras_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("extras");
137150
let build_dir = out_dir.join("build");
138151
let lib_dir = out_dir.join("lib");
139152
let include_dir = out_dir.join("include");
@@ -147,9 +160,13 @@ impl Build {
147160
.unwrap_or_else(|e| panic!("cannot create {}: {}", dir.display(), e));
148161
}
149162
cp_r(&source_dir, &build_dir);
163+
cp_r(&extras_dir, &build_dir.join("src"));
150164

151165
let mut msvcbuild = Command::new(build_dir.join("src").join("msvcbuild.bat"));
152166
msvcbuild.current_dir(build_dir.join("src"));
167+
if self.options.lua52compat {
168+
msvcbuild.arg("lua52c");
169+
}
153170
msvcbuild.arg("static");
154171

155172
let cl = cc::windows_registry::find_tool(&target, "cl.exe").expect("failed to find cl");

testcrate/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = ["Aleksandr Orlenko <[email protected]>"]
55

66
[features]
7-
lua52compat = ["luajit-src/lua52compat"]
7+
lua52compat = []
88

99
[build-dependencies]
1010
luajit-src = { path = ".." }

testcrate/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
fn main() {
22
println!("cargo:rerun-if-changed=build.rs");
3-
let artifacts = luajit_src::Build::new().build();
3+
let mut builder = luajit_src::Build::new();
4+
if cfg!(feature = "lua52compat") {
5+
builder.lua52compat(true);
6+
}
7+
let artifacts = builder.build();
48
artifacts.print_cargo_metadata();
59
}

testcrate/src/lib.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ extern "C" {
55
pub fn luaL_openlibs(state: *mut c_void);
66
pub fn lua_getfield(state: *mut c_void, index: c_int, k: *const c_char);
77
pub fn lua_tolstring(state: *mut c_void, index: c_int, len: *mut c_long) -> *const c_char;
8+
pub fn luaL_loadstring(state: *mut c_void, s: *const c_char) -> c_int;
9+
pub fn lua_pcall(state: *mut c_void, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
810
}
911

1012
pub unsafe fn lua_getglobal(state: *mut c_void, k: *const c_char) {
1113
lua_getfield(state, -10002 /* LUA_GLOBALSINDEX */, k);
1214
}
1315

1416
#[test]
15-
fn lua_works() {
17+
fn test_lua() {
1618
use std::{ptr, slice};
1719
unsafe {
1820
let state = luaL_newstate();
@@ -27,6 +29,44 @@ fn lua_works() {
2729
slice::from_raw_parts(version_ptr as *const u8, len as usize)
2830
};
2931

30-
assert_eq!(version, "Lua 5.1".as_bytes());
32+
assert_eq!(version, b"Lua 5.1");
33+
}
34+
}
35+
36+
#[test]
37+
fn test_lua52compat() {
38+
use std::{ptr, slice};
39+
unsafe {
40+
let state = luaL_newstate();
41+
assert!(state != ptr::null_mut());
42+
43+
luaL_openlibs(state);
44+
45+
let code = "
46+
lua52compat = \"no\"
47+
t = setmetatable({}, {
48+
__pairs = function(t)
49+
lua52compat = \"yes\"
50+
return next, t, nil
51+
end
52+
})
53+
for k,v in pairs(t) do end
54+
\0";
55+
let ret1 = luaL_loadstring(state, code.as_ptr().cast());
56+
assert_eq!(0, ret1);
57+
let ret2 = lua_pcall(state, 0, 0, 0);
58+
assert_eq!(0, ret2);
59+
60+
let lua52compat = {
61+
lua_getglobal(state, "lua52compat\0".as_ptr().cast());
62+
let mut len: c_long = 0;
63+
let lua52compat_ptr = lua_tolstring(state, -1, &mut len);
64+
slice::from_raw_parts(lua52compat_ptr as *const u8, len as usize)
65+
};
66+
67+
#[cfg(feature = "lua52compat")]
68+
assert_eq!(lua52compat, b"yes");
69+
#[cfg(not(feature = "lua52compat"))]
70+
assert_eq!(lua52compat, b"no");
3171
}
3272
}

0 commit comments

Comments
 (0)