[libc++] Fix build with older mingw-w64 lacking _strtof_l #234640
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Himadhith
<details>
<summary>Changes</summary>
This patch fixes the MinGW cross-compilation break reported after #<!-- -->229456
landed:
libcxx/include/__locale_dir/support/windows.h:183:12: error: no member
named '_strtof_l' in the global namespace
ERANGE is set on overflow to infinity and when the result is nonzero and below FLT_MIN in magnitude (subnormals included), same as glibc's strtof and mingw-w64/mingw-w64@<!-- -->03ad04e.
The only difference is that we compare the narrowed float and not the double, so a double just below FLT_MIN that rounds up to FLT_MIN gets no ERANGE.
---
Full diff: https://github.com/llvm/llvm-project/pull/234640.diff
1 Files Affected:
````````````diff --git a/libcxx/include/__locale_dir/support/windows.h b/libcxx/include/__locale_dir/support/windows.h
index a72aaccccef15..726fc00f9968f 100644
--- a/libcxx/include/__locale_dir/support/windows.h
+++ b/libcxx/include/__locale_dir/support/windows.h
@@ -15,6 +15,8 @@
#include <clocale> // std::lconv & friends
#include <cstddef>
#include <ctype.h> // ::_isupper_l & friends
+#include <errno.h> // errno, ERANGE
+#include <float.h> // FLT_MIN
#include <locale.h> // ::_locale_t
#include <stdio.h> // ::_sscanf_l
#include <stdlib.h> // ::_strtod_l & friends
@@ -180,7 +182,17 @@ _LIBCPP_HIDE_FROM_ABI _FloatT __str_to_float_c_locale(const char* __nptr, char**
template <>
inline _LIBCPP_HIDE_FROM_ABI float __str_to_float_c_locale<float>(const char* __nptr, char** __endptr) {
+#if defined(_LIBCPP_MSVCRT)
return ::_strtof_l(__nptr, __endptr, __get_c_locale());
+#else
+ // MinGW has no _strtof_l, so we narrow the _strtod_l result to float and set
+ // ERANGE on overflow and underflow like mingw-w64.
+ const double __res = ::_strtod_l(__nptr, __endptr, __get_c_locale());
+ const float __f = static_cast<float>(__res);
+ if (__builtin_isfinite(__res) && (__builtin_isinf(__f) || (__res != 0 && __builtin_fabsf(__f) < FLT_MIN)))
+ errno = ERANGE;
+ return __f;
+#endif
}
template <>
</details>
Older versions of mingw lack it; recent versions do have _strtof_l polyfilled for all CRT versions.
Code wise this looks good to me, thanks!
One tweak request about a code comment, and one about the commit message/PR description:
This patch fixes the MinGW cross-compilation break reported after https://github.com/llvm/llvm-project/pull/229456 landed:
This makes it sound like MinGW cross compilation in general is broken - it isn't, it isn't related to whether cross compiling or compiling natively at all. It's about building with older versions of mingw-w64.
Specifically, the polyfill is available in mingw-w64 v11 and newer, so it is only broken if building with v10 or older.
Tested this against Android's toolchain setup (cross-compiling libc++ for x86_64-w64-windows-gnu with our prebuilt MinGW sysroot). The patch cleanly compiles and resolves the build breakage. The float range check logic and use of compiler builtins look very solid. LGTM from our side, thanks!
Approved these changes.
LGTM, thanks!
What is the expected support window for MinGW? We can't keep accumulating workarounds forever.
I think Android's mingw is ancient and needs to be upgraded. We'll take a look to see how feasible that is.
cc @llvm/android-maintainers
@philnik777 Can I go ahead and merge this?
I'd really like an answer here before doing anything. I don't think there is precedent for adding workarounds for older mingw installations. Given that the Android people are looking into just updating I'm also not sure this is too urgent.
I'd really like an answer here before doing anything.
Who do you want to answer?
For my take on your question,
What is the expected support window for MinGW?
I don't exactly know; we do have CI testing which usually uses a very recent git version - so we don't catch issues occurring on releases that may be slightly older. Usually there aren't many issues like this, but apparently it does happen some times. Fixing issues with reasonably recent releases of mingw should definitely be allowed IMO.
This case is only an issue with really old versions of mingw, so it's arguably less important. That said, I don't mind this workaround either.
Given that the Android people are looking into just updating I'm also not sure this is too urgent.
Yes - if they're able to update to something more recent (and set things up so they can update at a somewhat regular basis, i.e. at least every other year or so), that's certainly better. So I agree we can probably wait and hear back from them if they manage to sort out upgrading it.
IIUC, Android is using a build of mingw based on v7.0.0 which I suspect is from around ~2018. We're looking to upgrade to v14.0.0 (and improve our processes so that we can update more frequently). @Sharjeel-Khan is driving that effort. I met with @ldionne and @compnerd at the dev meeting to discuss, and @compnerd is stopping by today for lunch. Sorry we missed you @mstorsjo and @philnik777 at the dev meeting. Looks like we missed the libc++ monthly meeting (conflicted with the dev meeting), but @Sharjeel-Khan and I can stop by the next libc++ monthly meeting on 11/24 to share any updates.
I was able to upgrade to the latest version of MinGW in Android so we can close this PR. I also set up a method to regularly update MinGW (every new release) so we do not need to worry about these workarounds anymore for us in the future.
I was able to upgrade to the latest version of MinGW in Android so we can close this PR. I also set up a method to regularly update MinGW (every new release) so we do not need to worry about these workarounds anymore for us in the future.
Closing the PR.
venk-ks
Himadhith
This patch fixes the build break reported after #229456 landed:
This only happens when building with mingw-w64 v10 or older. v11 and newer
polyfill
_strtof_l(mingw-w64/mingw-w64@03ad04e), so they are not affected.ERANGEis set on overflow to infinity and when the result is nonzero and belowFLT_MINin magnitude (subnormals included), same as glibc's strtof and mingw-w64/mingw-w64@03ad04e.The only difference is that we compare the narrowed float and not the double, so a double just below
FLT_MINthat rounds up toFLT_MINgets noERANGE.