]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Add auto depend mode for NVCC on Windows (#1750)
authorXing Xiwen <6387840+risingwen@users.noreply.github.com>
Wed, 29 Jul 2026 11:07:48 +0000 (19:07 +0800)
committerGitHub <noreply@github.com>
Wed, 29 Jul 2026 11:07:48 +0000 (13:07 +0200)
src/ccache/argprocessing.cpp
src/ccache/compiler/msvc.cpp
unittest/test_argprocessing.cpp
unittest/test_compiler_msvc.cpp

index f9cf363f996ab5c3d74c480e891aa7c33153fd79..75859a3e079d533deeb96f2997e6c781529a7dae 100644 (file)
@@ -376,6 +376,15 @@ is_msvc_z_debug_option(std::string_view arg)
          != std::end(debug_options);
 }
 
+bool
+is_msvc_show_includes_option(std::string_view arg)
+{
+  return arg == "-showIncludes"
+         || arg == "/showIncludes"
+         // clang-cl:
+         || arg == "-showIncludes:user" || arg == "/showIncludes:user";
+}
+
 // Returns std::nullopt if the option wasn't recognized, otherwise the error
 // code (with Statistic::none for "no error").
 std::optional<Statistic>
@@ -931,11 +940,19 @@ process_option_arg(const Context& ctx,
     return Statistic::none;
   }
 
-  if (arg == "-showIncludes"
-      // clang-cl:
-      || arg == "-showIncludes:user") {
+  if (is_msvc_show_includes_option(arg)) {
+    args_info.generating_includes = true;
+    state.add_compiler_only_arg(args[i]);
+    return Statistic::none;
+  }
+
+  if (ctx.config.compiler_type() == CompilerType::nvcc
+      && (arg == "-Xcompiler" || arg == "--compiler-options")
+      && i < args.size() - 1 && is_msvc_show_includes_option(args[i + 1])) {
     args_info.generating_includes = true;
     state.add_compiler_only_arg(args[i]);
+    state.add_compiler_only_arg(args[i + 1]);
+    ++i;
     return Statistic::none;
   }
 
@@ -1912,11 +1929,20 @@ process_args(Context& ctx)
     state.add_compiler_only_arg_no_hash(*diagnostics_color_arg);
   }
 
