pw_toolchain: Use RSP file for linking

Moves libraries into .rsp files for link steps to allow projects to grow
past CLI limits.

Change-Id: I41bdf5cd8805bb3b0a9bc379b2609a50cf5fd15c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/62100
Pigweed-Auto-Submit: Armando Montanez <amontanez@google.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
diff --git a/pw_toolchain/generate_toolchain.gni b/pw_toolchain/generate_toolchain.gni
index 960cdbf..ce925be 100644
--- a/pw_toolchain/generate_toolchain.gni
+++ b/pw_toolchain/generate_toolchain.gni
@@ -265,12 +265,15 @@
         ]
       }
 
+      _rsp_file = "$_link_outfile.rsp"
+      _rsp_contents = []
+
       _link_group = defined(invoker.link_group) && invoker.link_group
       if (_link_group) {
-        _link_flags += [ "-Wl,--start-group" ]
+        _rsp_contents += [ "-Wl,--start-group" ]
       }
-      _link_flags += [ "{{inputs}}" ]
-      _link_flags += [ "{{frameworks}}" ]
+      _rsp_contents += [ "{{inputs}}" ]
+      _rsp_contents += [ "{{frameworks}}" ]
 
       if (defined(invoker.link_whole_archive) && invoker.link_whole_archive) {
         # Load all object files from all libraries to resolve symbols.
@@ -281,24 +284,29 @@
         # Make sure you use this with --gc-sections, otherwise the
         # resulting binary will contain every symbol defined in every
         # input file and every static library. That could be quite a lot.
-        _link_flags += [
+        _rsp_contents += [
           "-Wl,--whole-archive",
           "{{libs}}",
           "-Wl,--no-whole-archive",
         ]
       } else {
-        _link_flags += [ "{{libs}}" ]
+        _rsp_contents += [ "{{libs}}" ]
       }
 
       if (_link_group) {
-        _link_flags += [ "-Wl,--end-group" ]
+        _rsp_contents += [ "-Wl,--end-group" ]
       }
+      _rsp_command = string_join(" ", _rsp_contents)
+
+      _link_flags += [ "@$_rsp_file" ]
       _link_flags += [ "-o $_link_outfile" ]
 
       _link_command = string_join(" ", _link_flags)
 
       tool("link") {
         command = _link_command
+        rspfile = _rsp_file
+        rspfile_content = _rsp_command
         description = "ld $_link_outfile"
         outputs = [
           _link_outfile,
@@ -317,6 +325,8 @@
 
       tool("solink") {
         command = _link_command + " -shared"
+        rspfile = _rsp_file
+        rspfile_content = _rsp_command
         description = "ld -shared $_link_outfile"
         outputs = [
           _link_outfile,