Skip to content

Commit

Permalink
Use lld instead of lld-link when using clang
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Dec 23, 2024
1 parent fcc0f78 commit ca535c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
21 changes: 13 additions & 8 deletions src/compiler/clang.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;

Expand All @@ -9,7 +10,7 @@ use serde::Deserialize;

use crate::compiler::common::{
adjust_canonicalization, default_build_target_from_config, get_rustflags, http_agent,
setup_cmake_env, setup_env_path, setup_llvm_tools, setup_target_compiler_and_linker_env,
setup_cmake_env, setup_env_path, setup_target_compiler_and_linker_env,
};

const MSVC_SYSROOT_REPOSITORY: &str = "trcrsired/windows-msvc-sysroot";
Expand Down Expand Up @@ -62,7 +63,7 @@ impl Clang {
let sysroot_dir =
adjust_canonicalization(msvc_sysroot_dir.to_slash_lossy().to_string());
let clang_flags = format!(
"--target={target_no_vendor} -fuse-ld=lld-link -I{dir}/include -I{dir}/include/c++/stl -L{dir}/lib/{target_unknown_vendor}",
"--target={target_no_vendor} -fuse-ld=lld -I{dir}/include -I{dir}/include/c++/stl -L{dir}/lib/{target_unknown_vendor}",
dir = sysroot_dir,
);
cmd.env(
Expand All @@ -84,8 +85,6 @@ impl Clang {

let mut rustflags = get_rustflags(&workdir, target)?.unwrap_or_default();
rustflags.flags.extend([
"-C".to_string(),
"linker-flavor=lld-link".to_string(),
"-C".to_string(),
"link-arg=-defaultlib:oldnames".to_string(),
]);
Expand Down Expand Up @@ -263,18 +262,16 @@ set(CMAKE_SYSTEM_PROCESSOR {processor})
set(CMAKE_C_COMPILER clang CACHE FILEPATH "")
set(CMAKE_CXX_COMPILER clang++ CACHE FILEPATH "")
set(CMAKE_LINKER lld-link CACHE FILEPATH "")
set(CMAKE_RC_COMPILER llvm-rc CACHE FILEPATH "")
set(COMPILE_FLAGS
--target={target_no_vendor}
-fuse-ld=lld-link
-fuse-ld=lld
-I{dir}/include
-I{dir}/include/c++/stl)
set(LINK_FLAGS
/manifest:no
-libpath:"{dir}/lib/{target_unknown_vendor}")
-L"{dir}/lib/{target_unknown_vendor}")
"#,
dir = sysroot_dir,
);
Expand All @@ -293,3 +290,11 @@ struct GitHubReleaseAsset {
browser_download_url: String,
name: String,
}

fn setup_llvm_tools(env_path: &OsStr, cache_dir: &Path) -> Result<()> {
use crate::compiler::common::symlink_llvm_tool;

symlink_llvm_tool("rust-lld", "lld", env_path, cache_dir)?;
symlink_llvm_tool("llvm-ar", "llvm-dlltool", env_path, cache_dir)?;
Ok(())
}
15 changes: 12 additions & 3 deletions src/compiler/clang_cl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use xwin::util::ProgressTarget;

use crate::compiler::common::{
adjust_canonicalization, default_build_target_from_config, get_rustflags, http_agent,
setup_cmake_env, setup_env_path, setup_llvm_tools, setup_target_compiler_and_linker_env,
setup_cmake_env, setup_env_path, setup_target_compiler_and_linker_env,
};
use crate::options::XWinOptions;

Expand Down Expand Up @@ -398,7 +398,7 @@ set(CMAKE_USER_MAKE_RULES_OVERRIDE "${{CMAKE_CURRENT_LIST_DIR}}/override.cmake")
}

#[cfg(target_os = "macos")]
pub fn setup_clang_cl_symlink(env_path: &OsStr, cache_dir: &Path) -> Result<()> {
fn setup_clang_cl_symlink(env_path: &OsStr, cache_dir: &Path) -> Result<()> {
// Try PATH first, but skip system clang
let clang = which_in("clang", Some(env_path), env::current_dir()?)
.ok()
Expand Down Expand Up @@ -442,7 +442,7 @@ pub fn setup_clang_cl_symlink(env_path: &OsStr, cache_dir: &Path) -> Result<()>
}

#[cfg(not(target_os = "macos"))]
pub fn setup_clang_cl_symlink(env_path: &OsStr, cache_dir: &Path) -> Result<()> {
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)]
{
Expand All @@ -464,3 +464,12 @@ pub fn setup_clang_cl_symlink(env_path: &OsStr, cache_dir: &Path) -> Result<()>
}
Ok(())
}

fn setup_llvm_tools(env_path: &OsStr, cache_dir: &Path) -> Result<()> {
use crate::compiler::common::symlink_llvm_tool;

symlink_llvm_tool("rust-lld", "lld-link", env_path, cache_dir)?;
symlink_llvm_tool("llvm-ar", "llvm-lib", env_path, cache_dir)?;
symlink_llvm_tool("llvm-ar", "llvm-dlltool", env_path, cache_dir)?;
Ok(())
}
9 changes: 1 addition & 8 deletions src/compiler/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ pub fn setup_env_path(cache_dir: &Path) -> Result<OsString> {
Ok(env::join_paths(env_paths)?)
}

pub fn setup_llvm_tools(env_path: &OsStr, cache_dir: &Path) -> Result<()> {
symlink_llvm_tool("rust-lld", "lld-link", env_path, cache_dir)?;
symlink_llvm_tool("llvm-ar", "llvm-lib", env_path, cache_dir)?;
symlink_llvm_tool("llvm-ar", "llvm-dlltool", env_path, cache_dir)?;
Ok(())
}

pub fn setup_target_compiler_and_linker_env(
cmd: &mut Command,
env_target: &str,
Expand Down Expand Up @@ -76,7 +69,7 @@ pub fn rustc_target_bin_dir() -> Result<PathBuf> {
}

/// Symlink Rust provided llvm tool component
fn symlink_llvm_tool(
pub fn symlink_llvm_tool(
tool: &str,
link_name: &str,
env_path: &OsStr,
Expand Down

0 comments on commit ca535c9

Please sign in to comment.