Skip to content

Commit

Permalink
Improve error messages (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
messense authored Dec 24, 2024
1 parent 3e5cc04 commit 8a20b13
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
15 changes: 9 additions & 6 deletions src/compiler/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ impl Clang {

for target in &targets {
if target.contains("msvc") {
let msvc_sysroot_dir = self.setup_msvc_sysroot(cache_dir.clone())?;
let msvc_sysroot_dir = self
.setup_msvc_sysroot(cache_dir.clone())
.context("Failed to setup MSVC sysroot")?;
// x86_64-pc-windows-msvc -> x86_64-windows-msvc
let target_no_vendor = target.replace("-pc-", "-");
let target_unknown_vendor = target.replace("-pc-", "-unknown-");
let env_target = target.to_lowercase().replace('-', "_");

setup_llvm_tools(&env_path, &cache_dir)?;
setup_target_compiler_and_linker_env(cmd, &env_target, "clang")?;
setup_llvm_tools(&env_path, &cache_dir).context("Failed to setup LLVM tools")?;
setup_target_compiler_and_linker_env(cmd, &env_target, "clang");

let user_set_c_flags = env::var("CFLAGS").unwrap_or_default();
let user_set_cxx_flags = env::var("CXXFLAGS").unwrap_or_default();
Expand Down Expand Up @@ -97,9 +99,10 @@ impl Clang {
cmd.env("PATH", &env_path);

// CMake support
let cmake_toolchain =
self.setup_cmake_toolchain(target, &sysroot_dir, &cache_dir)?;
setup_cmake_env(cmd, target, cmake_toolchain)?;
let cmake_toolchain = self
.setup_cmake_toolchain(target, &sysroot_dir, &cache_dir)
.with_context(|| format!("Failed to setup CMake toolchain for {}", target))?;
setup_cmake_env(cmd, target, cmake_toolchain);
}
}
Ok(())
Expand Down
22 changes: 14 additions & 8 deletions src/compiler/clang_cl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ impl<'a> ClangCl<'a> {
let env_path = setup_env_path(&cache_dir)?;

let xwin_cache_dir = cache_dir.join("xwin");
fs::create_dir_all(&xwin_cache_dir)?;
let xwin_cache_dir = xwin_cache_dir.canonicalize()?;
fs::create_dir_all(&xwin_cache_dir).context("Failed to create xwin cache dir")?;
let xwin_cache_dir = xwin_cache_dir
.canonicalize()
.context("Failed to canonicalize xwin cache dir")?;

let workdir = manifest_path
.and_then(|p| p.parent().map(|x| x.to_path_buf()))
Expand All @@ -57,12 +59,14 @@ impl<'a> ClangCl<'a> {

for target in &targets {
if target.contains("msvc") {
self.setup_msvc_crt(xwin_cache_dir.clone())?;
self.setup_msvc_crt(xwin_cache_dir.clone())
.context("Failed to setup MSVC CRT")?;
let env_target = target.to_lowercase().replace('-', "_");

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")?;
setup_clang_cl_symlink(&env_path, &cache_dir)
.context("Failed to setup clang-cl symlink")?;
setup_llvm_tools(&env_path, &cache_dir).context("Failed to setup LLVM tools")?;
setup_target_compiler_and_linker_env(cmd, &env_target, "clang-cl");

let user_set_cl_flags = env::var("CL_FLAGS").unwrap_or_default();
let user_set_c_flags = env::var("CFLAGS").unwrap_or_default();
Expand Down Expand Up @@ -131,8 +135,10 @@ impl<'a> ClangCl<'a> {
cmd.env("PATH", &env_path);

// CMake support
let cmake_toolchain = self.setup_cmake_toolchain(target, &xwin_cache_dir)?;
setup_cmake_env(cmd, target, cmake_toolchain)?;
let cmake_toolchain = self
.setup_cmake_toolchain(target, &xwin_cache_dir)
.with_context(|| format!("Failed to setup CMake toolchain for {}", target))?;
setup_cmake_env(cmd, target, cmake_toolchain);
}
}
Ok(())
Expand Down
10 changes: 2 additions & 8 deletions src/compiler/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ pub fn setup_llvm_tools(env_path: &OsStr, cache_dir: &Path) -> Result<()> {
Ok(())
}

pub fn setup_target_compiler_and_linker_env(
cmd: &mut Command,
env_target: &str,
compiler: &str,
) -> Result<()> {
pub fn setup_target_compiler_and_linker_env(cmd: &mut Command, env_target: &str, compiler: &str) {
cmd.env("TARGET_CC", compiler);
cmd.env("TARGET_CXX", compiler);
cmd.env(format!("CC_{}", env_target), compiler);
Expand All @@ -51,18 +47,16 @@ pub fn setup_target_compiler_and_linker_env(
format!("CARGO_TARGET_{}_LINKER", env_target.to_uppercase()),
"lld-link",
);
Ok(())
}

pub fn setup_cmake_env(cmd: &mut Command, target: &str, toolchain_path: PathBuf) -> Result<()> {
pub fn setup_cmake_env(cmd: &mut Command, target: &str, toolchain_path: PathBuf) {
let env_target = target.to_lowercase().replace('-', "_");
cmd.env("CMAKE_GENERATOR", "Ninja")
.env("CMAKE_SYSTEM_NAME", "Windows")
.env(
format!("CMAKE_TOOLCHAIN_FILE_{}", env_target),
toolchain_path,
);
Ok(())
}

pub fn rustc_target_bin_dir() -> Result<PathBuf> {
Expand Down

0 comments on commit 8a20b13

Please sign in to comment.