Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefer_clang_cl_over_msvc #1367

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ pub struct Build {
shell_escaped_flags: Option<bool>,
build_cache: Arc<BuildCache>,
inherit_rustflags: bool,
prefer_clang_cl_over_msvc: bool,
}

/// Represents the types of errors that may occur while using cc-rs.
Expand Down Expand Up @@ -458,6 +459,7 @@ impl Build {
shell_escaped_flags: None,
build_cache: Arc::default(),
inherit_rustflags: true,
prefer_clang_cl_over_msvc: false,
}
}

Expand Down Expand Up @@ -1359,6 +1361,14 @@ impl Build {
self
}

/// Prefer to use clang-cl over msvc.
///
/// This option defaults to `false`.
pub fn prefer_clang_cl_over_msvc(&mut self, prefer_clang_cl_over_msvc: bool) -> &mut Build {
self.prefer_clang_cl_over_msvc = prefer_clang_cl_over_msvc;
self
}

#[doc(hidden)]
pub fn __set_env<A, B>(&mut self, a: A, b: B) -> &mut Build
where
Expand Down Expand Up @@ -2732,10 +2742,17 @@ impl Build {
}
let target = self.get_target()?;
let raw_target = self.get_raw_target()?;
let (env, msvc, gnu, traditional, clang) = if self.cpp {
("CXX", "cl.exe", "g++", "c++", "clang++")

let msvc = if self.prefer_clang_cl_over_msvc {
"clang-cl.exe"
} else {
"cl.exe"
};

let (env, gnu, traditional, clang) = if self.cpp {
("CXX", "g++", "c++", "clang++")
} else {
("CC", "cl.exe", "gcc", "cc", "clang")
("CC", "gcc", "cc", "clang")
};

// On historical Solaris systems, "cc" may have been Sun Studio, which
Expand All @@ -2748,7 +2765,7 @@ impl Build {
traditional
};

let cl_exe = self.windows_registry_find_tool(&target, "cl.exe");
let cl_exe = self.windows_registry_find_tool(&target, msvc);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @ChrisDenton I would need some help on how to find clang-cl, here I just pass clang-cl.exe to windows_registry::find_tool if user prefers clang-cl over msvc, hoping it would find it, not sure if it is correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might find it if it's installed with Visual Studio but it could be installed separately. We should more strongly prefer the environment for clang-cl (e.g. checking PATH as well).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add I a dedicated PATH checking logic for clang-cl.exe?

Or should I add that to windows_registry::find_tool?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding dedicated PATH checking logic is the safest option.

It'd be nice if find_tool where consistent but it's possible people may use it to specifically find the VS installed version. So unless someone complains about the find_tool behaviour, I'm not sure if it's worth the (admittedly small) risk of breakage.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might find it if it's installed with Visual Studio but it could be installed separately. We should more strongly prefer the environment for clang-cl (e.g. checking PATH as well).

If it is installed separately then the user should specify CC/TARGET_CC when building. Otherwise, I think the behavior should be "use the clang-cl provided with the version of MSVC that we were about to use."


let tool_opt: Option<Tool> = self
.env_tool(env)
Expand Down
Loading