]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Write debug log file in most argument processing error scenarios
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 4 Mar 2021 20:34:07 +0000 (21:34 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 5 Mar 2021 06:28:56 +0000 (07:28 +0100)
When the debug mode is enabled, ccache writes the log to
<objectfile>.ccache-log, but that can only be done if the object file
location has been determined. If an unsupported compiler option is
detected, ccache exits early and may not yet have determined the output
object filename.

Fix this by parsing all compiler options, then determining the object
file location and only then return an error from the process_args
function with. This will in practice make the debug mode work for most
invocations. There are still edge cases where it won’t work or will be
potentially confusing, for instance these:

    ccache gcc -dumpspecs
    ccache gcc -c nonexistent.c
    ccache gcc -c foo.c bar.c

Naturally, no debug log file will be written in the first case. In the
second case no debug log file will be written either since ccache
doesn’t consider a nonexistent file to be an input file. In the third
case the debug log file will be written foo.o.ccache-log but not
bar.o.ccache-log.

As mentioned in issue #806.

src/argprocessing.cpp
test/suites/base.bash

index 3dd0358a5ffd861803bd6ebb5130c551ccf1f7d7..ec8d7952b27bb3ab7f68b4089282d5b85acb19c3 100644 (file)
@@ -970,13 +970,35 @@ process_args(Context& ctx)
 
   state.common_args.push_back(args[0]); // Compiler
 
+  optional<Statistic> argument_error;
   for (size_t i = 1; i < args.size(); i++) {
-    auto error = process_arg(ctx, args, i, state);
-    if (error) {
-      return *error;
+    const auto error = process_arg(ctx, args, i, state);
+    if (error && !argument_error) {
+      argument_error = error;
     }
   }
 
+  // Don't try to second guess the compiler's heuristics for stdout handling.
+  if (args_info.output_obj == "-") {
+    LOG_RAW("Output file is -");
+    return Statistic::output_to_stdout;
+  }
+
+  // Determine output object file.
+  const bool implicit_output_obj = args_info.output_obj.empty();
+  if (implicit_output_obj && !args_info.input_file.empty()) {
+    string_view extension = state.found_S_opt ? ".s" : ".o";
+    args_info.output_obj =
+      Util::change_extension(Util::base_name(args_info.input_file), extension);
+  }
+
+  // On argument processing error, return now since we have determined
+  // args_info.output_obj which is needed to determine the log filename in
+  // CCACHE_DEBUG mode.
+  if (argument_error) {
+    return *argument_error;
+  }
+
   if (state.generating_debuginfo_level_3 && !config.run_second_cpp()) {
     LOG_RAW("Generating debug info level 3; not compiling preprocessed code");
     config.set_run_second_cpp(true);
@@ -1022,6 +1044,10 @@ process_args(Context& ctx)
     args_info.actual_language.find("-header") != std::string::npos
     || Util::is_precompiled_header(args_info.output_obj);
 
+  if (args_info.output_is_precompiled_header && implicit_output_obj) {
+    args_info.output_obj = args_info.input_file + ".gch";
+  }
+
   if (args_info.output_is_precompiled_header
       && !(config.sloppiness() & SLOPPY_PCH_DEFINES)) {
     LOG_RAW(
@@ -1069,22 +1095,6 @@ process_args(Context& ctx)
     config.set_cpp_extension(extension_for_language(p_language).substr(1));
   }
 
-  // Don't try to second guess the compilers heuristics for stdout handling.
-  if (args_info.output_obj == "-") {
-    LOG_RAW("Output file is -");
-    return Statistic::output_to_stdout;
-  }
-
-  if (args_info.output_obj.empty()) {
-    if (args_info.output_is_precompiled_header) {
-      args_info.output_obj = args_info.input_file + ".gch";
-    } else {
-      string_view extension = state.found_S_opt ? ".s" : ".o";
-      args_info.output_obj = Util::change_extension(
-        Util::base_name(args_info.input_file), extension);
-    }
-  }
-
   if (args_info.seen_split_dwarf) {
     args_info.output_dwo = Util::change_extension(args_info.output_obj, ".dwo");
   }
index e685f29b57999aa58f7fb3d1b1bfb6434cde3b34..4c3071d56347721a1a0c477570ab8233bcf92b9c 100644 (file)
@@ -351,6 +351,13 @@ fi
         fi
     done
 
+    # -------------------------------------------------------------------------
+    TEST "CCACHE_DEBUG with too hard option"
+
+    CCACHE_DEBUG=1 $CCACHE_COMPILE -c test1.c -save-temps
+    expect_stat 'unsupported compiler option' 1
+    expect_exists test1.o.ccache-log
+
     # -------------------------------------------------------------------------
     TEST "CCACHE_DISABLE"