|
1 | 1 | .. _entrypoints: |
2 | 2 |
|
| 3 | +======================== |
3 | 4 | Entrypoints in LLVM libc |
4 | | ------------------------- |
| 5 | +======================== |
5 | 6 |
|
6 | 7 | A public function or a global variable provided by LLVM-libc is called an |
7 | | -entrypoint. The notion of entrypoints is ingrained in LLVM-libc's |
8 | | -source layout, build system and source code. |
| 8 | +*entrypoint*. The notion of entrypoints is central to LLVM-libc's source layout, |
| 9 | +build system, and configuration management. This document provides a technical |
| 10 | +reference for how entrypoints are defined, implemented, and integrated into |
| 11 | +the final library. |
| 12 | + |
| 13 | +What is an Entrypoint? |
| 14 | +---------------------- |
| 15 | + |
| 16 | +In a typical C library, all functions are part of a monolithic archive. In |
| 17 | +LLVM-libc, each function (e.g., ``malloc``, ``printf``, ``isalpha``) is treated |
| 18 | +as a discrete "entrypoint" unit. This allows for: |
| 19 | + |
| 20 | +- **Granular build targets**: You can build just the objects you need. |
| 21 | +- **Configuration-driven selection**: Different operating systems and |
| 22 | + architectures can pick specific implementations for the same function. |
| 23 | +- **Support for multiple build modes**: Selectively replacing parts of a host's |
| 24 | + libc in :ref:`overlay_mode` or building a complete library in :ref:`full_host_build`. |
| 25 | + |
| 26 | +The Lifecycle of an Entrypoint |
| 27 | +------------------------------ |
| 28 | + |
| 29 | +1. **Implementation**: The function is implemented in a ``.cpp`` file using |
| 30 | + LLVM-libc's coding and implementation standards. |
| 31 | +2. **Registration**: The entrypoint is defined as a CMake target using the |
| 32 | + ``add_entrypoint_object`` rule. |
| 33 | +3. **Configuration**: The target name is added to an ``entrypoints.txt`` file |
| 34 | + to include it in a specific OS/Architecture configuration. |
| 35 | + |
| 36 | +Implementation Standards |
| 37 | +------------------------ |
| 38 | + |
| 39 | +Implementations live in the ``src/`` directory, organized by the public header |
| 40 | +they belong to (e.g., ``src/ctype/isalpha.cpp`` for ``ctype.h``). |
| 41 | + |
| 42 | +Header File Structure |
| 43 | +^^^^^^^^^^^^^^^^^^^^^ |
| 44 | + |
| 45 | +Every entrypoint has an internal implementation header file (e.g., |
| 46 | +``src/ctype/isalpha.h``). This header declares the function within the |
| 47 | +``LIBC_NAMESPACE_DECL`` namespace:: |
| 48 | + |
| 49 | + namespace LIBC_NAMESPACE_DECL { |
| 50 | + int isalpha(int c); |
| 51 | + } // namespace LIBC_NAMESPACE_DECL |
| 52 | + |
| 53 | +Source File Structure |
| 54 | +^^^^^^^^^^^^^^^^^^^^^ |
| 55 | + |
| 56 | +The implementation file (e.g., ``src/ctype/isalpha.cpp``) defines the function |
| 57 | +using the ``LLVM_LIBC_FUNCTION`` macro. This macro handles C-linkage and |
| 58 | +aliasing:: |
| 59 | + |
| 60 | + namespace LIBC_NAMESPACE_DECL { |
| 61 | + LLVM_LIBC_FUNCTION(int, isalpha, (int c)) { |
| 62 | + // ... implementation ... |
| 63 | + } |
| 64 | + } // namespace LIBC_NAMESPACE_DECL |
| 65 | + |
| 66 | +For more details on implementation conventions, see the |
| 67 | +:ref:`implementation_standard` page. |
| 68 | + |
| 69 | +Registration: CMake Rules |
| 70 | +------------------------- |
| 71 | + |
| 72 | +Entrypoints are registered as CMake targets to make them available to the |
| 73 | +build system. These rules are usually defined in the ``CMakeLists.txt`` file |
| 74 | +within the function's source directory. |
| 75 | + |
| 76 | +``add_entrypoint_object`` |
| 77 | +^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 78 | + |
| 79 | +This rule generates a single object file containing the implementation of the |
| 80 | +entrypoint. |
| 81 | + |
| 82 | +.. code-block:: cmake |
| 83 | +
|
| 84 | + add_entrypoint_object( |
| 85 | + isalpha |
| 86 | + SRCS isalpha.cpp |
| 87 | + HDRS isalpha.h |
| 88 | + DEPENDS |
| 89 | + .some_internal_dependency |
| 90 | + ) |
| 91 | +
|
| 92 | +For redirecting entrypoints (e.g., when one function is a simple alias for |
| 93 | +another), the ``REDIRECTED`` option can be specified to the rule. |
| 94 | + |
| 95 | +``add_entrypoint_library`` |
| 96 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 97 | + |
| 98 | +Standard library files like ``libc.a`` and ``libm.a`` are produced by |
| 99 | +aggregating multiple entrypoint objects. The ``add_entrypoint_library`` target |
| 100 | +takes a list of ``add_entrypoint_object`` targets and produces a static library. |
| 101 | + |
| 102 | +Configuration: ``entrypoints.txt`` |
| 103 | +---------------------------------- |
| 104 | + |
| 105 | +The final selection of which entrypoints are included in a specific build is |
| 106 | +determined by ``entrypoints.txt`` files located in the ``libc/config`` tree. |
| 107 | + |
| 108 | +- **Location**: Typically found in ``libc/config/<os>/entrypoints.txt`` or |
| 109 | + ``libc/config/<os>/<arch>/entrypoints.txt``. |
| 110 | +- **Role**: This file acts as the "source of truth" for what is supported on a |
| 111 | + given platform. A typical bring-up procedure involves progressively adding |
| 112 | + targets to this file as they are implemented and tested. |
| 113 | + |
| 114 | +If you are implementing a new entrypoint, you must add its target name to the |
| 115 | +relevant ``entrypoints.txt`` files for it to be included in the library build. |
| 116 | +For more details on platform configuration, see the :ref:`porting` guide. |
0 commit comments