Skip to content

Commit

Permalink
Detect duplicate sccache wrappers (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks authored Dec 17, 2023
1 parent ef9103c commit 34fad7c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 7 additions & 5 deletions crates/rb-sys-build/src/cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use std::{

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

const WELL_KNOWN_WRAPPERS: &[&str] = &["sccache", "cachepot"];

#[derive(Default, Debug)]
pub struct Build {
files: Vec<PathBuf>,
Expand Down Expand Up @@ -223,27 +225,27 @@ fn get_common_args() -> Vec<String> {

fn get_compiler() -> Command {
let cmd = get_tool("CC", "cc");
let cmd_program = cmd.get_program().to_str().unwrap_or_default();
let already_wrapped = WELL_KNOWN_WRAPPERS.iter().any(|w| cmd_program.contains(w));

match get_tool_from_rb_config_or_env("CC_WRAPPER") {
Some(wrapper) if !wrapper.is_empty() => {
Some(wrapper) if !wrapper.is_empty() && !already_wrapped => {
debug_log!("INFO: using CC_WRAPPER ({:?})", wrapper);
cmd.wrapped(wrapper)
}
_ => match rustc_wrapper_fallback() {
Some(wrapper) => cmd.wrapped(wrapper),
Some(wrapper) if !already_wrapped => cmd.wrapped(wrapper),
_ => cmd,
},
}
}

fn rustc_wrapper_fallback() -> Option<String> {
const VALID_WRAPPERS: &[&str] = &["sccache", "cachepot"];

let rustc_wrapper = std::env::var_os("RUSTC_WRAPPER")?;
let wrapper_path = Path::new(&rustc_wrapper);
let wrapper_stem = wrapper_path.file_stem()?;

if VALID_WRAPPERS.contains(&wrapper_stem.to_str()?) {
if WELL_KNOWN_WRAPPERS.contains(&wrapper_stem.to_str()?) {
debug_log!("INFO: using RUSTC_WRAPPER ({:?})", rustc_wrapper);
Some(rustc_wrapper.to_str()?.to_owned())
} else {
Expand Down
7 changes: 7 additions & 0 deletions gem/lib/rb_sys/cargo_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module RbSys
# A class to build a Ruby gem Cargo. Extracted from `rubygems` gem, with some modifications.
# @api private
class CargoBuilder < Gem::Ext::Builder
WELL_KNOWN_WRAPPERS = %w[sccache cachepot].freeze

attr_accessor :spec, :runner, :env, :features, :target, :extra_rustc_args, :dry_run, :ext_dir, :extra_rustflags,
:extra_cargo_args
attr_writer :profile
Expand Down Expand Up @@ -148,6 +150,11 @@ def platform_specific_rustc_args(dest_dir, flags = [])
def linker_args
cc_flag = Shellwords.split(makefile_config("CC"))
linker = cc_flag.shift

if WELL_KNOWN_WRAPPERS.any? { |w| linker.include?(w) }
linker = cc_flag.shift
end

link_args = cc_flag.flat_map { |a| ["-C", "link-arg=#{a}"] }

return mswin_link_args if linker == "cl"
Expand Down

0 comments on commit 34fad7c

Please sign in to comment.