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 include_dir = out_dir.join("include");
111
112        if !include_dir.exists() {
113            fs::create_dir_all(&include_dir)
114                .context(|| format!("Cannot create '{}'", include_dir.display()))?;
115        }
116
117        let mut config = cc::Build::new();
118        config.warnings(false).cargo_metadata(false).target(target);
119
120        match &self.host {
121            Some(host) => {
122                config.host(host);
123            }
124            // Host will be taken from the environment variable
125            None if env::var("HOST").is_ok() => {}
126            None => {
127                // If called outside of build script, set default host
128                config.host(target);
129            }
130        }
131
132        match target {
133            _ if target.contains("linux") => {
134                config.define("LUA_USE_LINUX", None);
135            }
136            _ if target.ends_with("bsd") => {
137                config.define("LUA_USE_LINUX", None);
138            }
139            _ if target.contains("apple-darwin") => {
140                match version {
141                    Lua51 => config.define("LUA_USE_LINUX", None),
142                    _ => config.define("LUA_USE_MACOSX", None),
143                };
144            }
145            _ if target.contains("apple-ios") => {
146                match version {
147                    Lua54 => config.define("LUA_USE_IOS", None),
148                    _ => config.define("LUA_USE_POSIX", None),
149                };
150            }
151            _ if target.contains("windows") => {
152                // Defined in Lua >= 5.3
153                config.define("LUA_USE_WINDOWS", None);
154            }
155            _ if target.ends_with("emscripten") => {
156                config
157                    .define("LUA_USE_POSIX", None)
158                    .cpp(true)
159                    .flag("-fexceptions"); // Enable exceptions to be caught
160
161                let cpp_source_dir = out_dir.join("cpp_source");
162                if cpp_source_dir.exists() {
163                    fs::remove_dir_all(&cpp_source_dir)
164                        .context(|| format!("Cannot remove '{}'", cpp_source_dir.display()))?;
165                }
166                fs::create_dir_all(&cpp_source_dir)
167                    .context(|| format!("Cannot create '{}'", cpp_source_dir.display()))?;
168
169                for file in fs::read_dir(&source_dir)
170                    .context(|| format!("Cannot read '{}'", source_dir.display()))?
171                {
172                    let file = file?;
173                    let filename = file.file_name();
174                    let filename = &*filename.to_string_lossy();
175                    let src_file = source_dir.join(file.file_name());
176                    let dst_file = cpp_source_dir.join(file.file_name());
177
178                    let mut content = fs::read(&src_file)
179                        .context(|| format!("Cannot read '{}'", src_file.display()))?;
180                    if ["lauxlib.h", "lua.h", "lualib.h"].contains(&filename) {
181                        content.splice(0..0, b"extern \"C\" {\n".to_vec());
182                        content.extend(b"\n}".to_vec())
183                    }
184                    fs::write(&dst_file, content)
185                        .context(|| format!("Cannot write to '{}'", dst_file.display()))?;
186                }
187                source_dir = cpp_source_dir
188            }
189            _ => Err(format!("don't know how to build Lua for {target}"))?,
190        }
191
192        if let Lua54 = version {
193            config.define("LUA_COMPAT_5_3", None);
194            #[cfg(feature = "ucid")]
195            config.define("LUA_UCID", None);
196        }
197
198        let debug = self.debug.unwrap_or(cfg!(debug_assertions));
199        if debug {
200            config.define("LUA_USE_APICHECK", None);
201            config.debug(true);
202        }
203
204        match &self.opt_level {
205            Some(opt_level) => {
206                config.opt_level_str(opt_level);
207            }
208            // Opt level will be taken from the environment variable
209            None if env::var("OPT_LEVEL").is_ok() => {}
210            None => {
211                // If called outside of build script, set default opt level
212                config.opt_level(if debug { 0 } else { 2 });
213            }
214        }
215
216        config
217            .include(&source_dir)
218            .flag("-w") // Suppress all warnings
219            .flag_if_supported("-fno-common") // Compile common globals like normal definitions
220            .add_files_by_ext(&source_dir, "c")?
221            .out_dir(out_dir)
222            .try_compile(version.lib_name())?;
223
224        for f in &["lauxlib.h", "lua.h", "luaconf.h", "lualib.h"] {
225            let from = source_dir.join(f);
226            let to = include_dir.join(f);
227            fs::copy(&from, &to)
228                .context(|| format!("Cannot copy '{}' to '{}'", from.display(), to.display()))?;
229        }
230
231        Ok(Artifacts {
232            include_dir,
233            lib_dir: out_dir.clone(),
234            libs: vec![version.lib_name().to_string()],
235        })
236    }
237}
238
239impl Version {
240    fn source_dir(&self) -> &str {
241        match self {
242            Lua51 => "lua-5.1.5",
243            Lua52 => "lua-5.2.4",
244            Lua53 => "lua-5.3.6",
245            Lua54 => "lua-5.4.8",
246        }
247    }
248
249    fn lib_name(&self) -> &str {
250        match self {
251            Lua51 => "lua5.1",
252            Lua52 => "lua5.2",
253            Lua53 => "lua5.3",
254            Lua54 => "lua5.4",
255        }
256    }
257}
258
259impl Artifacts {
260    /// Returns the directory containing the Lua headers.
261    pub fn include_dir(&self) -> &Path {
262        &self.include_dir
263    }
264
265    /// Returns the directory containing the Lua libraries.
266    pub fn lib_dir(&self) -> &Path {
267        &self.lib_dir
268    }
269
270    /// Returns the names of the Lua libraries built.
271    pub fn libs(&self) -> &[String] {
272        &self.libs
273    }
274
275    /// Prints the necessary Cargo metadata for linking the Lua libraries.
276    ///
277    /// This method is typically called in a build script to inform Cargo
278    /// about the location of the Lua libraries and how to link them.
279    pub fn print_cargo_metadata(&self) {
280        println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
281        for lib in self.libs.iter() {
282            println!("cargo:rustc-link-lib=static={lib}");
283        }
284    }
285}
286
287trait ErrorContext<T> {
288    fn context(self, f: impl FnOnce() -> String) -> Result<T, Box<dyn Error>>;
289}
290
291impl<T, E: Error> ErrorContext<T> for Result<T, E> {
292    fn context(self, f: impl FnOnce() -> String) -> Result<T, Box<dyn Error>> {
293        self.map_err(|e| format!("{}: {e}", f()).into())
294    }
295}
296
297trait AddFilesByExt {
298    fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> Result<&mut Self, Box<dyn Error>>;
299}
300
301impl AddFilesByExt for cc::Build {
302    fn add_files_by_ext(&mut self, dir: &Path, ext: &str) -> Result<&mut Self, Box<dyn Error>> {
303        for entry in fs::read_dir(dir)
304            .context(|| format!("Cannot read '{}'", dir.display()))?
305            .filter_map(|e| e.ok())
306            .filter(|e| e.path().extension() == Some(ext.as_ref()))
307        {
308            self.file(entry.path());
309        }
310        Ok(self)
311    }
312}