1use std::env;
2use std::error::Error;
3use std::fs;
4use std::path::{Path, PathBuf};
5
6#[derive(Debug, PartialEq, Eq)]
8pub enum Version {
9 Lua51,
10 Lua52,
11 Lua53,
12 Lua54,
13 Lua55,
14}
15pub use self::Version::*;
16
17pub 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#[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 pub fn new() -> Build {
49 Build::default()
50 }
51
52 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 pub fn target(&mut self, target: &str) -> &mut Build {
64 self.target = Some(target.to_string());
65 self
66 }
67
68 pub fn host(&mut self, host: &str) -> &mut Build {
73 self.host = Some(host.to_string());
74 self
75 }
76
77 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 pub fn debug(&mut self, debug: bool) -> &mut Build {
91 self.debug = Some(debug);
92 self
93 }
94
95 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 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 None if env::var("HOST").is_ok() => {}
128 None => {
129 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 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"); }
169 _ if target.contains("wasi") => {
170 config.define("LUA_USE_POSIX", None);
173
174 config.define("_WASI_EMULATED_SIGNAL", None);
178 libs.push("wasi-emulated-signal".to_string());
179
180 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 None if env::var("OPT_LEVEL").is_ok() => {}
211 None => {
212 config.opt_level(if debug { 0 } else { 2 });
214 }
215 }
216
217 config
218 .include(&source_dir)
219 .warnings(false) .flag_if_supported("-fno-common") .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 pub fn include_dir(&self) -> &Path {
265 &self.include_dir
266 }
267
268 pub fn lib_dir(&self) -> &Path {
270 &self.lib_dir
271 }
272
273 pub fn libs(&self) -> &[String] {
275 &self.libs
276 }
277
278 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}