-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbuild.rs
127 lines (116 loc) · 4.19 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
extern crate bindgen;
extern crate cc;
use bindgen::{
callbacks::{ParseCallbacks, TypeKind},
NonCopyUnionStyle, RustTarget,
};
use cc::Build;
use std::{
env,
fs::{read_to_string, write},
path::PathBuf,
process::Command,
};
#[derive(Debug)]
struct BindgenCallbacks;
impl ParseCallbacks for BindgenCallbacks {
fn enum_variant_name(
&self,
enum_name: Option<&str>,
original_variant_name: &str,
_variant_value: bindgen::callbacks::EnumVariantValue,
) -> Option<String> {
enum_name.map(|name| original_variant_name.replace(name, ""))
}
fn add_derives(&self, info: &bindgen::callbacks::DeriveInfo<'_>) -> Vec<String> {
if info.kind == TypeKind::Enum {
["PartialOrd", "Ord"].into_iter().map(String::from).collect()
} else {
vec![]
}
}
fn add_attributes(&self, info: &bindgen::callbacks::AttributeInfo<'_>) -> Vec<String> {
if info.kind == TypeKind::Enum {
vec![r#"#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]"#.into()]
} else {
vec![]
}
}
}
fn main() {
println!("cargo:rerun-if-changed=src/yoga/yoga");
// Update submodule
Command::new("git")
.args(["submodule", "init"])
.status()
.expect("Unable to initialize git submodules");
Command::new("git")
.args(["submodule", "update"])
.status()
.expect("Unable to update the submodule repositories");
// Build Yoga using cc crate
Build::new()
// Build configuation
// See https://github.com/facebook/yoga/blob/9591210a7ac5289fa00f15d5068383612d3225b9/cmake/project-defaults.cmake
.cpp(true)
.std("c++20")
.flag("-fno-omit-frame-pointer")
.flag("-fexceptions")
.flag("-Wall")
.flag("-O3")
.flag("-fPIC")
// Include path
.include("src/yoga")
// C++ Files
.file("src/yoga/yoga/algorithm/AbsoluteLayout.cpp")
.file("src/yoga/yoga/algorithm/Baseline.cpp")
.file("src/yoga/yoga/algorithm/Cache.cpp")
.file("src/yoga/yoga/algorithm/CalculateLayout.cpp")
.file("src/yoga/yoga/algorithm/FlexLine.cpp")
.file("src/yoga/yoga/algorithm/PixelGrid.cpp")
.file("src/yoga/yoga/config/Config.cpp")
.file("src/yoga/yoga/event/event.cpp")
.file("src/yoga/yoga/debug/AssertFatal.cpp")
.file("src/yoga/yoga/debug/Log.cpp")
.file("src/yoga/yoga/node/LayoutResults.cpp")
.file("src/yoga/yoga/node/Node.cpp")
.file("src/yoga/yoga/YGConfig.cpp")
.file("src/yoga/yoga/YGEnums.cpp")
.file("src/yoga/yoga/YGNode.cpp")
.file("src/yoga/yoga/YGNodeLayout.cpp")
.file("src/yoga/yoga/YGNodeStyle.cpp")
.file("src/yoga/yoga/YGPixelGrid.cpp")
.file("src/yoga/yoga/YGValue.cpp")
// Enable if debugging this script
// .cargo_debug(true)
.compile("libyoga");
// Generate bindings using bindgen
let bindings = bindgen::Builder::default()
.rust_target(RustTarget::stable(47, 0).unwrap_or_default())
.clang_args(&["-x", "c++", "-std=c++20", "-Isrc/yoga"])
.enable_cxx_namespaces()
.allowlist_type("YG.*")
.allowlist_function("YG.*")
.allowlist_var("YG.*")
.layout_tests(false)
.formatter(bindgen::Formatter::Rustfmt)
.rustified_enum("YG.*")
.prepend_enum_name(false)
.parse_callbacks(Box::new(BindgenCallbacks))
.manually_drop_union(".*")
.default_non_copy_union_style(NonCopyUnionStyle::ManuallyDrop)
.header("src/wrapper.hpp")
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_file = out_path.join("bindings.rs");
bindings.write_to_file(&out_file).expect("Unable to write bindings!");
// Patch bindings because bindgen is incorecctly detected float type
let buf = read_to_string(&out_file).expect("Unable to read bindings");
let patched = buf.replace(
"pub const YGUndefined: f32 = f64::NAN;",
"pub const YGUndefined: f32 = f32::NAN;",
);
write(&out_file, patched).expect("Unable to write patched bindings");
println!("Bindings written to {}", &out_file.display());
}