Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Rev 5 | Rev 7 | 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_PATH="../qnx800"
  7. export QNXSDK_HOSTPATH="host/linux/x86_64"
  8. export QNXSDK_TARGETPATH="target/qnx"
  9. export BUILD_DIR_NAME="llvm-build"
  10. export LLVM_VERSION="18.1.4"
  11. export LLVM_SOURCES_FILE="llvmorg-${LLVM_VERSION}.tar.gz" # name of file containing LLVM version LLVM_VERSION sources
  12. export LLVM_SOURCES_URL="https://github.com/llvm/llvm-project/archive/refs/tags/${LLVM_SOURCES_FILE}" # download URL of LLVM_SOURCES_FILE
  13. export LLVM_SOURCES_DIR="llvm-project-llvmorg-${LLVM_VERSION}" # name of directory created when extracting LLVM_SOURCES_FILE
  14.  
  15. # target triple settings
  16. export TARGET_ARCH="x86_64"
  17. #export TARGET_ARCH="aarch64"
  18. test "${TARGET_ARCH}" = "x86_64" && export TARGET_VENDOR="pc" || export TARGET_VENDOR="unknown"
  19. export TARGET_KERNEL="nto"
  20. export TARGET_SYSTEM="qnx8.0.0"
  21.  
  22. # see where we are
  23. export CURRENT_DIR="$(pwd)"
  24.  
  25. # verify we're a x86_64 Linux host
  26. if [ ! "$(uname)" = "Linux" ] || [ ! "$(uname -m)" = "x86_64" ]; then
  27.         echo ""
  28.         echo "Error: this script requires a x86_64 Linux machine (possibly a virtual machine) as the build host." | fold -s -w 79
  29.         echo ""
  30.         exit 1
  31. fi
  32.  
  33. # verify that we have the QNX platform SDK
  34. if [ ! -d "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" ]; then
  35.         echo ""
  36.         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 ${TARGET_ARCH} platform. Please deploy these directories and try again." | fold -s -w 79
  37.         echo ""
  38.         exit 1
  39. fi
  40.  
  41. # verify that we have wget, python3, cmake, gcc, g++ and ninja
  42. if    ! wget    --version > /dev/null 2>&1 \
  43.    || ! python3 --version > /dev/null 2>&1 \
  44.    || ! cmake   --version > /dev/null 2>&1 \
  45.    || ! gcc     --version > /dev/null 2>&1 \
  46.    || ! g++     --version > /dev/null 2>&1 \
  47.    || ! ninja   --version > /dev/null 2>&1; then
  48.         echo ""
  49.         echo "Error: this script requires at the very least the following tools installed:"
  50.         echo "  wget"
  51.         echo "  python3"
  52.         echo "  cmake"
  53.         echo "  gcc"
  54.         echo "  g++"
  55.         echo "  ninja"
  56.         echo "Please install them (possibly as binary packages with apt-get) and try again."
  57.         echo ""
  58.         exit 1
  59. fi
  60.  
  61. # verify that the symlinks are deployed in the SDK -- just test one of them
  62. if [ ! -e "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}/usr/include/readline.h" ]; then
  63.         echo ""
  64.         echo "Error: the toolchain platform-specific symbolic links have not been deployed in this QNX SDK. Please run" | fold -s -w 79
  65.         echo "(on a POSIX machine:)"
  66.         echo "  cd ${QNXSDK_PATH}" | fold -s -w 79
  67.         echo "  find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" | fold -s -w 79
  68.         echo "(else on a Windows machine:)"
  69.         echo "  cd ${QNXSDK_PATH}" | fold -s -w 79
  70.         echo "  host\\win64\\x86_64\\usr\\bin\\busybox.exe sh -c \"" \
  71.                "find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \
  72.                "\"" | fold -s -w 79
  73.         echo "Note that this step WILL take time on a Win32 machine, but is only done once." | fold -s -w 79
  74.         echo ""
  75.         exit 1
  76. fi
  77.  
  78. # construct the target triple (actually a quadruple)
  79. export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}"
  80. echo "Will build LLVM for ${TARGET_TRIPLE}"
  81.  
  82. # change to an immediately visible path
  83. cd ~/Desktop
  84.  
  85. # create a symlink in /tmp that will lead to the QNX platform SDK so as to avoid spaces in paths
  86. # (this is totally prohibitive with the official QNX toolchain)
  87. test -L /tmp/qnxsdk && rm /tmp/qnxsdk
  88. ln -fs "${CURRENT_DIR}/${QNXSDK_PATH}" /tmp/qnxsdk
  89.  
  90. # setup the environment
  91. export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}"
  92. export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}"
  93. export MAKEFLAGS="-I${QNX_TARGET}/usr/include"
  94. export PATH="${QNX_HOST}/usr/bin:${PATH}"
  95. #export PYTHONDONTWRITEBYTECODE="1"
  96. #export PYTHONPATH=""
  97.  
  98. # download the LLVM source package and unpack it if not done yet
  99. if [ ! -d "${LLVM_SOURCES_DIR}" ]; then
  100.         if [ ! -f "${CURRENT_DIR}/${LLVM_SOURCES_FILE}" ]; then
  101.                 echo "Downloading LLVM ${LLVM_VERSION} sources from LLVM GitHub..."
  102.                 wget "${LLVM_SOURCES_URL}" || exit 1
  103.         fi
  104.         echo "Extracting LLVM ${LLVM_VERSION} sources..."
  105.         cd "$(dirname "${LLVM_SOURCES_DIR}")"
  106.         tar xzf "${CURRENT_DIR}/${LLVM_SOURCES_FILE}" || exit 1
  107.         if [ ! -d "${LLVM_SOURCES_DIR}" ]; then
  108.                 echo "Error: couldn't find ${LLVM_SOURCES_DIR} in extracted LLVM sources."
  109.                 exit 1
  110.         fi
  111. fi
  112.  
  113. # create the build directory
  114. echo "Wiping out build directory..."
  115. test -e "${BUILD_DIR_NAME}" && rm -rf "${BUILD_DIR_NAME}"
  116. mkdir "${BUILD_DIR_NAME}" || exit 1
  117. cd "${BUILD_DIR_NAME}" || exit 1
  118.  
  119. # print the build environment
  120. echo "QNX_HOST=${QNX_HOST}"
  121. echo "QNX_TARGET=${QNX_TARGET}"
  122. echo "MAKEFLAGS=${MAKEFLAGS}"
  123.  
  124. # create the QNX CMake toolchain file
  125. echo '# '${TARGET_TRIPLE}' CMake toolchain file by Pierre-Marie Baty <pm@pmbaty.com>
  126.  
  127. SET(CMAKE_SYSTEM_NAME "QNX")
  128. SET(CMAKE_SYSTEM_VERSION "8.0.0")
  129.  
  130. SET(QNX "1")
  131. SET(QNXNTO "1")
  132. if ("$ENV{QNX_HOST}" STREQUAL "")
  133.         message(FATAL_ERROR "the QNX_HOST environment variable is not set")
  134. endif()
  135. SET(QNX_HOST "$ENV{QNX_HOST}")
  136. if ("$ENV{QNX_TARGET}" STREQUAL "")
  137.         message(FATAL_ERROR "the QNX_TARGET environment variable is not set")
  138. endif()
  139. SET(QNX_TARGET "$ENV{QNX_TARGET}")
  140. SET(QNX_PROCESSOR "'${TARGET_ARCH}'")
  141.  
  142. SET(CMAKE_ASM_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
  143. SET(CMAKE_ASM_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  144.  
  145. SET(CMAKE_C_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
  146. SET(CMAKE_C_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  147. SET(CMAKE_C_FLAGS_DEBUG "-g")
  148. SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
  149. SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
  150. SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
  151. 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")
  152.  
  153. SET(CMAKE_CXX_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-g++")
  154. SET(CMAKE_CXX_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  155. SET(CMAKE_CXX_FLAGS_DEBUG "-g")
  156. SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
  157. SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
  158. SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
  159. 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")
  160.  
  161. SET(CMAKE_LINKER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ld.bfd")
  162. SET(CMAKE_SHARED_LINKER_FLAGS "-lsocket")
  163. SET(CMAKE_EXE_LINKER_FLAGS "-lsocket")
  164.  
  165. SET(CMAKE_RANLIB "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ranlib")
  166.  
  167. SET(CMAKE_AR "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ar")
  168.  
  169. SET(CMAKE_FIND_ROOT_PATH "${QNX_TARGET}")
  170. SET(CMAKE_FIND_ROOT_PATH_HOST_PROGRAM NEVER)
  171. SET(CMAKE_FIND_ROOT_PATH_HOST_LIBRARY ONLY)
  172. SET(CMAKE_FIND_ROOT_PATH_HOST_INCLUDE ONLY)
  173. ' > "${TARGET_TRIPLE}.cmake"
  174.  
  175. # patch llvm/Support/Unix/Path.inc if not done yet
  176. if ! grep -q __QNXNTO__ "../${LLVM_SOURCES_DIR}/llvm/lib/Support/Unix/Path.inc"; then
  177.         echo "Patching llvm/lib/Support/Unix/Path.inc..."
  178.  
  179.         # have a backup first
  180.         test -f "${CURRENT_DIR}/Path [BEFORE PATCH].inc" || cp "../${LLVM_SOURCES_DIR}/llvm/lib/Support/Unix/Path.inc" "${CURRENT_DIR}/Path [BEFORE PATCH].inc"
  181.  
  182.         # replace "defined(__FreeBSD_kernel__)" with "(defined(__FreeBSD_kernel__) || defined(__QNXNTO__))"
  183.         # 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.
  184.         # 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,
  185.         # 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).
  186.         # 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
  187.         # 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.
  188.         # 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.
  189.         # FIXME: these hacks should be moved elsewhere not to pollute the QNX8 platform SDK -- it *SHOULD* be possible to build without devs
  190.         sed 's/defined(__FreeBSD_kernel__)/(defined(__FreeBSD_kernel__) || defined(__QNXNTO__))/g' "${CURRENT_DIR}/Path [BEFORE PATCH].inc" > "${CURRENT_DIR}/Path [AFTER PATCH].inc"
  191.  
  192.         # verify that we did it successfully
  193.         if ! grep -q __QNXNTO__ "${CURRENT_DIR}/Path [AFTER PATCH].inc"; then
  194.                 echo "Error: the LLVM support library source file llvm/Support/Unix/Path.inc could not be patched to support QNX neutrino. Please investigate and fix manually." | fold -s -w 79
  195.                 exit 1
  196.         fi
  197.  
  198.         # and put the patched file in place
  199.         cp -f "${CURRENT_DIR}/Path [AFTER PATCH].inc" "../${LLVM_SOURCES_DIR}/llvm/lib/Support/Unix/Path.inc" || exit 1
  200. fi
  201.  
  202. # now configure LLVM
  203. echo "Configuring LLVM build..."
  204. cmake \
  205.     -D CMAKE_TOOLCHAIN_FILE="${TARGET_TRIPLE}.cmake" \
  206.     -D CMAKE_BUILD_TYPE="MinSizeRel" \
  207.     -D CMAKE_INSTALL_PREFIX="/tmp/qnxsdk/host/qnx8" \
  208.     -D CMAKE_STAGING_PREFIX="/usr/bin" \
  209.     -D LLVM_HOST_TRIPLE="${TARGET_TRIPLE}" \
  210.     -D LLVM_ENABLE_PROJECTS="clang;lld;lldb" \
  211.     -D LLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi" \
  212.     -D LLVM_TARGETS_TO_BUILD="AArch64;X86" \
  213.     -G Ninja \
  214.     "../${LLVM_SOURCES_DIR}/llvm" || exit 1
  215.  
  216. # hijack the "bin" and "lib/clang" output directories and redirect them to the hypervisor's filesystem
  217. test -e "${CURRENT_DIR}/llvm-build" && rm -rf "${CURRENT_DIR}/llvm-build"
  218. mkdir -p "${CURRENT_DIR}/llvm-build"
  219. test -d bin && mv bin "${CURRENT_DIR}/llvm-build/bin" || mkdir "${CURRENT_DIR}/llvm-build/bin"
  220. ln -s "${CURRENT_DIR}/llvm-build/bin" bin
  221. mkdir -p "${CURRENT_DIR}/llvm-build/lib"
  222. test -d lib/clang && mv lib/clang "${CURRENT_DIR}/llvm-build/lib/clang" || mkdir "${CURRENT_DIR}/llvm-build/lib/clang"
  223. ln -s "${CURRENT_DIR}/llvm-build/lib/clang" lib/clang
  224.  
  225. # and do the Lord's work. https://youtu.be/jcyYmCnkbEE
  226. echo "Building LLVM..."
  227. cmake --build . || exit 1
  228.  
  229. echo "Champagne, James. Champagne."
  230. exit 0
  231.