Skip to content

support picolibc#319

Open
abobija wants to merge 1 commit intointel:mainfrom
abobija:support-picolibc
Open

support picolibc#319
abobija wants to merge 1 commit intointel:mainfrom
abobija:support-picolibc

Conversation

@abobija
Copy link
Copy Markdown

@abobija abobija commented Apr 2, 2026

With current HAVE_OPEN_FUNOPEN and HAVE_OPEN_FOPENCOOKIE defines i can not configure src/open_memstream.c to work with picolibc's version of funopen.

This PR is just POC (i'm sure there is a cleaner way) how to configure it to work.

picolibc is default libc for espressif's esp-idf from version 6 so it's going to be used there a lot so it would be cool if tinycbor could support picolibc somehow as well.

Copy link
Copy Markdown
Member

@thiagomacieira thiagomacieira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the POC, but we're going to need to work on this. The patch cannot be right.

Please start with what error messages you're getting.

Comment on lines +57 to +61
#ifdef __PICOLIBC__
static RetType write_to_buffer(void *cookie, const void *data, LenType len)
#else
static RetType write_to_buffer(void *cookie, const char *data, LenType len)
#endif
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same thing, so you didn't do anything.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for response.

Error that I'm facing:

tinycbor/src/open_memstream.c:103:29: error: passing argument 3 of 'funopen' from incompatible pointer type [-Wincompatible-pointer-types]
  103 |     return funopen(b, NULL, write_to_buffer, NULL, close_buffer);
      |                             ^~~~~~~~~~~~~~~
      |                             |
      |                             RetType (*)(void *, const char *, LenType) {aka int (*)(void *, const char *, int)}

In file included from tinycbor/src/open_memstream.c:31:
picolibc/include/stdio.h:429:28: note: expected '_ssize_t (*)(void *, const void *, size_t)' {aka 'int (*)(void *, const void *, unsigned int)'} but argument is of type 'RetType (*)(void *, const char *, LenType)' {aka 'int (*)(void *, const char *, int)'}
  429 |                 _ssize_t (*writefn)(void *cookie, const void *buf, size_t n),
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The goal is to align signature of tinycbor's write_to_buffer function with picolibc's writefn function signature:

_ssize_t (*writefn)(void *cookie, const void *buf, size_t n)

In my POC, for __PICOLIBC__, I've set:

  • RetType to be ssize_t
  • LenType to be size_t

With that I'm still getting error of signature mismatch since second parameter (data) of tinycbor's function write_to_buffer is type of const char* but picolibc expects that param to be type ofconst void*. I've fixed that with this:

+ #ifdef __PICOLIBC__
+ static RetType write_to_buffer(void *cookie, const void *data, LenType len)
+ #else
static RetType write_to_buffer(void *cookie, const char *data, LenType len)
+ #endif

^ you can see there that type of second parameter is changed from const char* to const void* for picolibc.

Maybe we need new DataType beside RetType and LenType to make open_memstream.c more scalable?

Hope I've explain it better this time.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see the problem: for BSD systems (including Apple's macOS), funopen's callback functions take a const char * second parameter, but in picolibc, it's const void *. I suppose that makes sense, as we could be writing binary data through fwrite and such. However, this is very stupid of picolibc to mismatch the API: if you're trying to implement the API that 4.4 BSD introduced, it should match the signature.

To fix this, we need to update the CMakeLists.txt test to do more than check_symbol_exists:

tinycbor/CMakeLists.txt

Lines 133 to 135 in 6442e74

check_symbol_exists(open_memstream stdio.h HAVE_OPEN_MEMSTREAM)
check_symbol_exists(funopen stdio.h HAVE_OPEN_FUNOPEN)
check_symbol_exists(fopencookie stdio.h HAVE_OPEN_FOPENCOOKIE)

We need a compile check that we are matching the arguments correctly, to be able to match the two variants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants