Skip to content

Path to lld is not detected robustly on MacOS #499

@ivwumupy

Description

@ivwumupy

The path to lld is detected by invoking clang empty.cc -v and inspect the last line of the output:

linker_command = result.stderr.splitlines()[-1]
return linker_command.strip().split(" ")[0].strip("\"'")

However lld may emit warnings (see below) building empty.cc and subsequently breaks the detection logic resulting in an argument of the form -fuse-ld=ld64.lld: (note the colon at the end) being passed to the compiler.

Here is one situation when lld emits a warning.

  1. Apple Clang is detected as the compiler and ld64.lld is present in PATH (for example installed by homebrew).
  2. Apple Clang then calls ld64.lld with -L/usr/local/lib in the flags.
  3. If the directory /usr/local/lib does not exist, then lld emits a warning.
    In this situation, the last lines of result.stderr is
... (omitted)
End of search list.
 "/opt/homebrew/bin/ld64.lld" -demangle -no_deduplicate -dynamic -arch arm64 -platform_version macos 15.0.0 15.2 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -mllvm -enable-linkonceodr-outlining -o /dev/null -L/usr/local/lib /var/folders/0v/nn5rkhrs3r72b72dfbrlwfbh0000gn/T/empty-da6fa5.o --start-lib --end-lib -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16/lib/darwin/libclang_rt.osx.a
ld64.lld: warning: directory not found for option -L/usr/local/lib

How to reproduce

MODULE.bazel

bazel_dep(name = "rules_cc", version = "0.2.10")

BUILD.bazel

load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
    name = "hello-world",
    srcs = ["main.cc"],
)

main.cc

int main() { return 0; }

Command:

bazel build //:hello-world

Metadata

Metadata

Assignees

No one assigned

    Labels

    untriagedTeam member has to triage this issue - assign priority, type, and owner (if possible).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions