|
| 1 | +# Foundation libc |
| 2 | + |
| 3 | +[](https://github.com/ZigEmbeddedGroup/foundation-libc/actions/workflows/build.yml) |
| 4 | + |
| 5 | +A C standard library that only implements a subset of functions that can be safely used without an operating system. |
| 6 | +This is called a [freestanding environment](https://en.cppreference.com/w/cpp/freestanding). |
| 7 | + |
| 8 | +This libc is primarily meant to be used with microcontrollers, hobbyist operating systems and so on. |
| 9 | + |
| 10 | +## Support |
| 11 | + |
| 12 | +The first goal is to reach full C11 *freestanding* support. |
| 13 | + |
| 14 | +- No support for locales |
| 15 | +- No allocator (ship your own!) |
| 16 | +- No support for functions that require an operating system of sorts in the background. |
| 17 | +- No support for `wchar_t` and `wchar.h` as it isn't portable between compilers. |
| 18 | +- Multi-byte character strings are implemented as UTF-8. |
| 19 | + |
| 20 | +## Customization |
| 21 | + |
| 22 | +Foundation libc doesn't really support much customization/configuration except for the hard required options. |
| 23 | + |
| 24 | +There is [`foundation/libc.h`](include/foundation/libc.h) which documents the behaviour of all required configurations. |
| 25 | + |
| 26 | +Right now, the following configurations exist: |
| 27 | + |
| 28 | +- `foundation_libc_panic_handler`, which allows users to catch detectable undefined behaviour. |
| 29 | + |
| 30 | +You can also configure the libc by chosing the build mode: |
| 31 | + |
| 32 | +- `Debug`: Implements additional safety checks and adds breakpoints in panics. |
| 33 | +- `ReleaseSafe`: Keeps the safety checks, but removes breakpoints. |
| 34 | +- `ReleaseSmall`: Still keeps a certain amount of safety, but drops long internal strings to reduce code and ram size. |
| 35 | +- `ReleaseFast`: Gotta go fast. Drops all safety and assumes all code behaves well. |
| 36 | + |
| 37 | +There are also certain "usage" configurations that can be chosen to affect behaviour when *using* the headers. Those are implemented as C macros/defines: |
| 38 | + |
| 39 | +- `FOUNDATION_LIBC_ASSERT` is a global macro that defines how `assert()` should behave: |
| 40 | + - `FOUNDATION_LIBC_ASSERT_DEFAULT=0`: Behaves like a regular assert that can print file name, assertion message and line. |
| 41 | + - `FOUNDATION_LIBC_ASSERT_NOFILE=1`: Drops the filename from the assertion to reduce code size. |
| 42 | + - `FOUNDATION_LIBC_ASSERT_NOMSG=2`: Additionally drops the assertion message from the assertion to reduce code size. |
| 43 | + - `FOUNDATION_LIBC_ASSERT_EXPECTED=3`: Replaces `assert(…)` with a construct that tells the compiler the assertion is always met. Makes code very fast. Assertions aren't checked. |
| 44 | + |
| 45 | +## Development |
| 46 | + |
| 47 | +Zig Version: 0.11 |
| 48 | + |
| 49 | +Run |
| 50 | +```sh-session |
| 51 | +user@microzig ~/foundation-libc $ zig build |
| 52 | +user@microzig ~/foundation-libc $ |
| 53 | +``` |
| 54 | + |
| 55 | +to compile the libc and generate a lib file in `zig-out/lib` as well as the headers in `zig-out/include`. |
| 56 | + |
| 57 | +## Contribution |
| 58 | + |
| 59 | +Start by grabbing a header marked with ⏳ or 🛠 and implement the functions from that header. See if others already have a PR open for those functions so you don't do work twice! |
| 60 | + |
| 61 | +Leverage functions from Zig `std` if possible as they are already well tested and should work. |
| 62 | + |
| 63 | +Which functions belong into which header can be figured out by taking a look at the *C11 Standard Draft* document or the *IBM libc functions* list. [cppreference.com](https://en.cppreference.com/w/c) usually has the better docs though, so best check out both. |
| 64 | + |
| 65 | +### Style Guides |
| 66 | + |
| 67 | +- The header files are ment to be as minimal as possible |
| 68 | + - Do not use comments documenting the functions, they are well documented everywhere else. |
| 69 | + - Only insert empty lines between functions if necessarity for clarity |
| 70 | + - Keep function names sorted alphabetically |
| 71 | +- Try not to use macros at all |
| 72 | +- Use `clang-format` with the provided style file. |
| 73 | + |
| 74 | + |
| 75 | +## Links |
| 76 | + |
| 77 | +- [C11 Standard](https://www.iso.org/standard/57853.html) |
| 78 | +- [C11 Standard Draft](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) |
| 79 | +- [ziglibc](https://github.com/marler8997/ziglibc) |
| 80 | +- [libc-test](https://wiki.musl-libc.org/libc-test.html) by musl |
| 81 | +- [cppreference on freestanding](https://en.cppreference.com/w/cpp/freestanding) |
| 82 | +- [GCC on freestanding](https://gcc.gnu.org/onlinedocs/gcc/Standards.html#C-Language) |
| 83 | +- [IBM libc functions](https://www.ibm.com/docs/en/i/7.5?topic=extensions-standard-c-library-functions-table-by-name) (function to header map) |
| 84 | +- [cppreference](https://en.cppreference.com/w/c) |
| 85 | +- [clang-format style options](https://releases.llvm.org/16.0.0/tools/clang/docs/ClangFormatStyleOptions.html) |
| 86 | + |
| 87 | +## Status |
| 88 | + |
| 89 | +⏳ (not started), 🛠 (work in progress), ⚠️ (partial support), ✅ (full support), ❌ (no support), 🔮 (potential future support), 🔀 (implemented by compiler) |
| 90 | + |
| 91 | +| Header File | Header Status | Implementation Status | Description | |
| 92 | +| --------------- | ------------- | --------------------- | ------------------------------------------------------------------------------------------------------- | |
| 93 | +| `assert.h` | ✅ | ✅ | Conditionally compiled macro that compares its argument to zero | |
| 94 | +| `complex.h` | ❌ | | (since C99) Complex number arithmetic | |
| 95 | +| `ctype.h` | ✅ | ✅ | Functions to determine the type contained in character data | |
| 96 | +| `errno.h` | ✅ | ✅ | Macros reporting error conditions | |
| 97 | +| `fenv.h` | 🔮 | | (since C99) Floating-point environment | |
| 98 | +| `float.h` | 🔀 | | Limits of floating-point types | |
| 99 | +| `inttypes.h` | ⏳ | ⏳ | (since C99) Format conversion of integer types | |
| 100 | +| `iso646.h` | 🔀 | | (since C95) Alternative operator spellings | |
| 101 | +| `limits.h` | 🔀 | | Ranges of integer types | |
| 102 | +| `locale.h` | ❌ | | Localization utilities | |
| 103 | +| `math.h` | 🛠 | ⏳ | Common mathematics functions | |
| 104 | +| `setjmp.h` | 🛠 | ⏳ | Nonlocal jumps | |
| 105 | +| `signal.h` | ❌ | | Signal handling | |
| 106 | +| `stdalign.h` | 🔀 | | (since C11) alignas and alignof convenience macros | |
| 107 | +| `stdarg.h` | 🔀 | | Variable arguments | |
| 108 | +| `stdatomic.h` | 🔮 | | (since C11) Atomic operations | |
| 109 | +| `stdbit.h` | 🔮 | | (since C23) Macros to work with the byte and bit representations of types | |
| 110 | +| `stdbool.h` | 🔀 | | (since C99) Macros for boolean type | |
| 111 | +| `stdckdint.h` | 🔮 | | (since C23) macros for performing checked integer arithmetic | |
| 112 | +| `stddef.h` | 🔀 | | Common macro definitions | |
| 113 | +| `stdint.h` | 🔀 | | (since C99) Fixed-width integer types | |
| 114 | +| `stdio.h` | ❌ | | Input/output | |
| 115 | +| `stdlib.h` | 🛠 | 🛠 | General utilities: memory management, program utilities, string conversions, random numbers, algorithms | |
| 116 | +| `stdnoreturn.h` | 🔀 | | (since C11) noreturn convenience macro | |
| 117 | +| `string.h` | ✅ | 🛠 | String handling | |
| 118 | +| `tgmath.h` | ⏳ | ⏳ | (since C99) Type-generic math (macros wrapping math.h and complex.h) | |
| 119 | +| `threads.h` | ❌ | | (since C11) Thread library | |
| 120 | +| `time.h` | ❌ | | Time/date utilities | |
| 121 | +| `uchar.h` | 🛠 | ⏳ | (since C11) UTF-16 and UTF-32 character utilities | |
| 122 | +| `wchar.h` | ❌ | | (since C95) Extended multibyte and wide character utilities | |
| 123 | +| `wctype.h` | ❌ | | (since C95) Functions to determine the type contained in wide character data | |
| 124 | + |
| 125 | + |
0 commit comments