From: Tadej Novak Date: Wed, 12 Jul 2023 11:50:31 +0000 (+0200) Subject: feat: Support multiple -Xarch_* arguments matching -arch (#1131) X-Git-Tag: v4.9~135 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab7dd6ff61dc8a39f0f19f63b82943917c9cf2c8;p=thirdparty%2Fccache.git feat: Support multiple -Xarch_* arguments matching -arch (#1131) --- diff --git a/src/ArgsInfo.hpp b/src/ArgsInfo.hpp index b131f42d8..60cadce20 100644 --- a/src/ArgsInfo.hpp +++ b/src/ArgsInfo.hpp @@ -22,6 +22,7 @@ #include #include +#include #include // This class holds meta-information derived from the compiler arguments. @@ -131,6 +132,9 @@ struct ArgsInfo // Architectures from -arch options. std::vector arch_args; + // Values for -Xarch_* options. + std::unordered_map> xarch_args; + // Relocating debuginfo in the format old=new. std::vector debug_prefix_maps; diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 9acee120d..63a432ca6 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -66,7 +66,7 @@ struct ArgumentProcessingState ColorDiagnostics color_diagnostics = ColorDiagnostics::automatic; bool found_directives_only = false; bool found_rewrite_includes = false; - std::optional found_xarch_arch; + std::unordered_map> xarch_args; bool found_mf_opt = false; bool found_wp_md_or_mmd_opt = false; bool found_md_or_mmd_opt = false; @@ -399,20 +399,17 @@ process_option_arg(const Context& ctx, config.set_direct_mode(false); } - // -Xarch_* options are too hard. + // -Xarch_* options need to be handled with care if (util::starts_with(arg, "-Xarch_")) { if (i == args.size() - 1) { LOG("Missing argument to {}", args[i]); return Statistic::bad_compiler_arguments; } const auto arch = arg.substr(7); - if (!state.found_xarch_arch) { - state.found_xarch_arch = arch; - } else if (*state.found_xarch_arch != arch) { - LOG_RAW("Multiple different -Xarch_* options not supported"); - return Statistic::unsupported_compiler_option; - } - state.common_args.push_back(args[i]); + auto [it, inserted] = + state.xarch_args.emplace(arch, std::vector()); + it->second.emplace_back(args[i + 1]); + ++i; return Statistic::none; } @@ -1513,20 +1510,28 @@ process_args(Context& ctx) compiler_args.push_back("-dc"); } - if (state.found_xarch_arch && !args_info.arch_args.empty()) { - if (args_info.arch_args.size() > 1) { - LOG_RAW( - "Multiple -arch options in combination with -Xarch_* not supported"); - return Statistic::unsupported_compiler_option; - } else if (args_info.arch_args[0] != *state.found_xarch_arch) { - LOG_RAW("-arch option not matching -Xarch_* option not supported"); - return Statistic::unsupported_compiler_option; + if (!state.xarch_args.empty()) { + for (const auto& arch : args_info.arch_args) { + auto it = state.xarch_args.find(arch); + if (it != state.xarch_args.end()) { + args_info.xarch_args.emplace(arch, it->second); + } } } for (const auto& arch : args_info.arch_args) { compiler_args.push_back("-arch"); compiler_args.push_back(arch); + + auto it = args_info.xarch_args.find(arch); + if (it != args_info.xarch_args.end()) { + args_info.xarch_args.emplace(arch, it->second); + + for (const auto& xarch : it->second) { + compiler_args.push_back("-Xarch_" + arch); + compiler_args.push_back(xarch); + } + } } Args preprocessor_args = state.common_args; diff --git a/src/ccache.cpp b/src/ccache.cpp index 351007708..bc21ea18a 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2012,6 +2012,15 @@ calculate_result_and_manifest_key(Context& ctx, for (const auto& arch : ctx.args_info.arch_args) { hash.hash_delimiter("-arch"); hash.hash(arch); + + // Adding -Xarch_* to hash since cpp output is affected. + auto it = ctx.args_info.xarch_args.find(arch); + if (it != ctx.args_info.xarch_args.end()) { + for (const auto& xarch : it->second) { + hash.hash_delimiter("-Xarch_" + arch); + hash.hash(xarch); + } + } } std::optional result_key; @@ -2037,19 +2046,31 @@ calculate_result_and_manifest_key(Context& ctx, } else { preprocessor_args->push_back("-arch"); for (size_t i = 0; i < ctx.args_info.arch_args.size(); ++i) { - preprocessor_args->push_back(ctx.args_info.arch_args[i]); + const auto& arch = ctx.args_info.arch_args[i]; + size_t xarch_count{}; + preprocessor_args->push_back(arch); + auto it = ctx.args_info.xarch_args.find(arch); + if (it != ctx.args_info.xarch_args.end()) { + for (const auto& xarch : it->second) { + preprocessor_args->push_back("-Xarch_" + arch); + preprocessor_args->push_back(xarch); + xarch_count += 2; + } + } const auto digest = get_result_key_from_cpp(ctx, *preprocessor_args, hash); if (!digest) { return nonstd::make_unexpected(digest.error()); } result_key = *digest; - LOG("Got result key from preprocessor with -arch {}", - ctx.args_info.arch_args[i]); + LOG("Got result key from preprocessor with -arch {}", arch); if (i != ctx.args_info.arch_args.size() - 1) { result_key = std::nullopt; } preprocessor_args->pop_back(); + if (xarch_count > 0) { + preprocessor_args->pop_back(xarch_count); + } } preprocessor_args->pop_back(); } diff --git a/test/suites/multi_arch.bash b/test/suites/multi_arch.bash index 058bae2a7..460ff16de 100644 --- a/test/suites/multi_arch.bash +++ b/test/suites/multi_arch.bash @@ -36,20 +36,43 @@ SUITE_multi_arch() { expect_stat direct_cache_hit 2 expect_stat cache_miss 3 - # A single -Xarch_* matching -arch is supported. CCACHE_DEBUG=1 $CCACHE_COMPILE -arch x86_64 -Xarch_x86_64 -I. -c test1.c expect_stat direct_cache_hit 2 expect_stat cache_miss 4 - expect_contains test1.o.*.ccache-log "clang -Xarch_x86_64 -I. -arch x86_64 -E" - expect_not_contains test1.o.*.ccache-log "clang -Xarch_x86_64 -I. -I. -arch x86_64 -E" + expect_contains test1.o.*.ccache-log "clang -arch x86_64 -Xarch_x86_64 -I. -E" + expect_not_contains test1.o.*.ccache-log "clang -arch x86_64 -Xarch_x86_64 -I. -I. -E" $CCACHE_COMPILE -arch x86_64 -Xarch_x86_64 -I. -c test1.c expect_stat direct_cache_hit 3 expect_stat cache_miss 4 - # The parameter following -Xarch should be processed. - $CCACHE_COMPILE -arch x86_64 -Xarch_x86_64 -analyze -I. -c test1.c - expect_stat unsupported_compiler_option 1 + # ------------------------------------------------------------------------- + TEST "cache hit, direct mode, multiple -Xarch_* arguments" + + CCACHE_DEBUG=1 $CCACHE_COMPILE -arch x86_64 -arch arm64 -Xarch_x86_64 -I. -Xarch_arm64 -I. -c test1.c + expect_stat direct_cache_hit 0 + expect_stat cache_miss 1 + expect_contains test1.o.*.ccache-log "clang -arch x86_64 -Xarch_x86_64 -I. -E" + expect_not_contains test1.o.*.ccache-log "clang -arch x86_64 -Xarch_x86_64 -I. -I. -E" + expect_contains test1.o.*.ccache-log "clang -arch arm64 -Xarch_arm64 -I. -E" + expect_not_contains test1.o.*.ccache-log "clang -arch arm64 -Xarch_arm64 -I. -I. -E" + + $CCACHE_COMPILE -arch x86_64 -arch arm64 -Xarch_x86_64 -I. -Xarch_arm64 -I. -c test1.c + expect_stat direct_cache_hit 1 + expect_stat cache_miss 1 + + # ------------------------------------------------------------------------- + TEST "cache hit, direct mode, multiple -Xarch_* arguments for single arch" + + CCACHE_DEBUG=1 $CCACHE_COMPILE -arch x86_64 -Xarch_x86_64 -I. -Xarch_x86_64 -Ifoo -c test1.c + expect_stat direct_cache_hit 0 + expect_stat cache_miss 1 + expect_contains test1.o.*.ccache-log "clang -arch x86_64 -Xarch_x86_64 -I. -Xarch_x86_64 -Ifoo -E" + expect_not_contains test1.o.*.ccache-log "clang -arch x86_64 -Xarch_x86_64 -I. -I. -Xarch_x86_64 -Ifoo -E" + + $CCACHE_COMPILE -arch x86_64 -Xarch_x86_64 -I. -Xarch_x86_64 -Ifoo -c test1.c + expect_stat direct_cache_hit 1 + expect_stat cache_miss 1 # ------------------------------------------------------------------------- TEST "cache hit, preprocessor mode"