This is an archive of the discontinued LLVM Phabricator instance.

[llvm] [CMake] Set LLVM_ATOMIC_LIB variable for convenient linking against libatomic
ClosedPublic

Authored by thesamesam on Oct 19 2022, 12:08 PM.

Details

Summary
  • Set LLVM_ATOMIC_LIB to keep track of when we need to link against libatomic.
  • Add detection of mold linker which is required for this.
  • Use --as-needed when linking against libatomic as a bonus. On some platforms, libatomic may be required only sometimes.

Bug: https://bugs.gentoo.org/832675
Thanks-to: Arfrever Frehtes Taifersar Arahesis <Arfrever@Apache.Org>
Tested-by: erhard_f@mailbox.org <erhard_f@mailbox.org>

Diff Detail

Event Timeline

thesamesam created this revision.Oct 19 2022, 12:08 PM
Herald added a project: Restricted Project. · View Herald Transcript
thesamesam requested review of this revision.Oct 19 2022, 12:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 19 2022, 12:08 PM
mgorny accepted this revision.Oct 19 2022, 8:03 PM

I haven't tested it but at a first glance, it makes sense.

This revision is now accepted and ready to land.Oct 19 2022, 8:03 PM

Is -latomic only needed on some 32-bit architectures? Does this change link against -latomic for major 64-bit architectures?

Is -latomic only needed on some 32-bit architectures? Does this change link against -latomic for major 64-bit architectures?

Earlier unchanged code in CheckAtomic.cmake tries to detect if libatomic exists and if linking against libatomic is possibly needed (see variables HAVE_LIBATOMIC, HAVE_CXX_LIBATOMICS64).
In such case, HAVE_CXX_ATOMICS_WITH_LIB and/or HAVE_CXX_ATOMICS64_WITH_LIB would be set.
LLVM_ATOMIC_LIB is set to non-empty value when HAVE_CXX_ATOMICS_WITH_LIB or HAVE_CXX_ATOMICS64_WITH_LIB are set.

When there was no linking against -latomic, there should still be none.
When there was linking against -latomic, there will be now (with all modern linkers) -Wl,--push-state,--as-needed,-latomic,--pop-state or (with archaic linkers) -latomic.
Due to --as-needed, on some architectures, some particular libraries or executables will no longer be unnecessarily linked against libatomic.