-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
308 lines (276 loc) · 9.61 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use cc::Build;
#[cfg(feature = "drivers")]
use std::collections::HashSet;
use std::{
env,
path::{Path, PathBuf},
};
static CONFIG_NAME: &str = "DEP_LV_CONFIG_PATH";
// See https://github.com/rust-lang/rust-bindgen/issues/687#issuecomment-450750547
#[cfg(feature = "drivers")]
#[derive(Debug)]
struct IgnoreMacros(HashSet<String>);
#[cfg(feature = "drivers")]
impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if self.0.contains(name) {
bindgen::callbacks::MacroParsingBehavior::Ignore
} else {
bindgen::callbacks::MacroParsingBehavior::Default
}
}
}
fn main() {
let project_dir = canonicalize(PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()));
let shims_dir = project_dir.join("shims");
let vendor = project_dir.join("vendor");
let lvgl_src = project_dir.join("lvgl").join("src");
#[cfg(feature = "rust_timer")]
let timer_shim = vendor.join("include").join("timer");
let font_extra_src: Option<PathBuf>;
if let Ok(v) = env::var("PWD") {
let current_dir = canonicalize(PathBuf::from(v));
font_extra_src = {
if let Ok(p) = env::var("LVGL_FONTS_DIR") {
Some(canonicalize(PathBuf::from(p)))
} else if current_dir.join("fonts").exists() {
Some(current_dir.join("fonts"))
} else {
None
}
};
} else {
font_extra_src = None
}
// Some basic defaults; SDL2 is the only driver enabled in the provided
// driver config by default
#[cfg(feature = "drivers")]
let incl_extra =
env::var("LVGL_INCLUDE").unwrap_or("/usr/include,/usr/local/include".to_string());
let cflags_extra_string = env::var("LVGL_CFLAGS").unwrap_or_default();
let cflags_extra = if cflags_extra_string.is_empty() {
None
} else {
Some(cflags_extra_string.split(','))
};
#[cfg(feature = "drivers")]
let link_extra = env::var("LVGL_LINK").unwrap_or("SDL2".to_string());
#[cfg(feature = "drivers")]
let drivers = vendor.join("lv_drivers");
let lv_config_dir = {
let conf_path = env::var(CONFIG_NAME)
.map(PathBuf::from)
.unwrap_or_else(|_| {
match std::env::var("DOCS_RS") {
Ok(_) => {
// We've detected that we are building for docs.rs
// so let's use the vendored `lv_conf.h` file.
vendor.join("include")
}
Err(_) => {
#[cfg(not(feature = "use-vendored-config"))]
panic!(
"The environment variable {} is required to be defined",
CONFIG_NAME
);
#[cfg(feature = "use-vendored-config")]
vendor.join("include")
}
}
});
if !conf_path.exists() {
panic!(
"Directory {} referenced by {} needs to exist",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}
if !conf_path.is_dir() {
panic!("{} needs to be a directory", CONFIG_NAME);
}
if !conf_path.join("lv_conf.h").exists() {
panic!(
"Directory {} referenced by {} needs to contain a file called lv_conf.h",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}
#[cfg(feature = "drivers")]
if !conf_path.join("lv_drv_conf.h").exists() {
panic!(
"Directory {} referenced by {} needs to contain a file called lv_drv_conf.h",
conf_path.to_string_lossy(),
CONFIG_NAME
);
}
if let Some(p) = &font_extra_src {
println!("cargo:rerun-if-changed={}", p.to_str().unwrap())
}
println!(
"cargo:rerun-if-changed={}",
conf_path.join("lv_conf.h").to_str().unwrap()
);
#[cfg(feature = "drivers")]
println!(
"cargo:rerun-if-changed={}",
conf_path.join("lv_drv_conf.h").to_str().unwrap()
);
conf_path
};
#[cfg(feature = "drivers")]
{
println!("cargo:rerun-if-env-changed=LVGL_INCLUDE");
println!("cargo:rerun-if-env-changed=LVGL_LINK");
}
let mut cfg = Build::new();
if let Some(p) = &font_extra_src {
add_c_files(&mut cfg, p)
}
add_c_files(&mut cfg, &lvgl_src);
add_c_files(&mut cfg, &lv_config_dir);
add_c_files(&mut cfg, &shims_dir);
#[cfg(feature = "drivers")]
add_c_files(&mut cfg, &drivers);
cfg.define("LV_CONF_INCLUDE_SIMPLE", Some("1"))
.include(&lvgl_src)
.include(&vendor)
.warnings(true)
.include(&lv_config_dir);
if let Some(p) = &font_extra_src {
cfg.include(p);
}
#[cfg(feature = "rust_timer")]
cfg.include(&timer_shim);
#[cfg(feature = "drivers")]
cfg.include(&drivers);
#[cfg(feature = "drivers")]
cfg.includes(incl_extra.split(','));
if let Some(ref cflags_extra) = cflags_extra {
cflags_extra.clone().for_each(|e| {
let mut it = e.split('=');
cfg.define(it.next().unwrap(), it.next().unwrap_or_default());
});
}
cfg.compile("lvgl");
let mut cc_args = vec![
"-DLV_CONF_INCLUDE_SIMPLE=1",
"-I",
lv_config_dir.to_str().unwrap(),
"-I",
vendor.to_str().unwrap(),
"-fvisibility=default",
];
// Set correct target triple for bindgen when cross-compiling
let target = env::var("TARGET").expect("Cargo build scripts always have TARGET");
let host = env::var("HOST").expect("Cargo build scripts always have HOST");
if target != host {
cc_args.push("-target");
cc_args.push(target.as_str());
}
let mut additional_args = Vec::new();
if target.ends_with("emscripten") {
match env::var("EMSDK") {
Ok(em_path) =>
{
additional_args.push("-I".to_string());
additional_args.push(format!(
"{}/upstream/emscripten/system/include/libc",
em_path
));
additional_args.push("-I".to_string());
additional_args.push(format!(
"{}/upstream/emscripten/system/lib/libc/musl/arch/emscripten",
em_path
));
additional_args.push("-I".to_string());
additional_args.push(format!(
"{}/upstream/emscripten/system/include/SDL",
em_path
));
}
Err(_) => panic!("The EMSDK environment variable is not set. Has emscripten been properly initialized?")
}
}
#[cfg(feature = "drivers")]
let ignored_macros = IgnoreMacros(
vec![
"FP_INFINITE".into(),
"FP_NAN".into(),
"FP_NORMAL".into(),
"FP_SUBNORMAL".into(),
"FP_ZERO".into(),
"IPPORT_RESERVED".into(),
]
.into_iter()
.collect(),
);
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings =
bindgen::Builder::default().header(shims_dir.join("lvgl_sys.h").to_str().unwrap());
let bindings = add_font_headers(bindings, &font_extra_src);
#[cfg(feature = "drivers")]
let bindings = bindings
.header(shims_dir.join("lvgl_drv.h").to_str().unwrap())
.parse_callbacks(Box::new(ignored_macros));
#[cfg(feature = "rust_timer")]
let bindings = bindings.header(shims_dir.join("rs_timer.h").to_str().unwrap());
let bindings = bindings
.generate_comments(false)
.derive_default(true)
.layout_tests(false)
.use_core()
.ctypes_prefix("core::ffi")
.clang_args(&cc_args)
.clang_args(&additional_args)
.clang_args(
cflags_extra
.map(|s| s.collect::<Vec<_>>())
.unwrap_or(Vec::new()),
)
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Can't write bindings!");
#[cfg(feature = "drivers")]
link_extra.split(',').for_each(|a| {
println!("cargo:rustc-link-lib={a}");
//println!("cargo:rustc-link-search=")
})
}
fn add_font_headers(
bindings: bindgen::Builder,
dir: &Option<impl AsRef<Path>>,
) -> bindgen::Builder {
if let Some(p) = dir {
let mut temp = bindings;
for e in p.as_ref().read_dir().unwrap() {
let e = e.unwrap();
let path = e.path();
if !e.file_type().unwrap().is_dir()
&& path.extension().and_then(|s| s.to_str()) == Some("h")
{
temp = temp.header(path.to_str().unwrap());
}
}
temp
} else {
bindings
}
}
fn add_c_files(build: &mut cc::Build, path: impl AsRef<Path>) {
for e in path.as_ref().read_dir().unwrap() {
let e = e.unwrap();
let path = e.path();
if e.file_type().unwrap().is_dir() {
add_c_files(build, e.path());
} else if path.extension().and_then(|s| s.to_str()) == Some("c") {
build.file(&path);
}
}
}
fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
let canonicalized = path.as_ref().canonicalize().unwrap();
let canonicalized = &*canonicalized.to_string_lossy();
PathBuf::from(canonicalized.strip_prefix(r"\\?\").unwrap_or(canonicalized))
}