2022-08-28 21:13:21 +03:00
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
2021-04-11 01:09:09 +03:00
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
2021-03-13 04:10:19 +03:00
#![ cfg_attr(doc_cfg, feature(doc_cfg)) ]
pub use anyhow ::Result ;
2022-07-11 22:07:39 +03:00
use heck ::AsShoutySnakeCase ;
2022-02-08 19:13:21 +03:00
use tauri_utils ::resources ::{ external_binaries , resource_relpath , ResourcePaths } ;
2021-03-13 04:10:19 +03:00
2021-05-03 16:42:29 +03:00
use std ::path ::{ Path , PathBuf } ;
2021-03-13 04:10:19 +03:00
#[ cfg(feature = " codegen " ) ]
mod codegen ;
2022-05-27 20:33:04 +03:00
#[ cfg(windows) ]
mod static_vcruntime ;
2021-03-13 04:10:19 +03:00
#[ cfg(feature = " codegen " ) ]
2021-08-13 17:40:57 +03:00
#[ cfg_attr(doc_cfg, doc(cfg(feature = " codegen " ))) ]
2021-03-13 04:10:19 +03:00
pub use codegen ::context ::CodegenContext ;
2022-02-08 19:13:21 +03:00
fn copy_file ( from : impl AsRef < Path > , to : impl AsRef < Path > ) -> Result < ( ) > {
let from = from . as_ref ( ) ;
let to = to . as_ref ( ) ;
if ! from . exists ( ) {
return Err ( anyhow ::anyhow! ( " {:?} does not exist " , from ) ) ;
}
if ! from . is_file ( ) {
return Err ( anyhow ::anyhow! ( " {:?} is not a file " , from ) ) ;
}
2022-02-10 17:21:02 +03:00
let dest_dir = to . parent ( ) . expect ( " No data in parent " ) ;
std ::fs ::create_dir_all ( dest_dir ) ? ;
2022-02-08 19:13:21 +03:00
std ::fs ::copy ( from , to ) ? ;
Ok ( ( ) )
}
2022-12-15 23:56:23 +03:00
fn copy_binaries (
binaries : ResourcePaths ,
2022-08-02 16:37:16 +03:00
target_triple : & str ,
path : & Path ,
package_name : Option < & String > ,
) -> Result < ( ) > {
2022-02-08 19:13:21 +03:00
for src in binaries {
let src = src ? ;
2022-02-15 00:33:40 +03:00
println! ( " cargo:rerun-if-changed= {} " , src . display ( ) ) ;
2022-08-02 16:37:16 +03:00
let file_name = src
. file_name ( )
. expect ( " failed to extract external binary filename " )
. to_string_lossy ( )
2022-12-15 23:56:23 +03:00
. replace ( & format! ( " - {target_triple} " ) , " " ) ;
2022-08-02 16:37:16 +03:00
if package_name . map_or ( false , | n | n = = & file_name ) {
return Err ( anyhow ::anyhow! (
" Cannot define a sidecar with the same name as the Cargo package name `{}`. Please change the sidecar name in the filesystem and the Tauri configuration. " ,
file_name
) ) ;
}
let dest = path . join ( file_name ) ;
2022-05-19 04:49:53 +03:00
if dest . exists ( ) {
std ::fs ::remove_file ( & dest ) . unwrap ( ) ;
}
2022-02-08 19:13:21 +03:00
copy_file ( & src , & dest ) ? ;
}
Ok ( ( ) )
}
/// Copies resources to a path.
fn copy_resources ( resources : ResourcePaths < '_ > , path : & Path ) -> Result < ( ) > {
for src in resources {
let src = src ? ;
2022-02-15 00:33:40 +03:00
println! ( " cargo:rerun-if-changed= {} " , src . display ( ) ) ;
2022-02-08 19:13:21 +03:00
let dest = path . join ( resource_relpath ( & src ) ) ;
2022-12-15 23:56:23 +03:00
copy_file ( & src , dest ) ? ;
2022-02-08 19:13:21 +03:00
}
Ok ( ( ) )
}
2022-05-25 16:51:33 +03:00
// checks if the given Cargo feature is enabled.
fn has_feature ( feature : & str ) -> bool {
// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
2022-07-11 22:07:39 +03:00
std ::env ::var ( format! ( " CARGO_FEATURE_ {} " , AsShoutySnakeCase ( feature ) ) )
. map ( | x | x = = " 1 " )
. unwrap_or ( false )
2022-05-25 16:51:33 +03:00
}
// creates a cfg alias if `has_feature` is true.
// `alias` must be a snake case string.
fn cfg_alias ( alias : & str , has_feature : bool ) {
if has_feature {
2022-12-15 23:56:23 +03:00
println! ( " cargo:rustc-cfg= {alias} " ) ;
2022-05-25 16:51:33 +03:00
}
}
2021-05-03 16:42:29 +03:00
/// Attributes used on Windows.
#[ allow(dead_code) ]
2022-05-13 16:39:04 +03:00
#[ derive(Debug, Default) ]
2021-05-03 16:42:29 +03:00
pub struct WindowsAttributes {
2022-05-13 16:39:04 +03:00
window_icon_path : Option < PathBuf > ,
2022-06-12 20:06:15 +03:00
/// The path to the sdk location.
///
/// For the GNU toolkit this has to be the path where MinGW put windres.exe and ar.exe.
/// This could be something like: "C:\Program Files\mingw-w64\x86_64-5.3.0-win32-seh-rt_v4-rev0\mingw64\bin"
///
/// For MSVC the Windows SDK has to be installed. It comes with the resource compiler rc.exe.
/// This should be set to the root directory of the Windows SDK, e.g., "C:\Program Files (x86)\Windows Kits\10" or,
2022-09-03 07:03:02 +03:00
/// if multiple 10 versions are installed, set it directly to the correct bin directory "C:\Program Files (x86)\Windows Kits\10\bin\10.0.14393.0\x64"
2022-06-12 20:06:15 +03:00
///
/// If it is left unset, it will look up a path in the registry, i.e. HKLM\SOFTWARE\Microsoft\Windows Kits\Installed Roots
2021-11-16 17:18:13 +03:00
sdk_dir : Option < PathBuf > ,
2021-05-03 16:42:29 +03:00
}
impl WindowsAttributes {
/// Creates the default attribute set.
pub fn new ( ) -> Self {
Self ::default ( )
}
/// Sets the icon to use on the window. Currently only used on Windows.
/// It must be in `ico` format. Defaults to `icons/icon.ico`.
2022-01-17 16:46:14 +03:00
#[ must_use ]
2021-05-03 16:42:29 +03:00
pub fn window_icon_path < P : AsRef < Path > > ( mut self , window_icon_path : P ) -> Self {
2022-05-13 16:39:04 +03:00
self
. window_icon_path
. replace ( window_icon_path . as_ref ( ) . into ( ) ) ;
2021-05-03 16:42:29 +03:00
self
}
2021-11-16 17:18:13 +03:00
2022-06-19 16:23:35 +03:00
/// Sets the sdk dir for windows. Currently only used on Windows. This must be a valid UTF-8
2021-11-16 17:18:13 +03:00
/// path. Defaults to whatever the `winres` crate determines is best.
2022-01-17 16:46:14 +03:00
#[ must_use ]
2021-11-16 17:18:13 +03:00
pub fn sdk_dir < P : AsRef < Path > > ( mut self , sdk_dir : P ) -> Self {
self . sdk_dir = Some ( sdk_dir . as_ref ( ) . into ( ) ) ;
self
}
2021-05-03 16:42:29 +03:00
}
/// The attributes used on the build.
2021-08-11 08:07:39 +03:00
#[ derive(Debug, Default) ]
2021-05-03 16:42:29 +03:00
pub struct Attributes {
#[ allow(dead_code) ]
windows_attributes : WindowsAttributes ,
}
impl Attributes {
/// Creates the default attribute set.
pub fn new ( ) -> Self {
Self ::default ( )
}
/// Sets the icon to use on the window. Currently only used on Windows.
2022-01-17 16:46:14 +03:00
#[ must_use ]
2021-05-03 16:42:29 +03:00
pub fn windows_attributes ( mut self , windows_attributes : WindowsAttributes ) -> Self {
self . windows_attributes = windows_attributes ;
self
}
}
2021-03-13 04:10:19 +03:00
/// Run all build time helpers for your Tauri Application.
///
/// The current helpers include the following:
/// * Generates a Windows Resource file when targeting Windows.
///
/// # Platforms
///
/// [`build()`] should be called inside of `build.rs` regardless of the platform:
/// * New helpers may target more platforms in the future.
/// * Platform specific code is handled by the helpers automatically.
/// * A build script is required in order to activate some cargo environmental variables that are
/// used when generating code and embedding assets - so [`build()`] may as well be called.
///
/// In short, this is saying don't put the call to [`build()`] behind a `#[cfg(windows)]`.
///
/// # Panics
///
/// If any of the build time helpers fail, they will [`std::panic!`] with the related error message.
/// This is typically desirable when running inside a build script; see [`try_build`] for no panics.
pub fn build ( ) {
2021-05-03 16:42:29 +03:00
if let Err ( error ) = try_build ( Attributes ::default ( ) ) {
2022-12-15 23:56:23 +03:00
let error = format! ( " {error:#} " ) ;
println! ( " {error} " ) ;
2022-07-06 16:33:45 +03:00
if error . starts_with ( " unknown field " ) {
print! ( " found an unknown configuration field. This usually means that you are using a CLI version that is newer than `tauri-build` and is incompatible. " ) ;
println! (
" Please try updating the Rust crates by running `cargo update` in the Tauri app folder. "
) ;
}
std ::process ::exit ( 1 ) ;
2021-03-13 04:10:19 +03:00
}
}
/// Non-panicking [`build()`].
2021-05-03 16:42:29 +03:00
#[ allow(unused_variables) ]
pub fn try_build ( attributes : Attributes ) -> Result < ( ) > {
2022-01-09 16:55:09 +03:00
use anyhow ::anyhow ;
2021-10-25 17:50:38 +03:00
use cargo_toml ::{ Dependency , Manifest } ;
2022-02-04 19:37:23 +03:00
use tauri_utils ::config ::{ Config , TauriConfig } ;
2022-01-09 16:55:09 +03:00
2022-04-27 02:02:06 +03:00
println! ( " cargo:rerun-if-env-changed=TAURI_CONFIG " ) ;
2022-02-03 16:15:32 +03:00
println! ( " cargo:rerun-if-changed=tauri.conf.json " ) ;
#[ cfg(feature = " config-json5 " ) ]
println! ( " cargo:rerun-if-changed=tauri.conf.json5 " ) ;
2022-08-02 20:12:26 +03:00
#[ cfg(feature = " config-toml " ) ]
println! ( " cargo:rerun-if-changed=Tauri.toml " ) ;
2022-02-03 16:15:32 +03:00
2022-08-02 17:25:28 +03:00
let target_os = std ::env ::var ( " CARGO_CFG_TARGET_OS " ) . unwrap ( ) ;
let mobile = target_os = = " ios " | | target_os = = " android " ;
cfg_alias ( " desktop " , ! mobile ) ;
cfg_alias ( " mobile " , mobile ) ;
2022-07-06 15:29:26 +03:00
let mut config = serde_json ::from_value ( tauri_utils ::config ::parse ::read_from (
std ::env ::current_dir ( ) . unwrap ( ) ,
) ? ) ? ;
if let Ok ( env ) = std ::env ::var ( " TAURI_CONFIG " ) {
let merge_config : serde_json ::Value = serde_json ::from_str ( & env ) ? ;
json_patch ::merge ( & mut config , & merge_config ) ;
}
let config : Config = serde_json ::from_value ( config ) ? ;
2022-01-09 16:55:09 +03:00
2022-08-21 16:35:34 +03:00
let s = config . tauri . bundle . identifier . split ( '.' ) ;
let last = s . clone ( ) . count ( ) - 1 ;
let mut domain = String ::new ( ) ;
for ( i , w ) in s . enumerate ( ) {
2022-08-24 18:41:57 +03:00
if i ! = last {
2022-08-21 16:35:34 +03:00
domain . push_str ( w ) ;
domain . push ( '_' ) ;
}
}
domain . pop ( ) ;
2022-12-15 23:56:23 +03:00
println! ( " cargo:rustc-env=TAURI_ANDROID_DOMAIN= {domain} " ) ;
2022-08-21 16:35:34 +03:00
2022-05-25 16:51:33 +03:00
cfg_alias ( " dev " , ! has_feature ( " custom-protocol " ) ) ;
2021-10-25 17:50:38 +03:00
let mut manifest = Manifest ::from_path ( " Cargo.toml " ) ? ;
if let Some ( tauri ) = manifest . dependencies . remove ( " tauri " ) {
2022-02-04 19:37:23 +03:00
let features = match tauri {
2021-10-25 17:50:38 +03:00
Dependency ::Simple ( _ ) = > Vec ::new ( ) ,
Dependency ::Detailed ( dep ) = > dep . features ,
2022-09-28 17:44:40 +03:00
Dependency ::Inherited ( dep ) = > dep . features ,
2021-10-25 17:50:38 +03:00
} ;
2022-02-04 19:37:23 +03:00
let all_cli_managed_features = TauriConfig ::all_features ( ) ;
let diff = features_diff (
& features
. into_iter ( )
. filter ( | f | all_cli_managed_features . contains ( & f . as_str ( ) ) )
. collect ::< Vec < String > > ( ) ,
& config
. tauri
. features ( )
. into_iter ( )
. map ( | f | f . to_string ( ) )
. collect ::< Vec < String > > ( ) ,
) ;
let mut error_message = String ::new ( ) ;
if ! diff . remove . is_empty ( ) {
error_message . push_str ( " remove the ` " ) ;
2022-02-04 20:03:27 +03:00
error_message . push_str ( & diff . remove . join ( " , " ) ) ;
2022-02-04 19:37:23 +03:00
error_message . push_str ( if diff . remove . len ( ) = = 1 {
" ` feature "
} else {
" ` features "
} ) ;
if ! diff . add . is_empty ( ) {
error_message . push_str ( " and " ) ;
}
}
if ! diff . add . is_empty ( ) {
error_message . push_str ( " add the ` " ) ;
2022-02-04 20:03:27 +03:00
error_message . push_str ( & diff . add . join ( " , " ) ) ;
2022-02-04 19:37:23 +03:00
error_message . push_str ( if diff . add . len ( ) = = 1 {
" ` feature "
} else {
" ` features "
} ) ;
}
if ! error_message . is_empty ( ) {
2021-10-25 17:50:38 +03:00
return Err ( anyhow! ( "
2022-01-09 16:55:09 +03:00
The ` tauri ` dependency features on the ` Cargo . toml ` file does not match the allowlist defined under ` tauri . conf . json ` .
2022-02-04 19:37:23 +03:00
Please run ` tauri dev ` or ` tauri build ` or { } .
" , error_message));
2021-10-25 17:50:38 +03:00
}
2022-01-09 16:55:09 +03:00
}
2022-02-08 19:13:21 +03:00
let target_triple = std ::env ::var ( " TARGET " ) . unwrap ( ) ;
2022-08-30 19:22:26 +03:00
2022-12-15 23:56:23 +03:00
println! ( " cargo:rustc-env=TAURI_TARGET_TRIPLE= {target_triple} " ) ;
2022-08-30 19:22:26 +03:00
2022-06-12 20:06:15 +03:00
let out_dir = PathBuf ::from ( std ::env ::var ( " OUT_DIR " ) . unwrap ( ) ) ;
2022-02-08 19:13:21 +03:00
// TODO: far from ideal, but there's no other way to get the target dir, see <https://github.com/rust-lang/cargo/issues/5457>
2022-06-12 20:06:15 +03:00
let target_dir = out_dir
2022-02-08 19:13:21 +03:00
. parent ( )
. unwrap ( )
. parent ( )
. unwrap ( )
. parent ( )
. unwrap ( ) ;
2022-05-13 16:39:04 +03:00
if let Some ( paths ) = & config . tauri . bundle . external_bin {
2022-02-08 19:13:21 +03:00
copy_binaries (
2022-05-13 16:39:04 +03:00
ResourcePaths ::new ( external_binaries ( paths , & target_triple ) . as_slice ( ) , true ) ,
2022-02-08 19:13:21 +03:00
& target_triple ,
target_dir ,
2022-08-02 16:37:16 +03:00
manifest . package . as_ref ( ) . map ( | p | & p . name ) ,
2022-02-08 19:13:21 +03:00
) ? ;
}
2022-04-19 23:33:17 +03:00
2022-06-14 23:50:15 +03:00
#[ allow(unused_mut, clippy::redundant_clone) ]
2022-05-13 16:39:04 +03:00
let mut resources = config . tauri . bundle . resources . clone ( ) . unwrap_or_default ( ) ;
2022-06-10 22:40:16 +03:00
#[ cfg(windows) ]
if let Some ( fixed_webview2_runtime_path ) = & config . tauri . bundle . windows . webview_fixed_runtime_path
{
resources . push ( fixed_webview2_runtime_path . display ( ) . to_string ( ) ) ;
}
2022-04-19 23:33:17 +03:00
copy_resources ( ResourcePaths ::new ( resources . as_slice ( ) , true ) , target_dir ) ? ;
2022-02-08 19:13:21 +03:00
2022-02-18 01:00:19 +03:00
#[ cfg(target_os = " macos " ) ]
{
if let Some ( version ) = config . tauri . bundle . macos . minimum_system_version {
println! ( " cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET= {} " , version ) ;
}
}
2021-03-13 04:10:19 +03:00
#[ cfg(windows) ]
{
2022-01-09 16:55:09 +03:00
use anyhow ::Context ;
2022-05-03 20:04:23 +03:00
use semver ::Version ;
use winres ::{ VersionInfo , WindowsResource } ;
2021-03-13 04:10:19 +03:00
2022-05-13 16:39:04 +03:00
fn find_icon < F : Fn ( & & String ) -> bool > ( config : & Config , predicate : F , default : & str ) -> PathBuf {
let icon_path = config
. tauri
. bundle
. icon
. iter ( )
. find ( | i | predicate ( i ) )
. cloned ( )
. unwrap_or_else ( | | default . to_string ( ) ) ;
icon_path . into ( )
}
let window_icon_path = attributes
2021-05-03 16:42:29 +03:00
. windows_attributes
. window_icon_path
2022-05-13 16:39:04 +03:00
. unwrap_or_else ( | | find_icon ( & config , | i | i . ends_with ( " .ico " ) , " icons/icon.ico " ) ) ;
2021-05-03 16:42:29 +03:00
2022-11-20 15:49:23 +03:00
if target_triple . contains ( " windows " ) {
if window_icon_path . exists ( ) {
let mut res = WindowsResource ::new ( ) ;
2022-08-03 00:53:34 +03:00
2022-11-20 15:49:23 +03:00
res . set_manifest (
r #"
2022-08-03 00:53:34 +03:00
< assembly xmlns = " urn:schemas-microsoft-com:asm.v1 " manifestVersion = " 1.0 " >
< dependency >
< dependentAssembly >
< assemblyIdentity
type = " win32 "
name = " Microsoft.Windows.Common-Controls "
version = " 6.0.0.0 "
processorArchitecture = " * "
publicKeyToken = " 6595b64144ccf1df "
language = " * "
/ >
< / dependentAssembly >
< / dependency >
< / assembly >
" #,
2022-11-20 15:49:23 +03:00
) ;
if let Some ( sdk_dir ) = & attributes . windows_attributes . sdk_dir {
if let Some ( sdk_dir_str ) = sdk_dir . to_str ( ) {
res . set_toolkit_path ( sdk_dir_str ) ;
} else {
return Err ( anyhow! (
" sdk_dir path is not valid; only UTF-8 characters are allowed "
) ) ;
}
2021-11-16 17:18:13 +03:00
}
2022-11-20 15:49:23 +03:00
if let Some ( version ) = & config . package . version {
if let Ok ( v ) = Version ::parse ( version ) {
let version = v . major < < 48 | v . minor < < 32 | v . patch < < 16 ;
res . set_version_info ( VersionInfo ::FILEVERSION , version ) ;
res . set_version_info ( VersionInfo ::PRODUCTVERSION , version ) ;
}
res . set ( " FileVersion " , version ) ;
res . set ( " ProductVersion " , version ) ;
2022-05-03 20:04:23 +03:00
}
2022-11-20 15:49:23 +03:00
if let Some ( product_name ) = & config . package . product_name {
res . set ( " ProductName " , product_name ) ;
res . set ( " FileDescription " , product_name ) ;
}
res . set_icon_with_id ( & window_icon_path . display ( ) . to_string ( ) , " 32512 " ) ;
res . compile ( ) . with_context ( | | {
format! (
" failed to compile `{}` into a Windows Resource file during tauri-build " ,
window_icon_path . display ( )
)
} ) ? ;
} else {
return Err ( anyhow! ( format! (
" `{}` not found; required for generating a Windows Resource file during tauri-build " ,
2022-05-13 16:39:04 +03:00
window_icon_path . display ( )
2022-11-20 15:49:23 +03:00
) ) ) ;
}
2021-03-13 04:10:19 +03:00
}
2022-06-12 20:06:15 +03:00
let target_env = std ::env ::var ( " CARGO_CFG_TARGET_ENV " ) . unwrap ( ) ;
match target_env . as_str ( ) {
" gnu " = > {
let target_arch = match std ::env ::var ( " CARGO_CFG_TARGET_ARCH " ) . unwrap ( ) . as_str ( ) {
" x86_64 " = > Some ( " x64 " ) ,
" x86 " = > Some ( " x86 " ) ,
" aarch64 " = > Some ( " arm64 " ) ,
arch = > None ,
} ;
if let Some ( target_arch ) = target_arch {
for entry in std ::fs ::read_dir ( target_dir . join ( " build " ) ) ? {
let path = entry ? . path ( ) ;
let webview2_loader_path = path
. join ( " out " )
. join ( target_arch )
. join ( " WebView2Loader.dll " ) ;
if path . to_string_lossy ( ) . contains ( " webview2-com-sys " ) & & webview2_loader_path . exists ( )
{
std ::fs ::copy ( webview2_loader_path , target_dir . join ( " WebView2Loader.dll " ) ) ? ;
break ;
}
}
}
}
" msvc " = > {
if std ::env ::var ( " STATIC_VCRUNTIME " ) . map_or ( false , | v | v = = " true " ) {
static_vcruntime ::build ( ) ;
}
}
_ = > ( ) ,
}
2021-03-13 04:10:19 +03:00
}
Ok ( ( ) )
}
2022-02-04 19:37:23 +03:00
#[ derive(Debug, Default, PartialEq, Eq) ]
struct Diff {
remove : Vec < String > ,
add : Vec < String > ,
}
fn features_diff ( current : & [ String ] , expected : & [ String ] ) -> Diff {
let mut remove = Vec ::new ( ) ;
let mut add = Vec ::new ( ) ;
for feature in current {
2022-02-04 20:03:27 +03:00
if ! expected . contains ( feature ) {
2022-02-04 19:37:23 +03:00
remove . push ( feature . clone ( ) ) ;
}
}
for feature in expected {
if ! current . contains ( feature ) {
add . push ( feature . clone ( ) ) ;
}
}
Diff { remove , add }
}
#[ cfg(test) ]
mod tests {
use super ::Diff ;
#[ test ]
fn array_diff ( ) {
for ( current , expected , result ) in [
( vec! [ ] , vec! [ ] , Default ::default ( ) ) ,
(
vec! [ " a " . into ( ) ] ,
vec! [ ] ,
Diff {
remove : vec ! [ " a " . into ( ) ] ,
add : vec ! [ ] ,
} ,
) ,
( vec! [ " a " . into ( ) ] , vec! [ " a " . into ( ) ] , Default ::default ( ) ) ,
(
vec! [ " a " . into ( ) , " b " . into ( ) ] ,
vec! [ " a " . into ( ) ] ,
Diff {
remove : vec ! [ " b " . into ( ) ] ,
add : vec ! [ ] ,
} ,
) ,
(
vec! [ " a " . into ( ) , " b " . into ( ) ] ,
vec! [ " a " . into ( ) , " c " . into ( ) ] ,
Diff {
remove : vec ! [ " b " . into ( ) ] ,
add : vec ! [ " c " . into ( ) ] ,
} ,
) ,
] {
assert_eq! ( super ::features_diff ( & current , & expected ) , result ) ;
}
}
}