From 155fb56889724ef3364e6accbf92fdf4a3edab53 Mon Sep 17 00:00:00 2001 From: messense Date: Sun, 22 Dec 2024 20:23:22 +0800 Subject: [PATCH] Prefer homebrew clang as `clang-cl` on macOS (#136) --- src/compiler/clang.rs | 4 ++-- src/compiler/clang_cl.rs | 30 +++++++++++++++++++++++++++--- src/compiler/common.rs | 29 +++-------------------------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/compiler/clang.rs b/src/compiler/clang.rs index 529289d..5e02fcf 100644 --- a/src/compiler/clang.rs +++ b/src/compiler/clang.rs @@ -179,9 +179,9 @@ impl Clang { .progress_chars("=> "), ); pb.set_prefix("sysroot"); - pb.set_message("⏬ Downloading"); + pb.set_message("📥 downloading"); if pb.is_hidden() { - eprintln!("⏬ Start downloading MSVC sysroot..."); + eprintln!("📥 Start downloading MSVC sysroot..."); } let reader = pb.wrap_read(response.into_reader()); let tar = XzDecoder::new(reader); diff --git a/src/compiler/clang_cl.rs b/src/compiler/clang_cl.rs index 49ad394..da57428 100644 --- a/src/compiler/clang_cl.rs +++ b/src/compiler/clang_cl.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; use std::convert::TryInto; use std::env; +use std::ffi::OsStr; use std::path::{Path, PathBuf}; use std::process::Command; @@ -8,12 +9,12 @@ use anyhow::{Context, Result}; use fs_err as fs; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use path_slash::PathExt; +use which::which_in; use xwin::util::ProgressTarget; use crate::compiler::common::{ adjust_canonicalization, default_build_target_from_config, get_rustflags, http_agent, - setup_clang_cl_symlink, setup_cmake_env, setup_env_path, setup_llvm_tools, - setup_target_compiler_and_linker_env, + setup_cmake_env, setup_env_path, setup_llvm_tools, setup_target_compiler_and_linker_env, }; use crate::options::XWinOptions; @@ -59,7 +60,7 @@ impl<'a> ClangCl<'a> { self.setup_msvc_crt(xwin_cache_dir.clone())?; let env_target = target.to_lowercase().replace('-', "_"); - setup_clang_cl_symlink(&cache_dir)?; + setup_clang_cl_symlink(&env_path, &cache_dir)?; setup_llvm_tools(&env_path, &cache_dir)?; setup_target_compiler_and_linker_env(cmd, &env_target, "clang-cl")?; @@ -395,3 +396,26 @@ set(CMAKE_USER_MAKE_RULES_OVERRIDE "${{CMAKE_CURRENT_LIST_DIR}}/override.cmake") Ok(toolchain_file) } } + +pub fn setup_clang_cl_symlink(env_path: &OsStr, cache_dir: &Path) -> Result<()> { + if let Ok(clang) = which_in("clang", Some(env_path), env::current_dir()?) { + #[cfg(windows)] + { + let symlink = cache_dir.join("clang-cl.exe"); + if symlink.exists() { + fs::remove_file(&symlink)?; + } + std::os::windows::fs::symlink_file(clang, symlink)?; + } + + #[cfg(unix)] + { + let symlink = cache_dir.join("clang-cl"); + if symlink.exists() { + fs::remove_file(&symlink)?; + } + std::os::unix::fs::symlink(clang, symlink)?; + } + } + Ok(()) +} diff --git a/src/compiler/common.rs b/src/compiler/common.rs index 89d4ab0..b7e8c1a 100644 --- a/src/compiler/common.rs +++ b/src/compiler/common.rs @@ -4,7 +4,7 @@ use std::env; use std::ffi::{OsStr, OsString}; use std::path::{Path, PathBuf}; use std::process::Command; -use which::{which, which_in}; +use which::which_in; pub fn setup_env_path(cache_dir: &Path) -> Result { let env_path = env::var("PATH").unwrap_or_default(); @@ -17,12 +17,12 @@ pub fn setup_env_path(cache_dir: &Path) -> Result { && Path::new(&usr_llvm).is_dir() && !env_paths.contains(&usr_llvm) { - env_paths.push(usr_llvm); + env_paths.insert(0, usr_llvm); } else if cfg!(target_arch = "aarch64") && Path::new(&opt_llvm).is_dir() && !env_paths.contains(&opt_llvm) { - env_paths.push(opt_llvm); + env_paths.insert(0, opt_llvm); } } env_paths.push(cache_dir.to_path_buf()); @@ -36,29 +36,6 @@ pub fn setup_llvm_tools(env_path: &OsStr, cache_dir: &Path) -> Result<()> { Ok(()) } -pub fn setup_clang_cl_symlink(cache_dir: &Path) -> Result<()> { - if let Ok(clang) = which("clang") { - #[cfg(windows)] - { - let symlink = cache_dir.join("clang-cl.exe"); - if symlink.exists() { - fs::remove_file(&symlink)?; - } - std::os::windows::fs::symlink_file(clang, symlink)?; - } - - #[cfg(unix)] - { - let symlink = cache_dir.join("clang-cl"); - if symlink.exists() { - fs::remove_file(&symlink)?; - } - std::os::unix::fs::symlink(clang, symlink)?; - } - } - Ok(()) -} - pub fn setup_target_compiler_and_linker_env( cmd: &mut Command, env_target: &str,