Skip to content

Commit

Permalink
Prefer homebrew clang as clang-cl on macOS (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
messense authored Dec 22, 2024
1 parent cda7dcd commit 155fb56
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/compiler/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 27 additions & 3 deletions src/compiler/clang_cl.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::collections::HashSet;
use std::convert::TryInto;
use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;

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;

Expand Down Expand Up @@ -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")?;

Expand Down Expand Up @@ -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(())
}
29 changes: 3 additions & 26 deletions src/compiler/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OsString> {
let env_path = env::var("PATH").unwrap_or_default();
Expand All @@ -17,12 +17,12 @@ pub fn setup_env_path(cache_dir: &Path) -> Result<OsString> {
&& 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());
Expand All @@ -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,
Expand Down

0 comments on commit 155fb56

Please sign in to comment.