#!/bin/sh
export QNXSDK_PATH="../qnx800"
export QNXSDK_HOSTPATH="host/linux/x86_64"
export QNXSDK_TARGETPATH="target/qnx"
export BUILD_DIR_NAME="llvm-build"
export LLVM_VERSION="18.1.4"
export LLVM_SOURCES_FILE="llvmorg-${LLVM_VERSION}.tar.gz" # name of file containing LLVM version LLVM_VERSION sources
export LLVM_SOURCES_URL="https://github.com/llvm/llvm-project/archive/refs/tags/${LLVM_SOURCES_FILE}" # download URL of LLVM_SOURCES_FILE
export LLVM_SOURCES_DIR="llvm-project-llvmorg-${LLVM_VERSION}" # name of directory created when extracting LLVM_SOURCES_FILE
export TARGET_ARCH="x86_64"
export TARGET_VENDOR="pc"
#export TARGET_ARCH="aarch64"
#export TARGET_VENDOR="unknown"
export TARGET_KERNEL="nto"
export TARGET_SYSTEM="qnx8.0.0"
# see where we are
export CURRENT_DIR="$(pwd)"
# verify we're a x86_64 Linux host
if [ ! "$(uname)" = "Linux" ] || [ ! "$(uname -m)" = "x86_64" ]; then
echo ""
echo "Error: this script requires a x86_64 Linux machine (possibly a virtual machine) as the build host."
echo ""
exit 1
fi
# verify that we have the QNX platform SDK
if [ ! -d "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" ]; then
echo ""
echo "Error: the ${QNXSDK_PATH} path doesn't contain a QNX SDK. It must contain the"
echo "'host' and 'target' directories of the QNX SDP for the targeted version of QNX"
echo "and the ${TARGET_ARCH} platform. Please deploy these directories and try again."
echo "NOTE: if you're using a VMWare virtual machine and this path points to a shared"
echo "folder, you may need to initiate the share by unticking the 'Enable shared"
echo "folders' checkbox and ticking it again in the VM settings (VMWare bug)."
echo ""
exit 1
fi
# verify that we have wget, python3, cmake, gcc, g++ and ninja
if ! wget --version > /dev/null 2>&1 \
|| ! python3 --version > /dev/null 2>&1 \
|| ! cmake --version > /dev/null 2>&1 \
|| ! gcc --version > /dev/null 2>&1 \
|| ! g++ --version > /dev/null 2>&1 \
|| ! ninja --version > /dev/null 2>&1; then
echo ""
echo "Error: this script requires at the very least the following tools installed:"
echo " wget"
echo " python3"
echo " cmake"
echo " gcc"
echo " g++"
echo " ninja"
echo "Please install them (possibly as binary packages with apt-get) and try again."
echo ""
exit 1
fi
# verify that the symlinks are deployed in the SDK -- just test one of them
if [ ! -e "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}/usr/include/readline.h" ]; then
echo ""
echo "Error: the toolchain platform-specific symbolic links have not been deployed in"
echo "this QNX SDK. Please run"
echo "(on a POSIX machine:)"
echo " cd ${QNXSDK_PATH}"
echo " find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state"
echo "(else on a Windows machine:)"
echo " cd ${QNXSDK_PATH}"
echo " host\\win64\\${TARGET_ARCH}\\usr\\bin\\busybox.exe sh -c \"" \
"find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \
"\""
echo "Note that this step WILL take time on a Win32 machine, but is only done once."
echo ""
exit 1
fi
# construct the target triple (actually a quadruple)
export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}"
echo "Will build LLVM for ${TARGET_TRIPLE}"
# change to an immediately visible path
cd ~/Desktop
# create a symlink in /tmp that will lead to the QNX platform SDK so as to avoid spaces in paths
# (this is totally prohibitive with the official QNX toolchain)
test -L /tmp/qnxsdk && rm /tmp/qnxsdk
ln -fs "${CURRENT_DIR}/${QNXSDK_PATH}" /tmp/qnxsdk
# setup the environment
export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}"
export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}"
export MAKEFLAGS="-I${QNX_TARGET}/usr/include"
export PATH="${QNX_HOST}/usr/bin:${PATH}"
#export PYTHONDONTWRITEBYTECODE="1"
#export PYTHONPATH=""
# download the LLVM source package and unpack it if not done yet
if [ ! -d "${LLVM_SOURCES_DIR}" ]; then
if [ ! -f "${CURRENT_DIR}/${LLVM_SOURCES_FILE}" ]; then
echo "Downloading LLVM ${LLVM_VERSION} sources from LLVM GitHub..."
wget "${LLVM_SOURCES_URL}" || exit 1
fi
echo "Extracting LLVM ${LLVM_VERSION} sources..."
cd "$(dirname "${LLVM_SOURCES_DIR}")"
tar xzf "${CURRENT_DIR}/${LLVM_SOURCES_FILE}" || exit 1
if [ ! -d "${LLVM_SOURCES_DIR}" ]; then
echo "Error: couldn't find ${LLVM_SOURCES_DIR} in extracted LLVM sources."
exit 1
fi
fi
# create the build directory
echo "Wiping out build directory..."
test -e "${BUILD_DIR_NAME}" && rm -rf "${BUILD_DIR_NAME}"
mkdir "${BUILD_DIR_NAME}" || exit 1
cd "${BUILD_DIR_NAME}" || exit 1
# print the build environment
echo "QNX_HOST=${QNX_HOST}"
echo "QNX_TARGET=${QNX_TARGET}"
echo "MAKEFLAGS=${MAKEFLAGS}"
# create the QNX CMake toolchain file
echo '# '${TARGET_TRIPLE}' CMake toolchain file by Pierre-Marie Baty <pm@pmbaty.com>
SET(CMAKE_SYSTEM_NAME "QNX")
SET(CMAKE_SYSTEM_VERSION "8.0.0")
SET(QNX "1")
SET(QNXNTO "1")
if ("$ENV{QNX_HOST}" STREQUAL "")
message(FATAL_ERROR "the QNX_HOST environment variable is not set")
endif()
SET(QNX_HOST "$ENV{QNX_HOST}")
if ("$ENV{QNX_TARGET}" STREQUAL "")
message(FATAL_ERROR "the QNX_TARGET environment variable is not set")
endif()
SET(QNX_TARGET "$ENV{QNX_TARGET}")
SET(QNX_PROCESSOR "'${TARGET_ARCH}'")
SET(CMAKE_ASM_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
SET(CMAKE_ASM_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
SET(CMAKE_C_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
SET(CMAKE_C_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
SET(CMAKE_C_FLAGS_DEBUG "-g")
SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
SET(CMAKE_C_FLAGS "-D_QNX_SOURCE=1 -I${QNX_TARGET}/usr/include/devs/include_'${TARGET_ARCH}' -I${QNX_TARGET}/usr/include/devs -DLLVM_BUILD=1")
SET(CMAKE_CXX_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-g++")
SET(CMAKE_CXX_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
SET(CMAKE_CXX_FLAGS "-D_QNX_SOURCE=1 -I${QNX_TARGET}/usr/include/devs/include_'${TARGET_ARCH}' -I${QNX_TARGET}/usr/include/devs -DLLVM_BUILD=1")
SET(CMAKE_LINKER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ld.bfd")
SET(CMAKE_EXE_LINKER_FLAGS "-lsocket")
SET(CMAKE_RANLIB "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ranlib")
SET(CMAKE_AR "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ar")
SET(CMAKE_FIND_ROOT_PATH "${QNX_TARGET}")
SET(CMAKE_FIND_ROOT_PATH_HOST_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_HOST_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_HOST_INCLUDE ONLY)
' > "${TARGET_TRIPLE}.cmake"
# now configure LLVM
echo "Configuring LLVM build..."
# TODO: add compiler-rt project (requires probably QNX-specific work)
cmake \
-D CMAKE_TOOLCHAIN_FILE="${TARGET_TRIPLE}.cmake" \
-D CMAKE_BUILD_TYPE="MinSizeRel" \
-D CMAKE_INSTALL_PREFIX="/tmp/qnxsdk/host/qnx8" \
-D CMAKE_STAGING_PREFIX="/usr/bin" \
-D LLVM_HOST_TRIPLE="${TARGET_TRIPLE}" \
-D LLVM_ENABLE_PROJECTS="clang;lld;lldb" \
-D LLVM_TARGETS_TO_BUILD="AArch64;X86" \
-G Ninja \
"../${LLVM_SOURCES_DIR}/llvm" || exit 1
# and do the Lord's work. https://youtu.be/jcyYmCnkbEE
echo "Building LLVM..."
cmake --build . || exit 1
echo "Champagne, James. Champagne."
exit 0