Support cross compilation

This commit teaches the build script to recognize when it is
cross-compiling and switch to an alternative approach for generating
the `glue.rs` module.

It defaults to the equivalent logic found in the lua headers to
set the default types and parameters.

Notably: it doesn't statically produce the default lua paths as we
cannot know these without either executing the code (not guaranteed
possible when cross compiling) or regexing out the paths from the
headers (a bit brittle).  An alternative approach might be to use
something like `lazy_static` to ask the library for its compiled in
values once at runtime.

I've tested this with:

```
cargo build --target armv7-unknown-linux-gnueabihf --features lua51,vendored
cargo build --target armv7-unknown-linux-gnueabihf --features lua52,vendored
cargo build --target armv7-unknown-linux-gnueabihf --features lua53,vendored
cargo build --target armv7-unknown-linux-gnueabihf --features lua54,vendored
cargo build --target armv7-unknown-linux-gnueabihf --features luajit,vendored
```

All except luajit compile.  Luajit itself doesn't cross compile, so I
don't think we can ever reasonably get that to work.

I haven't tried to run any of this yet; my use case is actually for mac
(https://github.com/wez/wezterm/pull/426) so I need to commit this and
try patching it in over there before I can see if that truly worked
end-to-end.

refs: https://github.com/khvzak/mlua/issues/14
This commit is contained in:
Wez Furlong 2021-01-13 09:55:53 -08:00
parent 618874ef3c
commit e0d9ec41e2
1 changed files with 123 additions and 1 deletions

View File

@ -91,6 +91,124 @@ fn build_glue<P: AsRef<Path> + std::fmt::Debug>(include_path: &P) {
.unwrap();
}
// When cross-compiling, we cannot use `build_glue` as we cannot run the generated
// executable. Instead, let's take a stab at synthesizing the likely values.
// If you're cross-compiling and using a non-vendored library then there is a chance
// that the values selected here may be incorrect, but we have no way to determine
// that here.
fn generate_glue() -> std::io::Result<()> {
use std::io::Write;
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut glue = std::fs::File::create(build_dir.join("glue.rs"))?;
write!(
glue,
"/* This file was generated by build/main.rs; do not modify by hand */\n"
)?;
write!(glue, "use std::os::raw::*;\n")?;
// We can't statically determine the default paths.
// It is possible though to use something like lazy_static! to create a new
// lua context and extract that information.
// For my (@wez) purposes, I actually don't want there to be a default path,
// so I'm just leaving this blank for the moment.
write!(glue, "pub const LUA_PATH_DEFAULT: &str = \"\";\n")?;
write!(glue, "pub const LUA_CPATH_DEFAULT: &str = \"\";\n")?;
write!(
glue,
"#[cfg(windows)] pub const LUA_DIRSEP: &str = \"\\\\\";\n"
)?;
write!(glue, "#[cfg(unix)] pub const LUA_DIRSEP: &str = \"/\";\n")?;
let pointer_bit_width: usize = env::var("CARGO_CFG_TARGET_POINTER_WIDTH")
.unwrap()
.parse()
.unwrap();
write!(
glue,
"pub const LUA_EXTRASPACE: c_int = {} / 8;\n",
pointer_bit_width
)?;
// This is generally hardcoded to this size
write!(glue, "pub const LUA_IDSIZE: c_int = 60;\n")?;
write!(glue, "pub const LUAL_BUFFERSIZE: c_int = 16 * ({} / 8) * std::mem::size_of::<LUA_NUMBER>() as c_int;\n", pointer_bit_width)?;
// Unless the target is restricted, the defaults are 64 bit
write!(glue, "pub type LUA_NUMBER = c_double;\n")?;
write!(glue, "pub type LUA_INTEGER = i64;\n")?;
write!(glue, "pub type LUA_UNSIGNED = u64;\n")?;
let version = if cfg!(feature = "luajit") || cfg!(feature = "lua51") {
// Note: luajit is ~lua 5.1, but it doesn't itself cross compile
(5, 1, 0)
} else if cfg!(feature = "lua52") {
(5, 2, 0)
} else if cfg!(feature = "lua53") {
(5, 3, 0)
} else if cfg!(feature = "lua54") {
(5, 4, 0)
} else {
unreachable!();
};
write!(
glue,
"pub const LUA_VERSION_NUM: c_int = {};\n",
(version.0 * 100) + version.1
)?;
write!(
glue,
"pub const LUA_VERSION: &str = \"Lua {}.{}\";\n",
version.0, version.1
)?;
write!(
glue,
"pub const LUA_RELEASE: &str = \"Lua {}.{}.{}\";\n",
version.0, version.1, version.2
)?;
let max_stack = if pointer_bit_width >= 32 {
1_000_000
} else {
15_000
};
write!(
glue,
"pub const LUA_REGISTRYINDEX: c_int = -{} - 1000;\n",
max_stack
)?;
// These two are only defined in lua 5.1
write!(glue, "pub const LUA_ENVIRONINDEX: c_int = -10001;\n")?;
write!(glue, "pub const LUA_GLOBALSINDEX: c_int = -10002;\n")?;
// This is only defined in lua 5.3 and up, but we can always generate its value here,
// even if we don't use it.
// This matches the default definition in lauxlib.h
write!(glue, "pub const LUAL_NUMSIZES: c_int = std::mem::size_of::<LUA_INTEGER>() as c_int * 16 + std::mem::size_of::<LUA_NUMBER>() as c_int;\n")?;
write!(
glue,
r#"
pub const LUA_BITLIBNAME: &str = "bit32";
pub const LUA_COLIBNAME: &str = "coroutine";
pub const LUA_DBLIBNAME: &str = "debug";
pub const LUA_IOLIBNAME: &str = "io";
pub const LUA_LOADLIBNAME: &str = "package";
pub const LUA_MATHLIBNAME: &str = "math";
pub const LUA_OSLIBNAME: &str = "os";
pub const LUA_STRLIBNAME: &str = "string";
pub const LUA_TABLIBNAME: &str = "table";
pub const LUA_UTF8LIBNAME: &str = "utf8";
"#
)?;
Ok(())
}
fn main() {
#[cfg(not(any(
feature = "lua54",
@ -132,5 +250,9 @@ fn main() {
);
let include_dir = find::probe_lua();
if env::var("TARGET").unwrap() != env::var("HOST").unwrap() {
generate_glue().unwrap();
} else {
build_glue(&include_dir);
}
}