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