-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
173 lines (158 loc) · 6.26 KB
/
Copy pathbuild.rs
File metadata and controls
173 lines (158 loc) · 6.26 KB
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
// build.rs - STFSC Engine build script
use std::env;
use std::path::Path;
use std::path::PathBuf;
fn main() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if target_os == "android" && target_arch == "aarch64" {
// Link OpenXR Loader
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
println!(
"cargo:rustc-link-search=native={}/libs/arm64-v8a",
manifest_dir
);
println!("cargo:rustc-link-lib=openxr_loader");
// Resolve the active NDK instead of baking in one developer machine's path.
let ndk_path = find_android_ndk();
let host_tag = android_ndk_host_tag();
let arch_libs = ndk_path.as_ref().map(|ndk| {
ndk.join("toolchains")
.join("llvm")
.join("prebuilt")
.join(host_tag)
.join("sysroot")
.join("usr")
.join("lib")
.join("aarch64-linux-android")
});
let android_api = env::var("ANDROID_PLATFORM")
.ok()
.and_then(|value| value.trim_start_matches("android-").parse::<u32>().ok())
.unwrap_or(29);
if let Some(arch_libs) = &arch_libs {
println!(
"cargo:rustc-link-search=native={}",
arch_libs.join(android_api.to_string()).display()
);
} else {
println!(
"cargo:warning=Android NDK was not found; set ANDROID_NDK_HOME before linking"
);
}
println!("cargo:rustc-link-lib=vulkan");
// Link C++ shared library (required for shaderc and other C++ deps)
println!("cargo:rustc-link-lib=c++_shared");
// Copy libc++_shared.so to cargo-apk's temp directory so it gets bundled
let target_dir = Path::new(&manifest_dir)
.join("target")
.join("cargo-apk-temp-extra-link-libraries");
if let Some(libcpp_path) = arch_libs
.as_ref()
.map(|path| path.join("libc++_shared.so"))
.filter(|path| path.exists())
{
// Create directory if it doesn't exist
std::fs::create_dir_all(&target_dir).ok();
let dest_path = target_dir.join("libc++_shared.so");
std::fs::copy(&libcpp_path, &dest_path).ok();
println!("cargo:rustc-link-search=native={}", target_dir.display());
}
}
// Copy pre-compiled SPIR-V shaders to OUT_DIR
// Shaders are pre-compiled and stored as .spv files in src/graphics/
// Naming convention in repo: shader_type.spv (e.g., vert_vert.spv)
// Required naming for include_bytes!: shader.type.spv (e.g., vert.vert.spv)
let src_dir = Path::new("src/graphics");
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir);
if src_dir.exists() {
println!("cargo:rerun-if-changed=src/graphics");
for entry in std::fs::read_dir(src_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let filename = path.file_name().unwrap().to_str().unwrap();
if filename.ends_with(".spv") {
// Convert underscore naming to dot naming: vert_vert.spv -> vert.vert.spv
let out_name = filename.replace("_", ".");
std::fs::copy(&path, out_path.join(&out_name)).unwrap();
}
}
}
// Exported projects can provide a generated native script module. Editor
// builds also look for the viewport play cache written by the editor so
// custom scripts can be baked into the native registry at compile time.
println!("cargo:rerun-if-env-changed=STFSC_SCRIPT_BINDINGS_RS");
println!("cargo:rerun-if-changed=script_cache/generated_script_bindings.rs");
let generated_script_dst = out_path.join("generated_fuckscript.rs");
if let Ok(script_bindings) = env::var("STFSC_SCRIPT_BINDINGS_RS") {
let script_bindings = Path::new(&script_bindings);
if script_bindings.exists() {
println!("cargo:rerun-if-changed={}", script_bindings.display());
std::fs::copy(script_bindings, &generated_script_dst).unwrap();
} else {
write_empty_generated_scripts(&generated_script_dst);
}
} else if Path::new("script_cache/generated_script_bindings.rs").exists() {
std::fs::copy(
"script_cache/generated_script_bindings.rs",
&generated_script_dst,
)
.unwrap();
} else {
write_empty_generated_scripts(&generated_script_dst);
}
}
fn find_android_ndk() -> Option<PathBuf> {
for key in ["ANDROID_NDK_HOME", "ANDROID_NDK_ROOT"] {
println!("cargo:rerun-if-env-changed={key}");
if let Some(path) = env::var_os(key)
.map(PathBuf::from)
.filter(|path| path.exists())
{
return Some(path);
}
}
for key in ["ANDROID_HOME", "ANDROID_SDK_ROOT"] {
println!("cargo:rerun-if-env-changed={key}");
if let Some(sdk) = env::var_os(key).map(PathBuf::from) {
if let Some(ndk) = newest_ndk_in(&sdk.join("ndk")) {
return Some(ndk);
}
}
}
let common_sdk = if cfg!(target_os = "windows") {
env::var_os("LOCALAPPDATA")
.map(PathBuf::from)
.map(|path| path.join("Android").join("Sdk"))
} else {
env::var_os("HOME")
.map(PathBuf::from)
.map(|path| path.join("Android").join("Sdk"))
};
common_sdk.and_then(|sdk| newest_ndk_in(&sdk.join("ndk")))
}
fn newest_ndk_in(directory: &Path) -> Option<PathBuf> {
std::fs::read_dir(directory)
.ok()?
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.is_dir())
.max()
}
fn android_ndk_host_tag() -> &'static str {
if cfg!(target_os = "windows") {
"windows-x86_64"
} else if cfg!(target_os = "macos") {
"darwin-x86_64"
} else {
"linux-x86_64"
}
}
fn write_empty_generated_scripts(path: &Path) {
std::fs::write(
path,
"use super::scripting::ScriptRegistry;\n\npub fn register_generated_scripts(_registry: &mut ScriptRegistry) {}\n",
)
.unwrap();
}