-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
160 lines (133 loc) · 4.92 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// luau-rs - Rust bindings to Roblox's Luau
// Copyright (C) 2021 LoganDark
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU General Public License as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
fn main() {
#[cfg(any(feature = "ast", feature = "compiler", feature = "analysis", feature = "vm"))]
let out_dir = std::env::var("OUT_DIR").expect("couldn't find output directory");
{ // build luau with cmake
eprintln!("building Luau...");
let destination = {
let mut config = cmake::Config::new("luau");
config.define("LUAU_BUILD_CLI", "OFF")
.define("LUAU_BUILD_TESTS", "OFF")
.no_build_target(true);
// Windows is special, and needs to have an extra flag defined
// This is due to cmake-rs wiping them for no reason
// Luau needs unwinding in order for exceptions to work (obviously)
#[cfg(target_os = "windows")]
config.cxxflag("/EHsc");
config.build()
};
eprintln!("successfully built Luau");
#[cfg(not(target_os = "windows"))]
println!("cargo:rustc-link-search=native={}/build", destination.display());
// Windows is once again special and outputs libs in even more subdirs
#[cfg(target_os = "windows")] {
println!("cargo:rustc-link-search=native={}/build/Debug", destination.display());
println!("cargo:rustc-link-search=native={}/build/Release", destination.display());
}
macro_rules! link {
($feat:literal, $module:literal) => {
#[cfg(feature = $feat)]
println!(concat!("cargo:rustc-link-lib=static=Luau.", $module));
}
}
// link to C++ stdlib, unless we're on windows, which is special
#[cfg(all(not(target_os = "windows"), any(feature = "ast", feature = "compiler", feature = "analysis", feature = "vm")))]
println!("cargo:rustc-link-lib=stdc++");
// link only to requested features
link!("ast", "Ast");
link!("compiler", "Compiler");
link!("analysis", "Analysis");
link!("vm", "VM");
}
#[cfg(feature = "vm")] { // generate bindings to what we can
bindgen::builder()
.clang_arg("-Iluau/VM/include")
.clang_arg("-std=c++17")
.header("vm.hpp")
.generate()
.expect("couldn't generate Luau VM bindings")
.write_to_file(format!("{}/vm.rs", out_dir))
.expect("couldn't write Luau VM bindings to file");
}
#[cfg(any(feature = "ast", feature = "compiler", feature = "analysis", feature = "vm"))] {
eprintln!("compiling glue...");
// cc links automatically
let mut build = cc::Build::new();
build.flag("-Iluau/Ast/include")
.flag("-Iluau/Compiler/include")
.flag("-Iluau/Analysis/include")
.flag("-Iluau/VM/include")
// MSVC requires /std:c++latest for designated initializers
// I quite like them so I'm not going to stop using them
// Windows being special for the fourth time
.flag(if cfg!(target_os = "windows") { "/std:c++latest" } else { "-std=c++17" });
macro_rules! build_glue {
($feat:literal) => {
build.file(concat!("src/glue/", $feat, ".cpp"));
}
}
macro_rules! maybe_build_glue {
($feat:literal) => {
// cc links to it automatically
#[cfg(feature = $feat)]
build_glue!($feat);
}
}
// compile in glue code
#[cfg(any(feature = "ast", feature = "compiler", feature = "analysis", feature = "vm"))]
build_glue!("common");
maybe_build_glue!("ast");
maybe_build_glue!("compiler");
maybe_build_glue!("analysis");
maybe_build_glue!("vm");
build.compile("gluau");
eprintln!("compiled and linked to glue");
}
#[cfg(any(feature = "ast", feature = "compiler", feature = "analysis", feature = "vm"))] {
let mut glue_builder = bindgen::builder()
.clang_args([
"-Iluau/Ast/include",
"-Iluau/Compiler/include",
"-Iluau/Analysis/include",
"-Iluau/VM/include"
]);
#[allow(deprecated)] { // don't care about your unnecessary renaming
glue_builder = glue_builder.whitelist_function("gluau_.*");
}
macro_rules! bind_glue {
($builder:ident, $feat:literal) => {{
$builder = $builder.header(concat!("src/glue/", $feat, ".h"));
}}
}
macro_rules! maybe_bind_glue {
($builder:ident, $feat:literal) => {
#[cfg(feature = $feat)]
bind_glue!($builder, $feat);
}
}
bind_glue!(glue_builder, "common");
maybe_bind_glue!(glue_builder, "ast");
maybe_bind_glue!(glue_builder, "compiler");
maybe_bind_glue!(glue_builder, "analysis");
maybe_bind_glue!(glue_builder, "vm");
glue_builder
.generate()
.expect("couldn't generate Luau glue bindings")
.write_to_file(format!("{}/glue.rs", out_dir))
.expect("couldn't write Luau glue bindings to file");
}
// all should be good
}