From a39a121fc0cc900302cce82400f10b568cc790c8 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Fri, 21 Jul 2023 10:36:56 -0400 Subject: [PATCH] compiler: Support detecting compilers with pre-amble in -E --- src/compiler/compiler.rs | 67 +++++++++++++++++++++------------------- src/test/tests.rs | 2 +- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index efbd7ac6..b2e257ee 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -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?; @@ -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() { @@ -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() @@ -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() @@ -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, @@ -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() @@ -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() @@ -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(), @@ -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() @@ -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], @@ -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], @@ -1820,7 +1825,7 @@ LLVM version: 6.0", let storage = MockStorage::new(None); let storage: Arc = 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], @@ -1901,7 +1906,7 @@ LLVM version: 6.0", let storage = MockStorage::new(Some(storage_delay)); let storage: Arc = 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], @@ -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], @@ -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(), @@ -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], diff --git a/src/test/tests.rs b/src/test/tests.rs index 821ba91e..003cbafa 100644 --- a/src/test/tests.rs +++ b/src/test/tests.rs @@ -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),