Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Rev 3 | Rev 5 | 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."
  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"
  37.         echo "'host' and 'target' directories of the QNX SDP for the targeted version of QNX"
  38.         echo "and the ${TARGET_ARCH} platform. Please deploy these directories and try again."
  39.         echo ""
  40.         exit 1
  41. fi
  42.  
  43. # verify that we have wget, python3, cmake, gcc, g++ and ninja
  44. if    ! wget    --version > /dev/null 2>&1 \
  45.    || ! python3 --version > /dev/null 2>&1 \
  46.    || ! cmake   --version > /dev/null 2>&1 \
  47.    || ! gcc     --version > /dev/null 2>&1 \
  48.    || ! g++     --version > /dev/null 2>&1 \
  49.    || ! ninja   --version > /dev/null 2>&1; then
  50.         echo ""
  51.         echo "Error: this script requires at the very least the following tools installed:"
  52.         echo "  wget"
  53.         echo "  python3"
  54.         echo "  cmake"
  55.         echo "  gcc"
  56.         echo "  g++"
  57.         echo "  ninja"
  58.         echo "Please install them (possibly as binary packages with apt-get) and try again."
  59.         echo ""
  60.         exit 1
  61. fi
  62.  
  63. # verify that the symlinks are deployed in the SDK -- just test one of them
  64. if [ ! -e "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}/usr/include/readline.h" ]; then
  65.         echo ""
  66.         echo "Error: the toolchain platform-specific symbolic links have not been deployed in"
  67.         echo "this QNX SDK. Please run"
  68.         echo "(on a POSIX machine:)"
  69.         echo "  cd ${QNXSDK_PATH}"
  70.         echo "  find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state"
  71.         echo "(else on a Windows machine:)"
  72.         echo "  cd ${QNXSDK_PATH}"
  73.         echo "  host\\win64\\x86_64\\usr\\bin\\busybox.exe sh -c \"" \
  74.                "find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \
  75.                "\""
  76.         echo "Note that this step WILL take time on a Win32 machine, but is only done once."
  77.         echo ""
  78.         exit 1
  79. fi
  80.  
  81. # construct the target triple (actually a quadruple)
  82. export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}"
  83. echo "Will build LLVM for ${TARGET_TRIPLE}"
  84.  
  85. # change to an immediately visible path
  86. cd ~/Desktop
  87.  
  88. # create a symlink in /tmp that will lead to the QNX platform SDK so as to avoid spaces in paths
  89. # (this is totally prohibitive with the official QNX toolchain)
  90. test -L /tmp/qnxsdk && rm /tmp/qnxsdk
  91. ln -fs "${CURRENT_DIR}/${QNXSDK_PATH}" /tmp/qnxsdk
  92.  
  93. # setup the environment
  94. export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}"
  95. export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}"
  96. export MAKEFLAGS="-I${QNX_TARGET}/usr/include"
  97. export PATH="${QNX_HOST}/usr/bin:${PATH}"
  98. #export PYTHONDONTWRITEBYTECODE="1"
  99. #export PYTHONPATH=""
  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. 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 "8.0.0")
  132.  
  133. SET(QNX "1")
  134. SET(QNXNTO "1")
  135. if ("$ENV{QNX_HOST}" STREQUAL "")
  136.         message(FATAL_ERROR "the QNX_HOST environment variable is not set")
  137. endif()
  138. SET(QNX_HOST "$ENV{QNX_HOST}")
  139. if ("$ENV{QNX_TARGET}" STREQUAL "")
  140.         message(FATAL_ERROR "the QNX_TARGET environment variable is not set")
  141. endif()
  142. SET(QNX_TARGET "$ENV{QNX_TARGET}")
  143. SET(QNX_PROCESSOR "'${TARGET_ARCH}'")
  144.  
  145. SET(CMAKE_ASM_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
  146. SET(CMAKE_ASM_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  147.  
  148. SET(CMAKE_C_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc")
  149. SET(CMAKE_C_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  150. SET(CMAKE_C_FLAGS_DEBUG "-g")
  151. SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
  152. SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
  153. SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
  154. 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")
  155.  
  156. SET(CMAKE_CXX_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-g++")
  157. SET(CMAKE_CXX_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}")
  158. SET(CMAKE_CXX_FLAGS_DEBUG "-g")
  159. SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
  160. SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
  161. SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
  162. 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")
  163.  
  164. SET(CMAKE_LINKER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ld.bfd")
  165. SET(CMAKE_SHARED_LINKER_FLAGS "-lsocket")
  166. SET(CMAKE_EXE_LINKER_FLAGS "-lsocket")
  167.  
  168. SET(CMAKE_RANLIB "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ranlib")
  169.  
  170. SET(CMAKE_AR "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ar")
  171.  
  172. SET(CMAKE_FIND_ROOT_PATH "${QNX_TARGET}")
  173. SET(CMAKE_FIND_ROOT_PATH_HOST_PROGRAM NEVER)
  174. SET(CMAKE_FIND_ROOT_PATH_HOST_LIBRARY ONLY)
  175. SET(CMAKE_FIND_ROOT_PATH_HOST_INCLUDE ONLY)
  176. ' > "${TARGET_TRIPLE}.cmake"
  177.  
  178. # now configure LLVM
  179. echo "Configuring LLVM build..."
  180. cmake \
  181.     -D CMAKE_TOOLCHAIN_FILE="${TARGET_TRIPLE}.cmake" \
  182.     -D CMAKE_BUILD_TYPE="MinSizeRel" \
  183.     -D CMAKE_INSTALL_PREFIX="/tmp/qnxsdk/host/qnx8" \
  184.     -D CMAKE_STAGING_PREFIX="/usr/bin" \
  185.     -D LLVM_HOST_TRIPLE="${TARGET_TRIPLE}" \
  186.     -D LLVM_ENABLE_PROJECTS="clang;lld;lldb" \
  187.     -D LLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi" \
  188.     -D LLVM_TARGETS_TO_BUILD="AArch64;X86" \
  189.     -G Ninja \
  190.     "../${LLVM_SOURCES_DIR}/llvm" || exit 1
  191.  
  192. # hijack the "bin" and "lib/clang" output directories and redirect them to the hypervisor's filesystem
  193. test -e "${CURRENT_DIR}/llvm-build" && rm -rf "${CURRENT_DIR}/llvm-build"
  194. mkdir -p "${CURRENT_DIR}/llvm-build"
  195. test -d bin && mv bin "${CURRENT_DIR}/llvm-build/bin" || mkdir "${CURRENT_DIR}/llvm-build/bin"
  196. ln -s "${CURRENT_DIR}/llvm-build/bin" bin
  197. mkdir -p "${CURRENT_DIR}/llvm-build/lib"
  198. test -d lib/clang && mv lib/clang "${CURRENT_DIR}/llvm-build/lib/clang" || mkdir "${CURRENT_DIR}/llvm-build/lib/clang"
  199. ln -s "${CURRENT_DIR}/llvm-build/lib/clang" lib/clang
  200.  
  201. # and do the Lord's work. https://youtu.be/jcyYmCnkbEE
  202. echo "Building LLVM..."
  203. cmake --build . || exit 1
  204.  
  205. echo "Champagne, James. Champagne."
  206. exit 0
  207.