This is an archive of the discontinued LLVM Phabricator instance.

[Clang] [Driver] Add option to set alternative toolchain path
ClosedPublic

Authored by qiucf on Mar 18 2022, 4:11 AM.

Details

Summary

In some cases, we need to set alternative toolchain path other than the default with system (headers, libraries, dynamic linker prefix, ld path, etc.), e.g., to pick up newer components, but keep sysroot at the same time (to pick up extra packages).

This patch introduces a new option --overlay-platform-toolchain to set up such alternative toolchain path.

Diff Detail

Event Timeline

qiucf created this revision.Mar 18 2022, 4:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2022, 4:11 AM
qiucf requested review of this revision.Mar 18 2022, 4:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2022, 4:11 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
hubert.reinterpretcast edited the summary of this revision. (Show Details)Mar 18 2022, 8:37 AM
clang/include/clang/Driver/Driver.h
152

I don't understand the use of "in" in the comment. Perhaps "used" was meant?

clang/lib/Driver/ToolChains/Linux.cpp
270–271

The /lib and /usr/lib cases should occur after the multiarch and OSLibDir cases taken from the sysroot.

clang/test/Driver/gcc-toolchain.cpp
42 ↗(On Diff #416454)

Should specify a sysroot and test relative ordering between resource paths taken from the "overlay platform toochain" and the sysroot.

qiucf updated this revision to Diff 417566.Mar 23 2022, 4:28 AM
qiucf marked 3 inline comments as done.
  • Move test to another file
  • Add documentation on option
This revision is now accepted and ready to land.Mar 23 2022, 10:42 AM
This revision was automatically updated to reflect the committed changes.

Why is --overlay-platform-toolchain added instead of using -isystem and -L?

In some cases, we need to set alternative toolchain path other than the default with system (headers, libraries, dynamic linker prefix, ld path, etc.), e.g., to pick up newer components, but keep sysroot at the same time (to pick up extra packages).

The functionality overlaps with -B. Unsure why introduce a new mechanism.

clang/include/clang/Driver/Options.td
4184

Separate-form driver options are not conventional. New driver options should just avoid them.

clang/lib/Driver/ToolChains/Gnu.cpp
1870

Why was this rule added?

qiucf added a comment.Mar 24 2022, 9:53 PM

Hi,

Why is --overlay-platform-toolchain added instead of using -isystem and -L?

The functionality overlaps with -B. Unsure why introduce a new mechanism.

We may want to use an extra toolchain like the Advance Toolchain (https://github.com/advancetoolchain/advance-toolchain) which includes Glibc/GCC/GDB/LD/etc. but is not a complete OS distribution. So we should not simply change sysroot here.

Using -isystem and -L is okay in principle, but (1) it breaks expected include order (-isystem just inserts it in the top); (2) we have to manually insert many -isystem paths; (3) we want to reuse the logic in clang driver code. -B changes search path of crt runtime files, but include/library/dynamic linker paths are the same.

What this option does is to insert the extra toolchain in all search paths but with higher priority than system default.

clang/include/clang/Driver/Options.td
4184

Thanks for the reminder!

clang/lib/Driver/ToolChains/Gnu.cpp
1870

Linker and other paths relies on location of specified GCC toolchain. And the toolchain specified by overlay-platform-toolchain is expected to have GCC installation included. But for sure, it has lower priority than gcc-toolchain.

MaskRay added a comment.EditedMar 24 2022, 11:54 PM

Hi,

Why is --overlay-platform-toolchain added instead of using -isystem and -L?

The functionality overlaps with -B. Unsure why introduce a new mechanism.

We may want to use an extra toolchain like the Advance Toolchain (https://github.com/advancetoolchain/advance-toolchain) which includes Glibc/GCC/GDB/LD/etc. but is not a complete OS distribution. So we should not simply change sysroot here.

OK. Then this information is missing from the commit message and makes the new option IMO less justified.

Using -isystem and -L is okay in principle, but (1) it breaks expected include order (-isystem just inserts it in the top); (2) we have to manually insert many -isystem paths; (3) we want to reuse the logic in clang driver code. -B changes search path of crt runtime files, but include/library/dynamic linker paths are the same.

What this option does is to insert the extra toolchain in all search paths but with higher priority than system default.

Some other groups may have similar needs. I feel that the addition of --overlay-platform-toolchain might better involve more clang driver folks.
(you can run git shortlog clang/lib/Driver to get some idea who usually care about this area)

I've left some other comments. At this point I am going to say it is probably cleaner reverting the patch and having more discussions.

I'm still not so convinced we need a new option. There are a bunch which perform similar tasks (--sysroot, -B, --gcc-toolchain). We really need to think about what the missing gap is harder.

Perhaps it's ll be helpful if you put to the description the intended search paths printed by clang ... -v '-###' |& sed -E 's/ "?-[iIL]/\n&/g' and we can revisit whether a new option is needed.

clang/docs/ClangCommandLineReference.rst
520

The file is generated. The doc should be added to Options.td

clang/lib/Driver/ToolChains/Gnu.cpp
1870

This makes --overlay-platform-toolchain conceptually a superset of --gcc-toolchain but their behaviors are not exactly the same. Anyway, the interaction with --gcc-toolchain is important but untested.

clang/lib/Driver/ToolChains/Linux.cpp
266

Only a sysroot-like directory have both /lib and /usr. If you have just an incomplete toolchain directory, it likely just needs /lib, not /lib plus /usr/lib.

clang/test/Driver/overlay-toolchain.cpp
9

usr/lib is an important detail which should be tested.

MaskRay added a comment.EditedMar 31 2022, 12:06 AM

To add more why I think the current semantics may need more discussion before we quickly commit to such a driver option:

  • interaction with --dyld-prefix: --sysroot does not affect the fallback --dyld-prefix. It seems even less appropriate for --overlay-platform-toolchain to affect the fallback --dyld-prefix.

If you intend to overlay ld.so, you'll necessarily overlay libc, then --sysroot seems just unneeded at all.

  • interaction with --gcc-toolchain: I am a bit unclear we still want the tricky GCC installation detection after --overlay-platform-toolchain is specified. Do you propose that both will add include and library search paths?
  • -B: in gcc, when -B prefix specifies a directory, GCC adds $prefix/include to the include search directory. Clang does not do this right now. I think adding it may be an alternative approach to introducing the new option.

The currently picked rules may be suitable for https://github.com/advancetoolchain/advance-toolchain, but could be arbitrary for many other use cases.
Have you considered putting some options into a configuration file and using --config?

I understand that you probably have some short-term goal to make somethings done, but as I mentioned, there might be some process issue here.
This significant new feature very quickly landed without other driver folks possibly had a chance to chime in, when AIUI neither you nor your reviewer is a regular contributor to clang driver.
(FWIW I decided to subscribe all clang/lib/Driver patches since I care about this area.)
I very rarely do this but I think it is probably cleaner to revert this patch and discuss it more carefully. I am happy to help you achieve your goal. It's possible that we may still need a driver option.
(I'll take a trip for one week shortly, but I realized that I should make this comment before lack of objection makes the patch set in stone)

To add more why I think the current semantics may need more discussion before we quickly commit to such a driver option:

  • interaction with --dyld-prefix: --sysroot does not affect the fallback --dyld-prefix. It seems even less appropriate for --overlay-platform-toolchain to affect the fallback --dyld-prefix.

If you intend to overlay ld.so, you'll necessarily overlay libc, then --sysroot seems just unneeded at all.

  • interaction with --gcc-toolchain: I am a bit unclear we still want the tricky GCC installation detection after --overlay-platform-toolchain is specified. Do you propose that both will add include and library search paths?
  • -B: in gcc, when -B prefix specifies a directory, GCC adds $prefix/include to the include search directory. Clang does not do this right now. I think adding it may be an alternative approach to introducing the new option.

The currently picked rules may be suitable for https://github.com/advancetoolchain/advance-toolchain, but could be arbitrary for many other use cases.
Have you considered putting some options into a configuration file and using --config?

I understand that you probably have some short-term goal to make somethings done, but as I mentioned, there might be some process issue here.
This significant new feature very quickly landed without other driver folks possibly had a chance to chime in.
(FWIW I decided to subscribe all clang/lib/Driver patches since I care about this area.)
I very rarely do this but I think it is probably cleaner to revert this patch and discuss it more carefully. I am happy to help you achieve your goal. It's possible that we may still need a driver option.

Thanks for further information. I agree that we should not change to break the consistent behavior in such short period of time. It's reasonable for --gcc-toolchain to not add include into path (since driver only expects it to have GCC). I also saw GCC's different behavior on -B. We may need to fix it, but that's beyond this patch's scope.

It's okay to me to compose 'orthogonal' options into config file when available. Anyway, I'd like to revert this commit since there're comments not addressed.

If you intend to overlay ld.so, you'll necessarily overlay libc, then --sysroot seems just unneeded at all.

Why? The header and library search paths are not restricted to artifacts from libc. I had consulted some Advance Toolchain developers and they indicted that it was important for paths from the --sysroot to be incorporated (as GCC from the Advance Toolchain does).

The library search order has the issue of needing the multiarch/"OS lib dir" paths from both the overlay toolchain and the sysroot before attempting fallback plain-"lib" paths. Aside from a new option, is there a way to achieve that?

I agree, however, that the new option doesn't have to incorporate effects on what the --dyld-prefix default is.