This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] Make it drastically simpler to link libc++.
ClosedPublic

Authored by EricWF on Aug 31 2015, 4:50 PM.

Details

Summary

Currently on most platforms you have to manually link the c++ abi library used with libc++ whenever you use libc++. So your typical libc++ command like invocation might look like:

clang++ -stdlib=libc++ foo.cpp -lc++abi

Having to manually link libc++abi.so makes it harder for libc++ to be used generically. This patch fixes that by generating a linker script for libc++.so that correctly links the ABI library. On linux the linker script for libc++abi would look like:

# libc++.so
INPUT(libc++.so.1 -lc++abi)

With the linker script you can now use libc++ using only -stdlib=libc++. This is the technique that is used on FreeBSD in ordered to link cxxrt and I think it's the best approach to make our users lives simpler.

The CMake option used to enable this is LIBCXX_ENABLE_ABI_LINKER_SCRIPT. In future I would like to enable this by default on all platforms except for Darwin.

Diff Detail

Event Timeline

EricWF updated this revision to Diff 33645.Aug 31 2015, 4:50 PM
EricWF retitled this revision from to [libcxx] Make it drastically simpler to link libc++..
EricWF updated this object.
EricWF added a subscriber: cfe-commits.
jroelofs added inline comments.Sep 3 2015, 8:30 AM
utils/gen_link_script/gen_link_script.py
2

Do you foresee these linker scripts being significantly more complicated for other platforms? ISTM that it would be much simpler to have CMake generate these directly.

EricWF added inline comments.Sep 4 2015, 2:40 PM
utils/gen_link_script/gen_link_script.py
2

The linker scripts will get no more complicated. The will always just be INPUT(libc++.so.x.x -l<abilib>). However generating these in CMake is a pain. I've tried before and I promise the python approach is a lot cleaner.

The main reason is that most CMake functions such as FILE(...) and CONFIG_FILE(...) are run at CMake configuration time and they don't allow generator expressions in them. I also don't know how to read the value of a symlink using CMake which is needed to generate the linker script.

If you really want me to rewrite this is CMake I will, but I promise that you probably don't.

jroelofs edited edge metadata.Sep 4 2015, 2:42 PM

I trust your judgement.

@mclow.lists ping. Do you think this is the right approach to fix linking the ABI library?
@rsmith: I think you asked for this functionality a while ago. Any thoughts?

rsmith edited edge metadata.Oct 12 2015, 2:16 PM

I think this is a good direction. I used to link libc++abi into libc++, and switched to using a hand-rolled linker script a while back. It's been working great for me.

EricWF updated this revision to Diff 37376.Oct 14 2015, 12:53 PM
EricWF edited edge metadata.

Updating diff so it merges cleanly against master.

EricWF accepted this revision.Oct 14 2015, 12:54 PM
EricWF added a reviewer: EricWF.

I'm accepting this revision because the feedback about this patch has been positive. I will submit another patch that makes this behavior default as suggested on cfe-dev.

This revision is now accepted and ready to land.Oct 14 2015, 12:54 PM
EricWF closed this revision.Oct 14 2015, 12:55 PM