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

Fix -Wformat / -Wformat-non-iso on MinGW UCRT #874

Merged
merged 2 commits into from
Mar 15, 2022

Conversation

tongyuantongyu
Copy link
Contributor

MinGW can be configured to use UCRT, which is the new C runtime library for Windows. I'm moving my MinGW dev environment to the UCRT variant.

UCRT supports "%zu" properly so MinGW didn't patch*printf functions and set __USE_MINGW_ANSI_STDIO to 1. This makes AVIF_FMT_ZU defined to "%Iu", and triggers warning from GCC and Clang.

Detect UCRT by check _UCRT macro, which MSSVC and MinGW both defines, and use "%zu" when using UCRT.

//
// Related mingw-w64 commit: bfd33f6c0ec5e652cc9911857dd1492ece8d8383

#if (defined(_MSVC) && _MSVC < 1800) || (defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 0)
#if !defined(_UCRT) && ((defined(_MSVC) && _MSVC < 1800) || (defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 0))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to use UCRT with Visual Studio before 2013? Otherwise it can be "simplified" to:

#if (defined(_MSVC) && _MSVC < 1800) || (defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 0 && !defined(_UCRT))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

VS <= 2013 never use UCRT and VS >= 2015 always use UCRT.
However, I find this expression easier to understand:

we want a feature in the C runtime.

  • UCRT definitely provides it.
  • msvcr120 (the version of msvcrt Visual Studio 2013 uses) provides it, but no macro can be used to test CRT version so test Visual Studio version instead.
  • MinGW sometimes provides it by patch and we can test if the patch is applied.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi Yuan,

It is reasonable to require Visual Studio 2015 or later today. For example, libaom requires Visual Studio 2017 or later:

https://aomedia.googlesource.com/aom/+/master/README.md#microsoft-visual-studio-builds

Let's drop support for Visual Studio 2013 or older. Could you simplify this expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@kmilos
Copy link
Contributor

kmilos commented Mar 11, 2022

Actually, this is much better/cleaner fixed by using standard PRI* print formatters from inttypes.h directly in libavif instead of trying to figure it out here.

inttypes.h under MinGW implementation already does the correct #if defined(_UCRT) || __USE_MINGW_ANSI_STDIO for you.

Edit: scratch that, sorry... I now see inttypes.h doesn't define a format for size_t, so this is ok... unless you want to use PRIuPTR (uintptr_t is the same or wider than size_t).

Copy link
Collaborator

@wantehchang wantehchang left a comment

Choose a reason for hiding this comment

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

LGTM. I edited the comment slightly.

// Additionally, with c99 set as the standard mingw-w64 toolchains built with
// the commit mentioned can patch format functions to support the %z specifier,
// even it's using the old msvcrt, and this can be detected by
// even if it's using the old msvcrt, and this can be detected by
Copy link
Collaborator

Choose a reason for hiding this comment

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

@tongyuantongyu Yuan: I added "if" after "even". I am less certain about this change. Please check it. Thanks.

@wantehchang wantehchang merged commit ac769a1 into AOMediaCodec:master Mar 15, 2022
@tongyuantongyu tongyuantongyu deleted the fix-mingw-wformat branch July 18, 2022 06:54
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.

4 participants