llvm-native: fix native sub-build linking against Redox relibc
The native tablegen/tools sub-build (CROSS_TOOLCHAIN_FLAGS_NATIVE) generated native.cmake against $COOKBOOK_TOOLCHAIN (the Redox cross sysroot) and never cleared the inherited cross CMAKE_*_FLAGS, so the host try_compile linked relibc and died with 'undefined reference to __libc_start_main'. Port llvm21's proven fix: save+unset LDFLAGS/CFLAGS/CXXFLAGS, generate native.cmake against /usr, CACHE FORCE-clear the cross flags for the native sub-cache, and restore the cross flags via -DCMAKE_*_FLAGS (not the environment) for the cross build. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,47 @@ script = """
|
||||
DYNAMIC_INIT
|
||||
ARCH="$(echo "${TARGET}" | cut -d - -f1)"
|
||||
|
||||
generate_cookbook_cmake_file "$COOKBOOK_HOST_TARGET" "" "$COOKBOOK_TOOLCHAIN" native.cmake
|
||||
# Save and unset the cross-compile flags DYNAMIC_INIT exports so the host cc
|
||||
# in the native sub-build (CROSS_TOOLCHAIN_FLAGS_NATIVE below) links against
|
||||
# the host libc, not the Redox sysroot's relibc — otherwise the native cmake
|
||||
# try_compile fails with "undefined reference to __libc_start_main". The cross
|
||||
# build restores them via -DCMAKE_*_FLAGS further down. Mirrors the llvm21
|
||||
# recipe, which solved this exact failure.
|
||||
_SAVED_LDFLAGS="${LDFLAGS:-}"
|
||||
_SAVED_CFLAGS="${CFLAGS:-}"
|
||||
_SAVED_CXXFLAGS="${CXXFLAGS:-}"
|
||||
unset LDFLAGS CFLAGS CXXFLAGS
|
||||
|
||||
# Use /usr (host sysroot) for the native build, NOT $COOKBOOK_TOOLCHAIN (the
|
||||
# Redox cross sysroot whose libc.a lacks __libc_start_main).
|
||||
generate_cookbook_cmake_file "$COOKBOOK_HOST_TARGET" "" "/usr" native.cmake
|
||||
|
||||
# The generated native.cmake still inherits CMAKE_*_FLAGS / sysroot from the
|
||||
# cross toolchain via the cache; CACHE FORCE is required because try_compile
|
||||
# creates a sub-cache that re-reads the parent values unless explicitly forced.
|
||||
cat >> native.cmake <<'NATIVE_EOF'
|
||||
|
||||
# Override cross-sysroot flags so the host cc links against the host libc.
|
||||
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
|
||||
|
||||
case "${ARCH}" in
|
||||
x86 | x86_64) LLVM_TARGETS_TO_BUILD="X86";;
|
||||
@@ -44,7 +84,6 @@ COOKBOOK_CMAKE_FLAGS+=(
|
||||
-DCLANG_LINK_CLANG_DYLIB=ON
|
||||
-DLIBCLANG_BUILD_STATIC=1
|
||||
-DCROSS_TOOLCHAIN_FLAGS_NATIVE="-DCMAKE_TOOLCHAIN_FILE=$(realpath native.cmake)"
|
||||
-DCMAKE_CXX_FLAGS="--std=gnu++11"
|
||||
-DBUILD_SHARED_LIBS=False
|
||||
-DLLVM_BUILD_EXAMPLES=Off
|
||||
-DLLVM_BUILD_TESTS=Off
|
||||
@@ -64,6 +103,24 @@ COOKBOOK_CMAKE_FLAGS+=(
|
||||
)
|
||||
|
||||
COOKBOOK_SOURCE="$COOKBOOK_SOURCE/llvm"
|
||||
|
||||
# Restore the cross-compile flags for the CROSS build via cmake vars rather
|
||||
# than the environment: an exported LDFLAGS leaks into the native try_compile
|
||||
# and re-introduces the Redox libc link failure. (Native.cmake above keeps the
|
||||
# host sub-build clean.)
|
||||
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
|
||||
|
||||
# Create unprefixed symlinks for native use
|
||||
|
||||
Reference in New Issue
Block a user