Skip to content

Commit 6ba9968

Browse files
authored
feat(codegen): use TARGET environment variable if set (#4921)
1 parent 8958e9f commit 6ba9968

2 files changed

Lines changed: 103 additions & 66 deletions

File tree

‎.changes/codegen-check-target.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-codegen": patch
3+
---
4+
5+
Use `TARGET` environment variable for code generation inside build scripts.

‎core/tauri-codegen/src/context.rs‎

Lines changed: 98 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct ContextData {
2828

2929
fn map_core_assets(
3030
options: &AssetOptions,
31+
target: Target,
3132
) -> impl Fn(&AssetKey, &Path, &mut Vec<u8>, &mut CspHashes) -> Result<(), EmbeddedAssetsError> {
3233
#[cfg(feature = "isolation")]
3334
let pattern = tauri_utils::html::PatternObject::from(&options.pattern);
@@ -40,8 +41,9 @@ fn map_core_assets(
4041

4142
#[allow(clippy::collapsible_if)]
4243
if csp {
43-
#[cfg(target_os = "linux")]
44-
::tauri_utils::html::inject_csp_token(&mut document);
44+
if target == Target::Linux {
45+
::tauri_utils::html::inject_csp_token(&mut document);
46+
}
4547

4648
inject_nonce_token(&mut document, &dangerous_disable_asset_csp_modification);
4749

@@ -106,6 +108,16 @@ fn map_isolation(
106108
}
107109
}
108110

111+
#[derive(PartialEq, Eq, Clone, Copy)]
112+
enum Target {
113+
Linux,
114+
Windows,
115+
Darwin,
116+
Android,
117+
// iOS.
118+
Ios,
119+
}
120+
109121
/// Build a `tauri::Context` for including in application code.
110122
pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsError> {
111123
let ContextData {
@@ -115,6 +127,34 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
115127
root,
116128
} = data;
117129

130+
let target = if let Ok(target) = std::env::var("TARGET") {
131+
if target.contains("unknown-linux") {
132+
Target::Linux
133+
} else if target.contains("pc-windows") {
134+
Target::Windows
135+
} else if target.contains("apple-darwin") {
136+
Target::Darwin
137+
} else if target.contains("android") {
138+
Target::Android
139+
} else if target.contains("apple-ios") {
140+
Target::Ios
141+
} else {
142+
panic!("unknown codegen target {}", target);
143+
}
144+
} else if cfg!(target_os = "linux") {
145+
Target::Linux
146+
} else if cfg!(windows) {
147+
Target::Windows
148+
} else if cfg!(target_os = "macos") {
149+
Target::Darwin
150+
} else if cfg!(target_os = "android") {
151+
Target::Android
152+
} else if cfg!(target_os = "ios") {
153+
Target::Ios
154+
} else {
155+
panic!("unknown codegen target");
156+
};
157+
118158
let mut options = AssetOptions::new(config.tauri.pattern.clone())
119159
.freeze_prototype(config.tauri.security.freeze_prototype)
120160
.dangerous_disable_asset_csp_modification(
@@ -162,7 +202,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
162202
path
163203
)
164204
}
165-
EmbeddedAssets::new(assets_path, &options, map_core_assets(&options))?
205+
EmbeddedAssets::new(assets_path, &options, map_core_assets(&options, target))?
166206
}
167207
_ => unimplemented!(),
168208
},
@@ -172,7 +212,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
172212
.map(|p| config_parent.join(p))
173213
.collect::<Vec<_>>(),
174214
&options,
175-
map_core_assets(&options),
215+
map_core_assets(&options, target),
176216
)?,
177217
_ => unimplemented!(),
178218
};
@@ -189,42 +229,41 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
189229
out_dir
190230
};
191231

