Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include/flatbuffers/base.h: fix build on musl #6773

Closed
wants to merge 1 commit into from

Conversation

ffontaine
Copy link

Build of applications using flatbuffers such as snort3 are broken on musl since version 1.11.0 and 5f32f94 because strtoll_l (and strtoull_l) are not available on musl. flatbuffers checks for the availability of strtoull_l in CMakeLists.txt so flatbuffers builds successfully but for applications using flatbuffers, the result of this check is not available and FLATBUFFERS_LOCALE_INDEPENDENT is set to 1 resulting in the following build failure:

In file included from /tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/flexbuffers.h:24,
                 from /tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/idl.h:26,
                 from /tmp/instance-0/output-1/build/snort3-3.1.6.0/src/network_inspectors/perf_monitor/fbs_formatter.cc:29:
/tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/util.h: In function 'void flatbuffers::strtoval_impl(int64_t*, const char*, char**, int)':
/tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/util.h:258:12: error: 'strtoll_l' was not declared in this scope; did you mean 'strcoll_l'?
  258 |     *val = __strtoll_impl(str, endptr, base);
      |            ^~~~~~~~~~~~~~

Fix this failure by checking if __GNUC__ is defined before setting FLATBUFFERS_LOCALE_INDEPENDENT to 1.

Fixes:

Signed-off-by: Fabrice Fontaine fontaine.fabrice@gmail.com

Build of applications using flatbuffers such as snort3 are broken on
musl since version 1.11.0 and
google@5f32f94
because strtoll_l (and strtoull_l) are not available on musl.
flatbuffers checks for the availability of strtoull_l in CMakeLists.txt
so flatbuffers builds successfully but for applications using
flatbuffers, the result of this check is not available and
FLATBUFFERS_LOCALE_INDEPENDENT is set to 1 resulting in the following
build failure:

In file included from /tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/flexbuffers.h:24,
                 from /tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/idl.h:26,
                 from /tmp/instance-0/output-1/build/snort3-3.1.6.0/src/network_inspectors/perf_monitor/fbs_formatter.cc:29:
/tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/util.h: In function 'void flatbuffers::strtoval_impl(int64_t*, const char*, char**, int)':
/tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/util.h:258:12: error: 'strtoll_l' was not declared in this scope; did you mean 'strcoll_l'?
  258 |     *val = __strtoll_impl(str, endptr, base);
      |            ^~~~~~~~~~~~~~

Fix this failure by checking if __GNUC__ is defined before setting
FLATBUFFERS_LOCALE_INDEPENDENT to 1.

