llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[libc] build clang

Open izaakschroeder opened this issue 2 years ago • 12 comments

Overview

Tracking issue from discussions with @jhuber6 on the LLVM discord in #libc. Everything that is within my meagre capabilities to be fixed has been done so here: https://github.com/llvm/llvm-project/pull/97231 Feel free to cherry pick commits out and make PRs as you see fit.

Known issues:

libc

  • [ ] libc + aarch64 missing at least:
    • [x] libc.include.sys_auxv
    • [x] libc.include.fcntl
    • [x] libc.include.sched
    • [x] libc.include.sys_stat
    • [x] libc.src.assert.__assert_fail
  • [x] linux/error-number-macros.h is not installed https://github.com/llvm/llvm-project/pull/97101
  • [ ] libunwind + libc:
    • [ ] LIBUNWIND_IS_BAREMETAL=OFF
      • [x] https://github.com/llvm/llvm-project/pull/97495
      • [x] https://github.com/llvm/llvm-project/pull/97772
      • [x] https://github.com/llvm/llvm-project/pull/97501
      • [x] https://github.com/llvm/llvm-project/pull/97924
      • [x] https://github.com/llvm/llvm-project/pull/97504
  • [ ] libcxx + libc
    • [x] https://github.com/llvm/llvm-project/issues/191075
  • [x] Add support for non-integer pthread_t to llvm::thread
  • [x] Add struct winsize and TIOCGWINSZ to <sys/ioctl.h>
  • [x] Add pthread_setschedparam
  • [x] Add getrusage and RUSAGE_SELF
  • [ ] clang -lc ./some/file + libc
    • [x] https://github.com/llvm/llvm-project/pull/97642
    • [ ] https://github.com/llvm/llvm-project/pull/97497
    • [ ] Missing stddef.h from clang

Reproduction

Dockerfile

# =====================================================================
# bootstrap
# =====================================================================
# Bootstrap image from which the new builder image will be produced
# from which the final image will be generated. This image could be any
# Linux distribution, but Alpine is small and straightforward.
FROM alpine:3.20.1 AS bootstrap

RUN apk add tar xz make python3 cmake gcc g++ git linux-headers \
  flex bison bash perl findutils elfutils openssl openssl-dev \
  diffutils elfutils-dev zstd zstd-dev rsync ninja unzip

WORKDIR /home

# =====================================================================
# kernel-sources
# =====================================================================
# This image contains only the kernel sources downloaded and extracted.
# It exists here as a way to cache that process instead of having to 
# re-download and repeat it for each step that needs these sources.

FROM bootstrap AS kernel-sources-build

ARG KERNEL_VERSION=6.9.6

RUN echo "${KERNEL_VERSION}" | sed -n 's/^\([0-9]*\).*$/\1/p'
RUN mkdir /dist
RUN wget -O- "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${KERNEL_VERSION}.tar.xz" \
      | tar --strip-components=1 -xJf- -C /dist

# TODO: If any patches need to happen, here is the spot to do it.

FROM scratch AS kernel-sources

COPY --from=kernel-sources-build /dist /

# =====================================================================
# llvm-sources
# =====================================================================
# This image contains only the llvm sources downloaded and extracted.
# It exists here as a way to cache that process instead of having to 
# re-download and repeat it for each step that needs these sources. 

FROM bootstrap AS llvm-sources-build

ARG LLVM_VERSION=18.1.8
RUN mkdir /dist
RUN wget -O- "https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-${LLVM_VERSION}.tar.gz" \
  | tar --strip-components=1 -xzf- -C /dist

FROM scratch AS llvm-sources

COPY --from=llvm-sources-build /dist /

# =====================================================================
# llvm-sources-stage2
# =====================================================================
# This is for testing right now and exists to avoid having to rebuild
# stage1 as patches are made. The list of changes is available here:
# https://github.com/llvm/llvm-project/pull/97231

FROM bootstrap AS llvm-sources-stage2-build

RUN mkdir /dist

RUN git clone --depth 1 --branch libc-bootstrap --single-branch https://github.com/izaakschroeder/llvm-project.git /dist
RUN cd /dist && git pull && git checkout 3f7f620af2fc0cc3e670975aeb17423e26062a2b && rm -rf .git

