<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/84476>84476</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            libcxx doesn't build on FreeBSD 14/amd64
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++,
            platform:freebsd
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          rorth
      </td>
    </tr>
</table>

<pre>
    When trying to build LLVM 18.1.0 on FreeBSD 14.0/amd64 with `test-release.sh`, I failed miserably for `libcxx`:
* As I'd painfully discovered during a similar attempt for 17.0.6, `libcxx` needs (a variant of) `libcxx/cmake/caches/FreeBSD.cmake` to build at all (which I consider misguided; more on that below) and a checkout of `libcxxrt` (since FreeBSD doesn't deliver all the necessary headers).
* The attempt for 18.1.0 failed however:
```
FAILED: libcxx/src/CMakeFiles/cxx_shared.dir/exception.cpp.o
[...]
In file included from /var/llvm/reltest/llvm-18.1.0/final/llvm-project/libcxx/sr
c/exception.cpp:14:
/var/llvm/reltest/llvm-18.1.0/final/Phase2/Release/llvmCore-18.1.0-final.obj/run
times/runtimes-bins/libcxx/private-abi-headers/cxxabi.h:207:7: error: '__cxa_all
ocate_exception' is missing exception specification 'throw()'
  207 | void *__cxa_allocate_exception(size_t thrown_size);
 |       ^
      | throw()
/var/llvm/reltest/llvm-18.1.0/final/Phase2/Release/llvmCore-18.1.0-final.obj/inc
lude/c++/v1/__exception/exception_ptr.h:32:36: note: previous declaration is her
e
   32 | _LIBCPP_OVERRIDABLE_FUNC_VIS void* __cxa_allocate_exception(size_t) thro
w();
      |                                    ^
```
  and similar failures for `__cxa_free_exception` and `__cxa_init_primary_exception`.
* That `libcxxrt` checkout had been at
```
commit 03c83f5a57be8c5b1a29a68de5638744f17d28ba (HEAD -> master, origin/master, origin/HEAD)
Author: itrofimow <i.trofimow@yandex.ru>
Date:   Wed Oct 11 17:15:26 2023 +0300

 add __cxa_init_primary_exception (#23)
```
  where the three declarations lack the `throw()`.
* When updating `libcxxrt` to `HEAD`, the compile failures vanished.  However, the freshly build `libc++.so` has a reference to `__cxa_init_primary_exception` which isn't defined in `/usr/lib/libcxxrt.so`.  It might be possible to avoid this by defining `_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION` to `0` in `libcxx/include/__availability`, but I got tired of the nightmares those problems had caused me for both 17.0.6 and 18.1.0.

In the end, I took a different route: I chose to forget about `libcxxrt` completely, built both `libc++abi` and `libunwind` and link those objects directly into `libc++.so`. I used the following cache file for that, which finally allowed the build to succeed (well, until the next unrelated failure):
```
set(LIBCXX_CXX_ABI libcxxabi CACHE STRING "")
set(LIBCXXABI_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBUNWIND_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXX_ENABLE_STATIC_ABI_LIBRARY ON CACHE BOOL "")
set(LIBCXXABI_ENABLE_STATIC_UNWINDER ON CACHE BOOL "")
```
To expand on the problems I see with `libcxx`'s use of cache files:
* They seem perfectly fine for cases where you just build a variant of `libcxx`, like different optimization or debug levels, use of asan or similar.
* However, in some cases (like FreeBSD), they are necessary for the build to succeed at all (well, they were in LLVM 17), with the exception that you still need a checkout of `libcxxrt`.  I'd argue that in cases like this they should be used automatically instead of having unaware users run into all sorts of build failures.  IMO the cmake files should diagnose issues like necessary but missing `libcxxrt` sources automatically and error out if necessary.  That would be way better than letting the build for hours and then fail for issues that could have been discovered up front.
* This is even more important for developers that don't build for some target every other day, but just want that build for comparison's sake with their primary targets.  Having been through such an ordeal once makes me very shy of trying this ever again.

Maybe the FreeBSD maintainers (@DimitryAndric, @brooksdavis) can comment on this?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8WEtz4zYS_jXwpcssCtTLBx8o2VqzymNPeZxJclKBZEtEDAIsAJSs_PqtBqiHndlJdg_rksdDiujn1193Uzgntxrxlk0WbHJ3JXr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