Add support for params files for darwin

Clang has supported params files for a while now. This updates the cc
toolchain for darwin to use them.

The logic for processing response files is mostly copied from
rules_swift where similar processing is done.

Closes #12265.

PiperOrigin-RevId: 342013390
diff --git a/tools/cpp/BUILD.tools b/tools/cpp/BUILD.tools
index dc72e23..db27b19 100644
--- a/tools/cpp/BUILD.tools
+++ b/tools/cpp/BUILD.tools
@@ -277,7 +277,7 @@
     linker_files = ":osx_wrapper",
     objcopy_files = ":empty",
     strip_files = ":empty",
-    supports_param_files = 0,
+    supports_param_files = 1,
     toolchain_config = ":local_darwin",
     toolchain_identifier = "local_darwin",
 )
diff --git a/tools/cpp/BUILD.tpl b/tools/cpp/BUILD.tpl
index 37986a9..d3514d1 100644
--- a/tools/cpp/BUILD.tpl
+++ b/tools/cpp/BUILD.tpl
@@ -66,7 +66,7 @@
     linker_files = ":compiler_deps",
     objcopy_files = ":empty",
     strip_files = ":empty",
-    supports_param_files = %{supports_param_files},
+    supports_param_files = 1,
     module_map = %{modulemap},
 )
 
diff --git a/tools/cpp/osx_cc_wrapper.sh b/tools/cpp/osx_cc_wrapper.sh
index bbb5d7e..8c9c111 100755
--- a/tools/cpp/osx_cc_wrapper.sh
+++ b/tools/cpp/osx_cc_wrapper.sh
@@ -34,20 +34,33 @@
 LIB_DIRS=
 RPATHS=
 OUTPUT=
-# let parse the option list
-for i in "$@"; do
+
+function parse_option() {
+    local -r opt="$1"
     if [[ "${OUTPUT}" = "1" ]]; then
-        OUTPUT=$i
-    elif [[ "$i" =~ ^-l(.*)$ ]]; then
+        OUTPUT=$opt
+    elif [[ "$opt" =~ ^-l(.*)$ ]]; then
         LIBS="${BASH_REMATCH[1]} $LIBS"
-    elif [[ "$i" =~ ^-L(.*)$ ]]; then
+    elif [[ "$opt" =~ ^-L(.*)$ ]]; then
         LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS"
-    elif [[ "$i" =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
+    elif [[ "$opt" =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
         RPATHS="${BASH_REMATCH[1]} ${RPATHS}"
-    elif [[ "$i" = "-o" ]]; then
+    elif [[ "$opt" = "-o" ]]; then
         # output is coming
         OUTPUT=1
     fi
+}
+
+# let parse the option list
+for i in "$@"; do
+    if [[ "$i" = @* ]]; then
+        while IFS= read -r opt
+        do
+            parse_option "$opt"
+        done < "${i:1}" || exit 1
+    else
+        parse_option "$i"
+    fi
 done
 
 # Call gcc
diff --git a/tools/cpp/osx_cc_wrapper.sh.tpl b/tools/cpp/osx_cc_wrapper.sh.tpl
index 4c85cd9..28bd47ba 100644
--- a/tools/cpp/osx_cc_wrapper.sh.tpl
+++ b/tools/cpp/osx_cc_wrapper.sh.tpl
@@ -33,20 +33,33 @@
 LIB_DIRS=
 RPATHS=
 OUTPUT=
-# let parse the option list
-for i in "$@"; do
+
+function parse_option() {
+    local -r opt="$1"
     if [[ "${OUTPUT}" = "1" ]]; then
-        OUTPUT=$i
-    elif [[ "$i" =~ ^-l(.*)$ ]]; then
+        OUTPUT=$opt
+    elif [[ "$opt" =~ ^-l(.*)$ ]]; then
         LIBS="${BASH_REMATCH[1]} $LIBS"
-    elif [[ "$i" =~ ^-L(.*)$ ]]; then
+    elif [[ "$opt" =~ ^-L(.*)$ ]]; then
         LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS"
-    elif [[ "$i" =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
+    elif [[ "$opt" =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
         RPATHS="${BASH_REMATCH[1]} ${RPATHS}"
-    elif [[ "$i" = "-o" ]]; then
+    elif [[ "$opt" = "-o" ]]; then
         # output is coming
         OUTPUT=1
     fi
+}
+
+# let parse the option list
+for i in "$@"; do
+    if [[ "$i" = @* ]]; then
+        while IFS= read -r opt
+        do
+            parse_option "$opt"
+        done < "${i:1}" || exit 1
+    else
+        parse_option "$i"
+    fi
 done
 
 # Set-up the environment
diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl
index 5270e9e..5cb1a9b 100644
--- a/tools/cpp/unix_cc_configure.bzl
+++ b/tools/cpp/unix_cc_configure.bzl
@@ -447,7 +447,6 @@
             "%{cc_toolchain_identifier}": cc_toolchain_identifier,
             "%{name}": cpu_value,
             "%{modulemap}": ("\":module.modulemap\"" if is_clang else "None"),
-            "%{supports_param_files}": "0" if darwin else "1",
             "%{cc_compiler_deps}": get_starlark_list([":builtin_include_directory_paths"] + (
                 [":cc_wrapper"] if darwin else []
             )),