Skip to main content

lua_src/
lib.rs

1use std::env;
2use std::error::Error;
3use std::fs;
4use std::path::{Path, PathBuf};
5
6/// Represents the version of Lua to build.
7#[derive(Debug, PartialEq, Eq)]
8pub enum Version {
9    Lua51,
10    Lua52,
11    Lua53,
12    Lua54,
13    Lua55,
14}
15pub use self::Version::*;
16
17/// Represents the configuration for building Lua artifacts.
18pub struct Build {
19    out_dir: Option<PathBuf>,
20    target: Option<String>,
21    host: Option<String>,
22    opt_level: Option<String>,
23    debug: Option<bool>,
24}
25
26/// Represents the artifacts produced by the build process.
27#[derive(Clone, Debug)]
28pub struct Artifacts {
29    include_dir: PathBuf,
30    lib_dir: PathBuf,
31    libs: Vec<String>,
32}
33
34impl Default for Build {
35    fn default() -> Build {
36        Build {
37            out_dir: env::var_os("OUT_DIR").map(PathBuf::from),
38            target: env::var("TARGET").ok(),
39            host: None,
40            opt_level: None,
41            debug: None,
42        }
43    }
44}
45
46impl Build {
47    /// Creates a new `Build` instance with default settings.
48    pub fn new() -> Build {
49        Build::default()
50    }
51
52    /// Sets the output directory for the build artifacts.
53    ///
54    /// This is required if called outside of a build script.
55    pub fn out_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Build {
56        self.out_dir = Some(path.as_ref().to_path_buf());
57        self
58    }
59
60    /// Sets the target architecture for the build.
61    ///
62    /// This is required if called outside of a build script.
63    pub fn target(&mut self, target: &str) -> &mut Build {
64        self.target = Some(target.to_string());
65        self
66    }
67
68    /// Sets the host architecture for the build.
69    ///
70    /// This is optional and will default to the environment variable `HOST` if not set.
71    /// If called outside of a build script, it will default to the target architecture.
72    pub fn host(&mut self, host: &str) -> &mut Build {
73        self.host = Some(host.to_string());
74        self
75    }
76
77    /// Sets the optimization level for the build.
78    ///
79    /// This is optional and will default to the environment variable `OPT_LEVEL` if not set.
80    /// If called outside of a build script, it will default to `0` in debug mode and `2` otherwise.
81    pub fn opt_level(&mut self, opt_level: &str) -> &mut Build {
82        self.opt_level = Some(opt_level.to_string());
83        self
84    }
85
86    /// Sets whether to build in debug mode.
87    ///
88    /// This is optional and will default to the value of `cfg!(debug_assertions)`.
89    /// If set to `true`, it also enables Lua API checks.
90    pub fn debug(&mut self, debug: bool) -> &mut Build {
91        self.debug = Some(debug);
92        self
93    }
94
95    /// Builds the Lua artifacts for the specified version.
96    pub fn build(&self, version: Version) -> Artifacts {
97        match self.try_build(version) {
98            Ok(artifacts) => artifacts,
99            Err(err) => panic!("{err}"),
100        }
101    }
102
103    /// Attempts to build the Lua artifacts for the specified version.
104    ///
105    /// Returns an error if the build fails.
106    pub fn try_build(&self, version: Version) -> Result<Artifacts, Box<dyn Error>> {
107        let target = self.target.as_ref().ok_or("TARGET is not set")?;
108        let out_dir = self.out_dir.as_ref().ok_or("OUT_DIR is not set")?;
109        let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
110        let source_dir = manifest_dir.join(version.source_dir());
111        let lib_dir = out_dir.join("lib");
112        let include_dir = out_dir.join("include");
113
114        if !include_dir.exists() {
115            fs::create_dir_all(&include_dir)
116                .context(|| format!("Cannot create '{}'", include_dir.display()))?;
117        }
118
119        let mut config = cc::Build::new();
120        config.warnings(false).cargo_metadata(false).target(target);
121
122        match &self.host {
123            Some(host) => {
124                config.host(host);
125            }
126            // Host will be taken from the environment variable
127            None if env::var("HOST").is_ok() => {}
128            None => {
129                // If called outside of build script, set default host
130                config.host(target);
131            }
132        }
133
134        let mut libs = vec![version.lib_name().to_string()];
135        match target {
136            _ if target.contains("linux") => {
137                config.define("LUA_USE_LINUX", None);
138            }
139            _ if target.ends_with("bsd") => {
140                config.define("LUA_USE_LINUX", None);
141            }
142            _ if target.ends_with("illumos") => {
143                config.define("LUA_USE_POSIX", None);
144            }
145            _ if target.ends_with("solaris") => {
146                config.define("LUA_USE_POSIX", None);
147            }
148            _ if target.contains("apple-darwin") => {
149                match version {
150                    Lua51 => config.define("LUA_USE_LINUX", None),
151                    _ => config.define("LUA_USE_MACOSX", None),
152                };
153            }
154            _ if target.contains("apple-ios") => {
155                match version {
156                    Lua54 | Lua55 => config.define("LUA_USE_IOS", None),
157                    _ => config.define("LUA_USE_POSIX", None),
158                };
159            }
160            _ if target.contains("windows") => {
161                // Defined in Lua >= 5.3
162                config.define("LUA_USE_WINDOWS", None);
163            }
164            _ if target.ends_with("emscripten") => {
165                config
166                    .define("LUA_USE_POSIX", None)
167                    .flag("-sSUPPORT_LONGJMP=wasm"); // Enable setjmp/longjmp support (WASM-specific)
168            }
169            _ if target.contains("wasi") => {
170                // WASI is posix-like, but further patches are needed to the Lua
171                // source to get it compiling.
172                config.define("LUA_USE_POSIX", None);
173
174                // Bring in just enough signal-handling support to get Lua at
175                // least compiling, but WASI in general does not support
176                // signals.
177                config.define("_WASI_EMULATED_SIGNAL", None);
178                libs.push("wasi-emulated-signal".to_string());
179
180                // https://github.com/WebAssembly/wasi-sdk/blob/main/SetjmpLongjmp.md
181                // for information about getting setjmp/longjmp working.
182                config.flag("-mllvm").flag("-wasm-enable-eh");
183                config.flag("-mllvm").flag("-wasm-use-legacy-eh=false");
184                config.flag("-mllvm").flag("-wasm-enable-sjlj");
185                libs.push("setjmp".to_string());
186            }
187            _ => Err(format!("don't know how to build Lua for {target}"))?,
188        }
189
190        if let Lua54 = version {
191            config.define("LUA_COMPAT_5_3", None);
192        }
193
194        #[cfg(feature = "ucid")]
195        if let Lua54 | Lua55 = version {
196            config.define("LUA_UCID", None);
197        }
198
199        let debug = self.debug.unwrap_or(cfg!(debug_assertions));
200        if debug {
201            config.define("LUA_USE_APICHECK", None);
202            config.debug(true);
203        }
204
205        match &self.opt_level {
206            Some(opt_level) => {
207                config.opt_level_str(opt_level);
208            }
209            // Opt level will be taken from the environment variable
210            None if env::var("OPT_LEVEL").is_ok() => {}
211            None => {
212                // If called outside of build script, set default opt level
213                config.opt_level(if debug { 0 } else { 2 });
214            }
215        }
216
217        config
218            .include(&source_dir)
219            .warnings(false) // Suppress all warnings
220            .flag_if_supported("-fno-common") // Compile common globals like normal definitions
221            .add_files_by_ext(&source_dir, "c")?
222            .out_dir(&lib_dir)
223            .try_compile(version.lib_name())?;
224
225        for f in &["lauxlib.h", "lua.h", "luaconf.h", "lualib.h"] {
226            let from = source_dir.join(f);
227            let to = include_dir.join(f);
228            fs::copy(&from, &to)
229                .context(|| format!("Cannot copy '{}' to '{}'", from.display(), to.display()))?;
230        }
231
232        Ok(Artifacts {
233            include_dir,
234            lib_dir,
235            libs,
236        })
237    }
238}
239
240impl Version {
241    fn source_dir(&self) -> &'static str {
242        match self {
243            Lua51 => "lua-5.1.5",
244            Lua52 => "lua-5.2.4",
245            Lua53 => "lua-5.3.6",
246            Lua54 => "lua-5.4.8",
247            Lua55 => "lua-5.5.0",
248        }
249    }
250
251    fn lib_name(&self) -> &'static str {
252        match self {
253            Lua51 => "lua5.1",
254            Lua52 => "lua5.2",
255            Lua53 => "lua5.3",
256            Lua54 => "lua5.4",
257            Lua55 => "lua5.5",
258        }
259    }
260}
261
262impl Artifacts {
263    /// Returns the directory containing the Lua headers.
264    pub fn include_dir(&self) -> &Path {
265        &self.include_dir
266    }
267
268    /// Returns the directory containing the Lua libraries.
269    pub fn lib_dir(&self) -> &Path {
270        &self.lib_dir
271    }
272
273    /// Returns the names of the Lua libraries built.
274    pub fn libs(&self) -> &[String] {
275        &self.libs
276    }
277
278    /// Prints the necessary Cargo metadata for linking the Lua libraries.
279    ///
280    /// This method is typically called in a build script to inform Cargo
281    /// about the location of the Lua libraries and how to link them.
282    pub fn print_cargo_metadata(&self) {
283        println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
284        for lib in self.libs.iter() {
285            println!("cargo:rustc-link-lib=static:-bundle={lib}");
286        }
287        println!("cargo:include={}", self.include_dir.display());
288        println!("cargo:lib={}", self.lib_dir.display());
289    }
290}
291
292trait ErrorContext<T> {
293    fn context(self, f: impl FnOnce() -> String) -> Result<T, Box<dyn Error>>;
294}
295
296impl<T, E: Error> ErrorContext<T> for Result<T, E> {
297    fn context(self, f: impl FnOnce() -> String) -> Result<T, Box<dyn Error>> {
298        self.map_err(|e| format!("{}: {e}", f()).into())
299    }
300}
301
302trait AddFilesByExt {
303    fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> Result<&mut Self, Box<dyn Error>>;
304}
305
306impl AddFilesByExt for cc::Build {
307    fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> Result<&mut Self, Box<dyn Error>> {
308        for entry in fs::read_dir(dir)
309            .context(|| format!("Cannot read '{}'", dir.display()))?
310            .filter_map(|e| e.ok())
311            .filter(|e| e.path().extension() == Some(ext.as_ref()))
312        {
313            self.file(entry.path());
314        }
315        Ok(self)
316    }
317}