Support C++20 modules, GCC part

Copybara Import from https://github.com/bazelbuild/rules_cc/pull/774

BEGIN_PUBLIC
Support C++20 modules, GCC part (#774)

Update gcc_deps_scanner_wrapper.sh.tpl to use a temporary file for dependency output and rename it after processing. Change file extension for gcc module artifacts from .pcm to .gcm in unix_cc_toolchain_config.bzl.

Related issue: https://github.com/bazelbuild/bazel/issues/30023

Closes #774
END_PUBLIC

Tested:
    TAP --sample ran all affected tests and none failed
    []
COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_cc/pull/774 from PikachuHyA:upstream_gcc_support 730bd1c18ad2afeb7f747d3fe9d43b777befc511
PiperOrigin-RevId: 949160476
Change-Id: I25488ca7f3fcd7a42726754b0942f91231e5a70b
diff --git a/cc/private/toolchain/gcc_deps_scanner_wrapper.sh.tpl b/cc/private/toolchain/gcc_deps_scanner_wrapper.sh.tpl
index 2c94f01..a43d239 100644
--- a/cc/private/toolchain/gcc_deps_scanner_wrapper.sh.tpl
+++ b/cc/private/toolchain/gcc_deps_scanner_wrapper.sh.tpl
@@ -9,4 +9,6 @@
 
 # Call the C++ compiler
 
-%{cc} -E -x c++ -fmodules-ts -fdeps-file=out.tmp -fdeps-format=p1689r5 "$@" >"$DEPS_SCANNER_OUTPUT_FILE"
+%{cc} -E -x c++ -fmodules-ts -fdeps-file="$DEPS_SCANNER_OUTPUT_FILE".tmp -fdeps-format=p1689r5 "$@"
+
+mv "$DEPS_SCANNER_OUTPUT_FILE".tmp "$DEPS_SCANNER_OUTPUT_FILE"
diff --git a/cc/private/toolchain/unix_cc_toolchain_config.bzl b/cc/private/toolchain/unix_cc_toolchain_config.bzl
index bc76ec1..3db2bce 100644
--- a/cc/private/toolchain/unix_cc_toolchain_config.bzl
+++ b/cc/private/toolchain/unix_cc_toolchain_config.bzl
@@ -13,6 +13,8 @@
 # limitations under the License.
 """A Starlark cc_toolchain configuration rule"""
 
+#buildifier: disable=bzl-visibility
+load("@bazel_features//private:util.bzl", "ge")
 load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
 load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
 load(
@@ -35,6 +37,12 @@
 load("@rules_cc//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo")
 load("@rules_cc//cc/toolchains:feature_injection.bzl", "FeatureInfo", "convert_feature")
 
+def _cpp_module_extension(compiler):
+    """Returns the BMI/CMI file extension for cpp_module artifact_name_pattern."""
+    if compiler == "gcc" and ge("9.0.0"):
+        return ".gcm"
+    return ".pcm"
+
 def _target_os_version(ctx):
     xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
     return xcode_config.minimum_os_for_platform_type(apple_common.platform_type.macos)
@@ -1891,7 +1899,7 @@
             artifact_name_pattern(
                 category_name = "cpp_module",
                 prefix = "",
-                extension = ".pcm",
+                extension = _cpp_module_extension(ctx.attr.compiler),
             ),
         ]
         features = [