Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Rev 7 | Rev 9 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #!/bin/sh
  2. # LLVM/Clang toolchain cross-compilation script for QNX 8.0 by Pierre-Marie Baty <pm@pmbaty.com>
  3.  
  4. # NOTE TO SELF: DO NOT USE $0 AS THIS SCRIPT CAN BE RUN *OR* SOURCED! (see build-llvm.sh in the VM)
  5.  
  6. export QNXSDK_VERSION="8.0.0" # version of the QNX SDK in use, in <major>.<minor>.<revision> format
  7. export QNXSDK_PATH="../qnx800" # relative location from the path of this script where to find the QNX platform SDK
  8. export QNXSDK_HOSTPATH="host/linux/x86_64" # relative location in QNXSDK_PATH of the tree containing the build tools that are runnable on the build host
  9. export QNXSDK_TARGETPATH="target/qnx" # relative location in QNXSDK_PATH of the tree containing the QNX8 system header files
  10.  
  11. export BUILD_DIR_NAME="llvm-build" # name of the directory on the build host's desktop where the LLVM sources will be built
  12. export BUILD_TARGET_ARCH="x86_64" # CPU architecture to build LLVM for, either "x86_64" or "aarch64"
  13.  
  14. export LLVM_VERSION="18.1.4" # version of LLVM that will be built
  15. export LLVM_SOURCES_FILE="llvmorg-${LLVM_VERSION}.tar.gz" # name of the file containing LLVM version LLVM_VERSION sources
  16. export LLVM_SOURCES_URL="https://github.com/llvm/llvm-project/archive/refs/tags/${LLVM_SOURCES_FILE}" # download URL of LLVM_SOURCES_FILE
  17. export LLVM_SOURCES_DIR="llvm-project-llvmorg-${LLVM_VERSION}" # name of directory created when extracting LLVM_SOURCES_FILE
  18.  
  19. # see where we are
  20. export CURRENT_DIR="$(pwd)"
  21.  
  22. # verify we're a x86_64 Linux host
  23. if [ ! "$(uname)" = "Linux" ] || [ ! "$(uname -m)" = "x86_64" ]; then
  24.         echo ""
  25.         echo "Error: this script requires a x86_64 Linux machine (possibly a virtual machine) as the build host." | fold -s -w 79
  26.         echo ""
  27.         exit 1
  28. fi
  29.  
  30. # verify that we have the QNX platform SDK
  31. if [ ! -d "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" ]; then
  32.         echo ""
  33.         echo "Error: the ${QNXSDK_PATH} path doesn't contain a QNX SDK. It must contain the 'host' and 'target' directories of the QNX SDP for the targeted version of QNX and the ${BUILD_TARGET_ARCH} platform. Please deploy these directories and try again." | fold -s -w 79
  34.         echo ""
  35.         exit 1
  36. fi
  37.  
  38. # verify that we have wget, python3, cmake, gcc, g++ and ninja
  39. if    ! wget    --version > /dev/null 2>&1 \
  40.    || ! python3 --version > /dev/null 2>&1 \
  41.    || ! cmake   --version > /dev/null 2>&1 \
  42.    || ! gcc     --version > /dev/null 2>&1 \
  43.    || ! g++     --version > /dev/null 2>&1 \
  44.    || ! ninja   --version > /dev/null 2>&1; then
  45.         echo ""
  46.         echo "Error: this script requires at the very least the following tools installed:"
  47.         echo "  wget"
  48.         echo "  python3"
  49.         echo "  cmake"
  50.         echo "  gcc"
  51.         echo "  g++"
  52.         echo "  ninja"
  53.         echo "Please install them (possibly as binary packages with apt-get) and try again."
  54.         echo ""
  55.         exit 1
  56. fi
  57.  
  58. # verify that the symlinks are deployed in the SDK -- just test one of them
  59. if [ ! -e "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}/usr/include/readline.h" ]; then
  60.         echo ""
  61.         echo "Error: the toolchain platform-specific symbolic links have not been deployed in this QNX SDK. Please run" | fold -s -w 79
  62.         echo "(on a POSIX machine:)"
  63.         echo "  cd ${QNXSDK_PATH}" | fold -s -w 79
  64.         echo "  find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" | fold -s -w 79
  65.         echo "(else on a Windows machine:)"
  66.         echo "  cd ${QNXSDK_PATH}" | fold -s -w 79
  67.         echo "  host\\win64\\x86_64\\usr\\bin\\busybox.exe sh -c \"" \
  68.                "find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \
  69.                "\"" | fold -s -w 79
  70.         echo "Note that this step WILL take time on a Win32 machine, but is only done once." | fold -s -w 79
  71.         echo ""
  72.         exit 1
  73. fi
  74.  
  75. # construct the target triple (actually a quadruple)
  76. export TARGET_ARCH="${BUILD_TARGET_ARCH}"
  77. test "${BUILD_TARGET_ARCH}" = "x86_64" && export TARGET_VENDOR="pc" || export TARGET_VENDOR="unknown"
  78. export TARGET_KERNEL="nto"
  79. export TARGET_SYSTEM="qnx${QNXSDK_VERSION}"
  80. export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}"
  81. echo "Will build LLVM for ${TARGET_TRIPLE}"
  82.  
  83. # change to an immediately visible path, i.e. the user's desktop (failsafe to $HOME if xdg-user-dir is unavailable)
  84. STAGING_PATH="$(xdg-user-dir DESKTOP 2>/dev/null || echo "${HOME}")"
  85. cd "${STAGING_PATH}"
  86.  
  87. # create a symlink in /tmp that will lead to the QNX platform SDK so as to avoid spaces in paths if it doesn't exist already
  88. # (this is totally prohibitive with the official QNX toolchain)
  89. if [ ! -L /tmp/qnxsdk ] || [ ! "$(readlink /tmp/qnxsdk)" = "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" ]; then
  90.         echo "Creating symlink to QNX toolchain in /tmp/qnxsdk..."
  91.         rm -rf /tmp/qnxsdk 2>/dev/null
  92.         ln -fs "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" /tmp/qnxsdk || exit 1
  93. fi
  94.  
  95. # setup the environment
  96. export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}"
  97. export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}"
  98. export MAKEFLAGS="-I${QNX_TARGET}/usr/include"
  99. export PATH="${QNX_HOST}/usr/bin:${PATH}"
  100.  
  101. # download the LLVM source package and unpack it if not done yet
  102. if [ ! -d "${LLVM_SOURCES_DIR}" ]; then
  103.         if [ ! -f "${CURRENT_DIR}/${LLVM_SOURCES_FILE}" ]; then
  104.                 echo "Downloading LLVM ${LLVM_VERSION} sources from LLVM GitHub..."
  105.                 wget "${LLVM_SOURCES_URL}" || exit 1
  106.         fi
  107.         echo "Extracting LLVM ${LLVM_VERSION} sources..."
  108.         cd "$(dirname "${LLVM_SOURCES_DIR}")"
  109.         tar xzf "${CURRENT_DIR}/${LLVM_SOURCES_FILE}" || exit 1
  110.         if [ ! -d "${LLVM_SOURCES_DIR}" ]; then
  111.                 echo "Error: couldn't find ${LLVM_SOURCES_DIR} in extracted LLVM sources."
  112.                 exit 1
  113.         fi
  114. fi
  115.  
  116. # create the build directory
  117. echo "Wiping out build directory..."
  118. test -e "${BUILD_DIR_NAME}" && rm -rf "${BUILD_DIR_NAME}"
  119. mkdir "${BUILD_DIR_NAME}" || exit 1
  120. cd "${BUILD_DIR_NAME}" || exit 1
  121.  
  122. # print the build environment
  123. echo "QNX_HOST=${QNX_HOST}"
  124. echo "QNX_TARGET=${QNX_TARGET}"
  125. echo "MAKEFLAGS=${MAKEFLAGS}"
  126.  
  127. # create the QNX CMake toolchain file
  128. test -e "${TARGET_TRIPLE}.cmake" || echo '# '${TARGET_TRIPLE}' CMake toolchain file by Pierre-Marie Baty <pm@pmbaty.com>
  129.  
  130. SET(CMAKE_SYSTEM_NAME "QNX")
  131. SET(CMAKE_SYSTEM_VERSION "'${QNXSDK_VERSION}'")
  132.  
  133. SET(QNX "1")
  134. SET(QNXNTO "1")
  135. SET(QNX_HOST "$ENV{QNX_HOST}")
  136. SET(QNX_TARGET "$ENV{QNX_TARGET}")
  137. SET(QNX_PROCESSOR "'${TARGET_ARCH}'")
  138.  
  139. SET(CMAKE_ASM_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
  140. SET(CMAKE_ASM_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  141.  
  142. SET(CMAKE_C_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
  143. SET(CMAKE_C_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  144. SET(CMAKE_C_FLAGS_DEBUG "-g")
  145. SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
  146. SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
  147. SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
  148. SET(CMAKE_C_FLAGS "-D_QNX_SOURCE=1 -I${QNX_TARGET}/usr/include/devs/include_'${TARGET_ARCH}' -I${QNX_TARGET}/usr/include/devs -DDONT_DEFINE_BSD -DDONT_DEFINE___FreeBSD_kernel__ -DDONT_DEFINE_FSCALE -DDONT_DEFINE_MACHINE")
  149.  
  150. SET(CMAKE_CXX_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-g++")
  151. SET(CMAKE_CXX_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  152. SET(CMAKE_CXX_FLAGS_DEBUG "-g")
  153. SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
  154. SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
  155. SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
  156. SET(CMAKE_CXX_FLAGS "-D_QNX_SOURCE=1 -I${QNX_TARGET}/usr/include/devs/include_'${TARGET_ARCH}' -I${QNX_TARGET}/usr/include/devs -DDONT_DEFINE_BSD -DDONT_DEFINE___FreeBSD_kernel__ -DDONT_DEFINE_FSCALE -DDONT_DEFINE_MACHINE")
  157.  
  158. SET(CMAKE_LINKER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ld.bfd")
  159. SET(CMAKE_SHARED_LINKER_FLAGS "-lsocket")
  160. SET(CMAKE_EXE_LINKER_FLAGS "-lsocket")
  161.  
  162. SET(CMAKE_RANLIB "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ranlib")
  163.  
  164. SET(CMAKE_NM "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-nm")
  165.  
  166. SET(CMAKE_AR "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ar")
  167.  
  168. SET(CMAKE_FIND_ROOT_PATH "${QNX_TARGET}")
  169. SET(CMAKE_FIND_ROOT_PATH_HOST_PROGRAM NEVER)
  170. SET(CMAKE_FIND_ROOT_PATH_HOST_LIBRARY ONLY)
  171. SET(CMAKE_FIND_ROOT_PATH_HOST_INCLUDE ONLY)
  172. ' > "${TARGET_TRIPLE}.cmake"
  173.  
  174. backup_and_patch_if_necessary()
  175. {
  176.         # handy function that patches a file in LLVM_SOURCES_DIR with a given sed replacement regex if necessary, creating backups
  177.         # args: <file pathname> <grep_string_to_test_for_presence_of_patch> <sed regex>
  178.         # test if already patched
  179.         grep -q "${2}" "../${LLVM_SOURCES_DIR}/${1}" && return
  180.         # tell what we're about to do
  181.         echo "Patching ${1}..."
  182.         # have a backup first
  183.         test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || cp "../${LLVM_SOURCES_DIR}/${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]"
  184.         # perform the patch
  185.         sed -E "${3}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" > "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]"
  186.         # verify that we did it successfully
  187.         if ! grep -q "${2}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]"; then
  188.                 echo "Error: the file ${1} could not be patched. Please investigate and fix manually." | fold -s -w 79; exit 1
  189.         fi
  190.         # and put the patched file in place
  191.         cp -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]" "../${LLVM_SOURCES_DIR}/${1}" || exit 1
  192. }
  193.  
  194. # patch llvm/Support/Unix/Path.inc if not done yet
  195. # replace [defined(__FreeBSD_kernel__)] with [(defined(__FreeBSD_kernel__) || defined(__QNXNTO__))]
  196. # RATIONALE: the concerned parts of the QNX platform SDK (/usr/include/devs/*) are actually largely based on FreeBSD code, to conveniently use the FreeBSD network stack.
  197. # NONETHELESS, QNX *IS NOT* FreeBSD and "__FreeBSD_kernel__" *SHOULD NOT* be defined, else userland code may falsely assume a genuine FreeBSD include tree to be available,
  198. # which leads to compilation errors caused by include files assumed to be there whereas they are in fact nowhere to be found (ex: /usr/include/sys/user.h, among others).
  199. # The QNX8 platform SDK has thus been patched to NOT define __FreeBSD_kernel__ when the -DDONT_DEFINE___FreeBSD_kernel__ flag is passed, and the feature tests in Path.inc
  200. # that branch into FreeBSD APIs (which ones *are* exposed in the QNX libc since QNX largely shares FreeBSD code) are about to be patched right now to accept __QNXNTO__ too.
  201. # Additionally, support for statfs()/fstatfs() has been restored in the QNX8 platform SDK in /usr/include/devs/sys/mount.h where it was claimed but absent from the QNX8 libc.
  202. # FIXME: these hacks should be moved elsewhere not to pollute the QNX8 platform SDK -- it *SHOULD* be possible to build without devs
  203. backup_and_patch_if_necessary "llvm/lib/Support/Unix/Path.inc" __QNXNTO__ 's/defined\(__FreeBSD_kernel__\)/\(defined\(__FreeBSD_kernel__\) \|\| \defined\(__QNXNTO__\)\)/g'
  204.  
  205. # patch lldb/Source/Host/common/Host.cpp if not done yet
  206. # replace [defined(SIGINFO)] with [(defined(SIGINFO) && !defined(__QNXNTO__))]
  207. # RATIONALE: the QNX people defined SIGINFO to the value of SIGUSR1 for a mysterious reason, defeating the purpose of SIGUSR1
  208. # which should be a User-Definable signal as mandated by POSIX. Consequently, userland code that enumerates POSIX signals
  209. # hits twice the same value and all is left to solve this problem is to filter either one out. Since SIGINFO, contrarily to SIGUSR1,
  210. # is an optional signal, this is the value that will be left out - even if on QNX nobody can actually use SIGUSR1's value.
  211. backup_and_patch_if_necessary "lldb/source/Host/common/Host.cpp" __QNXNTO__ 's/defined\(SIGINFO\)/\(defined\(SIGINFO\) \&\& \!defined\(__QNXNTO__\)\)/g'
  212.  
  213. # FIXME: another patch is needed for LLDB. Basically, the ptrace() POSIX system call doesn't exist on QNX. Instead
  214. # QNX use their own debug utility: pdebug. Note the /proc filesystem can be used to access a debuggee's virtual memory space.
  215. # in lldb/source/Host/posix/ProcessLauncherPosixFork.cpp line 196:
  216. #   if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1) // <--- undefined: ptrace, PT_TRACE_ME
  217.  
  218. #    -D LLVM_ENABLE_PROJECTS="clang;lld;lldb" \
  219.  
  220. # patch compiler-rt/lib/builtins/enable_execute_stack.c if not done yet
  221. # replace [#include "int_lib.h"] with [#ifdef __QNXNTO__\n#define _QNX_SOURCE 1\n#endif\n#include "int_lib.h"]
  222. # RATIONALE: the QNX people decided to hide most of the UNIX APIs of their libc behind a mandatory _QNX_SOURCE definition to separate the Neutrino primitives from the rest maybe.
  223. # This name does not "enforce" any standard at all (like _POSIX_SOURCE and similar names), it's just a convenient hack for the QNX people to hide stuff.
  224. # To put things back on track and expose the POSIX interfaces of their libc to the world, _QNX_SOURCE has been added to the toolchain CMake file, but it happens that
  225. # when building compiler-rt specifically, the CMAKE_C_FLAGS directive is ignored. Patching the source file seems like the best option to fix that.
  226. backup_and_patch_if_necessary "compiler-rt/lib/builtins/enable_execute_stack.c" __QNXNTO__ 's/#include "int_lib.h"/#ifdef __QNXNTO__\n#define _QNX_SOURCE 1\n#endif\n#include "int_lib.h"/g'
  227.  
  228. # patch libunwind/src/RWMutex.hpp
  229. # replace [#include <pthread.h>] with [#ifdef __QNXNTO__\n#define __EXT_QNX\n#define __EXT_POSIX1_200112\n#endif\n#include <pthread.h>]
  230. # RATIONALE: the pthreads implementation in QNX 'steals' things from the POSIX.1 standard and hides them behind the __EXT_QNX macro,
  231. # which is defined when either _QNX_SOURCE or __EXT without anything else is defined. This is not correct and low-level libraries
  232. # such as libunwind that do not define either of those fail to build. Also note that a FreeBSD-compatible header was added to the QNX8
  233. # platform SDK: /usr/include/link.h, which defines Elf types as Elf64/Elf32 types depending on the architecture and adds some glue.
  234. backup_and_patch_if_necessary "libunwind/src/RWMutex.hpp" __QNXNTO__ 's/#include <pthread.h>/#ifdef __QNXNTO__\n#define __EXT_QNX\n#define __EXT_POSIX1_200112\n#endif\n#include <pthread.h>/g'
  235.  
  236. # now configure LLVM
  237. echo "Configuring LLVM build..."
  238. cmake \
  239.     -D CMAKE_TOOLCHAIN_FILE="${TARGET_TRIPLE}.cmake" \
  240.     -D CMAKE_BUILD_TYPE="MinSizeRel" \
  241.     -D CMAKE_INSTALL_PREFIX="/tmp/qnxsdk/host/qnx$(echo "${QNXSDK_VERSION}"|awk -F. '{print $1}')/${BUILD_TARGET_ARCH}" \
  242.     -D CMAKE_STAGING_PREFIX="/usr/bin" \
  243.     -D LLVM_HOST_TRIPLE="${TARGET_TRIPLE}" \
  244.     -D LLVM_ENABLE_PROJECTS="clang;lld" \
  245.     -D LLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi;libunwind" \
  246.     -D LLVM_TARGETS_TO_BUILD="AArch64;X86" \
  247.     -D COMPILER_RT_BUILD_SANITIZERS="OFF" \
  248.     -G Ninja \
  249.     "../${LLVM_SOURCES_DIR}/llvm" || exit 1
  250.  
  251. # hijack the "bin" and "lib/clang" output directories and redirect them to the hypervisor's filesystem
  252. test -e "${CURRENT_DIR}/llvm-build" && rm -rf "${CURRENT_DIR}/llvm-build"
  253. mkdir -p "${CURRENT_DIR}/llvm-build"
  254. test -d bin && mv bin "${CURRENT_DIR}/llvm-build/bin" || mkdir "${CURRENT_DIR}/llvm-build/bin"
  255. ln -s "${CURRENT_DIR}/llvm-build/bin" bin
  256. mkdir -p "${CURRENT_DIR}/llvm-build/lib"
  257. test -d lib/clang && mv lib/clang "${CURRENT_DIR}/llvm-build/lib/clang" || mkdir "${CURRENT_DIR}/llvm-build/lib/clang"
  258. ln -s "${CURRENT_DIR}/llvm-build/lib/clang" lib/clang
  259.  
  260. # and do the Lord's work. https://youtu.be/jcyYmCnkbEE
  261. echo "Building LLVM..."
  262. cmake --build . || exit 1
  263.  
  264. echo "Champagne, James. Champagne."
  265. exit 0
  266.