Skip to content

Commit

Permalink
compiler: Support detecting compilers with pre-amble in -E
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmaynard authored and sylvestre committed Sep 28, 2023
1 parent 2a9f4e3 commit a39a121
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
67 changes: 36 additions & 31 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,34 +1082,37 @@ 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.
//
// We prefix the information we need with `compiler_id` and `compiler_version`
// so that we can support compilers that insert pre-amble code even in `-E` mode
let test = b"#if defined(__NVCC__) && !defined(_MSC_VER)
nvcc
compiler_id=nvcc
#elif defined(__NVCC__) && defined(_MSC_VER)
nvcc-msvc
compiler_id=nvcc-msvc
#elif defined(_MSC_VER) && !defined(__clang__)
msvc
compiler_id=msvc
#elif defined(_MSC_VER) && defined(_MT)
msvc-clang
compiler_id=msvc-clang
#elif defined(__clang__) && defined(__cplusplus) && defined(__apple_build_version__)
apple-clang++
compiler_id=apple-clang++
#elif defined(__clang__) && defined(__cplusplus)
clang++
compiler_id=clang++
#elif defined(__clang__) && defined(__apple_build_version__)
apple-clang
compiler_id=apple-clang
#elif defined(__clang__)
clang
compiler_id=clang
#elif defined(__GNUC__) && defined(__cplusplus)
g++
compiler_id=g++
#elif defined(__GNUC__)
gcc
compiler_id=gcc
#elif defined(__DCC__)
diab
compiler_id=diab
#elif defined(__CTC__)
tasking_vx
compiler_id=tasking_vx
#else
unknown
compiler_id=unknown
#endif
__VERSION__
compiler_version=__VERSION__
"
.to_vec();
let (tempdir, src) = write_temp_file(&pool, "testfile.c".as_ref(), test).await?;
Expand Down Expand Up @@ -1146,10 +1149,12 @@ __VERSION__
};
let mut lines = stdout.lines().filter_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
None
if line.starts_with("compiler_id=") {
Some(line.strip_prefix("compiler_id=").unwrap())
} else if line.starts_with("compiler_version=") {
Some(line.strip_prefix("compiler_version=").unwrap())
} else {
Some(line)
None
}
});
if let Some(kind) = lines.next() {
Expand Down Expand Up @@ -1293,7 +1298,7 @@ mod test {
let creator = new_creator();
let runtime = single_threaded_runtime();
let pool = runtime.handle();
next_command(&creator, Ok(MockChild::new(exit_status(0), "\n\ngcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "\n\ncompiler_id=gcc", "")));
let c = detect_compiler(creator, &f.bins[0], f.tempdir.path(), &[], &[], pool, None)
.wait()
.unwrap()
Expand All @@ -1307,7 +1312,7 @@ mod test {
let creator = new_creator();
let runtime = single_threaded_runtime();
let pool = runtime.handle();
next_command(&creator, Ok(MockChild::new(exit_status(0), "clang\n", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=clang\n", "")));
let c = detect_compiler(creator, &f.bins[0], f.tempdir.path(), &[], &[], pool, None)
.wait()
.unwrap()
Expand All @@ -1330,7 +1335,7 @@ mod test {
let prefix = String::from("blah: ");
let stdout = format!("{}{}\r\n", prefix, s);
// Compiler detection output
next_command(&creator, Ok(MockChild::new(exit_status(0), "\nmsvc\n", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "\ncompiler_id=msvc\n", "")));
// showincludes prefix detection output
next_command(
&creator,
Expand All @@ -1349,7 +1354,7 @@ mod test {
let creator = new_creator();
let runtime = single_threaded_runtime();
let pool = runtime.handle();
next_command(&creator, Ok(MockChild::new(exit_status(0), "nvcc\n", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=nvcc\n", "")));
let c = detect_compiler(creator, &f.bins[0], f.tempdir.path(), &[], &[], pool, None)
.wait()
.unwrap()
Expand Down Expand Up @@ -1481,7 +1486,7 @@ LLVM version: 6.0",
let creator = new_creator();
let runtime = single_threaded_runtime();
let pool = runtime.handle();
next_command(&creator, Ok(MockChild::new(exit_status(0), "\ndiab\n", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "\ncompiler_id=diab\n", "")));
let c = detect_compiler(creator, &f.bins[0], f.tempdir.path(), &[], &[], pool, None)
.wait()
.unwrap()
Expand Down Expand Up @@ -1544,7 +1549,7 @@ LLVM version: 6.0",
let results: Vec<_> = [11, 12]
.iter()
.map(|version| {
let output = format!("clang\n\"{}.0.0\"", version);
let output = format!("compiler_id=clang\ncompiler_version=\"{}.0.0\"", version);
next_command(&creator, Ok(MockChild::new(exit_status(0), output, "")));
let c = detect_compiler(
creator.clone(),
Expand Down Expand Up @@ -1583,7 +1588,7 @@ LLVM version: 6.0",
let pool = runtime.handle();
let f = TestFixture::new();
// Pretend to be GCC.
next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
let c = get_compiler_info(creator, &f.bins[0], f.tempdir.path(), &[], &[], pool, None)
.wait()
.unwrap()
Expand All @@ -1602,7 +1607,7 @@ LLVM version: 6.0",
let storage = DiskCache::new(f.tempdir.path().join("cache"), u64::MAX, &pool);
let storage = Arc::new(storage);
// Pretend to be GCC.
next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
let c = get_compiler_info(
creator.clone(),
&f.bins[0],
Expand Down Expand Up @@ -1713,7 +1718,7 @@ LLVM version: 6.0",
let storage = DiskCache::new(f.tempdir.path().join("cache"), u64::MAX, &pool);
let storage = Arc::new(storage);
// Pretend to be GCC.
next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
let c = get_compiler_info(
creator.clone(),
&f.bins[0],
Expand Down Expand Up @@ -1820,7 +1825,7 @@ LLVM version: 6.0",
let storage = MockStorage::new(None);
let storage: Arc<MockStorage> = Arc::new(storage);
// Pretend to be GCC.
next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
let c = get_compiler_info(
creator.clone(),
&f.bins[0],
Expand Down Expand Up @@ -1901,7 +1906,7 @@ LLVM version: 6.0",
let storage = MockStorage::new(Some(storage_delay));
let storage: Arc<MockStorage> = Arc::new(storage);
// Pretend to be GCC.
next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
let c = get_compiler_info(
creator.clone(),
&f.bins[0],
Expand Down Expand Up @@ -1974,7 +1979,7 @@ LLVM version: 6.0",
let storage = DiskCache::new(f.tempdir.path().join("cache"), u64::MAX, &pool);
let storage = Arc::new(storage);
// Pretend to be GCC.
next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
let c = get_compiler_info(
creator.clone(),
&f.bins[0],
Expand Down Expand Up @@ -2091,7 +2096,7 @@ LLVM version: 6.0",
next_command_calls(&creator, move |_| {
let mut f = File::create(&o)?;
f.write_all(b"file contents")?;
Ok(MockChild::new(exit_status(0), "gcc", ""))
Ok(MockChild::new(exit_status(0), "compiler_id=gcc", ""))
});
let c = get_compiler_info(
creator.clone(),
Expand Down Expand Up @@ -2165,7 +2170,7 @@ LLVM version: 6.0",
let storage = DiskCache::new(f.tempdir.path().join("cache"), u64::MAX, &pool);
let storage = Arc::new(storage);
// Pretend to be GCC.
next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
next_command(&creator, Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
let c = get_compiler_info(
creator.clone(),
&f.bins[0],
Expand Down
2 changes: 1 addition & 1 deletion src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn test_server_compile() {
{
let mut c = server_creator.lock().unwrap();
// The server will check the compiler. Pretend it's GCC.
c.next_command_spawns(Ok(MockChild::new(exit_status(0), "gcc", "")));
c.next_command_spawns(Ok(MockChild::new(exit_status(0), "compiler_id=gcc", "")));
// Preprocessor invocation.
c.next_command_spawns(Ok(MockChild::new(
exit_status(0),
Expand Down

0 comments on commit a39a121

Please sign in to comment.