FROM scratch AS llvm-sources-stage2

COPY --from=llvm-sources-stage2-build /dist /


# =====================================================================
# clang-stage1
# =====================================================================
# Part of the bootstrapping process of allowing `clang` to compile 
# itself. The stage1 build is built using the system compiler and 
# installed outside of the staging sysroot. The final generated image
# is just the stage1 clang distribution that can be overlayed onto
# the existing base image.

FROM bootstrap AS clang-stage1-build

COPY --from=llvm-sources / ./
COPY ./clang/clang.cmake ./clang.cmake

RUN mkdir build && cd build && cmake \
  -G 'Ninja' \
  -C ../clang.cmake \
  -DCMAKE_INSTALL_PREFIX=/dist \
  ../llvm && \
  ninja distribution && \
  ninja install-distribution-stripped

RUN cp build/bin/libc-hdrgen /dist/bin/

FROM scratch AS clang-stage1

COPY --from=clang-stage1-build /dist /

# =====================================================================
# kernel-headers
# =====================================================================
# This image contains only the installed headers for the kernel. The
# kernel configuration is the default one. The reason this one is apart
# from the others is that it uses the stage1 compiler to compile the
# configuration which isn't ideal but is needed for the stage2 compiler
# to be built correctly.

FROM bootstrap AS kernel-headers-build

COPY --from=kernel-sources / ./

COPY ./kernel/kernel.config.conf ./kernel.config

RUN mkdir -p /dist/usr
RUN make allnoconfig KCONFIG_ALLCONFIG="./kernel.config"
RUN make headers_install INSTALL_HDR_PATH=/dist

FROM scratch AS kernel-headers

COPY --from=kernel-headers-build ./dist /

# =====================================================================
# clang-stage2
# =====================================================================
# Fully bootstrapped compiler built from stage1. This compiler uses the
# new sysroot and is not suitable for compiling binaries that target 
# the host system.
#AS clang-stage2-build

FROM bootstrap AS clang-build-base

COPY --from=llvm-sources-stage2 / ./
COPY --from=kernel-headers / /sysroot
COPY --from=clang-stage1 / /opt/clang
COPY ./clang/clang-stage2.cmake ./clang.cmake

FROM clang-build-base AS cxu-libs-build

RUN mkdir build-runtimes && cd build-runtimes && cmake \
  -G 'Ninja' \
  -C ../clang.cmake \
  -DCMAKE_PREFIX_PATH="/sysroot" \
  -DLLVM_NATIVE_TOOL_DIR="/opt/clang/bin" \
  -DCMAKE_SYSROOT="/sysroot" \
  -DCMAKE_INSTALL_PREFIX="/dist" \
  -DLLVM_TABLEGEN="/opt/clang/bin/llvm-tblgen" \
  -DCLANG_TABLEGEN="/opt/clang/bin/clang-tblgen" \
  -DLIBC_HDRGEN_EXE="/opt/clang/bin/libc-hdrgen" \
  -DCMAKE_AR="/opt/clang/bin/llvm-ar" \
  -DCMAKE_RANLIB="/opt/clang/bin/llvm-ranlib" \
  -DCMAKE_C_COMPILER="/opt/clang/bin/clang" \
  -DCMAKE_CXX_COMPILER="/opt/clang/bin/clang++" \
  -DCMAKE_ASM_COMPILER="/opt/clang/bin/clang" \
  -DCMAKE_LINKER="/opt/clang/bin/lld" \
  -DLLVM_ENABLE_RUNTIMES="libcxx;libc;compiler-rt;libunwind" \
  -DCMAKE_CXX_FLAGS="-nostdinc++ -static" \
  ../runtimes

