diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2022-11-03 21:47:59 +0100 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2022-11-03 22:10:48 +0100 |
commit | 84c58a57e020174d0d4098a620cba03c3c0284c6 (patch) | |
tree | 500962caa4deb0b5820c4fc4f8ed8e6fcf9fd672 | |
parent | dfe680320c9208d172af1f22ccb065fa7bc5ad6f (diff) | |
download | ccache-84c58a57e020174d0d4098a620cba03c3c0284c6.tar.gz |
feat: Support -Wp,-M[M]D with -o without -MMD/-MQ/-MT for GCC and Clang
Closes #1203.
-rw-r--r-- | src/argprocessing.cpp | 39 | ||||
-rw-r--r-- | test/suites/direct.bash | 30 |
2 files changed, 51 insertions, 18 deletions
diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index e26ca590..0bc37e57 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -1153,16 +1153,6 @@ process_args(Context& ctx) LOG_RAW("-Wp,-M[M]D in combination with -MF is not supported"); return Statistic::unsupported_compiler_option; } - if (state.found_wp_md_or_mmd_opt && !args_info.output_obj.empty() - && !state.found_md_or_mmd_opt && !args_info.dependency_target) { - // GCC and Clang behave differently when "-Wp,-M[M]D,wp.d" is used with "-o" - // but with neither "-MMD" nor "-MT"/"-MQ": GCC uses a dependency target - // based on the source filename but Clang bases it on the output filename. - // We could potentially support by behaving differently depending on the - // compiler type, but let's just bail out for now. - LOG_RAW("-Wp,-M[M]D with -o without -MMD, -MQ or -MT is not supported"); - return Statistic::unsupported_compiler_option; - } // Don't try to second guess the compiler's heuristics for stdout handling. if (args_info.output_obj == "-") { @@ -1402,8 +1392,33 @@ process_args(Context& ctx) } if (!args_info.dependency_target) { - args_info.dependency_target = - Depfile::escape_filename(args_info.orig_output_obj); + std::string dep_target = args_info.orig_output_obj; + + // GCC and Clang behave differently when "-Wp,-M[M]D,wp.d" is used with + // "-o" but with neither "-MMD" nor "-MT"/"-MQ": GCC uses a dependency + // target based on the source filename but Clang bases it on the output + // filename. + if (state.found_wp_md_or_mmd_opt && !args_info.output_obj.empty() + && !state.found_md_or_mmd_opt) { + if (config.compiler_type() == CompilerType::clang) { + // Clang does the sane thing: the dependency target is the output file + // so that the dependency file actually makes sense. + } else if (config.compiler_type() == CompilerType::gcc) { + // GCC strangely uses the base name of the source file but with a .o + // extension. + dep_target = Util::change_extension( + Util::base_name(args_info.orig_input_file), + get_default_object_file_extension(ctx.config)); + } else { + // How other compilers behave is currently unknown, so bail out. + LOG_RAW( + "-Wp,-M[M]D with -o without -MMD, -MQ or -MT is only supported for" + " GCC or Clang"); + return Statistic::unsupported_compiler_option; + } + } + + args_info.dependency_target = Depfile::escape_filename(dep_target); } } diff --git a/test/suites/direct.bash b/test/suites/direct.bash index 560aac83..8a227d0b 100644 --- a/test/suites/direct.bash +++ b/test/suites/direct.bash @@ -262,12 +262,6 @@ fi done # ----------------------------------------------------------------- - TEST "Unsupported -Wp,-MMD with -o without -MMD/-MT/-MQ" - - $CCACHE_COMPILE -c test.c -Wp,-MMD,wp.d -o object.o - expect_stat unsupported_compiler_option 1 - - # ----------------------------------------------------------------- TEST "Unsupported -Wp,-MMD with -MF" $CCACHE_COMPILE -c test.c -Wp,-MMD,wp.d -MF mf.d -o object.o @@ -418,6 +412,30 @@ fi expect_equal_object_files reference_test.o test.o # ------------------------------------------------------------------------- + TEST "-Wp,-MMD with -o without -MMD/-MT/-MQ" + + $COMPILER -c -Wp,-MMD,expected.d -o out.o "$(pwd)/test.c" + + $CCACHE_COMPILE -c -Wp,-MMD,other.d -o out.o "$(pwd)/test.c" + expect_stat direct_cache_hit 0 + expect_stat preprocessed_cache_hit 0 + expect_stat cache_miss 1 + expect_equal_text_content other.d expected.d + + rm -f other.d + $CCACHE_COMPILE -c -Wp,-MMD,other.d -o out.o "$(pwd)/test.c" + expect_stat direct_cache_hit 1 + expect_stat preprocessed_cache_hit 0 + expect_stat cache_miss 1 + expect_equal_text_content other.d expected.d + + $CCACHE_COMPILE -c -Wp,-MMD,different_name.d -o out.o "$(pwd)/test.c" + expect_stat direct_cache_hit 2 + expect_stat preprocessed_cache_hit 0 + expect_stat cache_miss 1 + expect_equal_text_content different_name.d expected.d + + # ------------------------------------------------------------------------- TEST "-Wp,-D" $CCACHE_COMPILE -c -Wp,-DFOO test.c |