diff options
-rw-r--r-- | build-aux/measurestack/app_plugins.py | 9 | ||||
-rw-r--r-- | libfmt/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libfmt/include/libfmt/fmt.h | 10 | ||||
-rw-r--r-- | libfmt/libobj.c | 17 |
4 files changed, 36 insertions, 2 deletions
diff --git a/build-aux/measurestack/app_plugins.py b/build-aux/measurestack/app_plugins.py index 064ea06..1155098 100644 --- a/build-aux/measurestack/app_plugins.py +++ b/build-aux/measurestack/app_plugins.py @@ -507,6 +507,7 @@ class PicoFmtPlugin: return [x.as_qname() for x in self.known_fct.values()], False if "specifier_table" in line: return [ + # pico-fmt QName("conv_sint"), QName("conv_uint"), # QName("conv_double"), @@ -514,6 +515,8 @@ class PicoFmtPlugin: QName("conv_str"), QName("conv_ptr"), QName("conv_pct"), + # libfmt + QName("libfmt_conv_formatter"), ], False return None @@ -548,7 +551,7 @@ class PicoSDKPlugin: *, get_init_array: typing.Callable[[], typing.Collection[QName]], ) -> None: - # grep for '__attribute__((constructor))'. + # grep for '__attribute__((constructor))' / '[[gnu::constructor]]'. self.get_init_array = get_init_array self.app_init_array = None @@ -952,7 +955,9 @@ class LibGCCPlugin: return False def init_array(self) -> typing.Collection[QName]: - return [] + return [ + QName("libfmt_install_formatter"), + ] def extra_includes(self) -> typing.Collection[BaseName]: return [] diff --git a/libfmt/CMakeLists.txt b/libfmt/CMakeLists.txt index b8db764..2d44c03 100644 --- a/libfmt/CMakeLists.txt +++ b/libfmt/CMakeLists.txt @@ -7,8 +7,10 @@ add_library(libfmt INTERFACE) target_include_directories(libfmt SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) target_sources(libfmt INTERFACE libmisc.c + libobj.c ) target_link_libraries(libfmt INTERFACE pico_fmt libmisc + libobj ) diff --git a/libfmt/include/libfmt/fmt.h b/libfmt/include/libfmt/fmt.h index 3f2d4e7..4dba82f 100644 --- a/libfmt/include/libfmt/fmt.h +++ b/libfmt/include/libfmt/fmt.h @@ -10,4 +10,14 @@ #include <pico/fmt_printf.h> #include <pico/fmt_install.h> +#include <libobj/obj.h> + +/** + * An object that implements fmt_formatter can be printed using + * `printf("%v", boxed_obj)`. + */ +#define fmt_formatter_LO_IFACE \ + LO_FUNC(void, format, struct fmt_state *) +LO_INTERFACE(fmt_formatter) + #endif /* _LIBFMT_FMT_H_ */ diff --git a/libfmt/libobj.c b/libfmt/libobj.c new file mode 100644 index 0000000..e4b833b --- /dev/null +++ b/libfmt/libobj.c @@ -0,0 +1,17 @@ +/* libfmt/libobj.c - Integrate pico-fmt with libobj + * + * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <libfmt/fmt.h> + +static void libfmt_conv_formatter(struct fmt_state *state) { + lo_interface fmt_formatter obj = va_arg(*state->args, lo_interface fmt_formatter); + LO_CALL(obj, format, state); +} + +[[gnu::constructor]] +static void libfmt_install_formatter(void) { + fmt_install('v', libfmt_conv_formatter); +} |