This is an archive of the discontinued LLVM Phabricator instance.

[Driver] Move LIBRARY_PATH before user inputs
ClosedPublic

Authored by MaskRay on Aug 7 2019, 8:13 AM.

Details

Summary

Fixes PR16786

Currently, library paths specified by LIBRARY_PATH are placed after inputs: inputs LIBRARY_PATH stdlib
In gcc, the order is: LIBRARY_PATH inputs stdlib if not cross compiling.
(On Darwin targets, isCrossCompiling() always returns false.)

This patch changes the behavior to match gcc.

Diff Detail

Event Timeline

MaskRay created this revision.Aug 7 2019, 8:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2019, 8:13 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MaskRay edited the summary of this revision. (Show Details)Aug 7 2019, 8:18 AM
MaskRay updated this revision to Diff 213908.Aug 7 2019, 8:28 AM

Fix a comment

hfinkel accepted this revision.Aug 7 2019, 9:44 AM
hfinkel added a subscriber: hfinkel.

LGTM.

Local flags should certainly override LIBRARY_PATH.

This revision is now accepted and ready to land.Aug 7 2019, 9:44 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2019, 6:56 PM

I still see some clang-specific and system link directories listed in the linker line before the directories from LIBRARY_PATH:

$ LIBRARY_PATH=test1 /usr/local/clang/bin/clang -Ltest2 -v hello.c
 "/usr/bin/ld" .../crtbegin.o -Ltest2 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. -L/usr/local/clang/bin/../lib -L/lib -L/usr/lib -Ltest1

I think they are inserted by ToolChain.AddFilePathLibArgs in Gnu.cpp. Is this the intended ordering? My expectation would be

$ LIBRARY_PATH=test1 /usr/local/clang/bin/clang -Ltest2 -v hello.c
 "/usr/bin/ld" .../crtbegin.o -Ltest2 -Ltest1 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. -L/usr/local/clang/bin/../lib -L/lib -L/usr/lib

@hfinkel any opinion?

I still see some clang-specific and system link directories listed in the linker line before the directories from LIBRARY_PATH:

$ LIBRARY_PATH=test1 /usr/local/clang/bin/clang -Ltest2 -v hello.c
 "/usr/bin/ld" .../crtbegin.o -Ltest2 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. -L/usr/local/clang/bin/../lib -L/lib -L/usr/lib -Ltest1

I think they are inserted by ToolChain.AddFilePathLibArgs in Gnu.cpp. Is this the intended ordering? My expectation would be

$ LIBRARY_PATH=test1 /usr/local/clang/bin/clang -Ltest2 -v hello.c
 "/usr/bin/ld" .../crtbegin.o -Ltest2 -Ltest1 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. -L/usr/local/clang/bin/../lib -L/lib -L/usr/lib

@hfinkel any opinion?

In GCC, some system directories precede LIBRARY_PATH. I don't know the exact rule yet (but this patch made the behavior more similar)

% mkdir test1
% LIBRARY_PATH=test1 gcc-10 -Ltest2 -v a.c
...
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/10/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../../lib/:/l
ib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:test1/:/usr/lib/gcc/x86_64-linux-gnu/10/../../../:/lib/:/usr/lib/