Subversion Repositories QNX 8.QNX8 GNU binutils

Rev

Rev 1 | Rev 3 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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 GNU binutils sources will be built
  12. export BUILD_TARGET_ARCHS="x86_64 aarch64" # space-separated list of CPU architectures to build the GNU binutils for, among "x86_64" and "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_ARCHS} platform(s). 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.    || ! make    --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 "  make"
  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. # 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
  77. # (this is totally prohibitive with the official QNX toolchain)
  78. if [ ! -L /tmp/qnxsdk ] || [ ! "$(readlink /tmp/qnxsdk)" = "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" ]; then
  79.         echo "Creating symlink to QNX toolchain in /tmp/qnxsdk..."
  80.         rm -rf /tmp/qnxsdk 2>/dev/null
  81.         ln -fs "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" /tmp/qnxsdk || exit 1
  82. fi
  83.  
  84. # setup the environment
  85. export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}"
  86. export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}"
  87. export MAKEFLAGS="-I${QNX_TARGET}/usr/include"
  88. export PATH="${QNX_HOST}/usr/bin:${PATH}"
  89.  
  90. # print the build environment
  91. echo "QNX_HOST=${QNX_HOST}"
  92. echo "QNX_TARGET=${QNX_TARGET}"
  93. echo "MAKEFLAGS=${MAKEFLAGS}"
  94.  
  95. # change to an immediately visible path, i.e. the user's desktop (failsafe to $HOME if xdg-user-dir is unavailable)
  96. STAGING_PATH="$(xdg-user-dir DESKTOP 2>/dev/null || echo "${HOME}")"
  97. cd "${STAGING_PATH}"
  98.  
  99. # download the involved source packages and unpack them if not done yet
  100. download_and_unpack_if_necessary()
  101. {
  102.         # helper function that downloads a sources tarball, extracts it and patches it if necessary
  103.         # args: <sources dirname> <sources filename> <download URL> [optional patchset URL]
  104.         test -d "${1}" && return 0 # if sources directory already exists, nothing to do
  105.         if [ ! -s "${CURRENT_DIR}/${2}" ]; then # if sources archive isn't a nonempty file...
  106.                 echo "Downloading ${1} sources from ${3}..."
  107.                 if ! wget -O "${CURRENT_DIR}/${2}" "${3}"; then
  108.                         # remove output file in case an error occurs
  109.                         rm -f "${CURRENT_DIR}/${2}"
  110.                         exit 1
  111.                 fi
  112.         fi
  113.         echo "Extracting ${1} sources..."
  114.         cd "$(dirname "${1}")"
  115.         if echo "${2}"|grep -q "\.tar\.bz2$"; then
  116.                 # BZip2 tarball
  117.                 tar xjf "${CURRENT_DIR}/${2}" || exit 1
  118.         elif echo "${2}"|grep -q "\.tar\.xz$"; then
  119.                 # XZ tarball
  120.                 tar xJf "${CURRENT_DIR}/${2}" || exit 1
  121.         elif echo "${2}"|grep -q "\.tar\.gz$"; then
  122.                 # GZipped tarball
  123.                 tar xzf "${CURRENT_DIR}/${2}" || exit 1
  124.         else
  125.                 echo "Error: unsupported file extension. Please improve the download_and_unpack_if_necessary() shell function to support it."
  126.                 exit 1
  127.         fi
  128.         # make sure the expected directory is here after extraction
  129.         if [ ! -d "${1}" ]; then
  130.                 echo "Error: couldn't find ${1} in extracted sources."
  131.                 exit 1
  132.         fi
  133.         # do we have a patchset to apply ?
  134.         if [ -n "${4}" ]; then
  135.                 echo "Downloading ${1} patchset from ${4}..."
  136.                 wget -O "${CURRENT_DIR}/${2}.patchset" "${4}" || exit 1
  137.                 echo "Applying patchset..."
  138.                 OLDDIR="$(pwd)"
  139.                 cd "${1}"
  140.                 patch -N -Z -p1 < "${CURRENT_DIR}/${2}.patchset" || exit 1
  141.                 cd "${OLDDIR}"
  142.                 unset OLDDIR
  143.         fi
  144.         return 0
  145. }
  146. download_and_unpack_if_necessary "${BINUTILS_SOURCES_DIR}" "${BINUTILS_SOURCES_FILE}" "${BINUTILS_SOURCES_URL}" || exit 1
  147.  
  148. # change directory to the binutils sources
  149. cd "${BINUTILS_SOURCES_DIR}" || exit 1
  150.  
  151. backup_and_patch_if_necessary()
  152. {
  153.         # handy function that patches a file in the current directory with a given sed replacement regex if necessary, creating backups
  154.         # args: <file pathname> <grep_string_to_test_for_presence_of_patch> <sed regex>
  155.         # test if already patched
  156.         grep -q "${2}" "${1}" && return
  157.         # tell what we're about to do
  158.         echo "Patching ${1}..."
  159.         # have a backup first
  160.         test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || cp "${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || exit 1
  161.         # perform the patch
  162.         sed -E "${3}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" > "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]"
  163.         # verify that we did it successfully
  164.         if ! grep -q "${2}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]"; then
  165.                 echo "Error: the file ${1} could not be patched. Please investigate and fix manually." | fold -s -w 79; exit 1
  166.         fi
  167.         # and put the patched file in place
  168.         cp -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [PATCHED]" "${1}" || exit 1
  169. }
  170. backup_and_replace()
  171. {
  172.         # handy function that replaces a file in the current directory with a given replacement, creating backups
  173.         # args: <file pathname> <replacement pathname>
  174.         # tell what we're about to do
  175.         echo "Replacing ${1}..."
  176.         # have a backup first
  177.         test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || test -e "${1}" && cp "${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]"
  178.         # perform the replacement
  179.         cp -f "${2}" "${1}" || exit 1
  180. }
  181.  
  182. # patch libiberty/pex-unix.c
  183. # RATIONALE: QNX exposes the same spawnve() functions which were invented by Microsoft. Autoconf mistakenly believes it's a Cygwin system.
  184. # 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
  185. # 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.
  186. # Fix that by declaring that we want to use the traditional POSIX fork/exec scheme.
  187. backup_and_patch_if_necessary "libiberty/pex-unix.c" __QNXNTO__ 's/defined\(HAVE_SPAWNVE\)/\!defined\(__QNXNTO__\) \&\& defined\(HAVE_SPAWNVE\)/g'
  188.  
  189. # patch bfd/config.bfd
  190. # RATIONALE: insert x86_64 and aarch64 QNX definitions for the BFD library
  191. backup_and_replace "bfd/config.bfd" "${CURRENT_DIR}/bfd.config.bfd"
  192.  
  193. # patch ld/configure.tgt
  194. # RATIONALE: insert x86_64 and aarch64 QNX definitions for the classic linker
  195. backup_and_replace "ld/configure.tgt" "${CURRENT_DIR}/ld.configure.tgt"
  196.  
  197. # define the tools to use when compiling intermediary programs on the build host
  198. export AR_FOR_BUILD="/usr/bin/ar"
  199. export AS_FOR_BUILD="/usr/bin/as"
  200. export LD_FOR_BUILD="/usr/bin/ld"
  201. export NM_FOR_BUILD="/usr/bin/nm"
  202. export RANLIB_FOR_BUILD="/usr/bin/ranlib"
  203. export CC_FOR_BUILD="/usr/bin/gcc"; CFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}"
  204. export CXX_FOR_BUILD="/usr/bin/g++"; CXXFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}"
  205.  
  206. # now, for each target arch the native binutils should support...
  207. for BUILD_TARGET_ARCH in ${BUILD_TARGET_ARCHS}; do
  208.  
  209.         # construct the target triple (actually a quadruple)
  210.         TARGET_ARCH="${BUILD_TARGET_ARCH}"
  211.         test "${BUILD_TARGET_ARCH}" = "x86_64" && TARGET_VENDOR="pc" || TARGET_VENDOR="unknown"
  212.         TARGET_KERNEL="nto"
  213.         TARGET_SYSTEM="qnx${QNXSDK_VERSION}"
  214.         export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}"
  215.         echo "Will build for ${TARGET_TRIPLE}"
  216.  
  217.         # create the build directory
  218.         echo "Wiping out build directory..."
  219.         test -e ".build/${TARGET_TRIPLE}" && rm -rf ".build/${TARGET_TRIPLE}"
  220.         mkdir -p ".build/${TARGET_TRIPLE}" || exit 1
  221.         ln -fs "${BINUTILS_SOURCES_DIR}/.build" "../${BUILD_DIR_NAME}"
  222.         cd ".build/${TARGET_TRIPLE}"
  223.  
  224.         # configure and build the GNU binutils. Note that since these are low level tools, we want static binaries as much as possible.
  225.         # 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.
  226.         ../../configure \
  227.                 --prefix="$(dirname "$(dirname "${QNX_HOST}")")/qnx$(echo "${QNXSDK_VERSION}"|awk -F. '{print $1}')/${BUILD_TARGET_ARCH}/usr" \
  228.                --host="x86_64-pc-nto-qnx${QNXSDK_VERSION}" \
  229.                --target="${TARGET_TRIPLE}" \
  230.                --disable-shared \
  231.                --disable-nls \
  232.                --enable-static-link \
  233.                --disable-shared-plugins \
  234.                --disable-dynamicplugin \
  235.                --disable-pie \
  236.                --enable-static=yes \
  237.                --enable-shared=no \
  238.                --disable-doc || exit 1
  239.  
  240.         # and build the whole shit
  241.         make -j 4 MAKEINFO=/bin/true || exit 1
  242.  
  243.         # now gather all the binutils in one place on the hypervisor's filesystem and name them appropriately
  244.         test -e "${CURRENT_DIR}/binutils-build" || mkdir -p "${CURRENT_DIR}/binutils-build"
  245.         rm "${CURRENT_DIR}/binutils-build/${TARGET_TRIPLE}-"* 2>/dev/null
  246.         VERSION="$(grep PACKAGE_VERSION opcodes/config.h|awk -F'"' '{print $2}')"
  247.         VERSION_MAJ="$(echo "${VERSION}"|cut -d . -f 1)"; test -z "${VERSION_MAJ}" && VERSION_MAJ="0"
  248.         VERSION_MIN="$(echo "${VERSION}"|cut -d . -f 2)"; test -z "${VERSION_MIN}" && VERSION_MIN="0"
  249.         VERSION_REV="$(echo "${VERSION}"|cut -d . -f 3)"; test -z "${VERSION_REV}" && VERSION_REV="0"
  250.         for TOOL in \
  251.                         binutils/addr2line:addr2line \
  252.                         binutils/ar:ar \
  253.                         gas/as-new:as \
  254.                         binutils/cxxfilt:c++filt \
  255.                         binutils/elfedit:elfedit \
  256.                         gprof/gprof:gprof \
  257.                         ld/ld-new:ld.bfd \
  258.                         binutils/nm-new:nm \
  259.                         binutils/objcopy:objcopy \
  260.                         binutils/objdump:objdump \
  261.                         binutils/ranlib:ranlib \
  262.                         binutils/readelf:readelf \
  263.                         binutils/size:size \
  264.                         binutils/strings:strings \
  265.                         binutils/strip-new:strip \
  266.         ; do
  267.                 SRC="$(echo "${TOOL}"|cut -d : -f 1)"
  268.                 TGT="$(echo "${TOOL}"|cut -d : -f 2)"
  269.                 cp "${SRC}" "${CURRENT_DIR}/binutils-build/${TARGET_TRIPLE}-${TGT}-${VERSION_MAJ}.${VERSION_MIN}.${VERSION_REV}" || exit 1
  270.         done
  271.  
  272.         # return to the sources dir and proceed to the next arch
  273.         cd ../..
  274. done
  275.  
  276. echo "Tea time."
  277. exit 0
  278.