[PATCH] Added support to libc++ for GCC 4.3, GCC 4.5, and most recent head of Clang.

Hey folks, here's a patch I made that gets libc++ compiling with GCC
4.3, GCC 4.5 and the latest version of Clang.

Altogether, I was able to compile it with GCC 4.3.4, GCC 4.4.3, GCC
4.5.1 and latest Clang/LLVM 2.8 head on Ubuntu, with or without
-std=c++0x. It also compiles with GCC 4.1, but due to some ABI issues
with libstdc++, the shared library won't fully link.

I haven't run the test suite yet due to the missing locales
implementation on Linux, although most of the test cases appear to
compile properly. There's still the occasional warning message here
and there, and there's naturally a lot with -Wall -Wextra.

Change list:

- Better C++0x feature detection for both GCC and Clang (using Clang's
__has__feature builtin preprocessor macro). Added explicit detection
for defaulted and deleted functions.
- Fix for Clang compilation error: Changed __config header to
recognize Clang's support for builtin unicode character types char16_t
and char32_t.
- Fixes for GCC 4.3: Moved a lot of the GCC 4.3 C++0x feature support
up to 4.4 for now in __config header, as although they exist in 4.3,
various parts of libc++ assumed that they were fully implemented or
implied the existence of other C++0x features. Also fine-tuned some
preprocessor conditional checks in iterator, memory, thread,
type_traits and utility headers to account for this. It'd probably be
possible to rewrite a bunch of the offending areas (things that use
variadic templates and auto/decltype) so that they could work in 4.3.
- Fix for GCC 4.5 compilation error: Added implicit cast to void* in
nullptr_t implementation to handle stricter template argument type
matching in this compiler.
- Fix for GCC 4.5 compilation error: Changed both std::move and
std::forward C++0x-specific implementations to explicitly cast the
object to an r-value reference type as GCC 4.5 implements the new
wording for move semantics and rvalue references
(Defining Move Special Member Functions).
This didn't introduce warning messages in any of the other compilers I
tested with, so not sure what compiler was originally causing the
issues here.
- Preliminary fix for GCC 4.6: Changed libc++ to use GCC 4.6's builtin
nullptr keyword.

Cheers,

Jesse Towner

libcxx_compiler_support.patch (7.39 KB)

Thank you so much for this. Using clang with gcc 4.5.0 headers has been a real pain point for me.

No problem. I also just compiled it using the latest head of GCC 4.6 on Ubuntu as well, so that fix to make it work with GCC’s nullptr turned out to work fine. Finally figured out how to get libc++ all compiled and linked together on OS X too (I was missing the libc++abi package that was pushed out the list a while back), so now I’m running the test suite, going to diff the test output after applying the patch to see how it works with GCC 4.2.1, then going to try the latest Clang version on OS X too, not sure how that’s going to go.

  • Jesse Towner

Okay, please disregard the original patch, there were some bugs that
came up when I ran the test suite on OS X with GCC 4.2.1. This patch
should take care of them.

Just to recap, I've tested it now on GCC 4.3.4, GCC 4.4.3, GCC 4.5.1,
GCC 4.6 (head), and Clang/LLVM 2.8 (head) with and without-std=c++0x on
Ubuntu 10.04 x86-64; GCC 4.2.1 and Clang/LLVM 2.8 (head) on OSX 10.6.4
(both i386/x86-64); and GCC 4.2.1 on iOS 4.0.1 (both ARMv6/ARMv7).

Remarkably, many of the tests built and passed with Clang/LLVM 2.8 on
OSX, although not quite as many as GCC was able to handle.

I had to add an additional tweak to the cmath header for OSX when
building with Clang with -std=c++0x, as that flag disables the GNU
extensions mode and defines __STRICT_ANSI__, and the current math.h
headers with OSX/iOS (Xcode 3.2.3) don't bring in a number of the C99
math functions when __STRICT_ANSI__ is defined.

Here's the revised change list:

- Better C++0x feature detection for both GCC and Clang (using Clang's
__has_feature builtin preprocessor macro). Added explicit detection for
defaulted and deleted functions.
- Fix for Clang compilation error: Changed __config header to recognize
Clang's support for builtin unicode character types char16_t and
char32_t.
- Fix for Clang compilation error: #undef'd __STRICT_ANSI__ in cmath on
Apple platforms to get all C99 function symbols pulled in. Used clang's
new push_macro/pop_macro pragmas to restore __STRICT_ANSI__ afterward.
- Fixes for GCC 4.3: Moved a lot of the GCC 4.3 C++0x feature support up
to 4.4 for now in __config header, as although they exist in 4.3,
various parts of libc++ assumed that they were fully implemented or
implied the existence of other C++0x features. Also fine-tuned some
preprocessor conditional checks in iterator, memory, thread, type_traits
and utility headers to account for this. It'd probably be possible to
rewrite a bunch of the offending areas (things that use variadic
templates and auto/decltype) so that they could work in 4.3.
- Fix for GCC 4.5 compilation error: Added conditional operator
overloads for comparing generic pointer types against nullptr_t to
handle stricter/more fickle template argument type matching in this
compiler.
- Fix for GCC 4.5 compilation error: Changed both std::move and
std::forward C++0x-specific implementations to explicitly cast the
object to an r-value reference type as GCC 4.5 implements the new
wording for move semantics and rvalue references
(Defining Move Special Member Functions).
This didn't introduce warning messages in any of the other compilers I
tested with, so not sure what compiler was originally causing the issues
here.
- Fix for GCC 4.6: Changed libc++ to use GCC 4.6's builtin nullptr
keyword.

Cheers,

Jesse

libcxx_compiler_support_v2.patch (11.1 KB)

Thanks much for this work Jesse. Please know that I won't be able to review this patch until next week as I am out of the office (but still within email range).

-Howard

Okay, please disregard the original patch, there were some bugs that
came up when I ran the test suite on OS X with GCC 4.2.1. This patch
should take care of them.

Just to recap, I've tested it now on GCC 4.3.4, GCC 4.4.3, GCC 4.5.1,
GCC 4.6 (head), and Clang/LLVM 2.8 (head) with and without-std=c++0x on
Ubuntu 10.04 x86-64; GCC 4.2.1 and Clang/LLVM 2.8 (head) on OSX 10.6.4
(both i386/x86-64); and GCC 4.2.1 on iOS 4.0.1 (both ARMv6/ARMv7).

Very cool. Thanks for working on this!

I had to add an additional tweak to the cmath header for OSX when
building with Clang with -std=c++0x, as that flag disables the GNU
extensions mode and defines __STRICT_ANSI__, and the current math.h
headers with OSX/iOS (Xcode 3.2.3) don't bring in a number of the C99
math functions when __STRICT_ANSI__ is defined.

Use -std=gnu++0x to get C++0x with GNU extensions. Of course, C++0x mode doesn't do much in Clang... yet.

  - Doug