generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
build.rs
244 lines (230 loc) · 7.84 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
extern crate napi_build;
use std::env;
use std::path;
use std::process;
fn main() {
println!("cargo:rerun-if-env-changed=SKIA_DIR");
println!("cargo:rerun-if-env-changed=SKIA_LIB_DIR");
println!("cargo:rerun-if-changed=skia-c/skia_c.cpp");
println!("cargo:rerun-if-changed=skia-c/skia_c.hpp");
let compile_target = env::var("TARGET").expect("TARGET");
let compile_target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS");
let compile_target_env = env::var("CARGO_CFG_TARGET_ENV").expect("CARGO_CFG_TARGET_ENV");
let compile_target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH");
match compile_target_os.as_str() {
"windows" => {
env::set_var("CC", "clang-cl");
env::set_var("CXX", "clang-cl");
}
_ => {
if env::var("CC").is_err() {
env::set_var("CC", "clang");
}
if env::var("CXX").is_err() {
env::set_var("CXX", "clang++");
}
}
}
let skia_dir = env::var("SKIA_DIR").unwrap_or_else(|_| "./skia".to_owned());
let skia_path = path::Path::new(&skia_dir);
let skia_lib_dir = env::var("SKIA_LIB_DIR").unwrap_or_else(|_| "./skia/out/Static".to_owned());
let mut build = cc::Build::new();
build.cpp(true).file("skia-c/skia_c.cpp");
match compile_target.as_str() {
"aarch64-unknown-linux-musl" => {
let gcc_version = String::from_utf8(
process::Command::new("ls")
.arg("/aarch64-linux-musl-cross/aarch64-linux-musl/include/c++")
.output()
.unwrap()
.stdout,
)
.unwrap();
let gcc_version_trim = gcc_version.trim();
build
.include("/aarch64-linux-musl-cross/aarch64-linux-musl/include")
.include(format!(
"/aarch64-linux-musl-cross/aarch64-linux-musl/include/c++/{gcc_version_trim}"
))
.include(format!(
"/aarch64-linux-musl-cross/aarch64-linux-musl/include/c++/{gcc_version_trim}/aarch64-linux-musl"
));
}
"x86_64-unknown-linux-musl" => {
let gcc_version = String::from_utf8(
process::Command::new("ls")
.arg("/usr/include/c++")
.output()
.unwrap()
.stdout,
)
.unwrap();
let gcc_version_trim = gcc_version.trim();
build
.static_flag(true)
.include("/usr/include")
.include(format!("/usr/include/c++/{gcc_version_trim}"))
.include(format!(
"/usr/include/c++/{gcc_version_trim}/x86_64-alpine-linux-musl"
));
}
"aarch64-linux-android" => {
let nkd_home = env::var("ANDROID_NDK_LATEST_HOME").unwrap();
let host = if cfg!(target_os = "windows") {
"windows"
} else if cfg!(target_os = "macos") {
"darwin"
} else if cfg!(target_os = "linux") {
"linux"
} else {
panic!("Unsupported host OS");
};
env::set_var(
"CC",
format!(
"{nkd_home}/toolchains/llvm/prebuilt/{host}-x86_64/bin/aarch64-linux-android24-clang"
)
.as_str(),
);
env::set_var(
"CXX",
format!(
"{nkd_home}/toolchains/llvm/prebuilt/{host}-x86_64/bin/aarch64-linux-android24-clang++"
)
.as_str(),
);
build
.include(
format!(
"{nkd_home}/toolchains/llvm/prebuilt/{host}-x86_64/sysroot/usr/include"
)
.as_str(),
)
.include(
format!(
"{nkd_home}/toolchains/llvm/prebuilt/{host}-x86_64/sysroot/usr/include/c++/v1"
)
.as_str(),
)
.include(
format!(
"{nkd_home}/toolchains/llvm/prebuilt/{host}-x86_64/sysroot/usr/include/aarch64-linux-android"
)
.as_str(),
)
.archiver(
format!(
"{nkd_home}/toolchains/llvm/prebuilt/{host}-x86_64/bin/llvm-ar"
)
.as_str(),
);
}
_ => {}
}
if compile_target_os != "windows" {
build
.flag("-std=c++17")
.flag("-fPIC")
.flag("-fno-exceptions")
.flag("-fno-rtti")
.flag("-fstrict-aliasing")
.flag("-fvisibility=hidden")
.flag("-fvisibility-inlines-hidden")
.flag("-fdata-sections")
.flag("-ffunction-sections")
.flag("-Wno-unused-function")
.flag("-Wno-unused-parameter");
}
match compile_target_os.as_str() {
"windows" => {
build
.flag("/std:c++17")
.flag("-Wno-unused-function")
.flag("-Wno-unused-parameter")
.static_crt(true);
}
"linux" => {
if compile_target_arch != "arm" {
println!("cargo:rustc-cdylib-link-arg=-Wl,--allow-multiple-definition");
}
if compile_target_env != "gnu" {
build.cpp_set_stdlib("stdc++");
} else {
build.cpp_set_stdlib("c++").flag("-static");
println!("cargo:rustc-link-lib=static=c++");
match compile_target_arch.as_str() {
"aarch64" => {
build
.include(
"/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot/usr/include",
)
.flag("--sysroot=/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot");
println!("cargo:rustc-link-search=/usr/aarch64-unknown-linux-gnu/lib/llvm-18/lib");
println!("cargo:rustc-link-search=/usr/aarch64-unknown-linux-gnu/lib");
println!("cargo:rustc-link-search=/usr/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot/lib");
println!("cargo:rustc-link-search=/usr/aarch64-unknown-linux-gnu/lib/gcc/aarch64-unknown-linux-gnu/4.8.5");
}
"x86_64" => {
build.include("/usr/lib/llvm-18/include/c++/v1");
println!("cargo:rustc-link-search=/usr/lib/llvm-18/lib");
}
"arm" => {
let gcc_version = String::from_utf8(
process::Command::new("ls")
.arg("/usr/arm-linux-gnueabihf/include/c++")
.output()
.unwrap()
.stdout,
)
.unwrap();
let gcc_version_trim = gcc_version.trim();
build
.include("/usr/arm-linux-gnueabihf/include")
.include(format!(
"/usr/arm-linux-gnueabihf/include/c++/${gcc_version_trim}/arm-linux-gnueabihf"
));
const CROSS_LIB_PATH: &str = "/usr/lib/gcc-cross/arm-linux-gnueabihf";
if let Ok(version) = std::process::Command::new("ls")
.arg(CROSS_LIB_PATH)
.output()
.map(|o| String::from_utf8(o.stdout).unwrap().trim().to_string())
{
println!("cargo:rustc-link-search={CROSS_LIB_PATH}/{version}");
};
println!("cargo:rustc-link-search=/usr/arm-linux-gnueabihf/lib");
println!("cargo:rustc-link-search=/usr/arm-linux-gnueabihf/lib/llvm-18/lib");
}
_ => {}
}
}
}
"macos" => {
build.cpp_set_stdlib("c++");
if compile_target_arch == "aarch64" {
build.flag("-mmacosx-version-min=11.0");
} else {
build.flag("-mmacosx-version-min=10.13");
}
println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=framework=ApplicationServices");
}
"android" => {
build.cpp_set_stdlib("c++").flag("-static");
println!("cargo:rustc-link-lib=static=c++");
println!("cargo:rustc-cdylib-link-arg=-Wl,--allow-multiple-definition");
}
_ => {}
}
let out_dir = env::var("OUT_DIR").unwrap();
build
.include("./skia-c")
.include(skia_path)
// https://github.com/rust-lang/rust/pull/93901#issuecomment-1119360260
.cargo_metadata(false)
.out_dir(&out_dir)
.compile("skiac");
println!("cargo:rustc-link-search={skia_lib_dir}");
println!("cargo:rustc-link-search={}", &out_dir);
println!("cargo:rustc-link-lib=static=skshaper");
napi_build::setup();
}