]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Add knowledge about options related to react-native builds (#1567)
authorkzlar <120426485+kzlar@users.noreply.github.com>
Wed, 5 Mar 2025 17:37:04 +0000 (19:37 +0200)
committerGitHub <noreply@github.com>
Wed, 5 Mar 2025 17:37:04 +0000 (18:37 +0100)
- 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

src/ccache/argprocessing.cpp
src/ccache/argsinfo.hpp
src/ccache/ccache.cpp
src/ccache/compopt.cpp
test/suites/basedir.bash
unittest/test_argprocessing.cpp

index c2eaa6f9d67a1ac0fa902996097340f3d83af870..0293b1f73ceee3673dc5f8d6a40206eb37d8cca0 100644 (file)
@@ -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
index 9e6c37d9ad04add83b99f09a82e0a85b7506e752..e7f878ed29a7e951652e0d6674db748e7fe7e3b6 100644 (file)
@@ -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;
 };
index 7a37d5760d4120d8976d21bce9f9093222b4a994..d60b3307c22da2ea559e85784644d2317f778e89 100644 (file)
@@ -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())) {
index 49d0bff95d52575cfe4c79669ade0e184cbd187a..dd9cceca8b355604515757cb053abc0823cbe20a 100644 (file)
@@ -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",
index e739cf559d7047369acaa8fce3783da6be5b287a..43e26dafc7fb9ad6fd0349097ec93dbb13d55b3b 100644 (file)
@@ -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=
index 187024ded27c2038603f1450489ae9f58a214b29..b8f51ae3b2f9bc74c5cd56dfa8d9e71b2a60d85f 100644 (file)
@@ -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;