192-
// handle default window icons for Windows targets
193-
#[cfg(windows)]
194232
let default_window_icon = {
195-
let icon_path = find_icon(
196-
&config,
197-
&config_parent,
198-
|i| i.ends_with(".ico"),
199-
"icons/icon.ico",
200-
);
201-
if icon_path.exists() {
202-
ico_icon(&root, &out_dir, icon_path)?
203-
} else {
233+
if target == Target::Windows {
234+
// handle default window icons for Windows targets
235+
let icon_path = find_icon(
236+
&config,
237+
&config_parent,
238+
|i| i.ends_with(".ico"),
239+
"icons/icon.ico",
240+
);
241+
if icon_path.exists() {
242+
ico_icon(&root, &out_dir, icon_path)?
243+
} else {
244+
let icon_path = find_icon(
245+
&config,
246+
&config_parent,
247+
|i| i.ends_with(".png"),
248+
"icons/icon.png",
249+
);
250+
png_icon(&root, &out_dir, icon_path)?
251+
}
252+
} else if target == Target::Linux {
253+
// handle default window icons for Linux targets
204254
let icon_path = find_icon(
205255
&config,
206256
&config_parent,
207257
|i| i.ends_with(".png"),
208258
"icons/icon.png",
209259
);
210260
png_icon(&root, &out_dir, icon_path)?
261+
} else {
262+
quote!(None)
211263
}
212264
};
213-
#[cfg(target_os = "linux")]
214-
let default_window_icon = {
215-
let icon_path = find_icon(
216-
&config,
217-
&config_parent,
218-
|i| i.ends_with(".png"),
219-
"icons/icon.png",
220-
);
221-
png_icon(&root, &out_dir, icon_path)?
222-
};
223-
#[cfg(not(any(windows, target_os = "linux")))]
224-
let default_window_icon = quote!(None);
225265

226-
#[cfg(target_os = "macos")]
227-
let app_icon = if dev {
266+
let app_icon = if target == Target::Darwin && dev {
228267
let mut icon_path = find_icon(
229268
&config,
230269
&config_parent,
@@ -243,8 +282,6 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
243282
} else {
244283
quote!(None)
245284
};
246-
#[cfg(not(target_os = "macos"))]
247-
let app_icon = quote!(None);
248285

249286
let package_name = if let Some(product_name) = &config.package.product_name {
250287
quote!(#product_name.to_string())
@@ -283,43 +320,40 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
283320
};
284321

285322
#[cfg(target_os = "macos")]
286-
let info_plist = {
287-
if dev {
288-
let info_plist_path = config_parent.join("Info.plist");
289-
let mut info_plist = if info_plist_path.exists() {
290-
plist::Value::from_file(&info_plist_path)
291-
.unwrap_or_else(|e| panic!("failed to read plist {}: {}", info_plist_path.display(), e))
292-
} else {
293-
plist::Value::Dictionary(Default::default())
294-
};
323+
let info_plist = if target == Target::Darwin && dev {
324+
let info_plist_path = config_parent.join("Info.plist");
325+
let mut info_plist = if info_plist_path.exists() {
326+
plist::Value::from_file(&info_plist_path)
327+
.unwrap_or_else(|e| panic!("failed to read plist {}: {}", info_plist_path.display(), e))
328+
} else {
329+
plist::Value::Dictionary(Default::default())
330+
};
295331

296-
if let Some(plist) = info_plist.as_dictionary_mut() {
297-
if let Some(product_name) = &config.package.product_name {
298-
plist.insert("CFBundleName".into(), product_name.clone().into());
299-
}
300-
if let Some(version) = &config.package.version {
301-
plist.insert("CFBundleShortVersionString".into(), version.clone().into());
302-
}
303-
let format =
304-
time::format_description::parse("[year][month][day].[hour][minute][second]").unwrap();
305-
if let Ok(build_number) = time::OffsetDateTime::now_utc().format(&format) {
306-
plist.insert("CFBundleVersion".into(), build_number.into());
307-
}
332+
if let Some(plist) = info_plist.as_dictionary_mut() {
333+
if let Some(product_name) = &config.package.product_name {
334+
plist.insert("CFBundleName".into(), product_name.clone().into());
335+
}
336+
if let Some(version) = &config.package.version {
337+
plist.insert("CFBundleShortVersionString".into(), version.clone().into());
338+
}
339+
let format =
340+
time::format_description::parse("[year][month][day].[hour][minute][second]").unwrap();
341+
if let Ok(build_number) = time::OffsetDateTime::now_utc().format(&format) {
342+
plist.insert("CFBundleVersion".into(), build_number.into());
308343
}
344+
}
309345

310-
let out_path = out_dir.join("Info.plist");
311-
info_plist
312-
.to_file_xml(&out_path)
313-
.expect("failed to write Info.plist");
346+
let out_path = out_dir.join("Info.plist");
347+
info_plist
348+
.to_file_xml(&out_path)
349+
.expect("failed to write Info.plist");
314350

315-
let info_plist_path = out_path.display().to_string();
316-
quote!({
317-
#[cfg(desktop)]
318-
tauri::embed_plist::embed_info_plist!(#info_plist_path);
319-
})
320-
} else {
321-
quote!(())
322-
}
351+
let info_plist_path = out_path.display().to_string();
352+
quote!({
353+
tauri::embed_plist::embed_info_plist!(#info_plist_path);
354+
})
355+
} else {
356+
quote!(())
323357
};
324358
#[cfg(not(target_os = "macos"))]
325359
let info_plist = quote!(());
@@ -443,7 +477,6 @@ fn ico_icon<P: AsRef<Path>>(
443477
Ok(icon)
444478
}
445479

446-
#[cfg(target_os = "macos")]
447480
fn raw_icon<P: AsRef<Path>>(out_dir: &Path, path: P) -> Result<TokenStream, EmbeddedAssetsError> {
448481
let path = path.as_ref();
449482
let bytes = std::fs::read(&path)
@@ -508,7 +541,6 @@ fn write_if_changed(out_path: &Path, data: &[u8]) -> std::io::Result<()> {
508541
out_file.write_all(data)
509542
}
510543

511-
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
512544
fn find_icon<F: Fn(&&String) -> bool>(
513545
config: &Config,
514546
config_parent: &Path,

0 commit comments

Comments
 (0)