-  if (ctx.config.depend_mode() && !args_info.generating_includes
-      && ctx.config.compiler_type() == CompilerType::msvc) {
-    ctx.auto_depend_mode = true;
-    args_info.generating_includes = true;
-    state.add_compiler_only_arg_no_hash("/showIncludes");
+  if (ctx.config.depend_mode() && !args_info.generating_includes) {
+    if (ctx.config.compiler_type() == CompilerType::msvc) {
+      ctx.auto_depend_mode = true;
+      args_info.generating_includes = true;
+      state.add_compiler_only_arg_no_hash("/showIncludes");
+    }
+#ifdef _WIN32
+    else if (ctx.config.compiler_type() == CompilerType::nvcc) {
+      ctx.auto_depend_mode = true;
+      args_info.generating_includes = true;
+      state.add_compiler_only_arg_no_hash("-Xcompiler");
+      state.add_compiler_only_arg_no_hash("/showIncludes");
+    }
+#endif
   }
 
   if (state.found_c_opt) {
index 24cb3581b974c6ff0e05875bc23805bb60d91daa..491b276dd2017efbd88f79d62977f3961a66397e 100644 (file)
@@ -57,8 +57,12 @@ strip_includes_from_msvc_show_includes(const Context& ctx,
   using Mode = Tokenizer::Mode;
   using IncludeDelimiter = Tokenizer::IncludeDelimiter;
 
-  if (stdout_data.empty() || !ctx.auto_depend_mode
-      || ctx.config.compiler_type() != CompilerType::msvc) {
+  const bool strip_auto_includes =
+    ctx.auto_depend_mode
+    && (ctx.config.compiler_type() == CompilerType::msvc
+        || ctx.config.compiler_type() == CompilerType::nvcc);
+
+  if (stdout_data.empty() || !strip_auto_includes) {
     return std::move(stdout_data);
   }
 
index 3b108589bcc31b73f4e6a1a6c1188fb2392e653c..aadacfdbd523469e859532edcc7ebda0b3917259 100644 (file)
@@ -590,6 +590,51 @@ TEST_CASE("nvcc_warning_flags_long")
         == "nvcc --Werror all-warnings -Xcompiler -Werror -c");
 }
 
+TEST_CASE("nvcc_show_includes_via_host_compiler_option")
+{
+  TestContext test_context;
+  Context ctx;
+  ctx.config.set_compiler_type(CompilerType::nvcc);
+  ctx.config.set_depend_mode(true);
+  ctx.orig_args = Args::from_string("nvcc -c foo.cu -Xcompiler /showIncludes");
+  REQUIRE(util::write_file("foo.cu", ""));
+  const auto result = process_args(ctx);
+
+  CHECK(result);
+  CHECK(ctx.args_info.generating_includes);
+  CHECK(result->preprocessor_args.to_string() == "nvcc");
+  CHECK(result->extra_args_to_hash.to_string() == "-Xcompiler /showIncludes");
+  CHECK(result->compiler_args.to_string()
+        == "nvcc -Xcompiler /showIncludes -c");
+}
+
+TEST_CASE("nvcc_auto_depend_mode")
+{
+  TestContext test_context;
+  Context ctx;
+  ctx.config.set_compiler_type(CompilerType::nvcc);
+  ctx.config.set_depend_mode(true);
+  ctx.orig_args = Args::from_string("nvcc -c foo.cu");
+  REQUIRE(util::write_file("foo.cu", ""));
+  const auto result = process_args(ctx);
+
+  CHECK(result);
+#ifdef _WIN32
+  CHECK(ctx.auto_depend_mode);
+  CHECK(ctx.args_info.generating_includes);
+  CHECK(result->preprocessor_args.to_string() == "nvcc");
+  CHECK(result->extra_args_to_hash.to_string() == "");
+  CHECK(result->compiler_args.to_string()
+        == "nvcc -Xcompiler /showIncludes -c");
+#else
+  CHECK(!ctx.auto_depend_mode);
+  CHECK(!ctx.args_info.generating_includes);
+  CHECK(result->preprocessor_args.to_string() == "nvcc");
+  CHECK(result->extra_args_to_hash.to_string() == "");
+  CHECK(result->compiler_args.to_string() == "nvcc -c");
+#endif
+}
+
 TEST_CASE("-Xclang")
 {
   TestContext test_context;
index 74c2241fafa881b517bc331626e73b74232ab6a2..8033e0965e64d5f09fc1622cc23aaeb87eb8e53e 100644 (file)
@@ -171,6 +171,25 @@ TEST_CASE("strip_includes_from_msvc_show_includes")
                     "Third custom line\n"));
     CHECK(result == util::to_span("First\nSecond\nThird custom line\n"));
   }
+
+  SUBCASE("NVCC auto depend mode")
+  {
+    ctx.config.set_compiler_type(CompilerType::nvcc);
+    const util::Bytes result =
+      compiler::strip_includes_from_msvc_show_includes(ctx, util::Bytes(input));
+    CHECK(result == util::to_span("First\nSecond\n"));
+  }
+
+  SUBCASE("NVCC explicit showIncludes")
+  {
+    ctx.auto_depend_mode = false;
+    ctx.config.set_compiler_type(CompilerType::nvcc);
+    ctx.config.set_depend_mode(true);
+    ctx.args_info.generating_includes = true;
+    const util::Bytes result =
+      compiler::strip_includes_from_msvc_show_includes(ctx, util::Bytes(input));
+    CHECK(result == input);
+  }
 }
 
 TEST_SUITE_END();