[llvm-dev] Compiling libc++ using GNU Arm Embedded Toolchain for arm-cortex-m4

Kacper Kowalski via llvm-dev llvm-dev at lists.llvm.org
Wed Nov 13 11:33:49 PST 2019


Hello,

lately, I have been thinking about how to resolve the problem with that the
program size increases enormously when including <iostream> when compiling
with libstdc++. In this library, in <iostream> there is a static object
__ioinit initialized like so:

  ...
  // For construction of filebuffers for cout, cin, cerr, clog et. al.
  static ios_base::Init __ioinit;
  ...

This makes the program size increase enormously. The thing is that I often
compile programs for small platforms, like arm-cortex-m3 or arm-cortex-m4
(stm32, nrf52, ...). Including this header leads to the program size exceed
the FLASH memory size.

This is not a problem when you have full control over what you include. But
if you use external libraries and you don't want to mess with their
sources, or you are not able to, then it gets complicated.

To resolve this issue I found out that libc++ can be compiled with the
flags:

    -D_LIBCPP_HAS_NO_STDOUT:BOOL=ON
    -D_LIBCPP_HAS_NO_STDIN:BOOL=ON

so I gave it a try: I started a struggle to compile libc++ and libc++ ABI
with ARMToolchain-9-2019-q4-major. Unfortunately, I failed.

This is how I build libc++:

TOOLCHAIN_ROOT_DIR="${HOME}/ARMToolchain-9-2019-q4-major"

export CC="${TOOLCHAIN_ROOT_DIR}/bin/arm-none-eabi-gcc"
export CXX="${TOOLCHAIN_ROOT_DIR}/bin/arm-none-eabi-g++"

C_AND_CXX_COMMON_FLAGS_BASE="-mcpu=cortex-m4 -mfpu=fpv4-sp-d16
-mfloat-abi=hard --specs=nosys.specs\
    --sysroot=${TOOLCHAIN_ROOT_DIR}
-I${TOOLCHAIN_ROOT_DIR}/arm-none-eabi/include\
    -L${TOOLCHAIN_ROOT_DIR}/lib
-L${TOOLCHAIN_ROOT_DIR}/lib/gcc/arm-none-eabi/9.2.1"
C_AND_CXX_COMMON_FLAGS_MORE="-mthumb -mabi=aapcs"
C_AND_CXX_COMMON_FLAGS="$C_AND_CXX_COMMON_FLAGS_BASE
$C_AND_CXX_COMMON_FLAGS_MORE"

cmake \
    -DCMAKE_CROSS_COMPILING:BOOL=ON \
    -DLLVM_ENABLE_PROJECTS='libcxxabi;libcxx' \
    -DCMAKE_INSTALL_PREFIX='${HOME}/ClangLibcxx' \
    -DCMAKE_BUILD_TYPE='Release' \
    -DLLVM_ENABLE_ASSERTIONS:BOOL=ON \
    -DLLVM_TARGET_ARCH='ARM' \
    -DLLVM_TARGETS_TO_BUILD='ARM' \
    -DLIBCXX_BUILD_32_BITS:BOOL=ON \
    -DLLVM_BUILD_32_BITS:BOOL=ON \
    -DLLVM_ENABLE_STATIC:BOOL=ON \
    -DLLVM_ENABLE_SHARED:BOOL=OFF \
    -DLLVM_DISABLE_PIC:BOOL=OFF \
    -D_LIBCPP_HAS_NO_STDOUT:BOOL=ON \
    -D_LIBCPP_HAS_NO_STDIN:BOOL=ON \
    -DCMAKE_C_FLAGS="$C_AND_CXX_COMMON_FLAGS" \
    -DCMAKE_CXX_FLAGS="$C_AND_CXX_COMMON_FLAGS" \
    ../llvm

Firstly, I encounter the known problem with __atomic_load_8.
arm-cortex-m{3,4} most probably doesn't support 64bit atomic read/write,
but as long as I don't use it, changing the line in CheckAtomic.cmake:

      message(FATAL_ERROR "Host compiler appears to require libatomic, but
cannot find it.")

to:

      message(WARNING "Host compiler appears to require libatomic, but
cannot find it.")

could push the things further.
But then I encounter that error:

    -- Targeting ARM
    CMake Error at
/home/kacper/Workspace/LLVMProject/libcxxabi/CMakeLists.txt:216 (message):
        LIBCXXABI_BUILD_32_BITS=ON is not supported on this platform.

which is confusing.

Can't the libc++abi be compiled for 32B ARM targets?

Best regards,
Kacper Kowalski
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20191113/50e98d61/attachment.html>


More information about the llvm-dev mailing list