From: kzlar <120426485+kzlar@users.noreply.github.com> Date: Wed, 5 Mar 2025 17:37:04 +0000 (+0200) Subject: feat: Add knowledge about options related to react-native builds (#1567) X-Git-Tag: v4.11~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7bade456324afd450dcdc107b7dee9fe3324c660;p=thirdparty%2Fccache.git feat: Add knowledge about options related to react-native builds (#1567) - Add compopt entries for -ivfsoverlay, -fmodules-cache-path, -fmodule-map-file and -fbuild-session-file - Add hashing of build session file mtime unless sloppiness flag is set - Add unit tests for the flags above as well as an e2e test for -fbuild-session-file --- diff --git a/src/ccache/argprocessing.cpp b/src/ccache/argprocessing.cpp index c2eaa6f9..0293b1f7 100644 --- a/src/ccache/argprocessing.cpp +++ b/src/ccache/argprocessing.cpp @@ -1120,6 +1120,11 @@ process_option_arg(const Context& ctx, return Statistic::none; } + if (util::starts_with(arg, "-fbuild-session-file") + && !(config.sloppiness().contains(core::Sloppy::time_macros))) { + args_info.build_session_file = arg.substr(arg.find('=') + 1); + } + if (config.sloppiness().contains(core::Sloppy::clang_index_store) && arg == "-index-store-path") { // Xcode 9 or later calls Clang with this option. The given path includes a diff --git a/src/ccache/argsinfo.hpp b/src/ccache/argsinfo.hpp index 9e6c37d9..e7f878ed 100644 --- a/src/ccache/argsinfo.hpp +++ b/src/ccache/argsinfo.hpp @@ -164,4 +164,7 @@ struct ArgsInfo // Compilation directory as passed in -ffile-compilation-dir or // -fdebug-compilation-dir. std::string compilation_dir; + + // Build session file as passed in -fbuild-session-file. + std::filesystem::path build_session_file; }; diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index 7a37d576..d60b3307 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -1688,6 +1688,14 @@ hash_common_info(const Context& ctx, const Args& args, Hash& hash) } } + if (!(ctx.args_info.build_session_file.empty())) { + // When using -fbuild-session-file, the actual mtime needs to be + // added to the hash to prevent false positive cache hits if the + // mtime of the file changes. + hash.hash_delimiter("-fbuild-session-file mtime"); + hash.hash(DirEntry(ctx.args_info.build_session_file).mtime().nsec()); + } + if (!ctx.config.extra_files_to_hash().empty()) { for (const auto& path : util::split_path_list(ctx.config.extra_files_to_hash())) { diff --git a/src/ccache/compopt.cpp b/src/ccache/compopt.cpp index 49d0bff9..dd9cceca 100644 --- a/src/ccache/compopt.cpp +++ b/src/ccache/compopt.cpp @@ -124,7 +124,10 @@ const CompOpt compopts[] = { {"-emit-pth", AFFECTS_COMP}, // Clang {"-external:I", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH}, // msvc + {"-fbuild-session-file=", TAKES_CONCAT_ARG | TAKES_PATH}, {"-fmodule-header", TOO_HARD}, + {"-fmodule-map-file=", TAKES_CONCAT_ARG | TAKES_PATH}, + {"-fmodules-cache-path=", TAKES_CONCAT_ARG | TAKES_PATH}, {"-fmodules-ts", TOO_HARD}, {"-fno-working-directory", AFFECTS_CPP}, {"-fplugin=libcc1plugin", TOO_HARD}, // interaction with GDB @@ -147,7 +150,7 @@ const CompOpt compopts[] = { {"-iquote", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH}, {"-isysroot", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH}, {"-isystem", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH}, - {"-ivfsoverlay", TAKES_ARG}, + {"-ivfsoverlay", TAKES_ARG | TAKES_PATH}, {"-ivfsstatcache", TAKES_ARG}, {"-iwithprefix", AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH}, {"-iwithprefixbefore", diff --git a/test/suites/basedir.bash b/test/suites/basedir.bash index e739cf55..43e26daf 100644 --- a/test/suites/basedir.bash +++ b/test/suites/basedir.bash @@ -17,6 +17,8 @@ int test; EOF cp -r dir1 dir2 backdate dir1/include/test.h dir2/include/test.h + mkdir dir3 + touch dir3/build-session-file.bin } SUITE_basedir() { @@ -253,6 +255,26 @@ EOF cd .. done + # ------------------------------------------------------------------------- + if $COMPILER_TYPE_CLANG; then + TEST "-fbuild-session-file/absolute/path" + + build_session_file_path="$(pwd)/dir3/build-session-file.bin" + cd dir1 + CCACHE_BASEDIR="$(pwd)" $CCACHE_COMPILE -I"$(pwd)/include" -fbuild-session-file="$build_session_file_path" -c src/test.c + expect_stat direct_cache_hit 0 + expect_stat preprocessed_cache_hit 0 + expect_stat cache_miss 1 + cd .. + + cd dir2 + CCACHE_BASEDIR="$(pwd)" $CCACHE_COMPILE -I"$(pwd)/include" -fbuild-session-file="$build_session_file_path" -c src/test.c + expect_stat direct_cache_hit 1 + expect_stat preprocessed_cache_hit 0 + expect_stat cache_miss 1 + cd .. + fi + # ------------------------------------------------------------------------- if $HOST_OS_WINDOWS; then additional_options= diff --git a/unittest/test_argprocessing.cpp b/unittest/test_argprocessing.cpp index 187024de..b8f51ae3 100644 --- a/unittest/test_argprocessing.cpp +++ b/unittest/test_argprocessing.cpp @@ -317,6 +317,95 @@ TEST_CASE( CHECK(result->preprocessor_args[2] == "foo"); } +TEST_CASE("fbuild_session_file_should_be_rewritten_if_basedir_is_used") +{ + TestContext test_context; + + Context ctx; + + util::write_file("foo.c", ""); + ctx.config.set_base_dir(get_root()); + std::string arg_string = + FMT("cc -fbuild-session-file={}/foo/bar -c foo.c", ctx.actual_cwd); + ctx.orig_args = Args::from_string(arg_string); + + const auto result = process_args(ctx); + CHECK(result); +#ifdef _WIN32 + CHECK(result->preprocessor_args[1] == "-fbuild-session-file=foo\\bar"); +#else + CHECK(result->preprocessor_args[1] == "-fbuild-session-file=foo/bar"); +#endif +} + +TEST_CASE( + "ivfsoverlay_with_separate_argument_should_be_rewritten_if_basedir_is_used") +{ + TestContext test_context; + + Context ctx; + ctx.config.update_from_map({{"sloppiness", "ivfsoverlay"}}); + + util::write_file("foo.c", ""); + ctx.config.set_base_dir(get_root()); + std::string arg_string = + FMT("cc -ivfsoverlay {}/foo -c foo.c", ctx.actual_cwd); + ctx.orig_args = Args::from_string(arg_string); + + const auto result = process_args(ctx); + CHECK(result); + CHECK(result->preprocessor_args[1] == "-ivfsoverlay"); + CHECK(result->preprocessor_args[2] == "foo"); +} + +TEST_CASE( + "fmodules_cache_path_with_separate_argument_should_be_rewritten_if_basedir_" + "is_used") +{ + TestContext test_context; + + Context ctx; + ctx.config.update_from_map({{"sloppiness", "modules"}}); + + util::write_file("foo.c", ""); + ctx.config.set_base_dir(get_root()); + std::string arg_string = + FMT("cc -fmodules-cache-path={}/foo/bar -c foo.c", ctx.actual_cwd); + ctx.orig_args = Args::from_string(arg_string); + + const auto result = process_args(ctx); + CHECK(result); +#ifdef _WIN32 + CHECK(result->preprocessor_args[1] == "-fmodules-cache-path=foo\\bar"); +#else + CHECK(result->preprocessor_args[1] == "-fmodules-cache-path=foo/bar"); +#endif +} + +TEST_CASE( + "fmodules_map_file_with_separate_argument_should_be_rewritten_if_basedir_" + "is_used") +{ + TestContext test_context; + + Context ctx; + ctx.config.update_from_map({{"sloppiness", "modules"}}); + + util::write_file("foo.c", ""); + ctx.config.set_base_dir(get_root()); + std::string arg_string = + FMT("cc -fmodule-map-file={}/foo/bar -c foo.c", ctx.actual_cwd); + ctx.orig_args = Args::from_string(arg_string); + + const auto result = process_args(ctx); + CHECK(result); +#ifdef _WIN32 + CHECK(result->preprocessor_args[1] == "-fmodule-map-file=foo\\bar"); +#else + CHECK(result->preprocessor_args[1] == "-fmodule-map-file=foo/bar"); +#endif +} + TEST_CASE("MF_flag_with_immediate_argument_should_work_as_last_argument") { TestContext test_context;