Trevor Gross 36b52c7d1b Introduce musl-math-sys for bindings to musl math symbols
This crate builds math symbols from a musl checkout and provides a Rust
interface. The intent is that we will be able to compare our
implementations against musl on more than just linux (which are the only
currently the only targets we run `*-musl` targets against for
comparison).

Musl libc can't compile on anything other than Linux; however, the
routines in `src/math` are cross platform enough to build on MacOS and
windows-gnu with only minor adjustments. We take advantage of this and
build only needed files using `cc`.

The build script also performs remapping (via defines) so that e.g.
`cos` gets defined as `musl_cos`. This gives us more certainty that we
are actually testing against the intended symbol; without it, it is easy
to unknowingly link to system libraries or even Rust's `libm` itself and
wind up with an ineffective test. There is also a small procedure to
verify remapping worked correctly by checking symbols in object files.
2024-10-28 12:59:38 -05:00

40 lines
1.2 KiB
C

/* This is meant to override Musl's src/include/features.h
*
* We use a separate file here to redefine some attributes that don't work on
* all platforms that we would like to build on.
*/
#ifndef FEATURES_H
#define FEATURES_H
/* Get the required `#include "../../include/features.h"` since we can't use
* the relative path. The C macros need double indirection to get a usable
* string. */
#define _stringify_inner(s) #s
#define _stringify(s) _stringify_inner(s)
#include _stringify(ROOT_INCLUDE_FEATURES)
#if defined(__APPLE__)
#define weak __attribute__((__weak__))
#define hidden __attribute__((__visibility__("hidden")))
/* We _should_ be able to define this as:
* _Pragma(_stringify(weak musl_ ## new = musl_ ## old))
* However, weak symbols aren't handled correctly [1]. So we manually write
* wrappers, which are in `alias.c`.
*
* [1]: https://github.com/llvm/llvm-project/issues/111321
*/
#define weak_alias(old, new) /* nothing */
#else
#define weak __attribute__((__weak__))
#define hidden __attribute__((__visibility__("hidden")))
#define weak_alias(old, new) \
extern __typeof(old) musl_ ## new \
__attribute__((__weak__, __alias__(_stringify(musl_ ## old))))
#endif /* defined(__APPLE__) */
#endif