tauri_utils/
platform.rs

1// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use std::{
6  env,
7  path::{PathBuf, MAIN_SEPARATOR},
8};
9
10use crate::PackageInfo;
11
12/// Try to determine the current target triple.
13///
14/// Returns a target triple (e.g. `x86_64-unknown-linux-gnu` or `i686-pc-windows-msvc`) or an
15/// `Error::Config` if the current config cannot be determined or is not some combination of the
16/// following values:
17/// `linux, mac, windows` -- `i686, x86, armv7` -- `gnu, musl, msvc`
18///
19/// * Errors:
20///     * Unexpected system config
21pub fn target_triple() -> crate::Result<String> {
22  let arch = if cfg!(target_arch = "x86") {
23    "i686"
24  } else if cfg!(target_arch = "x86_64") {
25    "x86_64"
26  } else if cfg!(target_arch = "arm") {
27    "armv7"
28  } else if cfg!(target_arch = "aarch64") {
29    "aarch64"
30  } else {
31    return Err(crate::Error::Architecture);
32  };
33
34  let os = if cfg!(target_os = "linux") {
35    "unknown-linux"
36  } else if cfg!(target_os = "macos") {
37    "apple-darwin"
38  } else if cfg!(target_os = "windows") {
39    "pc-windows"
40  } else if cfg!(target_os = "freebsd") {
41    "unknown-freebsd"
42  } else {
43    return Err(crate::Error::Os);
44  };
45
46  let os = if cfg!(target_os = "macos") || cfg!(target_os = "freebsd") {
47    String::from(os)
48  } else {
49    let env = if cfg!(target_env = "gnu") {
50      "gnu"
51    } else if cfg!(target_env = "musl") {
52      "musl"
53    } else if cfg!(target_env = "msvc") {
54      "msvc"
55    } else {
56      return Err(crate::Error::Environment);
57    };
58
59    format!("{}-{}", os, env)
60  };
61
62  Ok(format!("{}-{}", arch, os))
63}
64
65/// Computes the resource directory of the current environment.
66///
67/// On Windows, it's the path to the executable.
68///
69/// On Linux, when running in an AppImage the `APPDIR` variable will be set to
70/// the mounted location of the app, and the resource dir will be
71/// `${APPDIR}/usr/lib/${exe_name}`. If not running in an AppImage, the path is
72/// `/usr/lib/${exe_name}`.  When running the app from
73/// `src-tauri/target/(debug|release)/`, the path is
74/// `${exe_dir}/../lib/${exe_name}`.
75///
76/// On MacOS, it's `${exe_dir}../Resources` (inside .app).
77pub fn resource_dir(package_info: &PackageInfo) -> crate::Result<PathBuf> {
78  let exe = std::env::current_exe()?;
79  let exe_dir = exe.parent().expect("failed to get exe directory");
80  let curr_dir = exe_dir.display().to_string();
81
82  if curr_dir.ends_with(format!("{S}target{S}debug", S = MAIN_SEPARATOR).as_str())
83    || curr_dir.ends_with(format!("{S}target{S}release", S = MAIN_SEPARATOR).as_str())
84    || cfg!(target_os = "windows")
85  {
86    // running from the out dir or windows
87    return Ok(exe_dir.to_path_buf());
88  }
89
90  if cfg!(target_os = "linux") {
91    if curr_dir.ends_with("/data/usr/bin") {
92      // running from the deb bundle dir
93      Ok(exe_dir.join(format!("../lib/{}", package_info.package_name())))
94    } else if let Ok(appdir) = env::var("APPDIR") {
95      Ok(PathBuf::from(format!(
96        "{}/usr/lib/{}",
97        appdir,
98        package_info.package_name()
99      )))
100    } else {
101      // running bundle
102      Ok(PathBuf::from(format!(
103        "/usr/lib/{}",
104        package_info.package_name()
105      )))
106    }
107  } else if cfg!(target_os = "macos") {
108    Ok(exe_dir.join("../Resources"))
109  } else {
110    Err(crate::Error::UnsupportedPlatform)
111  }
112}