From 8a20b13b417763f58b1b77952e671d038a0dcdd4 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 24 Dec 2024 19:15:28 +0800 Subject: [PATCH] Improve error messages (#143) --- src/compiler/clang.rs | 15 +++++++++------ src/compiler/clang_cl.rs | 22 ++++++++++++++-------- src/compiler/common.rs | 10 ++-------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/compiler/clang.rs b/src/compiler/clang.rs index d63dac1..7e1f5e2 100644 --- a/src/compiler/clang.rs +++ b/src/compiler/clang.rs @@ -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(); @@ -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(()) diff --git a/src/compiler/clang_cl.rs b/src/compiler/clang_cl.rs index a397a5b..a0296f7 100644 --- a/src/compiler/clang_cl.rs +++ b/src/compiler/clang_cl.rs @@ -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())) @@ -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(); @@ -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(()) diff --git a/src/compiler/common.rs b/src/compiler/common.rs index 33a9836..f096431 100644 --- a/src/compiler/common.rs +++ b/src/compiler/common.rs @@ -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); @@ -51,10 +47,9 @@ 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") @@ -62,7 +57,6 @@ pub fn setup_cmake_env(cmd: &mut Command, target: &str, toolchain_path: PathBuf) format!("CMAKE_TOOLCHAIN_FILE_{}", env_target), toolchain_path, ); - Ok(()) } pub fn rustc_target_bin_dir() -> Result {