haiku/headers/private/kernel/math.h
Niels Sascha Reedijk 3fb092979e kernel: provide kernel-level equivalents for math.h and stdlib.h
GCC 13 is more strict about what C++ features are available in a freestanding
(= build without standard library) build. The `stdlib.h` and `math.h` headers
are redefined as part of libstdc++. If the object is built as freestanding, it
should be assumed that none of the standard library functions are available.

The solution in Haiku has been to add part of the standard library into the
kernel shared object. The method of exposing them has been to allow the
use of `stdlib.h` and `math.h` in the kernel and in kernel add-ons.

This change allows that approach to continue. What it does, is that it defines
specific headers which will be picked up when a module is built using private
kernel headers. When building for the kernel (or for the boot module), it will
bypass GCC 13's libstdc++ default behaviour of not including the POSIX/platform
header with all the function definitions.

An alternative to be considered for the longer term is to specifically define
the parts of the C/C++ standard library that is available in the kernel in
these headers, or create custom headers when building kernel modules (which is
how Linux approaches it).

Change-Id: Icab4614f642219fa77732b02401570708ee9a963
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6645
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-28 07:11:17 +00:00

34 lines
1.2 KiB
C

/*
* Copyright 2023, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _KERNEL_MATH_H
#define _KERNEL_MATH_H
/*
When building the kernel, the bootloader or kernel add-ons, the Haiku build system passes the
`-ffreestanding` argument to GCC, in order to make sure that only the C++ language features can
be used that do not require the C/C++ standard library.
The Haiku kernel and boot loaders include part of the standard library in the kernel/boot
loader. It uses the C/C++ standard library headers (like this one) to expose the
functions/features that are available in the kernel/boot loader.
If we are building for the kernel or the boot loader, the logic below will tell GCC's C++
headers to include the underlying posix headers, so that the kernel, the boot loader and kernel
add-ons can link to the symbols defined in them.
When we are NOT building for the kernel or the boot loader, we fall back to GCC's default
behaviour.
*/
#if (defined(_KERNEL_MODE) || defined(_BOOT_MODE)) && !defined(_GLIBCXX_INCLUDE_NEXT_C_HEADERS)
# define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
# include_next <math.h>
# undef _GLIBCXX_INCLUDE_NEXT_C_HEADERS
#else
# include_next <math.h>
#endif
#endif // _KERNEL_MATH_H