-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbuild.rs
More file actions
179 lines (152 loc) · 5.16 KB
/
Copy pathbuild.rs
File metadata and controls
179 lines (152 loc) · 5.16 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
174
175
176
177
178
179
#[cfg(target_os = "macos")]
use std::{path::PathBuf, process::Command};
const COMMANDS: &[&str] = &[
"register_listener",
"remove_listener",
"get_products",
"purchase",
"restore_purchases",
"get_purchase_history",
"acknowledge_purchase",
"consume_purchase",
"get_product_status",
];
fn main() {
tauri_plugin::Builder::new(COMMANDS)
.android_path("android")
.ios_path("ios")
.build();
#[cfg(target_os = "macos")]
{
// Only run macOS-specific build steps when building for macOS
if std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "macos" {
// Rebuild when target architecture or deployment target changes
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_ARCH");
println!("cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET");
let bridges = vec!["src/macos.rs"];
for path in &bridges {
println!("cargo:rerun-if-changed={path}");
}
println!("cargo:rerun-if-changed=macos/Sources/IapPlugin.swift");
swift_bridge_build::parse_bridges(bridges)
.write_all_concatenated(swift_bridge_out_dir(), env!("CARGO_PKG_NAME"));
compile_swift();
println!("cargo:rustc-link-lib=static=tauri-plugin-iap");
println!(
"cargo:rustc-link-search={}",
swift_library_static_lib_dir()
.to_str()
.expect("Swift library path must be valid UTF-8")
);
}
}
}
#[cfg(target_os = "macos")]
fn compile_swift() {
let swift_package_dir = manifest_dir().join("macos");
let target_triple = swift_target_triple();
let mut cmd = Command::new("swift");
cmd.current_dir(&swift_package_dir)
.arg("build")
// Build into OUT_DIR (under target/) instead of the default `.build`
// inside the crate source. Source-tree writes don't survive a clean
// registry re-extraction / cache restore, which leaves cargo's
// fingerprint saying "built" while the linked artifact is gone.
.args([
"--scratch-path",
swift_build_dir()
.to_str()
.expect("Swift build path must be valid UTF-8"),
])
.args(["--triple", &target_triple])
.args([
"-Xswiftc",
"-import-objc-header",
"-Xswiftc",
swift_source_dir()
.join("bridging-header.h")
.to_str()
.expect("Bridging header path must be valid UTF-8"),
]);
if is_release_build() {
cmd.args(["-c", "release"]);
}
let exit_status = cmd
.spawn()
.expect("Failed to spawn swift build command")
.wait_with_output()
.expect("Failed to wait for swift build output");
assert!(
exit_status.status.success(),
r"
Swift build failed for target: {}
Stderr: {}
Stdout: {}
",
target_triple,
String::from_utf8(exit_status.stderr).expect("Stderr must be valid UTF-8"),
String::from_utf8(exit_status.stdout).expect("Stdout must be valid UTF-8"),
);
}
#[cfg(target_os = "macos")]
fn swift_bridge_out_dir() -> PathBuf {
generated_code_dir()
}
#[cfg(target_os = "macos")]
fn manifest_dir() -> PathBuf {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set");
PathBuf::from(manifest_dir)
}
#[cfg(target_os = "macos")]
fn out_dir() -> PathBuf {
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR must be set");
PathBuf::from(out_dir)
}
/// `SwiftPM` scratch (build) directory, under `OUT_DIR` so it lives in `target/`
/// and is covered by cargo's fingerprint and any build cache.
#[cfg(target_os = "macos")]
fn swift_build_dir() -> PathBuf {
out_dir().join("swift-build")
}
#[cfg(target_os = "macos")]
fn is_release_build() -> bool {
std::env::var("PROFILE").expect("PROFILE must be set") == "release"
}
#[cfg(target_os = "macos")]
fn swift_source_dir() -> PathBuf {
manifest_dir().join("macos/Sources")
}
#[cfg(target_os = "macos")]
fn generated_code_dir() -> PathBuf {
swift_source_dir().join("generated")
}
#[cfg(target_os = "macos")]
fn target_arch() -> String {
std::env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH must be set")
}
#[cfg(target_os = "macos")]
fn swift_arch() -> &'static str {
match target_arch().as_str() {
"aarch64" => "arm64",
"x86_64" => "x86_64",
arch => panic!("Unsupported architecture for macOS: {arch}"),
}
}
#[cfg(target_os = "macos")]
fn macos_deployment_target() -> String {
std::env::var("MACOSX_DEPLOYMENT_TARGET").unwrap_or_else(|_| "13.0".to_string())
}
#[cfg(target_os = "macos")]
fn swift_target_triple() -> String {
format!("{}-apple-macosx{}", swift_arch(), macos_deployment_target())
}
#[cfg(target_os = "macos")]
fn swift_library_static_lib_dir() -> PathBuf {
let debug_or_release = if is_release_build() {
"release"
} else {
"debug"
};
let arch_dir = format!("{}-apple-macosx", swift_arch());
swift_build_dir().join(format!("{arch_dir}/{debug_or_release}"))
}