[libcxx] Fix gcc build.

Attempt to fix a horrible gcc include order problem.

llvm-svn: 283762
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 0c21b40d37d559800490560ad4037f792cf69fab
diff --git a/include/limits.h b/include/limits.h
index b69459a..1867b10 100644
--- a/include/limits.h
+++ b/include/limits.h
@@ -44,6 +44,22 @@
 #pragma GCC system_header
 #endif
 
+#ifndef __GNUC__
 #include_next <limits.h>
+#else
+// GCC header limits.h recursively includes itself through another header called
+// syslimits.h for some reason. This setup breaks down if we directly
+// #include_next GCC's limits.h (reasons not entirely clear to me). Therefore,
+// we manually re-create the necessary include sequence below:
+
+// Get the system limits.h defines (force recurse into the next level)
+#define _GCC_LIMITS_H_
+#define _GCC_NEXT_LIMITS_H
+#include_next <limits.h>
+
+// Get the ISO C defines
+#undef _GCC_LIMITS_H_
+#include_next <limits.h>
+#endif // __GNUC__
 
 #endif  // _LIBCPP_LIMITS_H