[Concept,00/18] ulib: Complete initial U-Boot library

Message ID 20250909151824.2327219-1-sjg@u-boot.org
Headers
Series ulib: Complete initial U-Boot library |

Message

Simon Glass Sept. 9, 2025, 3:17 p.m. UTC
  From: Simon Glass <sjg@chromium.org>


This series completes support for building U-Boot as a shared or static
library, enabling reuse of U-Boot functionality in external programs and
test suites.

The U-Boot library (ulib) allows developers to:
- Link against U-Boot functionality without a full U-Boot image
- Use U-Boot's OS abstraction layer, drivers, and utility functions
- Build test programs that can exercise U-Boot code in isolation
- Create applications that benefit from U-Boot's hardware support

Key features:
- Builds both shared (libu-boot.so) and static (libu-boot.a) libraries
- Preserves U-Boot linker lists for proper driver/subsystem init
- Configurable symbol renaming to avoid conflicts with system libraries
- Generated API headers with renamed function declarations
- Documentation and working examples
- Currently only supports sandbox architecture

The series includes:
- More build-infrastructure and Makefile integration
- Python-based mechanism for symbol renaming and API generation
- Test programs demonstrating basic library usage
- A simple example program showing real-world usage patterns

Symbol renaming ensures that U-Boot functions don't conflict with system
libraries. For example, printf() remains the standard library function
while ub_printf() provides access to U-Boot's printf implementation.
This is handled automatically during the build process.

The library excludes main() to allow external programs to provide their own
entry points while still accessing U-Boot functionality through ulib_init()
and ulib_uninit().

For example:

    #include <u-boot-lib.h>
    #include <u-boot-api.h>

    int main(int argc, char *argv[])
    {
        if (ulib_init(argv[0]) < 0)
            return 1;

        ub_printf("Hello from U-Boot library!\n");

        ulib_uninit();

        return 0;
    }

License implications are documented - the GPL-2.0+ license applies to
any programs linked with the library, requiring source code distribution
for compliant usage.

Future work will look at expanding support to other architectures.


Simon Glass (18):
  lib: Tidy up comments for vsprintf functions
  u_boot_pylib: Correct docs for run_test_coverage() required
  os.h: Add standard includes for types used in os.h
  sandbox: Enable ULIB just for the sandbox build
  test: Move the non-LTO test to sandbox_flattree
  test/py: Allow setting the cwd with run_and_log()
  ulib: Move ulib into its own directory
  ulib: scripts: Add a script to support symbol-renaming
  ulib: Use the correct copyright message
  ulib: Disable LTO when building the library
  ulib: Provide a symbol-rename file
  ulib: Makefile: Create a library with renamed symbols
  ulib: Makefile: Plumb in renaming symbols for ulib
  ulib: Makefile: Plumb in creation of the API header
  ulib: Expand the ulib example to have two files
  ulib: Adjust the test to check symbol renaming
  ulib: doc: Expand the documentation to cover new features
  test/py: Add a test for ulib functionality

 Kconfig                            |   3 +-
 Makefile                           |  54 ++-
 arch/sandbox/cpu/u-boot-lib.lds    |   2 +-
 configs/sandbox_defconfig          |   1 +
 doc/develop/ulib.rst               | 179 +++++++-
 examples/ulib/.gitignore           |   2 +
 examples/ulib/Makefile             |  60 ++-
 examples/ulib/demo.c               |  14 +-
 examples/ulib/demo_helper.c        |  28 ++
 examples/ulib/demo_helper.h        |  31 ++
 include/os.h                       |   2 +
 include/u-boot-lib.h               |   2 +-
 include/vsprintf.h                 |  25 +-
 lib/Makefile                       |   2 +-
 lib/ulib/Makefile                  |   6 +
 lib/ulib/rename.syms               |  30 ++
 lib/{ => ulib}/ulib.c              |   2 +-
 scripts/build_api.py               | 714 +++++++++++++++++++++++++++++
 test/py/tests/test_sandbox_opts.py |   8 +-
 test/py/tests/test_ulib.py         | 141 ++++++
 test/py/utils.py                   |  13 +-
 test/run                           |   8 +
 test/scripts/test_build_api.py     | 704 ++++++++++++++++++++++++++++
 test/ulib/ulib_test.c              |  12 +-
 tools/u_boot_pylib/test_util.py    |   2 +-
 25 files changed, 1970 insertions(+), 75 deletions(-)
 create mode 100644 examples/ulib/.gitignore
 create mode 100644 examples/ulib/demo_helper.c
 create mode 100644 examples/ulib/demo_helper.h
 create mode 100644 lib/ulib/Makefile
 create mode 100644 lib/ulib/rename.syms
 rename lib/{ => ulib}/ulib.c (95%)
 create mode 100755 scripts/build_api.py
 create mode 100644 test/py/tests/test_ulib.py
 create mode 100644 test/scripts/test_build_api.py