Skip to content

Commit

Permalink
Support msvc as the host compiler for nvcc
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmaynard authored and sylvestre committed Sep 28, 2023
1 parent 80c033c commit 2a9f4e3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,10 @@ where
// Both clang and clang-cl define _MSC_VER on Windows, so we first
// check for MSVC, then check whether _MT is defined, which is the
// difference between clang and clang-cl.
let test = b"#if defined(__NVCC__)
let test = b"#if defined(__NVCC__) && !defined(_MSC_VER)
nvcc
#elif defined(__NVCC__) && defined(_MSC_VER)
nvcc-msvc
#elif defined(_MSC_VER) && !defined(__clang__)
msvc
#elif defined(_MSC_VER) && defined(_MT)
Expand Down Expand Up @@ -1221,10 +1223,12 @@ __VERSION__
.await
.map(|c| Box::new(c) as Box<dyn Compiler<T>>);
}
"nvcc" => {
"nvcc" | "nvcc-msvc" => {
let is_msvc = kind == "nvcc-msvc";
debug!("Found NVCC");
return CCompiler::new(
Nvcc {
is_msvc,
version: version.clone(),
},
executable,
Expand Down
13 changes: 11 additions & 2 deletions src/compiler/nvcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::errors::*;
/// A unit struct on which to implement `CCompilerImpl`.
#[derive(Clone, Debug)]
pub struct Nvcc {
pub is_msvc: bool,
pub version: Option<String>,
}

Expand Down Expand Up @@ -148,8 +149,12 @@ impl CCompilerImpl for Nvcc {

//NVCC only supports `-E` when it comes after preprocessor
//and common flags.
let no_line_nums = match self.is_msvc {
true => "-Xcompiler=-EP",
false => "-Xcompiler=-P",
};
cmd.arg("-E")
.arg("-Xcompiler=-P")
.arg(no_line_nums)
.env_clear()
.envs(env_vars.iter().map(|&(ref k, ref v)| (k, v)))
.current_dir(cwd);
Expand Down Expand Up @@ -259,7 +264,11 @@ mod test {

fn parse_arguments_(arguments: Vec<String>) -> CompilerArguments<ParsedArguments> {
let arguments = arguments.iter().map(OsString::from).collect::<Vec<_>>();
Nvcc { version: None }.parse_arguments(&arguments, ".".as_ref())
Nvcc {
is_msvc: false,
version: None,
}
.parse_arguments(&arguments, ".".as_ref())
}

macro_rules! parses {
Expand Down
36 changes: 27 additions & 9 deletions tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ fn compile_cmdline<T: AsRef<OsStr>>(
mut extra_args: Vec<OsString>,
) -> Vec<OsString> {
let mut arg = match compiler {
"gcc" | "clang" | "clang++" | "nvcc" => vec_from!(OsString, exe.as_ref(), "-c", input, "-o", output),
"gcc" | "clang" | "clang++" | "nvcc" => {
vec_from!(OsString, exe.as_ref(), "-c", input, "-o", output)
}
"cl.exe" => vec_from!(OsString, exe, "-c", input, format!("-Fo{}", output)),
_ => panic!("Unsupported compiler: {}", compiler),
};
Expand All @@ -94,8 +96,8 @@ const INPUT_WITH_WHITESPACE_ALT: &str = "test_whitespace_alt.c";
const INPUT_ERR: &str = "test_err.c";
const INPUT_MACRO_EXPANSION: &str = "test_macro_expansion.c";
const INPUT_WITH_DEFINE: &str = "test_with_define.c";
const INPUT_FOR_CUDA_A : &str = "test_a.cu";
const INPUT_FOR_CUDA_B : &str = "test_b.cu";
const INPUT_FOR_CUDA_A: &str = "test_a.cu";
const INPUT_FOR_CUDA_B: &str = "test_b.cu";
const OUTPUT: &str = "test.o";

// Copy the source files into the tempdir so we can compile with relative paths, since the commandline winds up in the hash key.
Expand Down Expand Up @@ -450,7 +452,6 @@ fn run_sccache_command_tests(compiler: Compiler, tempdir: &Path) {
}
}


fn test_cuda_compiles(compiler: Compiler, tempdir: &Path) {
let Compiler {
name,
Expand All @@ -464,7 +465,13 @@ fn test_cuda_compiles(compiler: Compiler, tempdir: &Path) {
let out_file = tempdir.join(OUTPUT);
trace!("compile A");
sccache_command()
.args(&compile_cmdline(name, &exe, INPUT_FOR_CUDA_A, OUTPUT, Vec::new()))
.args(&compile_cmdline(
name,
&exe,
INPUT_FOR_CUDA_A,
OUTPUT,
Vec::new(),
))
.current_dir(tempdir)
.envs(env_vars.clone())
.assert()
Expand All @@ -481,7 +488,13 @@ fn test_cuda_compiles(compiler: Compiler, tempdir: &Path) {
trace!("compile A");
fs::remove_file(&out_file).unwrap();
sccache_command()
.args(&compile_cmdline(name, &exe, INPUT_FOR_CUDA_A, OUTPUT, Vec::new()))
.args(&compile_cmdline(
name,
&exe,
INPUT_FOR_CUDA_A,
OUTPUT,
Vec::new(),
))
.current_dir(tempdir)
.envs(env_vars.clone())
.assert()
Expand All @@ -500,7 +513,13 @@ fn test_cuda_compiles(compiler: Compiler, tempdir: &Path) {
// phase is correctly running and outputing text
trace!("compile B");
sccache_command()
.args(&compile_cmdline(name, &exe, INPUT_FOR_CUDA_B, OUTPUT, Vec::new()))
.args(&compile_cmdline(
name,
&exe,
INPUT_FOR_CUDA_B,
OUTPUT,
Vec::new(),
))
.current_dir(tempdir)
.envs(env_vars)
.assert()
Expand All @@ -517,9 +536,8 @@ fn test_cuda_compiles(compiler: Compiler, tempdir: &Path) {
});
}


fn run_sccache_cuda_command_tests(compiler: Compiler, tempdir: &Path) {
test_cuda_compiles(compiler.clone(), tempdir);
test_cuda_compiles(compiler, tempdir);
}

fn test_clang_multicall(compiler: Compiler, tempdir: &Path) {
Expand Down

0 comments on commit 2a9f4e3

Please sign in to comment.