Subversion Repositories QNX 8.QNX8 GNU binutils

Rev

Rev 19 | 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. export REQUIRED_TOOLS="wget python3 gcc g++ make m4" # list of build tools required to build all this on the build host
  21.  
  22. # see where we are
  23. export CURRENT_DIR="$(pwd)"
  24.  
  25. print_error_and_die()
  26. {
  27.         # a handy function that does what it says. Args: error strings to print, one line per argument
  28.         test -z "${COLUMNS}" && COLUMNS=80
  29.         echo ""
  30.         while [ -n "${1}" ]; do
  31.                 echo "${1}"|fold -s -w "${COLUMNS}"
  32.                 shift
  33.         done
  34.         echo ""
  35.         exit 1
  36. }
  37.  
  38. # verify we're a x86_64 Linux host
  39. if [ ! "$(uname)" = "Linux" ] || [ ! "$(uname -m)" = "x86_64" ]; then
  40.         print_error_and_die "Error: this script requires a x86_64 Linux machine (possibly a virtual machine, or WSL2) as the build host."
  41. fi
  42.  
  43. # verify that we have the required tools
  44. for REQUIRED_TOOL in ${REQUIRED_TOOLS}; do
  45.         "${REQUIRED_TOOL}" --version > /dev/null 2>&1 || print_error_and_die \
  46.                 "Error: this script requires at the very least the following tools installed:" \
  47.                 "       $(echo "${REQUIRED_TOOLS}"|sed 's/ /\n  /g')" \
  48.                 "Please install them (possibly as binary packages with apt-get) and try again." \
  49.                 "More specifically, the following tool was not found: '${REQUIRED_TOOL}'"
  50. done
  51.  
  52. # verify that we have the QNX platform SDK
  53. if [ ! -d "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" ]; then
  54.         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."
  55. fi
  56.  
  57. # 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)
  58. STAGING_PATH="$(xdg-user-dir DESKTOP 2>/dev/null || echo "${HOME}")"
  59. test -d "${STAGING_PATH}" || STAGING_PATH="${HOME}"
  60. cd "${STAGING_PATH}"
  61.  
  62. # are we running the Windows Subsystem for Linux, instead of a real Linux ?
  63. if [ -n "${WSL_DISTRO_NAME}" ]; then
  64.         # yes. In order to avoid the horrible 9p protocol for file interchange between NTFS and ext4 (a remarkably bad idea by Microsoft),
  65.         # 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.
  66.         QNXSDK_CANONICAL_PATH="${HOME}/$(basename "${QNXSDK_PATH}")"
  67.         if [ ! -d "${QNXSDK_CANONICAL_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_CANONICAL_PATH}/${QNXSDK_TARGETPATH}" ]; then
  68.                 print_error_and_die "Error: the QNX SDP hasn't been deployed to ${QNXSDK_CANONICAL_PATH}. Please do so and run this script again."
  69.         fi
  70. else
  71.         # 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.
  72.         QNXSDK_CANONICAL_PATH="$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")"
  73. fi
  74.  
  75. # 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)
  76. if [ ! -e "${QNXSDK_CANONICAL_PATH}/${QNXSDK_HOSTPATH}/usr/bin/ntox86_64-gcc" ]; then
  77.         # symlinks not deployed in the QNX SDK: do so
  78.         OLDDIR="$(pwd)"
  79.         cd "${QNXSDK_CANONICAL_PATH}"
  80.         find . -name symlinks.lst -exec ./symlinks.sh {} create \; && printf 'present-v2' > .symlinks-state
  81.         cd "${OLDDIR}"
  82. fi
  83.  
  84. # 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
  85. # (this is totally prohibitive with the official QNX toolchain)
  86. if [ ! -L /tmp/qnxsdk ] || [ ! "$(readlink /tmp/qnxsdk)" = "${QNXSDK_CANONICAL_PATH}" ]; then
  87.         echo "Creating symlink to QNX toolchain in /tmp/qnxsdk..."
  88.         rm -rf /tmp/qnxsdk 2>/dev/null
  89.         ln -fs "${QNXSDK_CANONICAL_PATH}" /tmp/qnxsdk || exit 1
  90. fi
  91.  
  92. # setup the environment
  93. export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}"
  94. export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}"
  95. export MAKEFLAGS="-I${QNX_TARGET}/usr/include"
  96. export PATH="${QNX_HOST}/usr/bin:${PATH}"
  97.  
  98. # print the build environment
  99. echo "QNX_HOST=${QNX_HOST}"
  100. echo "QNX_TARGET=${QNX_TARGET}"
  101. echo "MAKEFLAGS=${MAKEFLAGS}"
  102.  
  103. # download the involved source packages and unpack them if not done yet
  104. download_and_unpack_if_necessary()
  105. {
  106.         # helper function that downloads a sources tarball, extracts it and patches it if necessary
  107.         # args: <sources dirname> <sources filename> <download URL> [optional patchset URL]
  108.         test -d "${1}" && return 0 # if sources directory already exists, nothing to do
  109.         if [ ! -s "${CURRENT_DIR}/${2}" ]; then # if sources archive isn't a nonempty file...
  110.                 echo "Downloading ${1} sources from ${3}..."
  111.                 if ! wget -O "${CURRENT_DIR}/${2}" "${3}"; then
  112.                         # remove output file in case an error occurs
  113.                         rm -f "${CURRENT_DIR}/${2}"
  114.                         exit 1
  115.                 fi
  116.         fi
  117.         echo "Extracting ${1} sources..."
  118.         STARTDIR="$(pwd)"
  119.         cd "$(dirname "${1}")"
  120.         if echo "${2}"|grep -q "\.tar\.bz2$"; then
  121.                 # BZip2 tarball
  122.                 tar xjf "${CURRENT_DIR}/${2}" || exit 1
  123.         elif echo "${2}"|grep -q "\.tar\.xz$"; then
  124.                 # XZ tarball
  125.                 tar xJf "${CURRENT_DIR}/${2}" || exit 1
  126.         elif echo "${2}"|grep -q "\.tar\.gz$"; then
  127.                 # GZipped tarball
  128.                 tar xzf "${CURRENT_DIR}/${2}" || exit 1
  129.         else
  130.                 print_error_and_die "Error: unsupported file extension. Please improve the download_and_unpack_if_necessary() shell function to support it."
  131.         fi
  132.         # make sure the expected directory is here after extraction
  133.         if [ ! -d "$(basename "${1}")" ]; then
  134.                 print_error_and_die "Error: couldn't find ${1} in extracted sources."
  135.         fi
  136.         # do we have a patchset to apply ?
  137.         if [ -n "${4}" ]; then
  138.                 if [ ! -s "${CURRENT_DIR}/${2}.patchset" ]; then
  139.                         echo "Downloading ${1} patchset from ${4}..."
  140.                         wget -O "${CURRENT_DIR}/${2}.patchset" "${4}" || exit 1
  141.                 fi
  142.                 echo "Applying patchset..."
  143.                 OLDDIR="$(pwd)"
  144.                 cd "$(basename "${1}")"
  145.                 patch -N -Z -p1 < "${CURRENT_DIR}/${2}.patchset" || exit 1
  146.                 cd "${OLDDIR}"
  147.                 unset OLDDIR
  148.         fi
  149.         cd "${STARTDIR}"
  150.         unset STARTDIR
  151.         return 0
  152. }
  153. download_and_unpack_if_necessary "${BINUTILS_SOURCES_DIR}" "${BINUTILS_SOURCES_FILE}" "${BINUTILS_SOURCES_URL}" || exit 1
  154.  
  155. # change directory to the binutils sources
  156. cd "${BINUTILS_SOURCES_DIR}" || exit 1
  157.  
  158. backup_and_patch_if_necessary()
  159. {
  160.         # handy function that patches a file in the current directory with a given sed replacement regex if necessary, creating backups
  161.         # args: <file pathname> <grep_string_to_test_for_presence_of_patch> <sed regex>
  162.         _PATCHEE_PATHNAME="${1}"
  163.         _PATCHED_PATTERN="${2}"
  164.         # test if already patched
  165.         grep -q "${_PATCHED_PATTERN}" "${_PATCHEE_PATHNAME}" && return
  166.         # tell what we're about to do
  167.         echo "Patching ${_PATCHEE_PATHNAME}..."
  168.         _DOTTED_NAME="$(echo "${_PATCHEE_PATHNAME}"|tr '/' '.')"
  169.         # have a backup first
  170.         test -f "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" || cp "${_PATCHEE_PATHNAME}" "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" || exit 1
  171.         # perform the patch
  172.         cp -f "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]" || exit 1
  173.         while [ -n "${3}" ]; do
  174.                 sed -E -i "${3}" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]"
  175.                 shift
  176.         done
  177.         # verify that we did it successfully
  178.         if ! grep -q "${_PATCHED_PATTERN}" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]"; then
  179.                 echo "Error: the file ${_PATCHEE_PATHNAME} could not be patched. Please investigate and fix manually." | fold -s -w 79; exit 1
  180.         fi
  181.         # and put the patched file in place
  182.         cp -f "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]" "${_PATCHEE_PATHNAME}" || exit 1
  183. }
  184. backup_and_replace()
  185. {
  186.         # handy function that replaces a file in the current directory with a given replacement, creating backups
  187.         # args: <file pathname> <replacement pathname>
  188.         # tell what we're about to do
  189.         echo "Replacing ${1}..."
  190.         # have a backup first
  191.         test -f "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]" || test -e "${1}" && cp "${1}" "${CURRENT_DIR}/$(echo "${1}"|tr '/' '.') [ORIGINAL]"
  192.         # perform the replacement
  193.         cp -f "${2}" "${1}" || exit 1
  194. }
  195.  
  196. # patch libiberty/pex-unix.c
  197. # replace:
  198. #       defined(HAVE_SPAWNVE)
  199. # with:
  200. #       !defined(__QNXNTO__) && defined(HAVE_SPAWNVE)
  201. # RATIONALE: QNX exposes the same spawnve() functions which were invented by Microsoft. Autoconf mistakenly believes it's a Cygwin system.
  202. # 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
  203. # 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.
  204. # Fix that by declaring that we want to use the traditional POSIX fork/exec scheme, which QNX supports as well.
  205. backup_and_patch_if_necessary "libiberty/pex-unix.c" __QNXNTO__ \
  206.         's@defined\(HAVE_SPAWNVE\)@\!defined\(__QNXNTO__\) \&\& defined\(HAVE_SPAWNVE\)@g'
  207.  
  208. # patch bfd/config.bfd
  209. # RATIONALE: insert x86_64 and aarch64 QNX definitions for the BFD library
  210. backup_and_replace "bfd/config.bfd" "${CURRENT_DIR}/bfd.config.bfd"
  211.  
  212. # patch ld/configure.tgt
  213. # RATIONALE: insert x86_64 and aarch64 QNX definitions for the GNU linker
  214. backup_and_replace "ld/configure.tgt" "${CURRENT_DIR}/ld.configure.tgt"
  215.  
  216. # patch ld/emulparams/aarch64nto.sh
  217. # RATIONALE: insert aarch64 QNX particularities for the GNU linker
  218. backup_and_replace "ld/emulparams/aarch64nto.sh" "${CURRENT_DIR}/ld.emulparams.aarch64nto.sh"
  219.  
  220. # patch ld/emulparams/elf_x86_64nto.sh
  221. # RATIONALE: insert aarch64 QNX particularities for the GNU linker
  222. backup_and_replace "ld/emulparams/elf_x86_64.sh" "${CURRENT_DIR}/ld.emulparams.elf_x86_64.sh"
  223.  
  224. # patch ld/emulparams/i386nto.sh
  225. # RATIONALE: insert i386 QNX particularities for the GNU linker
  226. backup_and_replace "ld/emulparams/i386nto.sh" "${CURRENT_DIR}/ld.emulparams.i386nto.sh"
  227.  
  228. # patch ld/emultempl/nto.em
  229. # RATIONALE: insert i386 QNX particularities for the GNU linker
  230. backup_and_replace "ld/emultempl/nto.em" "${CURRENT_DIR}/ld.emultempl.nto.em"
  231.  
  232. # define the tools to use when compiling intermediary programs on the build host
  233. export AR_FOR_BUILD="/usr/bin/ar"
  234. export AS_FOR_BUILD="/usr/bin/as"
  235. export LD_FOR_BUILD="/usr/bin/ld"
  236. export NM_FOR_BUILD="/usr/bin/nm"
  237. export RANLIB_FOR_BUILD="/usr/bin/ranlib"
  238. export CC_FOR_BUILD="/usr/bin/gcc"; CFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}"
  239. export CXX_FOR_BUILD="/usr/bin/g++"; CXXFLAGS_FOR_BUILD="-fuse-ld=${LD_FOR_BUILD}"
  240.  
  241. # now, for each target arch the native binutils should support...
  242. for BUILD_TARGET_ARCH in ${BUILD_TARGET_ARCHS}; do
  243.  
  244.         # construct the target triple (actually a quadruple)
  245.         TARGET_ARCH="${BUILD_TARGET_ARCH}"
  246.         test "${BUILD_TARGET_ARCH}" = "x86_64" && TARGET_VENDOR="pc" || TARGET_VENDOR="unknown"
  247.         TARGET_KERNEL="nto"
  248.         TARGET_SYSTEM="qnx${QNXSDK_VERSION}"
  249.         export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}"
  250.         echo "Will build for ${TARGET_TRIPLE}"
  251.  
  252.         # create the build directory
  253.         echo "Wiping out build directory..."
  254.         test -e ".build/${TARGET_TRIPLE}" && rm -rf ".build/${TARGET_TRIPLE}"
  255.         mkdir -p ".build/${TARGET_TRIPLE}" || exit 1
  256.         ln -fs "${BINUTILS_SOURCES_DIR}/.build" "../${BUILD_DIR_NAME}"
  257.         cd ".build/${TARGET_TRIPLE}"
  258.  
  259.         # configure and build the GNU binutils. Note that since these are low level tools, we want static binaries as much as possible.
  260.         # 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.
  261.         ../../configure \
  262.                         --with-sysroot="${QNX_TARGET}" \
  263.                 --prefix="$(dirname "$(dirname "${QNX_HOST}")")/qnx$(echo "${QNXSDK_VERSION}"|awk -F. '{print $1}')/${BUILD_TARGET_ARCH}/usr" \
  264.                 --host="x86_64-pc-nto-qnx${QNXSDK_VERSION}" \
  265.                 --target="${TARGET_TRIPLE}" \
  266.                 --disable-shared \
  267.                 --disable-nls \
  268.                 --enable-static-link \
  269.                 --disable-shared-plugins \
  270.                 --disable-dynamicplugin \
  271.                 --disable-pie \
  272.                 --enable-static=yes \
  273.                 --enable-shared=no \
  274.                         --enable-threads \
  275.                         --disable-bootstrap \
  276.                 --disable-doc \
  277.                 --with-bugurl="pm@pmbaty.com" \
  278.                 || exit 1
  279.  
  280.         # and build the whole shit
  281.         make -j 4 MAKEINFO=/bin/true || exit 1
  282.  
  283.         # now gather all the binutils in one place on the hypervisor's filesystem and name them appropriately
  284.         test -e "${CURRENT_DIR}/binutils-build" || mkdir -p "${CURRENT_DIR}/binutils-build"
  285.         rm "${CURRENT_DIR}/binutils-build/${TARGET_TRIPLE}-"* 2>/dev/null
  286.         VERSION="$(grep PACKAGE_VERSION opcodes/config.h|awk -F'"' '{print $2}')"
  287.         VERSION_MAJ="$(echo "${VERSION}"|cut -d . -f 1)"; test -z "${VERSION_MAJ}" && VERSION_MAJ="0"
  288.         VERSION_MIN="$(echo "${VERSION}"|cut -d . -f 2)"; test -z "${VERSION_MIN}" && VERSION_MIN="0"
  289.         VERSION_REV="$(echo "${VERSION}"|cut -d . -f 3)"; test -z "${VERSION_REV}" && VERSION_REV="0"
  290.         for TOOL in \
  291.                         binutils/addr2line:addr2line \
  292.                         binutils/ar:ar \
  293.                         gas/as-new:as \
  294.                         binutils/cxxfilt:c++filt \
  295.                         binutils/elfedit:elfedit \
  296.                         gprof/gprof:gprof \
  297.                         ld/ld-new:ld.bfd \
  298.                         binutils/nm-new:nm \
  299.                         binutils/objcopy:objcopy \
  300.                         binutils/objdump:objdump \
  301.                         binutils/ranlib:ranlib \
  302.                         binutils/readelf:readelf \
  303.                         binutils/size:size \
  304.                         binutils/strings:strings \
  305.                         binutils/strip-new:strip \
  306.         ; do
  307.                 SRC="$(echo "${TOOL}"|cut -d : -f 1)"
  308.                 TGT="$(echo "${TOOL}"|cut -d : -f 2)"
  309.                 cp "${SRC}" "${CURRENT_DIR}/binutils-build/${TARGET_TRIPLE}-${TGT}-${VERSION_MAJ}.${VERSION_MIN}.${VERSION_REV}" || exit 1
  310.         done
  311.  
  312.         # return to the sources dir and proceed to the next arch
  313.         cd ../..
  314. done
  315.  
  316. test -z "${WSL_DISTRO_NAME}" && /bin/printf "\n\xf0\x9f\x8d\xba\x20\x43\x68\x65\x65\x72\x73\x2e\n"
  317. exit 0
  318.