This is an archive of the discontinued LLVM Phabricator instance.

Fix libcxx build on musl
AbandonedPublic

Authored by raj.khem on Mar 14 2016, 8:25 PM.

Details

Summary

constexpr is not available on musl

Diff Detail

Event Timeline

raj.khem updated this revision to Diff 50692.Mar 14 2016, 8:25 PM
raj.khem retitled this revision from to Fix libcxx build on musl.
raj.khem updated this object.
raj.khem added a reviewer: rengolin.

I'm adding a few library folks to make sure this is the correct behaviour. I honestly don't know.

cheers,
--renato

include/__config
397 ↗(On Diff #50692)

Unnecessary whitespace change

bcraig edited edge metadata.Mar 15 2016, 11:30 AM
bcraig added a subscriber: cfe-commits.
bcraig edited edge metadata.Mar 15 2016, 11:38 AM

If I understand it correctly, GLIBC is defined in features.h, so this won't work.

I suspect that the build isn't broken. You likely just need to define LIBCXX_HAS_MUSL_LIBC on your cmake line. That will cause __config_site.in to #define _LIBCPP_HAS_MUSL_LIBC for you.

@bcraig, right, check for GLIBC can be moved after including features.h.
https://chromium.googlesource.com/native_client/pnacl-libcxx/+/master%5E!/
seems to add needed tweaks which can be enabled by passing -Dmusl in CFLAGS
may be it can be backported to 3.8 branch

3.8 has it in there. So may be this is just not required. I will add
-DLIBCXX_HAS_MUSL_LIBC=ON to Cmake and see what comes out

The trouble is that features.h doesn't exist in musl (as far as I know). That's the biggest reason why _LIBCPP_HAS_MUSL_LIBC is needed.

I think my problem was that while compiling libcxxabi, it wants to peek into libcxx headers but then libcxxabi cmake infra doesnt have the musl support like libcxx. So Now I solved it by adding -D_LIBCPP_HAS_MUSL_LIBC to CXXFLAGS.

EricWF added a subscriber: EricWF.Mar 15 2016, 12:47 PM

@raj.khem Can you file a bug against libc++abi please?

libcxx still has problem compiling on musl/aarch64 though. I fixed it with this patch

https://github.com/kraj/meta-clang/blob/master/recipes-devtools/clang/libcxx/0001-use-constexpr-when-using-glibc.patch

does this make sense ?

That patch looks hugely dangerous because you've now changed when your static mutex's get initialized. Please file a bug against libc++.

I think my problem was that while compiling libcxxabi, it wants to peek into libcxx headers but then libcxxabi cmake infra doesnt have the musl support like libcxx. So Now I solved it by adding -D_LIBCPP_HAS_MUSL_LIBC to CXXFLAGS.

When cross-compiling, I have found it to be best to build libcxx and libcxxabi as sub-projects of LLVM (i.e. I place libcxx under llvm/projects/libcxx). I can then configure libcxxabi and libcxx at once. When I get around to building, I specifically dodge building LLVM and/or clang by running "make cxxabi cxx".

If you go with that approach, I think all of the _LIBCPP_HAS_MUSL_LIBC stuff gets worked out, because libcxxabi looks at the __ config header that #includes __config_site.

rengolin resigned from this revision.Oct 4 2016, 5:19 AM
rengolin removed a reviewer: rengolin.
zatrazz resigned from this revision.Dec 1 2016, 9:59 AM
zatrazz removed a reviewer: zatrazz.
raj.khem updated this revision to Diff 108853.Jul 30 2017, 7:08 PM
raj.khem edited the summary of this revision. (Show Details)
EricWF requested changes to this revision.Jul 30 2017, 7:59 PM

This patch is not OK, since it affects way more that just MUSL libc.

Also can you please provide a reduced reproducer as two why pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER is not a constant expression with MUSL?

include/__mutex_base
51

Limiting constexpr to GLIBC implementations only is incorrect; you want to exclude MUSL.

Also MUSL is wrong for not allowing pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER to be a constant expression, and MUSL should fix that.

This revision now requires changes to proceed.Jul 30 2017, 7:59 PM
smeenai added inline comments.
include/__mutex_base
51

I've used __builtin_constant_p to wok around a similarly non-conforming PTHREAD_MUTEX_INITIALIZER (from pthread-win32, which defines it to be (void *)-1): see the last part of https://stackoverflow.com/a/10376574/382079. It's a terrible hack, but it works.

@EricWF you are right. I have made the changes accordingly. This patch can be ignored.

bcraig added inline comments.Jul 31 2017, 5:58 AM
include/__mutex_base
51

musl's pthread_mutex_t has volatile members if I recall correctly. It's hard to validate that statement without building musl, because the header that defines pthread_mutex_t is code generated.

raj.khem abandoned this revision.Mar 24 2021, 10:58 AM

this is no longer needed.