Move bindgen to build script

This commit is contained in:
Lucas Schwiderski 2023-03-21 11:55:46 +01:00
parent 089acdbc28
commit 206f7884e3
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
6 changed files with 41 additions and 1145 deletions

View file

@ -15,5 +15,6 @@ links = "luajit"
libc = "0.2"
[build-dependencies]
bindgen = "0.64.0"
cc = "1.0.40"
fs_extra = "1.1.0"

View file

@ -1,26 +0,0 @@
#!/bin/bash
BINDGEN_VERSION=$(bindgen --version | grep -v -e '^cargo')
bindgen -o src/ffi.rs \
--raw-line "/// Generated with: ${BINDGEN_VERSION}" \
--whitelist-var "LUA.*" \
--whitelist-var "LUAJIT.*" \
--whitelist-type "lua_.*" \
--whitelist-type "luaL_.*" \
--whitelist-function "lua_.*" \
--whitelist-function "luaL_.*" \
--whitelist-function "luaJIT.*" \
--ctypes-prefix "libc" \
--use-core \
--impl-debug \
ffi.h -- -I luajit/src
sed -i -e 's/pub fn \(luaJIT_[^\(]*\)/\/\/\/ <https:\/\/luajit.org\/ext_c_api.html> \n pub fn \1/' src/ffi.rs
sed -i -e 's/pub fn \(lua_[^\(]*\)/\/\/\/ <https\:\/\/www.lua.org\/manual\/5.1\/manual.html#\1> \n pub fn \1/' src/ffi.rs
sed -i -e 's/pub fn \(luaL_[^\(]*\)/\/\/\/ <https\:\/\/www.lua.org\/manual\/5.1\/manual.html#\1> \n pub fn \1/' src/ffi.rs
sed -i -e 's/pub type \(lua_[^\=]*\)/\/\/\/ <https\:\/\/www.lua.org\/manual\/5.1\/manual.html#\1> \n pub type \1/' src/ffi.rs
sed -i -e 's/pub struct \(lua_[^\{]*\)/\/\/\/ <https\:\/\/www.lua.org\/manual\/5.1\/manual.html#\1> \n pub struct \1/' src/ffi.rs
sed -i -e 's/pub struct \(luaL_[^\{]*\)/\/\/\/ <https\:\/\/www.lua.org\/manual\/5.1\/manual.html#\1> \n pub struct \1/' src/ffi.rs
cargo +stable fmt

View file

@ -1,9 +1,11 @@
use fs_extra::dir;
use fs_extra::dir::CopyOptions;
use std::env;
use std::path::PathBuf;
use std::process::{Command, Stdio};
const LIB_NAME: &str = "luajit";
const LUAJIT_HEADERS: [&str; 4] = ["lua.h", "lualib.h", "lauxlib.h", "luajit.h"];
fn main() {
let luajit_dir = format!("{}/luajit", env!("CARGO_MANIFEST_DIR"));
@ -53,4 +55,36 @@ fn main() {
println!("cargo:include={}", src_dir);
println!("cargo:rustc-link-search=native={}", src_dir);
println!("cargo:rustc-link-lib=static={}", LIB_NAME);
let mut bindings = bindgen::Builder::default();
for header in LUAJIT_HEADERS {
println!("cargo:rerun-if-changed={}/src/{}", luajit_dir, header);
bindings = bindings.header(format!("{}/src/{}", luajit_dir, header));
}
let bindings = bindings
.allowlist_var("LUA.*")
.allowlist_var("LUAJIT.*")
.allowlist_type("lua_.*")
.allowlist_type("luaL_.*")
.allowlist_function("lua_.*")
.allowlist_function("luaL_.*")
.allowlist_function("luaJIT.*")
.ctypes_prefix("libc")
.impl_debug(true)
.use_core()
.clang_arg("-Iluajit/src")
// Make it pretty
.rustfmt_bindings(true)
.sort_semantically(true)
.merge_extern_blocks(true)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Failed to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Failed to write bindings");
}

4
ffi.h
View file

@ -1,4 +0,0 @@
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luajit.h"

1113
src/ffi.rs

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,8 @@
#![no_std]
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(clippy::deprecated_semver)]
#![allow(clippy::missing_safety_doc)]
//! # LuaJIT 2.1
//!
@ -15,7 +17,9 @@
//!
//! <http://wiki.luajit.org/NYI>
mod ffi;
mod ffi {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
pub use ffi::*;
use core::ptr;
@ -92,7 +96,7 @@ pub unsafe fn lua_isthread(L: *mut lua_State, idx: libc::c_int) -> libc::c_int {
/// <https://www.lua.org/manual/5.1/manual.html#lua_isnone>
#[inline]
pub unsafe fn lua_isnone(L: *mut lua_State, idx: libc::c_int) -> libc::c_int {
(lua_type(L, idx) == LUA_TNONE as i32) as i32
(lua_type(L, idx) == LUA_TNONE) as i32
}
/// <https://www.lua.org/manual/5.1/manual.html#lua_isnoneornil>