- #!/bin/sh 
- # GNU binutils toolchain cross-compilation script for QNX 8.0 by Pierre-Marie Baty <pm@pmbaty.com> 
-   
- # NOTE TO SELF: DO NOT USE $0 AS THIS SCRIPT CAN BE RUN *OR* SOURCED! (see build-llvm.sh in the VM) 
-   
- export QNXSDK_VERSION="8.0.0" # version of the QNX SDK in use, in <major>.<minor>.<revision> format 
- export QNXSDK_PATH="../qnx800" # relative location from the path of this script where to find the QNX platform SDK 
- 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 
- export QNXSDK_TARGETPATH="target/qnx" # relative location in QNXSDK_PATH of the tree containing the QNX8 system header files 
-   
- export BUILD_DIR_NAME="binutils-build" # name of the directory on the build host's desktop where the GCC sources will be built 
- export BUILD_TARGET_ARCH="x86_64" # CPU architecture to build GCC for, either "x86_64" or "aarch64" 
-   
- export BINUTILS_VERSION="2.41" # version of GNU binutils that will be built. Should match the Win32 and Linux tools. 
-   
- export BINUTILS_SOURCES_FILE="binutils-${BINUTILS_VERSION}.tar.gz" # name of the file containing GNU binutils version BINUTILS_VERSION sources 
- export BINUTILS_SOURCES_URL="https://ftp.gnu.org/gnu/binutils/${BINUTILS_SOURCES_FILE}" # download URL of BINUTILS_SOURCES_FILE 
- export BINUTILS_SOURCES_DIR="binutils-${BINUTILS_VERSION}" # name of directory created when extracting BINUTILS_SOURCES_FILE 
-   
- # 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." | fold -s -w 79 
-         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 '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 
-         echo "" 
-         exit 1 
- fi 
-   
- # verify that we have wget, python3, cmake, gcc, g++ and m4 
- 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 \ 
-    || ! m4      --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 "  m4" 
-         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 this QNX SDK. Please run" | fold -s -w 79 
-         echo "(on a POSIX machine:)" 
-         echo "  cd ${QNXSDK_PATH}" | fold -s -w 79 
-         echo "  find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" | fold -s -w 79 
-         echo "(else on a Windows machine:)" 
-         echo "  cd ${QNXSDK_PATH}" | fold -s -w 79 
-         echo "  host\\win64\\x86_64\\usr\\bin\\busybox.exe sh -c \"" \ 
-                "find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \ 
-                "\"" | fold -s -w 79 
-         echo "Note that this step WILL take time on a Win32 machine, but is only done once." | fold -s -w 79 
-         echo "" 
-         exit 1 
- fi 
-   
- # construct the target triple (actually a quadruple) 
- export TARGET_ARCH="${BUILD_TARGET_ARCH}" 
- test "${BUILD_TARGET_ARCH}" = "x86_64" && export TARGET_VENDOR="pc" || export TARGET_VENDOR="unknown" 
- export TARGET_KERNEL="nto" 
- export TARGET_SYSTEM="qnx${QNXSDK_VERSION}" 
- export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}" 
- echo "Will build for ${TARGET_TRIPLE}" 
-   
- # change to an immediately visible path, i.e. the user's desktop (failsafe to $HOME if xdg-user-dir is unavailable) 
- STAGING_PATH="$(xdg-user-dir DESKTOP 2>/dev/null || echo "${HOME}")" 
- cd "${STAGING_PATH}" 
-   
- # 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 
- # (this is totally prohibitive with the official QNX toolchain) 
- if [ ! -L /tmp/qnxsdk ] || [ ! "$(readlink /tmp/qnxsdk)" = "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" ]; then 
-         echo "Creating symlink to QNX toolchain in /tmp/qnxsdk..." 
-         rm -rf /tmp/qnxsdk 2>/dev/null 
-         ln -fs "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" /tmp/qnxsdk || exit 1 
- fi 
-   
- # 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}" 
-   
- # download the involved source packages and unpack them if not done yet 
- download_and_unpack_if_necessary() 
- { 
-         # helper function that downloads a sources tarball, extracts it and patches it if necessary 
-         # args: <sources dirname> <sources filename> <download URL> [optional patchset URL] 
-         test -d "${1}" && return 0 # if sources directory already exists, nothing to do 
-         if [ ! -s "${CURRENT_DIR}/${2}" ]; then # if sources archive isn't a nonempty file... 
-                 echo "Downloading ${1} sources from ${3}..." 
-                 if ! wget -O "${CURRENT_DIR}/${2}" "${3}"; then 
-                         # remove output file in case an error occurs 
-                         rm -f "${CURRENT_DIR}/${2}" 
-                         exit 1 
-                 fi 
-         fi 
-         echo "Extracting ${1} sources..." 
-         cd "$(dirname "${1}")" 
-         if echo "${2}"|grep -q "\.tar\.bz2$"; then 
-                 # BZip2 tarball 
-                 tar xjf "${CURRENT_DIR}/${2}" || exit 1 
-         elif echo "${2}"|grep -q "\.tar\.xz$"; then 
-                 # XZ tarball 
-                 tar xJf "${CURRENT_DIR}/${2}" || exit 1 
-         elif echo "${2}"|grep -q "\.tar\.gz$"; then 
-                 # GZipped tarball 
-                 tar xzf "${CURRENT_DIR}/${2}" || exit 1 
-         else 
-                 echo "Error: unsupported file extension. Please improve the download_and_unpack_if_necessary() shell function to support it." 
-                 exit 1 
-         fi 
-         # make sure the expected directory is here after extraction 
-         if [ ! -d "${1}" ]; then 
-                 echo "Error: couldn't find ${1} in extracted sources." 
-                 exit 1 
-         fi 
-         # do we have a patchset to apply ? 
-         if [ -n "${4}" ]; then 
-                 echo "Downloading ${1} patchset from ${4}..." 
-                 wget -O "${CURRENT_DIR}/${2}.patchset" "${4}" || exit 1 
-                 echo "Applying patchset..." 
-                 OLDDIR="$(pwd)" 
-                 cd "${1}" 
-                 patch -N -Z -p1 < "${CURRENT_DIR}/${2}.patchset" || exit 1 
-                 cd "${OLDDIR}" 
-                 unset OLDDIR 
-         fi 
-         return 0 
- } 
- download_and_unpack_if_necessary "${BINUTILS_SOURCES_DIR}" "${BINUTILS_SOURCES_FILE}" "${BINUTILS_SOURCES_URL}" || exit 1 
-   
- # print the build environment 
- echo "QNX_HOST=${QNX_HOST}" 
- echo "QNX_TARGET=${QNX_TARGET}" 
- echo "MAKEFLAGS=${MAKEFLAGS}" 
-   
- # change directory to the binutils sources 
- cd "${BINUTILS_SOURCES_DIR}" 
-   
- # create the build directory 
- echo "Wiping out build directory..." 
- test -e .build && rm -rf .build 
- mkdir .build || exit 1 
- ln -fs "${BINUTILS_SOURCES_DIR}/.build" "../${BUILD_DIR_NAME}" 
- cd .build 
-   
- backup_and_patch_if_necessary() 
- { 
-         # handy function that patches a file in .. with a given sed replacement regex if necessary, creating backups 
-         # args: <file pathname> <grep_string_to_test_for_presence_of_patch> <sed regex> 
-         # test if already patched 
-         grep -q "${2}" "../${1}" && return 
-         # tell what we're about to do 
-         echo "Patching ${1}..." 
-         # have a backup first 
-         test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || cp "../${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || exit 1 
-         # perform the patch 
-         sed -E "${3}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" > "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]" 
-         # verify that we did it successfully 
-         if ! grep -q "${2}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]"; then 
-                 echo "Error: the file ${1} could not be patched. Please investigate and fix manually." | fold -s -w 79; exit 1 
-         fi 
-         # and put the patched file in place 
-         cp -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]" "../${1}" || exit 1 
- } 
- backup_and_replace() 
- { 
-         # handy function that replaces a file in .. with a given replacement, creating backups 
-         # args: <file pathname> <replacement pathname> 
-         # tell what we're about to do 
-         echo "Replacing ${1}..." 
-         # have a backup first 
-         test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || test -e "../${1}" && cp "../${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" 
-         # perform the replacement 
-         cp -f "${2}" "../${1}" || exit 1 
- } 
-   
- # patch libiberty/pex-unix.c 
- # RATIONALE: QNX exposes the same spawnve() functions which were invented by Microsoft. Autoconf mistakenly believes it's a Cygwin system. 
- # Moreover the flags _P_NOWAIT and _P_NOWAITO don't have the same meaning on QNX: the second one doesn't create zombies waiting for their exit 
- # status to be read, whereas on Microsoft they're the same. This prevents process created with this flag (as libiberty does) from being wait()ed. 
- # Fix that by declaring that we want to use the traditional POSIX fork/exec scheme. 
- backup_and_patch_if_necessary "libiberty/pex-unix.c" __QNXNTO__ 's/defined\(HAVE_SPAWNVE\)/\!defined\(__QNXNTO__\) \&\& defined\(HAVE_SPAWNVE\)/g' 
-   
- # patch bfd/config.bfd 
- # RATIONALE: insert x86_64 and aarch64 QNX definitions for the BFD library 
- backup_and_replace "bfd/config.bfd" "${CURRENT_DIR}/bfd.config.bfd" 
-   
- # patch ld/configure.tgt 
- # RATIONALE: insert x86_64 and aarch64 QNX definitions for the classic linker 
- backup_and_replace "ld/configure.tgt" "${CURRENT_DIR}/ld.configure.tgt" 
-   
- # define the tools to use when compiling intermediary programs on the build host 
- export AR_FOR_BUILD="/usr/bin/ar" 
- export AS_FOR_BUILD="/usr/bin/as" 
- export LD_FOR_BUILD="/usr/bin/ld" 
- export NM_FOR_BUILD="/usr/bin/nm" 
- export RANLIB_FOR_BUILD="/usr/bin/ranlib" 
- export CC_FOR_BUILD="/usr/bin/gcc"; CFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}" 
- export CXX_FOR_BUILD="/usr/bin/g++"; CXXFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}" 
-   
- # configure and build the GNU binutils. Note that since these are low level tools, we want static binaries as much as possible. 
- ../configure \ 
-     --prefix="$(dirname "$(dirname "${QNX_HOST}")")/qnx$(echo "${QNXSDK_VERSION}"|awk -F. '{print $1}')/${BUILD_TARGET_ARCH}/usr" \ 
-     --host="${TARGET_TRIPLE}" \ 
-     --target="${TARGET_TRIPLE}" \ 
-     --disable-shared \ 
-     --disable-nls \ 
-     --enable-static-link \ 
-     --disable-shared-plugins \ 
-     --disable-dynamicplugin \ 
-     --disable-pie \ 
-     --enable-static=yes \ 
-     --enable-shared=no \ 
-     --disable-doc || exit 1 
-   
- # and build the whole shit 
- make -j 4 MAKEINFO=/bin/true || exit 1 
-   
- # now gather all the binutils in one place on the hypervisor's filesystem and name them appropriately 
- test -e "${CURRENT_DIR}/binutils-build" && rm -rf "${CURRENT_DIR}/binutils-build" 
- mkdir -p "${CURRENT_DIR}/binutils-build" 
- VERSION="$(grep PACKAGE_VERSION opcodes/config.h|awk -F'"' '{print $2}')" 
- VERSION_MAJ="$(echo "${VERSION}"|cut -d . -f 1)"; test -z "${VERSION_MAJ}" && VERSION_MAJ="0" 
- VERSION_MIN="$(echo "${VERSION}"|cut -d . -f 2)"; test -z "${VERSION_MIN}" && VERSION_MIN="0" 
- VERSION_REV="$(echo "${VERSION}"|cut -d . -f 3)"; test -z "${VERSION_REV}" && VERSION_REV="0" 
- for TOOL in binutils/addr2line:addr2line \ 
-             binutils/ar:ar \ 
-             gas/as-new:as \ 
-             binutils/cxxfilt:c++filt \ 
-             binutils/elfedit:elfedit \ 
-             gprof/gprof:gprof \ 
-             ld/ld-new:ld.bfd \ 
-             binutils/nm-new:nm \ 
-             binutils/objcopy:objcopy \ 
-             binutils/objdump:objdump \ 
-             binutils/ranlib:ranlib \ 
-             binutils/readelf:readelf \ 
-             binutils/size:size \ 
-             binutils/strings:strings \ 
-             binutils/strip-new:strip \ 
-         ; do 
-         SRC="$(echo "${TOOL}"|cut -d : -f 1)" 
-         TGT="$(echo "${TOOL}"|cut -d : -f 2)" 
-         cp "${SRC}" "${CURRENT_DIR}/binutils-build/${TARGET_TRIPLE}-${TGT}-${VERSION_MAJ}.${VERSION_MIN}.${VERSION_REV}" || exit 1 
- done 
-   
- echo "Tea time." 
- exit 0 
-