Fixes:
 - http://autobuild.buildroot.org/results/68045b83e94f8caa337b1af7ed5f493ac1a55c47

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
@github-actions github-actions bot added the c++ label Aug 10, 2021
@@ -266,7 +266,7 @@ namespace flatbuffers {
#ifndef FLATBUFFERS_LOCALE_INDEPENDENT
// Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}.
#if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \
(defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21))))
(defined(__GLIBC__) && defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Does FreeBSD CLIB declare __GLIBC__ macro?
What about BIONIC that uses _XOPEN_VERSION=700 and other CLIB that doesn't declare nor GLIBC neither other macros (as MUSL does)?

MUSL recommends use uselocale() call instead of _l methods.
https://man7.org/linux/man-pages/man3/uselocale.3.html
Probably it can be an acceptable solution for non-Windows platforms.
The uselocale() can be called only once in the Parser using RAII.

Copy link
Author

Choose a reason for hiding this comment

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

Indeed, this PR won't work with clib or Bionic.
I don't master enough flatbuffers to add uselocale() but if you can provide a PR, I can test that it builds fine on musl.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sorry. I will be unavailable until September even for code review.

The main idea is to add into utils.cpp or into idl_parser.cpp method like this:

  • flatbuffers::LocaleHandle UseLocale(flatbuffers::LocaleHandle loc=DefaultClassic) - activate locale and return the previous one.
    This method can do nothing if FLATBUFFERS_LOCALE_INDEPENDENT is defined and non-zero.
    The flatbuffers::LocaleHandle type can be committed if UseLocale is a cpp-local routine in idl_parser.cpp.

The Parser::DoParse and Parser::DoParseJson should create an RAII-object that will call the UseLocale in ctor and dtor. After this, we can limit FLATBUFFERS_LOCALE_INDEPENDENT auto-detection only by MSVC and CMake.

You can try this approach and prepare PR. Or we can start it in September.

@dbaileychess
Copy link
Collaborator

@ffontaine Any update to the comments @vglavnyy left?

@ffontaine
Copy link
Author

No update on my side, as I wrote before I don't master enough flatbuffers to make this kind of change. However, if you can provide a PR, I can test that it builds fine on musl.

@dbaileychess
Copy link
Collaborator

Closing, since there is no active work on this, and I don't plan to do it myself. Sorry!

robimarko pushed a commit to sartura/flatbuffers that referenced this pull request Nov 16, 2023
Build of applications using flatbuffers such as snort3 are broken on
musl since version 1.11.0 and
google@5f32f94
because strtoll_l (and strtoull_l) are not available on musl.
flatbuffers checks for the availability of strtoull_l in CMakeLists.txt
so flatbuffers builds successfully but for applications using
flatbuffers, the result of this check is not available and
FLATBUFFERS_LOCALE_INDEPENDENT is set to 1 resulting in the following
build failure:

In file included from /tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/flexbuffers.h:24,
                 from /tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/idl.h:26,
                 from /tmp/instance-0/output-1/build/snort3-3.1.6.0/src/network_inspectors/perf_monitor/fbs_formatter.cc:29:
/tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/util.h: In function 'void flatbuffers::strtoval_impl(int64_t*, const char*, char**, int)':
/tmp/instance-0/output-1/host/x86_64-buildroot-linux-musl/sysroot/usr/include/flatbuffers/util.h:258:12: error: 'strtoll_l' was not declared in this scope; did you mean 'strcoll_l'?
  258 |     *val = __strtoll_impl(str, endptr, base);
      |            ^~~~~~~~~~~~~~

Fix this failure by checking if __GNUC__ is defined before setting
FLATBUFFERS_LOCALE_INDEPENDENT to 1.

Fixes:
 - http://autobuild.buildroot.org/results/68045b83e94f8caa337b1af7ed5f493ac1a55c47

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
[Upstream status: Rejected:
 google#6773]
Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
(rebased and added fix for uClibc-build)
ibc added a commit to ibc/flatbuffers that referenced this pull request Dec 28, 2023
Build of applications using flatbuffers such as mediasoup (see related
issue versatica/mediasoup#1223) are broken on
musl (such as in Alpine Linux) since version 1.11.0 and google/flatbuffers@5f32f94
because strtoll_l (and strtoull_l) are not available on musl.

flatbuffers checks for the availability of strtoull_l in CMakeLists.txt
so flatbuffers builds successfully, but for applications using
flatbuffers the result of this check is not available and
FLATBUFFERS_LOCALE_INDEPENDENT is set to 1 resulting in the following
build failure:

In file included from ../../../subprojects/flatbuffers-23.3.3/include/flatbuffers/minireflect.h:21,
  from ../../../include/Channel/ChannelRequest.hpp:8,
  from ../../../include/Settings.hpp:6,
  from ../../../include/Logger.hpp:91,
  from ../../../src/RTC/RTCP/XrReceiverReferenceTime.cpp:5:
  ../../../subprojects/flatbuffers-23.3.3/include/flatbuffers/util.h: In function 'void flatbuffers::strtoval_impl(int64_t, const char*, char**, int)':
  ../../../subprojects/flatbuffers-23.3.3/include/flatbuffers/util.h:229:38: error: 'strtoll_l' was not declared in this scope; did you mean 'strtold_l'?
  229 | #define __strtoll_impl(s, pe, b) strtoll_l(s, pe, b, ClassicLocale::Get())
      |         ^~~~~~~~~~~~~~

This issue was reported in google#6773
but was closed without a solution. Then a flatbuffers fork made this
exact commit:
sartura@92bd624

Due to rationale given in the closed issue, I'm not sure this PR will
be accepted. If not, I strongly think that flatbuffers documentation
should say that applications using it in musl environments must define
FLATBUFFERS_LOCALE_INDEPENDENT=0 in their main project.
@matteius
Copy link

This is the patch I needed to build tensorflow-lite for my embedded device -- I keep having to re-add it when building.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants