pw_toolchain: Explicitly link with lld when using clang

Adds an explicit -fuse-ld=lld to clang toolchains to prevent system
configuration from causing another linker to be selected by clang.

Bug: b/363070027
Change-Id: I472c8189a402fe57c0a47748638c9e1d83349b84
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/232931
Reviewed-by: Ted Pudlik <tpudlik@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Pigweed-Auto-Submit: Armando Montanez <amontanez@google.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Petr Hosek <phosek@google.com>
diff --git a/pw_build/BUILD.gn b/pw_build/BUILD.gn
index 25fd6fd..3c9d009 100644
--- a/pw_build/BUILD.gn
+++ b/pw_build/BUILD.gn
@@ -384,6 +384,10 @@
   write_file(pw_build_RELATIVE_PATH_TRANSFORM_JSON, _transformations, "json")
 }
 
+config("link_with_lld") {
+  ldflags = [ "-fuse-ld=lld" ]
+}
+
 # This group is linked into all pw_executable, pw_static_library, and
 # pw_shared_library targets. This makes it possible to ensure symbols are
 # defined without a dependency on them.
diff --git a/pw_toolchain/arm_clang/toolchains.gni b/pw_toolchain/arm_clang/toolchains.gni
index 95e17e9..7a84e22 100644
--- a/pw_toolchain/arm_clang/toolchains.gni
+++ b/pw_toolchain/arm_clang/toolchains.gni
@@ -46,16 +46,19 @@
 
 # Configs specific to different architectures.
 _cortex_m0plus = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m0plus",
 ]
 
 _cortex_m3 = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m3",
 ]
 
 _cortex_m4 = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m4",
 ]
@@ -66,26 +69,31 @@
 ]
 
 _cortex_m7 = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m7",
 ]
 
 _cortex_m7f = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m7f",
 ]
 
 _cortex_m33 = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m33",
 ]
 
 _cortex_m33f = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m33f",
 ]
 
 _cortex_m55 = [
+  "$dir_pw_build:link_with_lld",
   "$dir_pw_toolchain/arm_clang:nosys",
   "$dir_pw_toolchain/arm_clang:cortex_m55",
 ]
diff --git a/pw_toolchain/host_clang/BUILD.bazel b/pw_toolchain/host_clang/BUILD.bazel
index 77e4b28..9dcf49d 100644
--- a/pw_toolchain/host_clang/BUILD.bazel
+++ b/pw_toolchain/host_clang/BUILD.bazel
@@ -33,6 +33,12 @@
 filegroup(name = "empty")
 
 pw_cc_flag_set(
+    name = "link_with_lld",
+    actions = ["@pw_toolchain//actions:all_link_actions"],
+    flags = ["-fuse-ld=lld"],
+)
+
+pw_cc_flag_set(
     name = "macos_link_libs",
     actions = ["@pw_toolchain//actions:all_link_actions"],
     flags = [
@@ -325,6 +331,7 @@
         "//conditions:default": [],
     }) + [
         ":thread_safety_warnings",
+        ":link_with_lld",
         "@pw_toolchain//flag_sets:debugging",
         "@pw_toolchain//flag_sets:reduced_size",
         "@pw_toolchain//flag_sets:no_canonical_prefixes",
diff --git a/pw_toolchain/host_clang/toolchain.cmake b/pw_toolchain/host_clang/toolchain.cmake
index 6df6124..7e5a065 100644
--- a/pw_toolchain/host_clang/toolchain.cmake
+++ b/pw_toolchain/host_clang/toolchain.cmake
@@ -96,6 +96,11 @@
     set(CMAKE_SYSROOT "$ENV{PW_PIGWEED_CIPD_INSTALL_DIR}/clang_sysroot")
 endif()
 
+# Explicitly use lld to link.
+foreach(TYPE IN ITEMS EXE SHARED MODULE)
+    set(CMAKE_${TYPE}_LINKER_FLAGS_INIT "${CMAKE_${TYPE}_LINKER_FLAGS_INIT} -fuse-ld=lld")
+endforeach()
+
 set(pw_build_WARNINGS
     pw_build.strict_warnings
     pw_build.extra_strict_warnings
diff --git a/pw_toolchain/host_clang/toolchains.gni b/pw_toolchain/host_clang/toolchains.gni
index 4646b90..9098712 100644
--- a/pw_toolchain/host_clang/toolchains.gni
+++ b/pw_toolchain/host_clang/toolchains.gni
@@ -60,6 +60,7 @@
   # remove this hack.
   default_configs = []
   default_configs = pigweed_default_configs + [
+                      "$dir_pw_build:link_with_lld",
                       "$dir_pw_build:extra_debugging",
                       "$dir_pw_toolchain/host_clang:no_system_libcpp",
                       "$dir_pw_toolchain/host_clang:xcode_sysroot",
diff --git a/pw_toolchain_bazel/build_external/llvm_clang.BUILD b/pw_toolchain_bazel/build_external/llvm_clang.BUILD
index 240d80a..d083c25 100644
--- a/pw_toolchain_bazel/build_external/llvm_clang.BUILD
+++ b/pw_toolchain_bazel/build_external/llvm_clang.BUILD
@@ -78,11 +78,13 @@
         "@platforms//os:windows": "//:bin/llvm-ar.exe",
         "//conditions:default": "//:bin/llvm-ar",
     }),
+    additional_files = glob(["bin/llvm"]),
 )
 
 pw_cc_tool(
     name = "llvm_libtool_darwin_tool",
     tool = "//:bin/llvm-libtool-darwin",
+    additional_files = glob(["bin/llvm"]),
 )
 
 pw_cc_action_config(
@@ -109,6 +111,7 @@
         "//conditions:default": "//:bin/clang++",
     }),
     additional_files = glob([
+        "bin/llvm",
         "include/**",
         "lib/clang/**/include/**",
     ]),
@@ -127,6 +130,7 @@
         "//conditions:default": "//:bin/clang",
     }),
     additional_files = glob([
+        "bin/llvm",
         "include/**",
         "lib/clang/**/include/**",
     ]),
@@ -150,8 +154,12 @@
         "//conditions:default": "//:bin/clang++",
     }),
     additional_files = glob([
+        "bin/llvm",
+        "bin/lld*",
+        "bin/ld*",
         "lib/**/*.a",
         "lib/**/*.so*",
+        "lib/**/*.o",
     ]),
 )
 
@@ -167,6 +175,7 @@
         "@platforms//os:windows": "//:bin/llvm-cov.exe",
         "//conditions:default": "//:bin/llvm-cov",
     }),
+    additional_files = glob(["bin/llvm"]),
 )
 
 pw_cc_action_config(
@@ -181,6 +190,7 @@
         "@platforms//os:windows": "//:bin/llvm-objcopy.exe",
         "//conditions:default": "//:bin/llvm-objcopy",
     }),
+    additional_files = glob(["bin/llvm"]),
 )
 
 pw_cc_action_config(
@@ -195,6 +205,7 @@
         "@platforms//os:windows": "//:bin/llvm-objdump.exe",
         "//conditions:default": "//:bin/llvm-objdump",
     }),
+    additional_files = glob(["bin/llvm"]),
 )
 
 pw_cc_action_config(
@@ -209,6 +220,7 @@
         "@platforms//os:windows": "//:bin/llvm-strip.exe",
         "//conditions:default": "//:bin/llvm-strip",
     }),
+    additional_files = glob(["bin/llvm"]),
 )
 
 pw_cc_action_config(