Rev 2 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | #!/bin/sh |
| 2 | # GNU binutils 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="binutils-build" # name of the directory on the build host's desktop where the GCC sources will be built |
||
| 12 | export BUILD_TARGET_ARCH="x86_64" # CPU architecture to build GCC for, either "x86_64" or "aarch64" |
||
| 13 | |||
| 14 | export BINUTILS_VERSION="2.41" # version of GNU binutils that will be built. Should match the Win32 and Linux tools. |
||
| 15 | |||
| 16 | export BINUTILS_SOURCES_FILE="binutils-${BINUTILS_VERSION}.tar.gz" # name of the file containing GNU binutils version BINUTILS_VERSION sources |
||
| 17 | export BINUTILS_SOURCES_URL="https://ftp.gnu.org/gnu/binutils/${BINUTILS_SOURCES_FILE}" # download URL of BINUTILS_SOURCES_FILE |
||
| 18 | export BINUTILS_SOURCES_DIR="binutils-${BINUTILS_VERSION}" # name of directory created when extracting BINUTILS_SOURCES_FILE |
||
| 19 | |||
| 20 | # see where we are |
||
| 21 | export CURRENT_DIR="$(pwd)" |
||
| 22 | |||
| 23 | # verify we're a x86_64 Linux host |
||
| 24 | if [ ! "$(uname)" = "Linux" ] || [ ! "$(uname -m)" = "x86_64" ]; then |
||
| 25 | echo "" |
||
| 26 | echo "Error: this script requires a x86_64 Linux machine (possibly a virtual machine) as the build host." | fold -s -w 79 |
||
| 27 | echo "" |
||
| 28 | exit 1 |
||
| 29 | fi |
||
| 30 | |||
| 31 | # verify that we have the QNX platform SDK |
||
| 32 | if [ ! -d "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" ]; then |
||
| 33 | echo "" |
||
| 34 | 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 |
||
| 35 | echo "" |
||
| 36 | exit 1 |
||
| 37 | fi |
||
| 38 | |||
| 39 | # verify that we have wget, python3, cmake, gcc, g++ and m4 |
||
| 40 | if ! wget --version > /dev/null 2>&1 \ |
||
| 41 | || ! python3 --version > /dev/null 2>&1 \ |
||
| 42 | || ! cmake --version > /dev/null 2>&1 \ |
||
| 43 | || ! gcc --version > /dev/null 2>&1 \ |
||
| 44 | || ! g++ --version > /dev/null 2>&1 \ |
||
| 45 | || ! m4 --version > /dev/null 2>&1; then |
||
| 46 | echo "" |
||
| 47 | echo "Error: this script requires at the very least the following tools installed:" |
||
| 48 | echo " wget" |
||
| 49 | echo " python3" |
||
| 50 | echo " cmake" |
||
| 51 | echo " gcc" |
||
| 52 | echo " g++" |
||
| 53 | echo " m4" |
||
| 54 | echo "Please install them (possibly as binary packages with apt-get) and try again." |
||
| 55 | echo "" |
||
| 56 | exit 1 |
||
| 57 | fi |
||
| 58 | |||
| 59 | # verify that the symlinks are deployed in the SDK -- just test one of them |
||
| 60 | if [ ! -e "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}/usr/include/readline.h" ]; then |
||
| 61 | echo "" |
||
| 62 | echo "Error: the toolchain platform-specific symbolic links have not been deployed in this QNX SDK. Please run" | fold -s -w 79 |
||
| 63 | echo "(on a POSIX machine:)" |
||
| 64 | echo " cd ${QNXSDK_PATH}" | fold -s -w 79 |
||
| 65 | echo " find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" | fold -s -w 79 |
||
| 66 | echo "(else on a Windows machine:)" |
||
| 67 | echo " cd ${QNXSDK_PATH}" | fold -s -w 79 |
||
| 68 | echo " host\\win64\\x86_64\\usr\\bin\\busybox.exe sh -c \"" \ |
||
| 69 | "find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \ |
||
| 70 | "\"" | fold -s -w 79 |
||
| 71 | echo "Note that this step WILL take time on a Win32 machine, but is only done once." | fold -s -w 79 |
||
| 72 | echo "" |
||
| 73 | exit 1 |
||
| 74 | fi |
||
| 75 | |||
| 76 | # construct the target triple (actually a quadruple) |
||
| 77 | export TARGET_ARCH="${BUILD_TARGET_ARCH}" |
||
| 78 | test "${BUILD_TARGET_ARCH}" = "x86_64" && export TARGET_VENDOR="pc" || export TARGET_VENDOR="unknown" |
||
| 79 | export TARGET_KERNEL="nto" |
||
| 80 | export TARGET_SYSTEM="qnx${QNXSDK_VERSION}" |
||
| 81 | export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}" |
||
| 82 | echo "Will build for ${TARGET_TRIPLE}" |
||
| 83 | |||
| 84 | # change to an immediately visible path, i.e. the user's desktop (failsafe to $HOME if xdg-user-dir is unavailable) |
||
| 85 | STAGING_PATH="$(xdg-user-dir DESKTOP 2>/dev/null || echo "${HOME}")" |
||
| 86 | cd "${STAGING_PATH}" |
||
| 87 | |||
| 88 | # 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 |
||
| 89 | # (this is totally prohibitive with the official QNX toolchain) |
||
| 90 | if [ ! -L /tmp/qnxsdk ] || [ ! "$(readlink /tmp/qnxsdk)" = "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" ]; then |
||
| 91 | echo "Creating symlink to QNX toolchain in /tmp/qnxsdk..." |
||
| 92 | rm -rf /tmp/qnxsdk 2>/dev/null |
||
| 93 | ln -fs "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" /tmp/qnxsdk || exit 1 |
||
| 94 | fi |
||
| 95 | |||
| 96 | # setup the environment |
||
| 97 | export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}" |
||
| 98 | export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}" |
||
| 99 | export MAKEFLAGS="-I${QNX_TARGET}/usr/include" |
||
| 100 | export PATH="${QNX_HOST}/usr/bin:${PATH}" |
||
| 101 | |||
| 102 | # download the involved source packages and unpack them if not done yet |
||
| 103 | download_and_unpack_if_necessary() |
||
| 104 | { |
||
| 105 | # helper function that downloads a sources tarball, extracts it and patches it if necessary |
||
| 106 | # args: <sources dirname> <sources filename> <download URL> [optional patchset URL] |
||
| 107 | test -d "${1}" && return 0 # if sources directory already exists, nothing to do |
||
| 108 | if [ ! -s "${CURRENT_DIR}/${2}" ]; then # if sources archive isn't a nonempty file... |
||
| 109 | echo "Downloading ${1} sources from ${3}..." |
||
| 110 | if ! wget -O "${CURRENT_DIR}/${2}" "${3}"; then |
||
| 111 | # remove output file in case an error occurs |
||
| 112 | rm -f "${CURRENT_DIR}/${2}" |
||
| 113 | exit 1 |
||
| 114 | fi |
||
| 115 | fi |
||
| 116 | echo "Extracting ${1} sources..." |
||
| 117 | cd "$(dirname "${1}")" |
||
| 118 | if echo "${2}"|grep -q "\.tar\.bz2$"; then |
||
| 119 | # BZip2 tarball |
||
| 120 | tar xjf "${CURRENT_DIR}/${2}" || exit 1 |
||
| 121 | elif echo "${2}"|grep -q "\.tar\.xz$"; then |
||
| 122 | # XZ tarball |
||
| 123 | tar xJf "${CURRENT_DIR}/${2}" || exit 1 |
||
| 124 | elif echo "${2}"|grep -q "\.tar\.gz$"; then |
||
| 125 | # GZipped tarball |
||
| 126 | tar xzf "${CURRENT_DIR}/${2}" || exit 1 |
||
| 127 | else |
||
| 128 | echo "Error: unsupported file extension. Please improve the download_and_unpack_if_necessary() shell function to support it." |
||
| 129 | exit 1 |
||
| 130 | fi |
||
| 131 | # make sure the expected directory is here after extraction |
||
| 132 | if [ ! -d "${1}" ]; then |
||
| 133 | echo "Error: couldn't find ${1} in extracted sources." |
||
| 134 | exit 1 |
||
| 135 | fi |
||
| 136 | # do we have a patchset to apply ? |
||
| 137 | if [ -n "${4}" ]; then |
||
| 138 | echo "Downloading ${1} patchset from ${4}..." |
||
| 139 | wget -O "${CURRENT_DIR}/${2}.patchset" "${4}" || exit 1 |
||
| 140 | echo "Applying patchset..." |
||
| 141 | OLDDIR="$(pwd)" |
||
| 142 | cd "${1}" |
||
| 143 | patch -N -Z -p1 < "${CURRENT_DIR}/${2}.patchset" || exit 1 |
||
| 144 | cd "${OLDDIR}" |
||
| 145 | unset OLDDIR |
||
| 146 | fi |
||
| 147 | return 0 |
||
| 148 | } |
||
| 149 | download_and_unpack_if_necessary "${BINUTILS_SOURCES_DIR}" "${BINUTILS_SOURCES_FILE}" "${BINUTILS_SOURCES_URL}" || exit 1 |
||
| 150 | |||
| 151 | # print the build environment |
||
| 152 | echo "QNX_HOST=${QNX_HOST}" |
||
| 153 | echo "QNX_TARGET=${QNX_TARGET}" |
||
| 154 | echo "MAKEFLAGS=${MAKEFLAGS}" |
||
| 155 | |||
| 156 | # change directory to the binutils sources |
||
| 157 | cd "${BINUTILS_SOURCES_DIR}" |
||
| 158 | |||
| 159 | # create the build directory |
||
| 160 | echo "Wiping out build directory..." |
||
| 161 | test -e .build && rm -rf .build |
||
| 162 | mkdir .build || exit 1 |
||
| 163 | ln -fs "${BINUTILS_SOURCES_DIR}/.build" "../${BUILD_DIR_NAME}" |
||
| 164 | cd .build |
||
| 165 | |||
| 166 | backup_and_patch_if_necessary() |
||
| 167 | { |
||
| 168 | # handy function that patches a file in .. with a given sed replacement regex if necessary, creating backups |
||
| 169 | # args: <file pathname> <grep_string_to_test_for_presence_of_patch> <sed regex> |
||
| 170 | # test if already patched |
||
| 171 | grep -q "${2}" "../${1}" && return |
||
| 172 | # tell what we're about to do |
||
| 173 | echo "Patching ${1}..." |
||
| 174 | # have a backup first |
||
| 175 | test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || cp "../${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || exit 1 |
||
| 176 | # perform the patch |
||
| 177 | sed -E "${3}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" > "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]" |
||
| 178 | # verify that we did it successfully |
||
| 179 | if ! grep -q "${2}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]"; then |
||
| 180 | echo "Error: the file ${1} could not be patched. Please investigate and fix manually." | fold -s -w 79; exit 1 |
||
| 181 | fi |
||
| 182 | # and put the patched file in place |
||
| 183 | cp -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]" "../${1}" || exit 1 |
||
| 184 | } |
||
| 185 | backup_and_replace() |
||
| 186 | { |
||
| 187 | # handy function that replaces a file in .. with a given replacement, creating backups |
||
| 188 | # args: <file pathname> <replacement pathname> |
||
| 189 | # tell what we're about to do |
||
| 190 | echo "Replacing ${1}..." |
||
| 191 | # have a backup first |
||
| 192 | test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || test -e "../${1}" && cp "../${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" |
||
| 193 | # perform the replacement |
||
| 194 | cp -f "${2}" "../${1}" || exit 1 |
||
| 195 | } |
||
| 196 | |||
| 197 | # patch libiberty/pex-unix.c |
||
| 198 | # RATIONALE: QNX exposes the same spawnve() functions which were invented by Microsoft. Autoconf mistakenly believes it's a Cygwin system. |
||
| 199 | # 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 |
||
| 200 | # 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. |
||
| 201 | # Fix that by declaring that we want to use the traditional POSIX fork/exec scheme. |
||
| 202 | backup_and_patch_if_necessary "libiberty/pex-unix.c" __QNXNTO__ 's/defined\(HAVE_SPAWNVE\)/\!defined\(__QNXNTO__\) \&\& defined\(HAVE_SPAWNVE\)/g' |
||
| 203 | |||
| 204 | # patch bfd/config.bfd |
||
| 205 | # RATIONALE: insert x86_64 and aarch64 QNX definitions for the BFD library |
||
| 206 | backup_and_replace "bfd/config.bfd" "${CURRENT_DIR}/bfd.config.bfd" |
||
| 207 | |||
| 208 | # patch ld/configure.tgt |
||
| 209 | # RATIONALE: insert x86_64 and aarch64 QNX definitions for the classic linker |
||
| 210 | backup_and_replace "ld/configure.tgt" "${CURRENT_DIR}/ld.configure.tgt" |
||
| 211 | |||
| 212 | # define the tools to use when compiling intermediary programs on the build host |
||
| 213 | export AR_FOR_BUILD="/usr/bin/ar" |
||
| 214 | export AS_FOR_BUILD="/usr/bin/as" |
||
| 215 | export LD_FOR_BUILD="/usr/bin/ld" |
||
| 216 | export NM_FOR_BUILD="/usr/bin/nm" |
||
| 217 | export RANLIB_FOR_BUILD="/usr/bin/ranlib" |
||
| 218 | export CC_FOR_BUILD="/usr/bin/gcc"; CFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}" |
||
| 219 | export CXX_FOR_BUILD="/usr/bin/g++"; CXXFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}" |
||
| 220 | |||
| 221 | # configure and build the GNU binutils. Note that since these are low level tools, we want static binaries as much as possible. |
||
| 222 | ../configure \ |
||
| 223 | --prefix="$(dirname "$(dirname "${QNX_HOST}")")/qnx$(echo "${QNXSDK_VERSION}"|awk -F. '{print $1}')/${BUILD_TARGET_ARCH}/usr" \ |
||
| 224 | --host="${TARGET_TRIPLE}" \ |
||
| 225 | --target="${TARGET_TRIPLE}" \ |
||
| 226 | --disable-shared \ |
||
| 227 | --disable-nls \ |
||
| 228 | --enable-static-link \ |
||
| 229 | --disable-shared-plugins \ |
||
| 230 | --disable-dynamicplugin \ |
||
| 231 | --disable-pie \ |
||
| 232 | --enable-static=yes \ |
||
| 233 | --enable-shared=no \ |
||
| 234 | --disable-doc || exit 1 |
||
| 235 | |||
| 236 | # and build the whole shit |
||
| 237 | make -j 4 MAKEINFO=/bin/true || exit 1 |
||
| 238 | |||
| 239 | # now gather all the binutils in one place on the hypervisor's filesystem and name them appropriately |
||
| 240 | test -e "${CURRENT_DIR}/binutils-build" && rm -rf "${CURRENT_DIR}/binutils-build" |
||
| 241 | mkdir -p "${CURRENT_DIR}/binutils-build" |
||
| 242 | VERSION="$(grep PACKAGE_VERSION opcodes/config.h|awk -F'"' '{print $2}')" |
||
| 243 | VERSION_MAJ="$(echo "${VERSION}"|cut -d . -f 1)"; test -z "${VERSION_MAJ}" && VERSION_MAJ="0" |
||
| 244 | VERSION_MIN="$(echo "${VERSION}"|cut -d . -f 2)"; test -z "${VERSION_MIN}" && VERSION_MIN="0" |
||
| 245 | VERSION_REV="$(echo "${VERSION}"|cut -d . -f 3)"; test -z "${VERSION_REV}" && VERSION_REV="0" |
||
| 246 | for TOOL in binutils/addr2line:addr2line \ |
||
| 247 | binutils/ar:ar \ |
||
| 248 | gas/as-new:as \ |
||
| 249 | binutils/cxxfilt:c++filt \ |
||
| 250 | binutils/elfedit:elfedit \ |
||
| 251 | gprof/gprof:gprof \ |
||
| 252 | ld/ld-new:ld.bfd \ |
||
| 253 | binutils/nm-new:nm \ |
||
| 254 | binutils/objcopy:objcopy \ |
||
| 255 | binutils/objdump:objdump \ |
||
| 256 | binutils/ranlib:ranlib \ |
||
| 257 | binutils/readelf:readelf \ |
||
| 258 | binutils/size:size \ |
||
| 259 | binutils/strings:strings \ |
||
| 260 | binutils/strip-new:strip \ |
||
| 261 | ; do |
||
| 262 | SRC="$(echo "${TOOL}"|cut -d : -f 1)" |
||
| 263 | TGT="$(echo "${TOOL}"|cut -d : -f 2)" |
||
| 264 | cp "${SRC}" "${CURRENT_DIR}/binutils-build/${TARGET_TRIPLE}-${TGT}-${VERSION_MAJ}.${VERSION_MIN}.${VERSION_REV}" || exit 1 |
||
| 265 | done |
||
| 266 | |||
| 267 | echo "Tea time." |
||
| 268 | exit 0 |