[go: nahoru, domu]

Get rust_gtest_interop_unittests to pass on Mac

The rlibs need to be moved from the rsp file to the command line,
like for other platforms, in order to have the
`-LinkWrapper,add-whole-archive` switch get applied to them in the
link_driver.py script.

Then when building for mac, replace --whole-archive with -force_load
which does the same thing there.

Bug: 1386212
Change-Id: Ie71019eeb83eca9ddd0cc4f423d648598abaee7b
Cq-Include-Trybots: luci.chromium.try:android-rust-arm32-rel,android-rust-arm64-dbg,android-rust-arm64-rel,linux-rust-x64-dbg,linux-rust-x64-rel,mac-rust-x64-dbg,win-rust-x64-dbg,win-rust-x64-rel
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4649892
Reviewed-by: Hans Wennborg <hans@chromium.org>
Commit-Queue: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1163677}
diff --git a/build/toolchain/apple/linker_driver.py b/build/toolchain/apple/linker_driver.py
index 4f58ebc..3b3ef95 100755
--- a/build/toolchain/apple/linker_driver.py
+++ b/build/toolchain/apple/linker_driver.py
@@ -138,7 +138,7 @@
         # test target. This is determined by switch
         # `-LinkWrapper,add-whole-archive`.
         compiler_driver_args = whole_archive.wrap_with_whole_archive(
-            compiler_driver_args)
+            compiler_driver_args, is_apple=True)
 
         linker_driver_outputs = [self._get_linker_output()]
 
diff --git a/build/toolchain/apple/toolchain.gni b/build/toolchain/apple/toolchain.gni
index 99933a1..0999cf2 100644
--- a/build/toolchain/apple/toolchain.gni
+++ b/build/toolchain/apple/toolchain.gni
@@ -502,14 +502,15 @@
         link_command += " -Wl,-install_name,@rpath/\"{{target_output_name}}{{output_extension}}\" "
       }
       link_command += dsym_switch
-      link_command += "{{ldflags}} -o \"$dylib\" \"@$rspfile\""
+      link_command += "{{ldflags}} -o \"$dylib\" \"@$rspfile\" {{rlibs}}"
 
       replace_command = "if ! cmp -s \"$temporary_tocname\" \"$tocname\"; then mv \"$temporary_tocname\" \"$tocname\""
       extract_toc_command = "{ $otool -l \"$dylib\" | grep LC_ID_DYLIB -A 5; $nm -gPp \"$dylib\" | cut -f1-2 -d' ' | grep -v U\$\$; true; }"
 
       command = "if $does_reexport_command ; then $link_command && $extract_toc_command > \"$tocname\"; else $link_command && $extract_toc_command > \"$temporary_tocname\" && $replace_command ; fi; fi"
 
-      rspfile_content = "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}} {{rlibs}}"
+      rspfile_content =
+          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
 
       description = "SOLINK {{output}}"
 
@@ -549,12 +550,12 @@
       rspfile = sofile + ".rsp"
       pool = "//build/toolchain:link_pool($default_toolchain)"
 
-      link_command =
-          "$linker_driver $ld -bundle {{ldflags}} -o \"$sofile\" \"@$rspfile\""
+      link_command = "$linker_driver $ld -bundle {{ldflags}} -o \"$sofile\" \"@$rspfile\" {{rlibs}}"
       link_command += dsym_switch
       command = link_command
 
-      rspfile_content = "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}} {{rlibs}}"
+      rspfile_content =
+          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
 
       description = "SOLINK_MODULE {{output}}"
 
@@ -579,9 +580,10 @@
       rspfile = "$outfile.rsp"
       pool = "//build/toolchain:link_pool($default_toolchain)"
 
-      command = "$linker_driver $ld $dsym_switch {{ldflags}} -o \"$outfile\" \"@$rspfile\""
+      command = "$linker_driver $ld $dsym_switch {{ldflags}} -o \"$outfile\" \"@$rspfile\" {{rlibs}}"
       description = "LINK $outfile"
-      rspfile_content = "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}} {{rlibs}}"
+      rspfile_content =
+          "{{inputs}} {{frameworks}} {{swiftmodules}} {{solibs}} {{libs}}"
       outputs = [ outfile ]
 
       if (_enable_dsyms) {
diff --git a/build/toolchain/whole_archive.py b/build/toolchain/whole_archive.py
index aeeb0ddc..2a15989a 100644
--- a/build/toolchain/whole_archive.py
+++ b/build/toolchain/whole_archive.py
@@ -5,7 +5,7 @@
 import re
 
 
-def wrap_with_whole_archive(command):
+def wrap_with_whole_archive(command, is_apple=False):
   """Modify and return `command` such that -LinkWrapper,add-whole-archive=X
   becomes a linking inclusion X (-lX) but wrapped in whole-archive
   modifiers."""
@@ -41,11 +41,17 @@
       # The arg is a full path to a library, we look if the the library name (a
       # suffix of the full arg) is one of `libnames`.
       if has_any_suffix(arg, libnames):
-        out.extend([before, arg, after])
+        out.extend([before, arg])
+        if after:
+          out.append(after)
       else:
         out.append(arg)
     return out
 
-  # Apply --whole-archive to the libraries that desire it.
-  return wrap_libs_with(command, whole_archive_libs, "-Wl,--whole-archive",
-                        "-Wl,--no-whole-archive")
+  if is_apple:
+    # Apply -force_load to the libraries that desire it.
+    return wrap_libs_with(command, whole_archive_libs, "-Wl,-force_load", None)
+  else:
+    # Apply --whole-archive to the libraries that desire it.
+    return wrap_libs_with(command, whole_archive_libs, "-Wl,--whole-archive",
+                          "-Wl,--no-whole-archive")
diff --git a/testing/rust_gtest_interop/rust_gtest_interop_unittest_main.cc b/testing/rust_gtest_interop/rust_gtest_interop_unittest_main.cc
index 058d380..0f06e1cb 100644
--- a/testing/rust_gtest_interop/rust_gtest_interop_unittest_main.cc
+++ b/testing/rust_gtest_interop/rust_gtest_interop_unittest_main.cc
@@ -7,6 +7,7 @@
 #include "base/strings/stringprintf.h"
 #include "base/test/launcher/unit_test_launcher.h"
 #include "base/test/test_suite.h"
+#include "base/test/test_switches.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 #include <iostream>
@@ -27,13 +28,13 @@
   // We verify that the test suite and test name written in the #[gtest] macro
   // is being propagated to Gtest by using a test filter that matches on the
   // test suites/names.
-  std::string filter =
-      base::StringPrintf("--gtest_filter=Test.*:ExactSuite.ExactTest");
+  std::string filter = "--gtest_filter=Test.*:ExactSuite.ExactTest";
 
   int my_argc = argc + 2;
   char** my_argv = new char*[argc];
-  for (int i = 0; i < argc; ++i)
+  for (int i = 0; i < argc; ++i) {
     my_argv = argv;
+  }
   my_argv[argc] = single_process.data();
   my_argv[argc + 1] = filter.data();
 
@@ -53,6 +54,8 @@
       std::cerr << "***ERROR***: Expected " << expected_success
                 << " tests to succeed, but we saw: " << succeed << '\n';
       return 1;
+    } else {
+      std::cerr << "***OK***: Ran " << succeed << " tests, yay!\n";
     }
   }