Files
u-boot/examples/ulib
Simon Glass f457b31524 test/py: Add a test for ulib functionality
Provide a test that checks that ulib operates as expected.

Add a .gitignore file for the executables thus created

There is a strange interaction with PLATFORM_LIBS which can cause the
examples to fail to build via 'make qcheck':

- CI runs 'make qcheck'
- the main Makefile sets PLATFORM_LIBS
- test/run calls test.py
- at some point test_ulib_demos() starts, with PLATFORM_LIBS set
- the test calls 'make' on examples/ulib/Makefile
- PLATFORM_LIBS is left alone, since it already has a value
- lSDL ends up not being in the link line

Thank you to Claude for helping to debug this and figure out the
PLATFORM_LIBS interaction.

Series-to: concept
Series-cc: heinrich
Cover-letter:
ulib: Complete initial U-Boot library

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.
END

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
2025-09-09 09:14:05 -06:00
..

U-Boot Library Example
======================

This directory contains example programs showing how to use the U-Boot library
(libu-boot.so) in external C programs.

Building U-Boot Library
-----------------------

First, build U-Boot with library support:

    make O=/tmp/b/sandbox -j$(nproc) sandbox_defconfig all

This creates:
- /tmp/b/sandbox/libu-boot.so (shared library)
- /tmp/b/sandbox/libu-boot.a (static library)

Example Programs
----------------

The examples are built automatically as part of the U-Boot build. So far there
is only one.

**demo.c** - Demonstrates using U-Boot library functions

- Shows how to init the library with ulib_init()
- Uses U-Boot's OS functions (os_open(), os_fgets(), os_close())
- Reads and displays system information
- Shows the U-Boot version

Building Examples
-----------------

The examples are built automatically when U-Boot has these options enabled::

    CONFIG_ULIB=y
    CONFIG_EXAMPLES=y

To build manually:

    # From this directory examples/ulib
    make UBOOT_BUILD=/tmp/b/sandbox/ srctree=../..

Running Examples
----------------

    # Run the demo
    LD_LIBRARY_PATH=/tmp/b/sandbox ./demo

    # Run the demo (static version)
    ./demo_static

Key Points
----------

- Avoid including U-Boot headers that conflict with system headers. This
  Makefile gives priority to the system headers
- Use ulib_init() to init the library
- Use ulib_uninit() to clean up
- Set LD_LIBRARY_PATH when running dynamically linked programs

Copying for External Use
-------------------------

This directory can be copied elsewhere and used independently:

    cp -r examples/ulib ~/my-project/
    cd ~/my-project/ulib
    make UBOOT_BUILD=/path/to/u-boot-build  srctree=/path/to/u-boot-source

License
-------

All examples are licensed under GPL-2.0+