Skip to main content

luau0_src/
lib.rs

1use std::env;
2use std::fs;
3use std::path::{Path, PathBuf};
4
5/// Represents the configuration for building Luau artifacts.
6pub struct Build {
7    out_dir: Option<PathBuf>,
8    target: Option<String>,
9    host: Option<String>,
10    // Max number of Lua stack slots that a C function can use
11    max_cstack_size: usize,
12    // Use longjmp instead of C++ exceptions
13    use_longjmp: bool,
14    // Enable code generator (jit)
15    enable_codegen: bool,
16    // Vector size, must be 3 (default) or 4
17    vector_size: usize,
18}
19
20/// Represents the artifacts produced by the build process.
21pub struct Artifacts {
22    lib_dir: PathBuf,
23    libs: Vec<String>,
24    cpp_stdlib: Option<String>,
25}
26
27impl Default for Build {
28    fn default() -> Self {
29        Build {
30            out_dir: env::var_os("OUT_DIR").map(PathBuf::from),
31            target: env::var("TARGET").ok(),
32            host: env::var("HOST").ok(),
33            max_cstack_size: 1000000,
34            use_longjmp: false,
35            enable_codegen: false,
36            vector_size: 3,
37        }
38    }
39}
40
41impl Build {
42    /// Creates a new `Build` instance with default settings.
43    pub fn new() -> Build {
44        Build::default()
45    }
46
47    /// Sets the output directory for the build artifacts.
48    ///
49    /// Default is the environment variable `OUT_DIR`.
50    pub fn out_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Build {
51        self.out_dir = Some(path.as_ref().to_path_buf());
52        self
53    }
54
55    /// Sets the target architecture for the build.
56    ///
57    /// Default is the environment variable `TARGET`.
58    pub fn target(&mut self, target: &str) -> &mut Build {
59        self.target = Some(target.to_string());
60        self
61    }
62
63    /// Sets the host architecture for the build.
64    ///
65    /// Default is the environment variable `HOST`.
66    pub fn host(&mut self, host: &str) -> &mut Build {
67        self.host = Some(host.to_string());
68        self
69    }
70
71    /// Sets the maximum number of Lua stack slots that a C function can use.
72    ///
73    /// Default is 1,000,000.
74    pub fn set_max_cstack_size(&mut self, size: usize) -> &mut Build {
75        self.max_cstack_size = size;
76        self
77    }
78
79    /// Sets whether to use longjmp instead of C++ exceptions.
80    ///
81    /// Default is false.
82    pub fn use_longjmp(&mut self, r#use: bool) -> &mut Build {
83        self.use_longjmp = r#use;
84        self
85    }
86
87    /// Sets whether to enable the code generator (JIT).
88    ///
89    /// Default is false.
90    pub fn enable_codegen(&mut self, enable: bool) -> &mut Build {
91        self.enable_codegen = enable;
92        self
93    }
94
95    /// Sets the vector size (3 or 4).
96    ///
97    /// Default is 3.
98    pub fn set_vector_size(&mut self, size: usize) -> &mut Build {
99        assert!(size == 3 || size == 4, "vector size must be 3 or 4");
100        self.vector_size = size;
101        self
102    }
103
104    /// Builds the Lua artifacts for the specified version.
105    pub fn build(&mut self) -> Artifacts {
106        let target = &self.target.as_ref().expect("TARGET is not set")[..];
107        let host = &self.host.as_ref().expect("HOST is not set")[..];
108        let out_dir = self.out_dir.as_ref().expect("OUT_DIR is not set");
109        let build_dir = out_dir.join("luau-build");
110
111        let source_base_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
112        let common_include_dir = source_base_dir.join("luau").join("Common").join("include");
113        let vm_source_dir = source_base_dir.join("luau").join("VM").join("src");
114        let vm_include_dir = source_base_dir.join("luau").join("VM").join("include");
115
116        // Cleanup
117        if build_dir.exists() {
118            fs::remove_dir_all(&build_dir).unwrap();
119        }
120
121        // Configure C++
122        let mut config = cc::Build::new();
123        config
124            .warnings(false)
125            .cargo_metadata(false)
126            .std("c++17")
127            .cpp(true);
128
129        if target.ends_with("emscripten") {
130            // Enable c++ exceptions for emscripten (it's disabled by default)
131            // Later we should switch to wasm exceptions
132            config.flag_if_supported("-fexceptions");
133        }
134
135        // Common defines
136        config.define("LUAI_MAXCSTACK", &*self.max_cstack_size.to_string());
137        config.define("LUA_VECTOR_SIZE", &*self.vector_size.to_string());
138        config.define("LUA_API", "extern \"C\"");
139
140        if self.use_longjmp {
141            config.define("LUA_USE_LONGJMP", "1");
142        }
143
144        if cfg!(debug_assertions) {
145            config.define("LUAU_ENABLE_ASSERT", None);
146        } else {
147            // this flag allows compiler to lower sqrt() into a single CPU instruction
148            config.flag_if_supported("-fno-math-errno");
149        }
150
151        config.include(&common_include_dir);
152
153        // Build `Ast` library
154        let ast_lib_name = "luauast";
155        let ast_source_dir = source_base_dir.join("luau").join("Ast").join("src");
156        let ast_include_dir = source_base_dir.join("luau").join("Ast").join("include");
157        config
158            .clone()
159            .include(&ast_include_dir)
160            .add_files_by_ext_sorted(&ast_source_dir, "cpp")
161            .out_dir(&build_dir)
162            .compile(ast_lib_name);
163
164        // Build `CodeGen` library
165        let codegen_lib_name = "luaucodegen";
166        let codegen_source_dir = source_base_dir.join("luau").join("CodeGen").join("src");
167        let codegen_include_dir = source_base_dir.join("luau").join("CodeGen").join("include");
168        if self.enable_codegen {
169            if target.ends_with("emscripten") {
170                panic!("codegen (jit) is not supported on emscripten");
171            }
172
173            config
174                .clone()
175                .include(&codegen_include_dir)
176                .include(&vm_include_dir)
177                .include(&vm_source_dir)
178                .define("LUACODEGEN_API", "extern \"C\"")
179                .add_files_by_ext_sorted(&codegen_source_dir, "cpp")
180                .out_dir(&build_dir)
181                .compile(codegen_lib_name);
182        }
183
184        // Build `Common` library
185        let common_lib_name = "luaucommon";
186        let common_source_dir = source_base_dir.join("luau").join("Common").join("src");
187        let common_include_dir = (source_base_dir.join("luau").join("Common")).join("include");
188        config
189            .clone()
190            .include(&common_include_dir)
191            .add_files_by_ext_sorted(&common_source_dir, "cpp")
192            .out_dir(&build_dir)
193            .compile(common_lib_name);
194
195        // Build `Compiler` library
196        let compiler_lib_name = "luaucompiler";
197        let compiler_source_dir = source_base_dir.join("luau").join("Compiler").join("src");
198        let compiler_include_dir = (source_base_dir.join("luau").join("Compiler")).join("include");
199        config
200            .clone()
201            .include(&compiler_include_dir)
202            .include(&ast_include_dir)
203            .define("LUACODE_API", "extern \"C\"")
204            .add_files_by_ext_sorted(&compiler_source_dir, "cpp")
205            .out_dir(&build_dir)
206            .compile(compiler_lib_name);
207
208        // Build `Config` library
209        let config_lib_name = "luauconfig";
210        let config_source_dir = source_base_dir.join("luau").join("Config").join("src");
211        let config_include_dir = source_base_dir.join("luau").join("Config").join("include");
212        config
213            .clone()
214            .include(&config_include_dir)
215            .include(&ast_include_dir)
216            .include(&compiler_include_dir)
217            .include(&vm_include_dir)
218            .add_files_by_ext_sorted(&config_source_dir, "cpp")
219            .out_dir(&build_dir)
220            .compile(config_lib_name);
221
222        // Build customization library
223        let custom_lib_name = "luaucustom";
224        let custom_source_dir = source_base_dir.join("luau").join("Custom").join("src");
225        config
226            .clone()
227            .include(&vm_include_dir)
228            .include(&vm_source_dir)
229            .add_files_by_ext_sorted(&custom_source_dir, "cpp")
230            .out_dir(&build_dir)
231            .compile(custom_lib_name);
232
233        // Build `Require` library
234        let require_lib_name = "luaurequire";
235        let require_source_dir = source_base_dir.join("luau").join("Require").join("src");
236        let require_include_dir = source_base_dir.join("luau").join("Require").join("include");
237        config
238            .clone()
239            .include(&require_include_dir)
240            .include(&ast_include_dir)
241            .include(&config_include_dir)
242            .include(&vm_include_dir)
243            .add_files_by_ext_sorted(&require_source_dir, "cpp")
244            .out_dir(&build_dir)
245            .compile(require_lib_name);
246
247        // Build VM
248        let vm_lib_name = "luauvm";
249        config
250            .clone()
251            .include(&vm_include_dir)
252            .add_files_by_ext_sorted(&vm_source_dir, "cpp")
253            .out_dir(&build_dir)
254            .compile(vm_lib_name);
255
256        let mut artifacts = Artifacts {
257            lib_dir: build_dir,
258            libs: vec![
259                vm_lib_name.to_string(),
260                compiler_lib_name.to_string(),
261                ast_lib_name.to_string(),
262                common_lib_name.to_string(),
263                config_lib_name.to_string(),
264                custom_lib_name.to_string(),
265                require_lib_name.to_string(),
266            ],
267            cpp_stdlib: Self::get_cpp_link_stdlib(target, host),
268        };
269
270        if self.enable_codegen {
271            artifacts.libs.push(codegen_lib_name.to_string());
272        }
273
274        artifacts
275    }
276
277    /// Returns the C++ standard library:
278    /// 1) Uses `CXXSTDLIB` environment variable if set
279    /// 2) The default `c++` for OS X and BSDs
280    /// 3) `c++_shared` for Android
281    /// 4) `None` for MSVC
282    /// 5) `stdc++` for anything else.
283    ///
284    /// Inspired by the `cc` crate.
285    fn get_cpp_link_stdlib(target: &str, host: &str) -> Option<String> {
286        // Try to get value from the `CXXSTDLIB` env variable
287        let kind = if host == target { "HOST" } else { "TARGET" };
288        let res = env::var(format!("CXXSTDLIB_{target}"))
289            .or_else(|_| env::var(format!("CXXSTDLIB_{}", target.replace('-', "_"))))
290            .or_else(|_| env::var(format!("{kind}_CXXSTDLIB")))
291            .or_else(|_| env::var("CXXSTDLIB"))
292            .ok();
293        if res.is_some() {
294            return res;
295        }
296
297        if target.contains("msvc") {
298            None
299        } else if target.contains("apple") | target.contains("freebsd") | target.contains("openbsd")
300        {
301            Some("c++".to_string())
302        } else if target.contains("android") {
303            Some("c++_shared".to_string())
304        } else {
305            Some("stdc++".to_string())
306        }
307    }
308}
309
310impl Artifacts {
311    pub fn lib_dir(&self) -> &Path {
312        &self.lib_dir
313    }
314
315    pub fn libs(&self) -> &[String] {
316        &self.libs
317    }
318
319    pub fn print_cargo_metadata(&self) {
320        println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
321        for lib in &self.libs {
322            println!("cargo:rustc-link-lib=static={lib}");
323        }
324        if let Some(ref cpp_stdlib) = self.cpp_stdlib {
325            println!("cargo:rustc-link-lib={cpp_stdlib}");
326        }
327        if let Some(version) = self.version() {
328            println!("cargo:rustc-env=LUAU_VERSION={version}");
329        }
330    }
331
332    pub fn version(&self) -> Option<String> {
333        let pkg_version = env!("CARGO_PKG_VERSION");
334        let (_, luau_version) = pkg_version.split_once("+luau")?;
335        Some(format!("0.{luau_version}"))
336    }
337}
338
339trait AddFilesByExt {
340    fn add_files_by_ext_sorted(&mut self, dir: &Path, ext: &str) -> &mut Self;
341}
342
343impl AddFilesByExt for cc::Build {
344    // It's important to keep the order of the files to get consistent builds between machines
345    // if the order is not always the same, the final binary produces a different SHA256 which
346    // might cause issues if one needs to verify which binary is being executed
347    fn add_files_by_ext_sorted(&mut self, dir: &Path, ext: &str) -> &mut Self {
348        let mut sources: Vec<_> = fs::read_dir(dir)
349            .unwrap()
350            .filter_map(|e| e.ok())
351            .filter(|e| e.path().extension() == Some(ext.as_ref()))
352            .map(|e| e.path())
353            .collect();
354
355        // Sort for determinism
356        sources.sort();
357
358        for source in sources {
359            self.file(source);
360        }
361
362        self
363    }
364}