Current GNU libstdc++ supports --disable-hosted-libstdcxx to support freestanding and allow hosted toolchain to use -ffreestanding flag to use freestanding C++. However LLVM libc++ does not provide these features. Leaving C++ extremely difficult for writing operating systems or using libcxx to bootstrap libc (MLIBC for example)
This patch adds a new toggle -DLIBCXX_FREESTANDING to cmake that would allow using libcxx with freestanding. Users can also use native toolchain with libcxx to deploy freestanding one.
Besides what N4917, we also provide extra headers like what libstdc++ does.
//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // Test that _LIBCPP_FREESTANDING is not defined when -ffreestanding is not passed // to the compiler but defined when -ffreestanding is passed to the compiler. // RUN: %{cxx} %{flags} %{compile_flags} -fsyntax-only %s // RUN: %{cxx} %{flags} %{compile_flags} -fsyntax-only -ffreestanding %s #include <__config> #if defined(__has_feature) # if __has_feature(modules) # define _LIBCPP_FREESTANDING_NO_TEST_MODULES # endif #elif defined(__cpp_modules) # define _LIBCPP_FREESTANDING_NO_TEST_MODULES #endif #if defined(_LIBCPP_FREESTANDING) && !defined(_LIBCPP_FREESTANDING_NO_TEST_MODULES) # include <cstddef> # include <limits> # include <cfloat> # include <version> # include <cstdint> # include <cstdlib> # include <new> # include <typeinfo> # if __has_include(<source_location>) # include <source_location> # endif # include <exception> # include <initializer_list> # include <compare> # include <coroutine> # include <cstdarg> # include <concepts> # include <type_traits> # include <bit> # include <atomic> # include <utility> # include <tuple> # include <memory> # include <functional> # include <ratio> # include <iterator> # include <ranges> # include <typeinfo> /* We tested these headers are for preventing build issues */ # include <iosfwd> # include <cstdio> # include <cerrno> # include <cstring> # include <cmath> # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS # include <cwchar> # endif /* * libc++ libstdc++ like extensions */ # include <array> # include <span> # include <algorithm> # include <bitset> # include <variant> # include <optional> # include <numbers> # include <scoped_allocator> # include <typeindex> # include <string_view> # include <numeric> # include <complex> # include <chrono> # include <charconv> # include <expected> # include <random> # include <any> /* * libc++ Containers Extensions */ # include <vector> # include <deque> # include <string> # include <stack> # include <queue> # include <list> # include <forward_list> # include <map> # include <set> # include <unordered_set> # include <unordered_map> # if __has_include(<mdspan>) # include <mdspan> # endif # include <valarray> # if _LIBCPP_STD_VER < 20 # include <ciso646> # if __has_include(<cstdalign>) # include <cstdalign> # endif # include <cstdbool> # endif #endif
source_location header is missing in libcxx currently and I know it would add in the future by other commits, I won't put them here. We provide all freestanding headers libstdc++ provides for freestanding. We provide one extra header which is string_view. However their functionalities may work very differently in the hosted one.