Files
RedBear-OS/recipes/dev/llvm21/recipe.toml
T
vasilito c775ea2539 llvm21: host libLLVM must keep rustc's CPU targets (X86;AArch64;RISCV) + GPU
The host libLLVM.so (COOKBOOK_HOST_SYSROOT=/usr branch) lives in the redoxer
toolchain and is dynamically linked by rustc's librustc_driver. Building it as
X86;AMDGPU;NVPTX only dropped AArch64/RISCV, so after the AMDGPU splice rustc
failed to load with 'undefined symbol: LLVMInitializeAArch64AsmPrinter' —
breaking every Rust compile in the tree. Build the host LLVM with all CPU
targets rustc emits (X86;AArch64;RISCV) PLUS the GPU backends (AMDGPU;NVPTX).
The Redox-target llvm21 stays X86;AMDGPU;NVPTX (Redox is amd64-only, no need to
bloat the shipped libLLVM with foreign CPU backends).

Fixes a latent regression from 80d20db8/a6999912.
2026-07-31 14:40:09 +03:00

174 lines
7.1 KiB
TOML

[package]
name = "llvm21"
version = "0.1.0"
[source]
git = "https://gitlab.redox-os.org/redox-os/llvm-project.git"
upstream = "https://github.com/rust-lang/llvm-project.git"
branch = "redox-2025-10-03"
shallow_clone = true
[build]
template = "custom"
dependencies = [
"zstd",
]
script = """
DYNAMIC_INIT
ARCH="$(echo "${TARGET}" | cut -d - -f1)"
# Save and unset cross-compile flags inherited from DYNAMIC_INIT so
# the host cc in the native TableGen build links against the host libc
# instead of the Redox sysroot libc (which would fail with "undefined
# reference to __libc_start_main"). These env vars are also cleared
# inside native.cmake for the sub-process, but the parent shell
# still needs them unset before generating native.cmake so that the
# generated file does not embed the cross sysroot paths. The cross
# build (cookbook_cmake below) restores them from the saved values.
_SAVED_LDFLAGS="${LDFLAGS:-}"
_SAVED_CFLAGS="${CFLAGS:-}"
_SAVED_CXXFLAGS="${CXXFLAGS:-}"
unset LDFLAGS CFLAGS CXXFLAGS
# Use /usr (host sysroot) for the native build, NOT $COOKBOOK_TOOLCHAIN
# (which points to the Redox cross sysroot). The Redox sysroot's libc.a
# (relibc) lacks __libc_start_main, causing the native cmake try_compile
# to fail with "undefined reference to __libc_start_main".
generate_cookbook_cmake_file "$COOKBOOK_HOST_TARGET" "" "/usr" native.cmake
# The generated native.cmake inherits CMAKE_C_FLAGS / CMAKE_LDFLAGS / etc.
# from the cross toolchain, which causes the host cc to link the native
# TableGen build against the Redox sysroot libc and fail with
# "undefined reference to __libc_start_main". CACHE FORCE is required
# because CMake's try_compile creates a sub-cache that re-reads the
# parent values unless they are explicitly forced in the cache.
cat >> native.cmake <<'NATIVE_EOF'
# Override cross-sysroot flags so the host cc links against the host libc.
# Also clear the LDFLAGS env var that DYNAMIC_INIT exports, because cc
# picks up -L<sysroot>/lib from it and links against the Redox libc,
# which then fails with "undefined reference to __libc_start_main".
set(CMAKE_C_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_LDFLAGS "" CACHE STRING "" FORCE)
set(CMAKE_LDFLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_LDFLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "" CACHE STRING "" FORCE)
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "" CACHE STRING "" FORCE)
set(CMAKE_SYSROOT "" CACHE STRING "" FORCE)
set(CMAKE_FIND_ROOT_PATH "" CACHE STRING "" FORCE)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM "NEVER" CACHE STRING "" FORCE)
set(ENV{LDFLAGS} "")
set(ENV{CFLAGS} "")
set(ENV{CXXFLAGS} "")
NATIVE_EOF
# amd64-only (see local/AGENTS.md): the CPU target is X86 (= amd64/x86_64).
# GPU codegen backends are a separate axis from the CPU platform and are driven
# by which Mesa drivers we ship:
# - AMDGPU -> radeonsi (GL) + radv (Vulkan), AMD hardware GPUs. Required now:
# Mesa's `dependency('llvm', modules: [... amdgpu ...])` fails without it.
# - NVPTX -> future NVIDIA support (nouveau/NVK). Included NOW so enabling
# NVIDIA later needs no (very expensive) LLVM rebuild.
# - Intel (iris/anvil) and virgl need NO LLVM GPU backend — Intel is NIR-based
# with its own shader compiler and virgl is virtualized; they are enabled via
# Mesa's gallium/vulkan driver lists, not an LLVM target.
# None of these add a second CPU platform, so amd64-only still holds.
case "${ARCH}" in
x86 | x86_64) LLVM_TARGETS_TO_BUILD="X86;AMDGPU;NVPTX";;
aarch64) LLVM_TARGETS_TO_BUILD="AArch64;AMDGPU;NVPTX";;
riscv64gc) LLVM_TARGETS_TO_BUILD="RISCV;AMDGPU;NVPTX";;
esac
# Host LLVM build (TARGET == the host triple): this produces the libLLVM.so that
# lives in the redoxer toolchain and is DYNAMICALLY LINKED by rustc's
# librustc_driver. rustc is a multi-target cross compiler and references the
# AsmPrinter/target symbols for every CPU backend it can emit (X86, AArch64,
# RISCV), so the shared libLLVM MUST retain all three or rustc fails to load
# ("undefined symbol: LLVMInitializeAArch64AsmPrinter"). We additionally add the
# GPU backends (AMDGPU for Mesa radeonsi/radv now, NVPTX for future NVIDIA) that
# get spliced into the toolchain. Net: the host libLLVM carries every CPU target
# rustc needs PLUS the GPU backends — amd64-only still holds (these are codegen
# backends, not host platforms). NB: detect the host build via TARGET, not
# COOKBOOK_HOST_SYSROOT (which is NOT "/usr" for host:llvm21).
if [ "${TARGET}" = "${COOKBOOK_HOST_TARGET}" ]; then
LLVM_TARGETS_TO_BUILD="X86;AArch64;RISCV;AMDGPU;NVPTX"
fi
# This just build the LLVM library and tools just enough for Rust, to build the rest of LLVM see
# https://github.com/llvm/llvm-zorg/blob/main/zorg/buildbot/builders/annotated/standalone-build.sh
COOKBOOK_CMAKE_FLAGS+=(
-DLLVM_BUILD_LLVM_DYLIB=On
-DLLVM_LINK_LLVM_DYLIB=On
-DLLVM_INCLUDE_UTILS=On
-DLLVM_INSTALL_UTILS=On
-DLLVM_TOOL_LLVM_COV_BUILD=On
-DLLVM_TOOL_LLVM_PROFDATA_BUILD=On
-DLLVM_TARGETS_TO_BUILD="$LLVM_TARGETS_TO_BUILD"
-DLLVM_ENABLE_ZLIB=Off
-DLLVM_USE_STATIC_ZSTD=On
-DLLVM_ENABLE_LIBXML2=Off
# the rest of options that shared to clang
-DCROSS_TOOLCHAIN_FLAGS_NATIVE="-DCMAKE_TOOLCHAIN_FILE=$(realpath "${COOKBOOK_RECIPE}/native.cmake")"
-DCMAKE_CXX_FLAGS="--std=gnu++11"
-DBUILD_SHARED_LIBS=False
-DLLVM_BUILD_EXAMPLES=Off
-DLLVM_BUILD_TESTS=Off
-DLLVM_DEFAULT_TARGET_TRIPLE="${TARGET}"
-DLLVM_ENABLE_LTO=Off
-DLLVM_ENABLE_RTTI=On
-DLLVM_ENABLE_THREADS=On
-DLLVM_INCLUDE_EXAMPLES=Off
-DLLVM_INCLUDE_TESTS=Off
-DLLVM_OPTIMIZED_TABLEGEN=On
-DLLVM_TARGET_ARCH=$ARCH
-DLLVM_TOOLS_INSTALL_DIR=bin
-DLLVM_UTILS_INSTALL_DIR=bin
-DUNIX=1
)
COOKBOOK_SOURCE="$COOKBOOK_SOURCE/llvm"
# Pass cross-compile linker flags via cmake variables instead of the
# LDFLAGS env var. LDFLAGS in the environment leaks into the native
# TableGen build's cmake try_compile, causing it to link against the
# Redox relibc (which lacks __libc_start_main) instead of host glibc.
# By keeping LDFLAGS unset and passing flags through cmake, the native
# build inherits a clean environment.
if [ -n "${_SAVED_LDFLAGS}" ]; then
COOKBOOK_CMAKE_FLAGS+=(-DCMAKE_EXE_LINKER_FLAGS="${_SAVED_LDFLAGS}")
COOKBOOK_CMAKE_FLAGS+=(-DCMAKE_SHARED_LINKER_FLAGS="${_SAVED_LDFLAGS}")
fi
if [ -n "${_SAVED_CFLAGS}" ]; then
COOKBOOK_CMAKE_FLAGS+=(-DCMAKE_C_FLAGS="${_SAVED_CFLAGS}")
fi
if [ -n "${_SAVED_CXXFLAGS}" ]; then
COOKBOOK_CMAKE_FLAGS+=(-DCMAKE_CXX_FLAGS="--std=gnu++11 ${_SAVED_CXXFLAGS}")
else
COOKBOOK_CMAKE_FLAGS+=(-DCMAKE_CXX_FLAGS="--std=gnu++11")
fi
cookbook_cmake
"""
# llvm runtime
[[optional-packages]]
name = "runtime"
files = [
"usr/bin/**",
]
[[optional-packages]]
name = "dev"
dependencies = [ ".runtime" ]
files = [
"usr/include/llvm*/**",
"usr/lib/libLLVM*.a",
"usr/lib/cmake/llvm/**",
]