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}
14pub use self::Version::*;
15
16/// Represents the configuration for building Lua artifacts.
17pub struct Build {
18    out_dir: Option<PathBuf>,
19    target: Option<String>,
20    host: Option<String>,
21    opt_level: Option<String>,
22    debug: Option<bool>,
23}
24
25/// Represents the artifacts produced by the build process.
26#[derive(Clone, Debug)]
27pub struct Artifacts {
28    include_dir: PathBuf,
29    lib_dir: PathBuf,
30    libs: Vec<String>,
31}
32
33impl Default for Build {
34    fn default() -> Build {
35        Build {
36            out_dir: env::var_os("OUT_DIR").map(PathBuf::from),
37            target: env::var("TARGET").ok(),
38            host: None,
39            opt_level: None,
40            debug: None,
41        }
42    }
43}
44
45impl Build {
46    /// Creates a new `Build` instance with default settings.
47    pub fn new() -> Build {
48        Build::default()
49    }
50
51    /// Sets the output directory for the build artifacts.
52    ///
53    /// This is required if called outside of a build script.
54    pub fn out_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Build {
55        self.out_dir = Some(path.as_ref().to_path_buf());
56        self
57    }
58
59    /// Sets the target architecture for the build.
60    ///
61    /// This is required if called outside of a build script.
62    pub fn target(&mut self, target: &str) -> &mut Build {
63        self.target = Some(target.to_string());
64        self
65    }
66
67    /// Sets the host architecture for the build.
68    ///
69    /// This is optional and will default to the environment variable `HOST` if not set.
70    /// If called outside of a build script, it will default to the target architecture.
71    pub fn host(&mut self, host: &str) -> &mut Build {
72        self.host = Some(host.to_string());
73        self
74    }
75
76    /// Sets the optimization level for the build.
77    ///
78    /// This is optional and will default to the environment variable `OPT_LEVEL` if not set.
79    /// If called outside of a build script, it will default to `0` in debug mode and `2` otherwise.
80    pub fn opt_level(&mut self, opt_level: &str) -> &mut Build {
81        self.opt_level = Some(opt_level.to_string());
82        self
83    }
84
85    /// Sets whether to build in debug mode.
86    ///
87    /// This is optional and will default to the value of `cfg!(debug_assertions)`.
88    /// If set to `true`, it also enables Lua API checks.
89    pub fn debug(&mut self, debug: bool) -> &mut Build {
90        self.debug = Some(debug);
91        self
92    }
93
94    /// Builds the Lua artifacts for the specified version.
95    pub fn build(&self, version: Version) -> Artifacts {
96        match self.try_build(version) {
97            Ok(artifacts) => artifacts,
98            Err(err) => panic!("{err}"),
99        }
100    }
101
102    /// Attempts to build the Lua artifacts for the specified version.
103    ///
104    /// Returns an error if the build fails.
105    pub fn try_build(&self, version: Version) -> Result<Artifacts, Box<dyn Error>> {
106        let target = self.target.as_ref().ok_or("TARGET is not set")?;
107        let out_dir = self.out_dir.as_ref().ok_or("OUT_DIR is not set")?;
108        let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
109        let mut source_dir = manifest_dir.join(version.source_dir());
110        let lib_dir = out_dir.join("lib");
111        let include_dir = out_dir.join("include");
112
113        if !include_dir.exists() {
114            fs::create_dir_all(&include_dir)
115                .context(|| format!("Cannot create '{}'", include_dir.display()))?;
116        }
117
118        let mut config = cc::Build::new();
119        config.warnings(false).cargo_metadata(false).target(target);
120
121        match &self.host {
122            Some(host) => {
123                config.host(host);
124            }
125            // Host will be taken from the environment variable
126            None if env::var("HOST").is_ok() => {}
127            None => {
128                // If called outside of build script, set default host
129                config.host(target);
130            }
131        }
132
133        match target {
134            _ if target.contains("linux") => {
135                config.define("LUA_USE_LINUX", None);
136            }
137            _ if target.ends_with("bsd") => {
138                config.define("LUA_USE_LINUX", None);
139            }
140            _ if target.contains("apple-darwin") => {
141                match version {
142                    Lua51 => config.define("LUA_USE_LINUX", None),
143                    _ => config.define("LUA_USE_MACOSX", None),
144                };
145            }
146            _ if target.contains("apple-ios") => {
147                match version {
148                    Lua54 => config.define("LUA_USE_IOS", None),
149                    _ => config.define("LUA_USE_POSIX", None),
150                };
151            }
152            _ if target.contains("windows") => {
153                // Defined in Lua >= 5.3
154                config.define("LUA_USE_WINDOWS", None);
155            }
156            _ if target.ends_with("emscripten") => {
157                config
158                    .define("LUA_USE_POSIX", None)
159                    .cpp(true)
160                    .flag("-fexceptions"); // Enable exceptions to be caught
161
162                let cpp_source_dir = out_dir.join("cpp_source");
163                if cpp_source_dir.exists() {
164                    fs::remove_dir_all(&cpp_source_dir)
165                        .context(|| format!("Cannot remove '{}'", cpp_source_dir.display()))?;
166                }
167                fs::create_dir_all(&cpp_source_dir)
168                    .context(|| format!("Cannot create '{}'", cpp_source_dir.display()))?;
169
170                for file in fs::read_dir(&source_dir)
171                    .context(|| format!("Cannot read '{}'", source_dir.display()))?
172                {
173                    let file = file?;
174                    let filename = file.file_name();
175                    let filename = &*filename.to_string_lossy();
176                    let src_file = source_dir.join(file.file_name());
177                    let dst_file = cpp_source_dir.join(file.file_name());
178
179                    let mut content = fs::read(&src_file)
180                        .context(|| format!("Cannot read '{}'", src_file.display()))?;
181                    if ["lauxlib.h", "lua.h", "lualib.h"].contains(&filename) {
182                        content.splice(0..0, b"extern \"C\" {\n".to_vec());
183                        content.extend(b"\n}".to_vec())
184                    }
185                    fs::write(&dst_file, content)
186                        .context(|| format!("Cannot write to '{}'", dst_file.display()))?;
187                }
188                source_dir = cpp_source_dir
189            }
190            _ => Err(format!("don't know how to build Lua for {target}"))?,
191        }
192
193        if let Lua54 = version {
194            config.define("LUA_COMPAT_5_3", None);
195            #[cfg(feature = "ucid")]
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            .flag("-w") // 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: vec![version.lib_name().to_string()],
236        })
237    }
238}
239
240impl Version {
241    fn source_dir(&self) -> &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        }
248    }
249
250    fn lib_name(&self) -> &str {
251        match self {
252            Lua51 => "lua5.1",
253            Lua52 => "lua5.2",
254            Lua53 => "lua5.3",
255            Lua54 => "lua5.4",
256        }
257    }
258}
259
260impl Artifacts {
261    /// Returns the directory containing the Lua headers.
262    pub fn include_dir(&self) -> &Path {
263        &self.include_dir
264    }
265
266    /// Returns the directory containing the Lua libraries.
267    pub fn lib_dir(&self) -> &Path {
268        &self.lib_dir
269    }
270
271    /// Returns the names of the Lua libraries built.
272    pub fn libs(&self) -> &[String] {
273        &self.libs
274    }
275
276    /// Prints the necessary Cargo metadata for linking the Lua libraries.
277    ///
278    /// This method is typically called in a build script to inform Cargo
279    /// about the location of the Lua libraries and how to link them.
280    pub fn print_cargo_metadata(&self) {
281        println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
282        for lib in self.libs.iter() {
283            println!("cargo:rustc-link-lib=static={lib}");
284        }
285    }
286}
287
288trait ErrorContext<T> {
289    fn context(self, f: impl FnOnce() -> String) -> Result<T, Box<dyn Error>>;
290}
291
292impl<T, E: Error> ErrorContext<T> for Result<T, E> {
293    fn context(self, f: impl FnOnce() -> String) -> Result<T, Box<dyn Error>> {
294        self.map_err(|e| format!("{}: {e}", f()).into())
295    }
296}
297
298trait AddFilesByExt {
299    fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> Result<&mut Self, Box<dyn Error>>;
300}
301
302impl AddFilesByExt for cc::Build {
303    fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> Result<&mut Self, Box<dyn Error>> {
304        for entry in fs::read_dir(dir)
305            .context(|| format!("Cannot read '{}'", dir.display()))?
306            .filter_map(|e| e.ok())
307            .filter(|e| e.path().extension() == Some(ext.as_ref()))
308        {
309            self.file(entry.path());
310        }
311        Ok(self)
312    }
313}