RUN cd build-runtimes && ninja install-libc-headers
RUN cp -R /dist/* /sysroot/

RUN cd build-runtimes && ninja install-libc-stripped
RUN cd build-runtimes && ninja install-cxx-stripped
RUN cp -R /dist/* /sysroot/

RUN cd build-runtimes && ninja install-unwind-stripped
RUN cp -R /dist/* /sysroot/

RUN cd compiler-rt/lib && /opt/clang/bin/clang++ \
  --sysroot=/sysroot -fno-exceptions -fno-rtti \
  -std=c++17 -nostdinc++ -static -Os -c \
  -I scudo/standalone/include \
  scudo/standalone/*.cpp

RUN /opt/clang/bin/llvm-ar rs /dist/lib/libc.a compiler-rt/lib/*.o

FROM scratch AS cxu-libs

COPY --from=cxu-libs-build /dist /

FROM clang-build-base AS compiler-rt-build

COPY --from=cxu-libs / /sysroot

RUN mkdir build && cd build && cmake \
  -G 'Ninja' \
  -C ../clang.cmake \
  -DCMAKE_PREFIX_PATH="/sysroot" \
  -DCMAKE_SYSROOT="/sysroot" \
  -DCMAKE_INSTALL_PREFIX="/dist" \
  -DCMAKE_AR="/opt/clang/bin/llvm-ar" \
  -DCMAKE_RANLIB="/opt/clang/bin/llvm-ranlib" \
  -DCMAKE_C_COMPILER="/opt/clang/bin/clang" \
  -DCMAKE_CXX_COMPILER="/opt/clang/bin/clang++" \
  -DCMAKE_ASM_COMPILER="/opt/clang/bin/clang" \
  -DCMAKE_LINKER="/opt/clang/bin/lld" \
  -DCOMPILER_RT_STANDALONE_BUILD=ON \
  -DCOMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH="/sysroot" \
  -DCMAKE_C_COMPILER_WORKS=1 \
  -DCMAKE_CXX_COMPILER_WORKS=1 \
  -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
  -DCMAKE_C_COMPILER_TARGET="aarch64-unknown-linux-gnu" \
  -DCMAKE_CXX_COMPILER_TARGET="aarch64-unknown-linux-gnu" \
  -DLLVM_CMAKE_DIR=/home \
  ../compiler-rt

RUN cd build && ninja install-compiler-rt-stripped

FROM scratch AS compiler-rt

COPY --from=compiler-rt-build /dist /

FROM clang-build-base

COPY --from=cxu-libs / /sysroot
COPY --from=compiler-rt / /sysroot

RUN cp -R /usr/include/stddef.h /sysroot/include/
RUN cp -R /usr/include/bits /sysroot/include/

RUN mkdir build-runtimes && cd build-runtimes && cmake \
  -G 'Ninja' \
  -C ../clang.cmake \
  -DCMAKE_PREFIX_PATH="/sysroot" \
  -DLLVM_NATIVE_TOOL_DIR="/opt/clang/bin" \
  -DCMAKE_SYSROOT="/sysroot" \
  -DCMAKE_INSTALL_PREFIX="/dist" \
  -DLLVM_TABLEGEN="/opt/clang/bin/llvm-tblgen" \
  -DCLANG_TABLEGEN="/opt/clang/bin/clang-tblgen" \
  -DLIBC_HDRGEN_EXE="/opt/clang/bin/libc-hdrgen" \
  -DCMAKE_AR="/opt/clang/bin/llvm-ar" \
  -DCMAKE_RANLIB="/opt/clang/bin/llvm-ranlib" \
  -DCMAKE_C_COMPILER="/opt/clang/bin/clang" \
  -DCMAKE_CXX_COMPILER="/opt/clang/bin/clang++" \
  -DCMAKE_ASM_COMPILER="/opt/clang/bin/clang" \
  -DCMAKE_LINKER="/opt/clang/bin/lld" \
  -DLLVM_ENABLE_RUNTIMES="libcxx;libc;compiler-rt;libunwind" \
  -DCMAKE_CXX_FLAGS="-nostdinc++ -static -I/sysroot/include/c++/v1 -resource-dir=/sysroot" \
  ../llvm

clang.cmake


set(LLVM_ENABLE_PROJECTS "clang;clang-tools-extra;lld" CACHE STRING "")
set(LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind;libc" CACHE STRING "")

# Only build the native target in stage1 since it is a throwaway build.
set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "")

set(CMAKE_BUILD_TYPE Release CACHE STRING "")
set(CMAKE_PLATFORM_NO_VERSIONED_SONAME ON CACHE BOOL "")

set(CMAKE_VISIBILITY_INLINES_HIDDEN ON CACHE BOOL "")
set(CMAKE_C_VISIBILITY_PRESET hidden CACHE STRING "")
set(CMAKE_CXX_VISIBILITY_PRESET hidden CACHE STRING "")

set(PACKAGE_VENDOR xxx CACHE STRING "")
set(BUILD_SHARED_LIBS OFF CACHE BOOL "")

set(LLVM_ENABLE_UNWIND_TABLES OFF CACHE BOOL "")
set(LLVM_STATIC_LINK_CXX_STDLIB ON CACHE BOOL "")
set(LLVM_INCLUDE_TESTS OFF CACHE BOOL "")
set(LLVM_INCLUDE_EXAMPLES OFF CACHE BOOL "")
set(LLVM_INCLUDE_BENCHMARKS OFF CACHE BOOL "")
set(LLVM_LIBC_FULL_BUILD ON CACHE BOOL "")
set(LIBC_HDRGEN_ONLY ON CACHE BOOL "")

set(CLANG_DEFAULT_CXX_STDLIB libc++ CACHE STRING "")
set(CLANG_DEFAULT_LINKER lld CACHE STRING "")
set(CLANG_DEFAULT_OBJCOPY llvm-objcopy CACHE STRING "")
set(CLANG_DEFAULT_RTLIB compiler-rt CACHE STRING "")
set(CLANG_DEFAULT_UNWINDLIB libunwind CACHE STRING "")
set(CLANG_PLUGIN_SUPPORT OFF CACHE BOOL "")

set(LIBUNWIND_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBUNWIND_INSTALL_LIBRARY ON CACHE BOOL "")
set(LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "")
set(LIBCXXABI_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXXABI_ENABLE_STATIC_UNWINDER ON CACHE BOOL "")
set(LIBCXXABI_INSTALL_LIBRARY ON CACHE BOOL "")
set(LIBCXXABI_USE_COMPILER_RT ON CACHE BOOL "")
set(LIBCXXABI_USE_LLVM_UNWINDER ON CACHE BOOL "")
set(LIBCXX_ABI_VERSION 2 CACHE STRING "")
set(LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXX_STATIC_SHARED ON CACHE BOOL "")
set(LIBCXX_ENABLE_STATIC_ABI_LIBRARY ON CACHE BOOL "")
set(LIBCXX_HARDENING_MODE "none" CACHE STRING "")
set(LIBCXX_USE_COMPILER_RT ON CACHE BOOL "")
set(LIBCXX_HAS_MUSL_LIBC ON CACHE BOOL "")
set(LIBCXX_INSTALL_LIBRARY ON CACHE BOOL "")

set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR OFF CACHE BOOL "")
set(COMPILER_RT_USE_BUILTINS_LIBRARY OFF CACHE BOOL "")
set(COMPILER_RT_BUILD_CRT ON CACHE BOOL "")

# Setting up the stage2 LTO option needs to be done on the stage1 build so that
# the proper LTO library dependencies can be connected.
set(LLVM_TOOLCHAIN_TOOLS 
  llvm-ar
  llvm-config
  llvm-cov
  llvm-dwarfdump
  llvm-link
  llvm-nm
  llvm-objcopy
  llvm-objdump
  llvm-profdata
  llvm-ranlib
  llvm-rc
  llvm-readelf
  llvm-readobj
  llvm-size
  llvm-strings
  llvm-strip 
  llvm-tblgen
  CACHE STRING "")

set(LLVM_DISTRIBUTION_COMPONENTS
  clang
  clang-resource-headers
  clang-tblgen
  lld
  runtimes
  builtins
  ${LLVM_TOOLCHAIN_TOOLS}
  CACHE STRING "")

clang-stake2.cmake

# This file sets up a CMakeCache for the second stage of a simple distribution
# bootstrap build.

set(LLVM_ENABLE_PROJECTS "clang;clang-tools-extra;lld" CACHE STRING "")
set(LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind;scudo_standalone" CACHE STRING "")

set(CMAKE_C_COMPILER_WORKS ON CACHE BOOL "")
set(CMAKE_CXX_COMPILER_WORKS ON CACHE BOOL "")

# Setup vendor-specific settings.
set(PACKAGE_VENDOR xxx CACHE STRING "")

set(CMAKE_C_COMPILER clang CACHE STRING "")
set(CMAKE_CXX_COMPILER clang++ CACHE STRING "")
set(CMAKE_ASM_COMPILER clang CACHE STRING "")
set(CMAKE_AR llvm-ar CACHE STRING "")
set(CMAKE_RANLIB llvm-ranlib CACHE STRING "")
set(CMAKE_LINKER lld CACHE STRING "")

set(CMAKE_VISIBILITY_INLINES_HIDDEN ON CACHE BOOL "")
set(CMAKE_C_VISIBILITY_PRESET hidden CACHE STRING "")
set(CMAKE_CXX_VISIBILITY_PRESET hidden CACHE STRING "")
set(CMAKE_PLATFORM_NO_VERSIONED_SONAME ON CACHE BOOL "")

set(BUILD_SHARED_LIBS OFF CACHE BOOL "")
set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "")
set(LLVM_ENABLE_LTO full CACHE STRING "")
set(LLVM_FORCE_BUILD_RUNTIME ON CACHE BOOL "")
set(LLVM_STATIC_LINK_CXX_STDLIB ON CACHE BOOL "")
# set(LLVM_USE_RELATIVE_PATHS_IN_FILES ON CACHE BOOL "")

set(LLVM_INCLUDE_DOCS OFF CACHE BOOL "")
set(LLVM_INCLUDE_TESTS OFF CACHE BOOL "")
set(LLVM_INCLUDE_EXAMPLES OFF CACHE BOOL "")
set(LLVM_INCLUDE_BENCHMARKS OFF CACHE BOOL "")

set(LLVM_LIBC_INCLUDE_SCUDO ON CACHE BOOL "")
set(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC ON CACHE BOOL "")
set(LLVM_LIBC_FULL_BUILD ON CACHE BOOL "")
set(LIBC_ENABLE_USE_BY_CLANG ON CACHE BOOL "")
set(COMPILER_RT_BUILD_GWP_ASAN OFF CACHE BOOL "")
set(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED OFF CACHE BOOL "")


set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O3 -gline-tables-only -DNDEBUG" CACHE STRING "")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -gline-tables-only -DNDEBUG" CACHE STRING "")
set(CMAKE_EXE_LINKER_FLAGS "-static" CACHE STRING "")

set(CLANG_DEFAULT_CXX_STDLIB libc++ CACHE STRING "")
set(CLANG_DEFAULT_LINKER lld CACHE STRING "")
set(CLANG_DEFAULT_OBJCOPY llvm-objcopy CACHE STRING "")
set(CLANG_DEFAULT_RTLIB compiler-rt CACHE STRING "")
set(CLANG_DEFAULT_UNWINDLIB libunwind CACHE STRING "")
set(CLANG_ENABLE_STATIC_ANALYZER ON CACHE BOOL "")

set(LIBUNWIND_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBUNWIND_INSTALL_LIBRARY ON CACHE BOOL "")
set(LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "")

set(LIBCXXABI_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXXABI_ENABLE_STATIC_UNWINDER ON CACHE BOOL "")
set(LIBCXXABI_HERMETIC_STATIC_LIBRARY ON CACHE BOOL "")
set(COMPILER_RT_CXX_LIBRARY "libcxx" CACHE STRING "")

set(CMAKE_INSTALL_LIBDIR lib CACHE STRING "")

set(COMPILER_RT_USE_LLVM_UNWINDER ON CACHE BOOL "")
set(COMPILER_RT_BUILD_BUILTINS ON CACHE BOOL "")
set(COMPILER_RT_BUILD_CRT ON CACHE BOOL "")
set(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC OFF CACHE BOOL "")
set(COMPILER_RT_HAS_FNO_BUILTIN_FLAG ON CACHE BOOL "")
set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS ON CACHE BOOL "")
set(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG ON CACHE BOOL "")
set(COMPILER_RT_BUILD_MEMPROF OFF CACHE BOOL "")
set(COMPILER_RT_BUILD_PROFILE OFF CACHE BOOL "")
set(COMPILER_RT_BUILD_SANITIZERS OFF CACHE BOOL "")
set(COMPILER_RT_BUILD_XRAY OFF CACHE BOOL "")
set(COMPILER_RT_BUILD_ORC OFF CACHE BOOL "")
set(COMPILER_RT_BUILD_LIBFUZZER OFF CACHE BOOL "")
set(COMPILER_RT_DEBUG OFF CACHE BOOL "")
set(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY ON CACHE BOOL "")

set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")

set(LIBCXXABI_USE_COMPILER_RT ON CACHE BOOL "")
set(LIBCXXABI_USE_LLVM_UNWINDER ON CACHE BOOL "")
set(LIBCXX_ABI_VERSION 2 CACHE STRING "")
set(LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
set(LIBCXX_ENABLE_STATIC_ABI_LIBRARY ON CACHE BOOL "")
set(LIBCXX_HARDENING_MODE "fast" CACHE STRING "")
set(LIBCXX_USE_COMPILER_RT ON CACHE BOOL "")
set(LIBCXX_INCLUDE_BENCHMARKS OFF CACHE BOOL "")

set(LIBCXX_ENABLE_STATIC ON CACHE BOOL "")
set(LIBCXX_INSTALL_LIBRARY ON CACHE BOOL "")
set(LIBCXX_CXX_ABI none CACHE STRING "")

# confirmed not available in libc
set(LIBCXX_ENABLE_LOCALIZATION OFF CACHE BOOL "")
set(LIBCXX_ENABLE_WIDE_CHARACTERS OFF CACHE BOOL "")

# missing at least `<sys/statvfs.h>` and `utimes`
set(LIBCXX_ENABLE_FILESYSTEM OFF CACHE BOOL "")

# missing at least `ioctl`
set(LIBCXX_ENABLE_RANDOM_DEVICE OFF CACHE BOOL "")

# unclear what this is missing
set(LIBCXX_ENABLE_MONOTONIC_CLOCK OFF CACHE BOOL "")

# requires LIBCXX_ENABLE_MONOTONIC_CLOCK=ON
set(LIBCXX_ENABLE_THREADS OFF CACHE BOOL "")

# these are fine
set(LIBCXX_ENABLE_UNICODE ON CACHE BOOL "")
set(LIBCXX_ENABLE_EXCEPTIONS ON CACHE BOOL "")
set(LIBCXX_ENABLE_RTTI ON CACHE BOOL "")

# setup toolchain
set(LLVM_BUILD_TOOLS ON CACHE BOOL "")
set(LLVM_INSTALL_TOOLCHAIN_ONLY ON CACHE BOOL "")
set(LLVM_TOOLCHAIN_TOOLS
  dsymutil
  llvm-ar
  llvm-cov
  llvm-cxxfilt
  llvm-debuginfod
  llvm-debuginfod-find
  llvm-dlltool
  llvm-dwarfdump
  llvm-dwp
  llvm-ifs
  llvm-gsymutil
  llvm-lib
  llvm-libtool-darwin
  llvm-lipo
  llvm-ml
  llvm-mt
  llvm-nm
  llvm-objcopy
  llvm-objdump
  llvm-otool
  llvm-pdbutil
  llvm-profdata
  llvm-rc
  llvm-ranlib
  llvm-readelf
  llvm-readobj
  llvm-size
  llvm-strings
  llvm-strip
  llvm-symbolizer
  llvm-undname
  llvm-xray
  opt-viewer
  sancov
  scan-build-py
  CACHE STRING "")

#   clang-tidy

set(LLVM_DISTRIBUTION_COMPONENTS
  clang
  LTO
  lld
  libc
  clang-apply-replacements
  clang-format
  clang-resource-headers
  clang-include-fixer
  clang-refactor
  clang-scan-deps
  find-all-symbols
  builtins
  runtimes
  ${LLVM_TOOLCHAIN_TOOLS}
  CACHE STRING "")

izaakschroeder avatar Jun 30 '24 05:06 izaakschroeder

Depends on #124027 for libcxxabi.

nickdesaulniers avatar Jan 27 '25 21:01 nickdesaulniers

I was playing around with getting this setup with a current tip of tree checkout and came up with about the following (intended to be run in the CI container): https://gist.github.com/boomanaiden154/3750344f9660948648486be06f2ba5dd

There are a couple minor patches needed to get libunwind building (mostly because it expects dladdr) that we can probably hack away in the build with a preprocessor define. I also have to manually build scudo (like done here) since the CMake to pull it into libc.a seems to not be working. Other than that, everything works minus the actual clang build.

boomanaiden154 avatar Aug 16 '25 18:08 boomanaiden154

We really need to just provide an implementation of malloc that doesn't require special CMake flags. Either by pulling it from somewhere else or changing SCUDO to be a header.

I'm thinking the proper way to start handling this is to use something like this.

-DLLVM_RUNTIME_TARGETS=x86_64-unknown-linux-llvm
-DRUNTIMES_x86-unknown-linux-llvm_LLVM_ENABLE_RUNTIMES=compiler-rt;libc;libunwind;libcxxabi;libcxx

Doing clang --target=x86-unknown-linux-llvm should then ideally tell it to use the LLVM toolchain instead of the GNU one. That's much less destructive than shipping the crt1.o files so that they conflict with the system libc. I think we're only missing a few changes in the clang driver to make that work as expected, pretty much not linking the GCC startup libraries.

jhuber6 avatar Aug 16 '25 18:08 jhuber6

We really need to just provide an implementation of malloc that doesn't require special CMake flags. Either by pulling it from somewhere else or changing SCUDO to be a header.

It should be pretty easy to just pull the sources into the main LLVM libc build and do everything there by default. If

cd compiler-rt/lib && /home/gha/sysroot/bin/clang++ \
  --sysroot=/home/gha/sysroot -fno-exceptions -fno-rtti \
  -std=c++17 -nostdinc++ -static -O3 -c \
  -I scudo/standalone/include \
  scudo/standalone/*.cpp

works, that can't be very hard. Although we do need to make sure we're building against the LLVM libc headers rather than the system libc headers.

boomanaiden154 avatar Aug 16 '25 21:08 boomanaiden154

We really need to just provide an implementation of malloc that doesn't require special CMake flags. Either by pulling it from somewhere else or changing SCUDO to be a header.

It should be pretty easy to just pull the sources into the main LLVM libc build and do everything there by default. If

cd compiler-rt/lib && /home/gha/sysroot/bin/clang++ \
  --sysroot=/home/gha/sysroot -fno-exceptions -fno-rtti \
  -std=c++17 -nostdinc++ -static -O3 -c \
  -I scudo/standalone/include \
  scudo/standalone/*.cpp

works, that can't be very hard. Although we do need to make sure we're building against the LLVM libc headers rather than the system libc headers.

I'm working on making a separate cmake target in LLVM libc build that access / build the SCUDO directly from SCUDO sources in compiler-rt folder. This should break the cmake build dependency between compiler-rt and LLVM libc, similar to libc++ usage of LLVM libc sources.

lntue avatar Aug 18 '25 21:08 lntue

I've recently ran a test on Linux-x86-64 to see where we are right now. This was done as a part of downstream Bazel-based configuration, so pasting the logs would not be particularly helpful. However, the list of issues observed is relatively small. I'll copy them below, and then update the bullets in the top comment above to track them:

  • Full libc++ support is needed - I've ran the lbc++ build with wide-characters and threads already enabled, but missing filesystem support (see https://github.com/llvm/llvm-project/issues/191075#issuecomment-4392220136) means we can't construct std::basic_ifstream<char>
  • llvm::thread should support non-integral pthread_t variants. We should use the same API / approach as libc++ which uses pthread_id_np_t - see 493ebf711e3f6641338e153ecdd284f262b4fb65
  • Missing struct winsize and TIOCGWINSZ in <sys/ioctl.h> for https://github.com/llvm/llvm-project/blob/889496c13bd4d3114e375bd0aeb1f161eff67c33/llvm/lib/Support/Unix/Process.inc#L325
  • Missing pthread_setschedparam for https://github.com/llvm/llvm-project/blob/889496c13bd4d3114e375bd0aeb1f161eff67c33/llvm/lib/Support/Unix/Threading.inc#L287
  • Support for _SC_ARG_MAX in sysconf for https://github.com/llvm/llvm-project/blob/889496c13bd4d3114e375bd0aeb1f161eff67c33/llvm/lib/Support/Unix/Program.inc#L550
  • Missing <pwd.h> and getpwnam_r for https://github.com/llvm/llvm-project/blob/889496c13bd4d3114e375bd0aeb1f161eff67c33/llvm/lib/Support/Unix/Path.inc#L715

The other issues on my end can be worked around by the using of feature macro:

  • Missing getrusage and RUSAGE_SELF (gated by HAVE_GETRUSAGE) https://github.com/llvm/llvm-project/blob/889496c13bd4d3114e375bd0aeb1f161eff67c33/llvm/lib/Support/Unix/Process.inc#L53
  • Missing <execinfo.h> (gated by HAVE_BACKTRACE)
  • Missing struct mallinfo (gated by HAVE_MALLINFO)

vonosmas avatar Jun 04 '26 20:06 vonosmas

The most recent round of testing that picks up merged updates to llvm-libc and enables unfinished endpoints (swprintf for wchar support in llvm-libc and realpath for filesystem support in llvm-libc), the only issues are:

  • Missing <pwd.h> and getpwnam_r
  • Missing pthread_setschedparam (under review in https://github.com/llvm/llvm-project/pull/205770)
  • Missing definitions for SA_NODEFER and SA_RESETHAND for https://github.com/llvm/llvm-project/blob/57a7e7538cfc73184b8280160e8d89cedab04c5d/llvm/lib/Support/Unix/Signals.inc#L329
  • llvm::thread should support non-integral pthread_t variants (under review in https://github.com/llvm/llvm-project/pull/207918)

As noted above, it's possible to disable at configuration time several other functionality that is currently missing: getrusage, mallinfo, backtrace, sbrk.

vonosmas avatar Jul 07 '26 08:07 vonosmas

Several issues from comment https://github.com/llvm/llvm-project/issues/97191#issuecomment-4901605134 has been fixed. getrusage was added by cd6ceaf83146e52113e15ddf3392d3a22e96212e (and thus can be re-enabled). With backtrace/sbrk/mallinfo disabled I observe the following issues, in

  • getpwnam_r and getpwuid_r from <pwd.h> (and _SC_GETPW_R_SIZE_MAX value). I believe @kaladron has a WIP patches to address this (next in line - https://github.com/llvm/llvm-project/pull/214720)
  • missing <sys/vfs.h> header on Linux (we'll just add a redirect to <sys/statfs.h> which is implemented).
  • missing struct flock (it's a configuration issue, the type header exists, but it's just not exposed through <fcntl.h>
  • missing umask from <sys/stat.h>
  • futimens or futimes can be technically disabled by configuration, but would be nice to have.

vonosmas avatar Aug 27 '26 02:08 vonosmas

After the recent updates, I see an issue with missing umask (tracked in https://github.com/llvm/llvm-project/issues/220759) which is used in Support/Path.inc: https://github.com/llvm/llvm-project/blob/2cbdbc9ad7ff40457023c8f353161069df8eb57f/llvm/lib/Support/Unix/Path.inc#L805-L811

and we still need to add a _SC_GETPW_R_SIZE_MAX value for sysconf.

vonosmas avatar Sep 05 '26 00:09 vonosmas

After that it's the link step that fails due to the missing math library functions: tanh, cosh, sinh, erf that the ConstantFolding requires to be available on the host platform. I suspect these pop us link errors because compilation picks up libc++ <cmath> headers, which direct those calls to __builtin_ variants which are in turn lowered to regular libcalls by Clang. @lntue FYI

vonosmas avatar Sep 05 '26 00:09 vonosmas

tanh, cosh, sinh, erf that the ConstantFolding requires

I wonder if we get host-specific rounding differences due to those? 🤔

nickdesaulniers avatar Sep 08 '26 15:09 nickdesaulniers

tanh, cosh, sinh, erf that the ConstantFolding requires

I wonder if we get host-specific rounding differences due to those? 🤔

Since ConstantFolding is executed on a host compiler with host libc -- definitely, unless the host libc has correct rounding.

vonosmas avatar Sep 08 '26 18:09 vonosmas