forked from gwihlidal/vk-mem-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
210 lines (185 loc) · 7.31 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
#[cfg(feature = "generate_bindings")]
extern crate bindgen;
extern crate cc;
use std::env;
fn main() {
let mut build = cc::Build::new();
build.include("vendor/VulkanMemoryAllocator/include");
build.include("vendor/Vulkan-Headers/include");
build.include("wrapper");
// Disable VMA_ASSERT when rust assertions are disabled
#[cfg(not(debug_assertions))]
build.define("NDEBUG", "");
// We want to use the loader in ash, instead of requiring us to link
// in vulkan.dll/.dylib in addition to ash. This is especially important
// for MoltenVK, where there is no default installation path, unlike
// Linux (pkconfig) and Windows (VULKAN_SDK environment variable).
build.define("VMA_STATIC_VULKAN_FUNCTIONS", "0");
// This prevents VMA from trying to fetch any remaining pointers
// that are still null after using the loader in ash, which can
// cause linker errors.
build.define("VMA_DYNAMIC_VULKAN_FUNCTIONS", "0");
// TODO: Add some configuration options under crate features
//#define VMA_HEAVY_ASSERT(expr) assert(expr)
//#define VMA_USE_STL_CONTAINERS 1
//#define VMA_DEDICATED_ALLOCATION 0
//#define VMA_DEBUG_MARGIN 16
//#define VMA_DEBUG_DETECT_CORRUPTION 1
//#define VMA_DEBUG_INITIALIZE_ALLOCATIONS 1
//#define VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY 256
#[cfg(feature = "recording")]
build.define("VMA_RECORDING_ENABLED", "1");
// Add the files we build
let source_files = ["wrapper/vma_lib.cpp"];
for source_file in &source_files {
build.file(&source_file);
}
let target = env::var("TARGET").unwrap();
if target.contains("darwin") {
build
.flag("-std=c++17")
.flag("-Wno-missing-field-initializers")
.flag("-Wno-unused-variable")
.flag("-Wno-unused-parameter")
.flag("-Wno-unused-private-field")
.flag("-Wno-reorder")
.flag("-Wno-nullability-completeness")
.cpp_link_stdlib("c++")
.cpp_set_stdlib("c++")
.cpp(true);
} else if target.contains("ios") {
build
.flag("-std=c++17")
.flag("-Wno-missing-field-initializers")
.flag("-Wno-unused-variable")
.flag("-Wno-unused-parameter")
.flag("-Wno-unused-private-field")
.flag("-Wno-reorder")
.cpp_link_stdlib("c++")
.cpp_set_stdlib("c++")
.cpp(true);
} else if target.contains("android") {
build
.flag("-std=c++17")
.flag("-Wno-missing-field-initializers")
.flag("-Wno-unused-variable")
.flag("-Wno-unused-parameter")
.flag("-Wno-unused-private-field")
.flag("-Wno-reorder")
.cpp_link_stdlib("c++")
.cpp(true);
} else if target.contains("linux") {
build
.flag("-std=c++17")
.flag("-Wno-missing-field-initializers")
.flag("-Wno-unused-variable")
.flag("-Wno-unused-parameter")
.flag("-Wno-unused-private-field")
.flag("-Wno-reorder")
.cpp_link_stdlib("stdc++")
.cpp(true);
} else if target.contains("windows") && target.contains("gnu") {
build
.flag("-std=c++17")
.flag("-Wno-missing-field-initializers")
.flag("-Wno-unused-variable")
.flag("-Wno-unused-parameter")
.flag("-Wno-unused-private-field")
.flag("-Wno-reorder")
.flag("-Wno-type-limits")
.cpp_link_stdlib("stdc++")
.cpp(true);
}
build.compile("vma_cpp");
link_vulkan();
generate_bindings("gen/bindings.rs");
}
#[cfg(feature = "link_vulkan")]
fn link_vulkan() {
use std::path::PathBuf;
let target = env::var("TARGET").unwrap();
if target.contains("windows") {
if let Ok(vulkan_sdk) = env::var("VULKAN_SDK") {
let mut vulkan_sdk_path = PathBuf::from(vulkan_sdk);
if target.contains("x86_64") {
vulkan_sdk_path.push("Lib");
} else {
vulkan_sdk_path.push("Lib32");
}
println!(
"cargo:rustc-link-search=native={}",
vulkan_sdk_path.to_str().unwrap()
);
}
println!("cargo:rustc-link-lib=dylib=vulkan-1");
} else {
if target.contains("apple") {
if let Ok(vulkan_sdk) = env::var("VULKAN_SDK") {
let mut vulkan_sdk_path = PathBuf::from(vulkan_sdk);
vulkan_sdk_path.push("macOS/lib");
println!(
"cargo:rustc-link-search=native={}",
vulkan_sdk_path.to_str().unwrap()
);
} else {
let lib_path = "wrapper/macOS/lib";
println!("cargo:rustc-link-search=native={}", lib_path);
}
println!("cargo:rustc-link-lib=dylib=vulkan");
}
}
}
#[cfg(not(feature = "link_vulkan"))]
fn link_vulkan() {}
#[cfg(feature = "generate_bindings")]
fn generate_bindings(output_file: &str) {
let bindings = bindgen::Builder::default()
.clang_arg("-I./wrapper")
.clang_arg("-I./vendor/Vulkan-Headers/include")
.header("vendor/VulkanMemoryAllocator/include/vk_mem_alloc.h")
.rustfmt_bindings(true)
.size_t_is_usize(true)
.blocklist_type("__darwin_.*")
.allowlist_function("vma.*")
.allowlist_function("PFN_vma.*")
.allowlist_type("Vma.*")
.parse_callbacks(Box::new(FixAshTypes))
.blocklist_type("Vk.*")
.blocklist_type("PFN_vk.*")
.raw_line("use ash::vk::*;")
.trust_clang_mangling(false)
.layout_tests(false)
.generate()
.expect("Unable to generate bindings!");
bindings
.write_to_file(std::path::Path::new(output_file))
.expect("Unable to write bindings!");
}
#[cfg(not(feature = "generate_bindings"))]
fn generate_bindings(_: &str) {}
#[cfg(feature = "generate_bindings")]
#[derive(Debug)]
struct FixAshTypes;
#[cfg(feature = "generate_bindings")]
impl bindgen::callbacks::ParseCallbacks for FixAshTypes {
fn item_name(&self, original_item_name: &str) -> Option<String> {
if original_item_name.starts_with("Vk") {
// Strip `Vk` prefix, will use `ash::vk::*` instead
Some(original_item_name.trim_start_matches("Vk").to_string())
} else if original_item_name.starts_with("PFN_vk") && original_item_name.ends_with("KHR") {
// VMA uses a few extensions like `PFN_vkGetBufferMemoryRequirements2KHR`,
// ash keeps these as `PFN_vkGetBufferMemoryRequirements2`
Some(original_item_name.trim_end_matches("KHR").to_string())
} else {
None
}
}
// When ignoring `Vk` types, bindgen loses derives for some type. Quick workaround.
fn add_derives(&self, name: &str) -> Vec<String> {
if name.starts_with("VmaAllocationInfo") || name.starts_with("VmaDefragmentationStats") {
vec!["Debug".into(), "Copy".into(), "Clone".into()]
} else {
vec![]
}
}
}