Link homebrew package(for Mac M1).
(name: &str)
| 78 | |
| 79 | /// Link homebrew package(for Mac M1). |
| 80 | fn link_homebrew_m1(name: &str) -> PathBuf { |
| 81 | let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); |
| 82 | let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); |
| 83 | if target_os != "macos" || target_arch != "aarch64" { |
| 84 | panic!("Couldn't find VCPKG_ROOT, also can't fallback to homebrew because it's only for macos aarch64."); |
| 85 | } |
| 86 | let mut path = PathBuf::from("/opt/homebrew/Cellar"); |
| 87 | path.push(name); |
| 88 | let entries = if let Ok(dir) = std::fs::read_dir(&path) { |
| 89 | dir |
| 90 | } else { |
| 91 | panic!("Could not find package in {}. Make sure your homebrew and package {} are all installed.", path.to_str().unwrap(),&name); |
| 92 | }; |
| 93 | let mut directories = entries |
| 94 | .into_iter() |
| 95 | .filter(|x| x.is_ok()) |
| 96 | .map(|x| x.unwrap().path()) |
| 97 | .filter(|x| x.is_dir()) |
| 98 | .collect::<Vec<_>>(); |
| 99 | // Find the newest version. |
| 100 | directories.sort_unstable(); |
| 101 | if directories.is_empty() { |
| 102 | panic!( |
| 103 | "There's no installed version of {} in /opt/homebrew/Cellar", |
| 104 | name |
| 105 | ); |
| 106 | } |
| 107 | path.push(directories.pop().unwrap()); |
| 108 | // Link the library. |
| 109 | println!( |
| 110 | "{}", |
| 111 | format!( |
| 112 | "cargo:rustc-link-lib=static={}", |
| 113 | name.trim_start_matches("lib") |
| 114 | ) |
| 115 | ); |
| 116 | // Add the library path. |
| 117 | println!( |
| 118 | "{}", |
| 119 | format!( |
| 120 | "cargo:rustc-link-search={}", |
| 121 | path.join("lib").to_str().unwrap() |
| 122 | ) |
| 123 | ); |
| 124 | // Add the include path. |
| 125 | let include = path.join("include"); |
| 126 | println!("{}", format!("cargo:include={}", include.to_str().unwrap())); |
| 127 | include |
| 128 | } |
| 129 | |
| 130 | /// Find package. By default, it will try to find vcpkg first, then homebrew(currently only for Mac M1). |
| 131 | /// If building for linux and feature "linux-pkg-config" is enabled, will try to use pkg-config |