#!/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 GNU binutils sources will be built
export BUILD_TARGET_ARCHS="x86_64 aarch64" # space-separated list of CPU architectures to build the GNU binutils for, among "x86_64" and "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
export REQUIRED_TOOLS="wget python3 gcc g++ make m4" # list of build tools required to build all this on the build host
# see where we are
export CURRENT_DIR="$(pwd)"
print_error_and_die()
{
# a handy function that does what it says. Args: error strings to print, one line per argument
test -z "${COLUMNS}" && COLUMNS=80
echo ""
while [ -n "${1}" ]; do
echo "${1}"|fold -s -w "${COLUMNS}"
shift
done
echo ""
exit 1
}
# verify we're a x86_64 Linux host
if [ ! "$(uname)" = "Linux" ] || [ ! "$(uname -m)" = "x86_64" ]; then
print_error_and_die "Error: this script requires a x86_64 Linux machine (possibly a virtual machine) as the build host."
fi
# verify that we have the required tools
for REQUIRED_TOOL in ${REQUIRED_TOOLS}; do
"${REQUIRED_TOOL}" --version > /dev/null 2>&1 || print_error_and_die \
"Error: this script requires at the very least the following tools installed:" \
" $(echo "${REQUIRED_TOOLS}"|sed 's/ /\n /g')" \
"Please install them (possibly as binary packages with apt-get) and try again." \
"More specifically, the following tool was not found: '${REQUIRED_TOOL}'"
done
# verify that we have the QNX platform SDK
if [ ! -d "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" ]; then
print_error_and_die "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."
fi
# change to an immediately visible path, i.e. the user's desktop (failsafe to $HOME if xdg-user-dir is unavailable or points to a nonexistent directory)
STAGING_PATH="$(xdg-user-dir DESKTOP 2>/dev/null || echo "${HOME}")"
test -d "${STAGING_PATH}" || STAGING_PATH="${HOME}"
cd "${STAGING_PATH}"
# are we running the Windows Subsystem for Linux, instead of a real Linux ?
if [ -n "${WSL_DISTRO_NAME}" ]; then
# yes. In order to avoid the horrible 9p protocol for file interchange between NTFS and ext4 (a remarkably bad idea by Microsoft),
# use the copy of the QNX SDP that our caller is supposed to have put in our $HOME in the WSL2 filesystem. The speed gain is considerable.
QNXSDK_CANONICAL_PATH="${HOME}/$(basename "${QNXSDK_PATH}")"
if [ ! -d "${QNXSDK_CANONICAL_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_CANONICAL_PATH}/${QNXSDK_TARGETPATH}" ]; then
print_error_and_die "Error: the QNX SDP hasn't been deployed to ${QNXSDK_CANONICAL_PATH}. Please do so and run this script again."
fi
else
# we're not in WSL. We can expect reasonably good file access speeds to the QNX SDP that's bundled with this repository, so just use it.
QNXSDK_CANONICAL_PATH="$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")"
fi
# verify that the symlinks are deployed in the SDK -- just test one of them in each relevant directory ($QNX_HOST for the tools, $QNX_TARGET for the sysroot)
if [ ! -e "${QNXSDK_CANONICAL_PATH}/${QNXSDK_HOSTPATH}/usr/bin/ntox86_64-gcc" ] || [ ! -e "${QNXSDK_CANONICAL_PATH}/${QNXSDK_TARGETPATH}/usr/include/readline.h" ]; then
print_error_and_die \
"Error: the toolchain platform-specific symbolic links have not been deployed in this QNX SDK. Please run" \
"(on a POSIX machine:)" \
" cd ${QNXSDK_CANONICAL_PATH}" \
" find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \
"(else on a Windows machine:)" \
" cd ${QNXSDK_CANONICAL_PATH}" \
" host\\win64\\x86_64\\usr\\bin\\busybox.exe sh -c \"find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state\"" \
"Note that this step WILL take time on a Win32 machine, but is only done once."
fi
# 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)" = "${QNXSDK_CANONICAL_PATH}" ]; then
echo "Creating symlink to QNX toolchain in /tmp/qnxsdk..."
rm -rf /tmp/qnxsdk 2>/dev/null
ln -fs "${QNXSDK_CANONICAL_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}"
# print the build environment
echo "QNX_HOST=${QNX_HOST}"
echo "QNX_TARGET=${QNX_TARGET}"
echo "MAKEFLAGS=${MAKEFLAGS}"
# 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
# change directory to the binutils sources
cd "${BINUTILS_SOURCES_DIR}" || exit 1
backup_and_patch_if_necessary()
{
# handy function that patches a file in the current directory with a given sed replacement regex if necessary, creating backups
# args: <file pathname> <grep_string_to_test_for_presence_of_patch> <sed regex>
_PATCHEE_PATHNAME="${1}"
_PATCHED_PATTERN="${2}"
# test if already patched
grep -q "${_PATCHED_PATTERN}" "${_PATCHEE_PATHNAME}" && return
# tell what we're about to do
echo "Patching ${_PATCHEE_PATHNAME}..."
_DOTTED_NAME="$(echo "${_PATCHEE_PATHNAME}"|tr '/' '.')"
# have a backup first
test -f "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" || cp "${_PATCHEE_PATHNAME}" "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" || exit 1
# perform the patch
cp -f "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]" || exit 1
while [ -n "${3}" ]; do
sed -E -i "${3}" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]"
shift
done
# verify that we did it successfully
if ! grep -q "${_PATCHED_PATTERN}" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]"; then
echo "Error: the file ${_PATCHEE_PATHNAME} 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}/${_DOTTED_NAME} [PATCHED]" "${_PATCHEE_PATHNAME}" || exit 1
}
backup_and_replace()
{
# handy function that replaces a file in the current directory 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
# replace:
# defined(HAVE_SPAWNVE)
# with:
# !defined(__QNXNTO__) && defined(HAVE_SPAWNVE)
# 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, which QNX supports as well.
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 GNU linker
backup_and_replace "ld/configure.tgt" "${CURRENT_DIR}/ld.configure.tgt"
# patch ld/emulparams/aarch64nto.sh
# RATIONALE: insert aarch64 QNX particularities for the GNU linker
backup_and_replace "ld/emulparams/aarch64nto.sh" "${CURRENT_DIR}/ld.emulparams.aarch64nto.sh"
# patch ld/emulparams/elf_x86_64nto.sh
# RATIONALE: insert aarch64 QNX particularities for the GNU linker
backup_and_replace "ld/emulparams/elf_x86_64.sh" "${CURRENT_DIR}/ld.emulparams.elf_x86_64.sh"
# patch ld/emulparams/i386nto.sh
# RATIONALE: insert i386 QNX particularities for the GNU linker
backup_and_replace "ld/emulparams/i386nto.sh" "${CURRENT_DIR}/ld.emulparams.i386nto.sh"
# patch ld/emultempl/nto.em
# RATIONALE: insert i386 QNX particularities for the GNU linker
backup_and_replace "ld/emultempl/nto.em" "${CURRENT_DIR}/ld.emultempl.nto.em"
# 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}"
# now, for each target arch the native binutils should support...
for BUILD_TARGET_ARCH in ${BUILD_TARGET_ARCHS}; do
# construct the target triple (actually a quadruple)
TARGET_ARCH="${BUILD_TARGET_ARCH}"
test "${BUILD_TARGET_ARCH}" = "x86_64" && TARGET_VENDOR="pc" || TARGET_VENDOR="unknown"
TARGET_KERNEL="nto"
TARGET_SYSTEM="qnx${QNXSDK_VERSION}"
export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}"
echo "Will build for ${TARGET_TRIPLE}"
# create the build directory
echo "Wiping out build directory..."
test -e ".build/${TARGET_TRIPLE}" && rm -rf ".build/${TARGET_TRIPLE}"
mkdir -p ".build/${TARGET_TRIPLE}" || exit 1
ln -fs "${BINUTILS_SOURCES_DIR}/.build" "../${BUILD_DIR_NAME}"
cd ".build/${TARGET_TRIPLE}"
# configure and build the GNU binutils. Note that since these are low level tools, we want static binaries as much as possible.
# NOTE: in this context, --host means the machine the built program is expected to RUN on, and --target the machine the built program will FOCUS on.
../../configure \
--with-sysroot="${QNX_TARGET}" \
--prefix="$(dirname "$(dirname "${QNX_HOST}")")/qnx$(echo "${QNXSDK_VERSION}"|awk -F. '{print $1}')/${BUILD_TARGET_ARCH}/usr" \
--host="x86_64-pc-nto-qnx${QNXSDK_VERSION}" \
--target="${TARGET_TRIPLE}" \
--disable-shared \
--disable-nls \
--enable-static-link \
--disable-shared-plugins \
--disable-dynamicplugin \
--disable-pie \
--enable-static=yes \
--enable-shared=no \
--enable-threads \
--disable-bootstrap \
--disable-doc \
--with-bugurl="pm@pmbaty.com" \
|| 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" || mkdir -p "${CURRENT_DIR}/binutils-build"
rm "${CURRENT_DIR}/binutils-build/${TARGET_TRIPLE}-"* 2>/dev/null
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
# return to the sources dir and proceed to the next arch
cd ../..
done
test -z "${WSL_DISTRO_NAME}" && /bin/printf "\n\xf0\x9f\x8d\xba\x20\x43\x68\x65\x65\x72\x73\x2e\n"
exit 0