Rev 24 | Rev 26 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pmbaty | 1 | #!/bin/sh |
3 | pmbaty | 2 | # LLVM/Clang toolchain cross-compilation script for QNX 8.0 by Pierre-Marie Baty <pm@pmbaty.com> |
2 | pmbaty | 3 | |
3 | pmbaty | 4 | # NOTE TO SELF: DO NOT USE $0 AS THIS SCRIPT CAN BE RUN *OR* SOURCED! (see build-llvm.sh in the VM) |
5 | |||
7 | pmbaty | 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="llvm-build" # name of the directory on the build host's desktop where the LLVM sources will be built |
||
12 | export BUILD_TARGET_ARCH="x86_64" # CPU architecture to build LLVM for, either "x86_64" or "aarch64" |
||
13 | |||
13 | pmbaty | 14 | export LLVM_VERSION="16.0.6" # version of LLVM that will be built (ideally the same version as the libc++ supplied by QNX in their platform SDK) |
7 | pmbaty | 15 | export LLVM_SOURCES_FILE="llvmorg-${LLVM_VERSION}.tar.gz" # name of the file containing LLVM version LLVM_VERSION sources |
2 | pmbaty | 16 | export LLVM_SOURCES_URL="https://github.com/llvm/llvm-project/archive/refs/tags/${LLVM_SOURCES_FILE}" # download URL of LLVM_SOURCES_FILE |
17 | export LLVM_SOURCES_DIR="llvm-project-llvmorg-${LLVM_VERSION}" # name of directory created when extracting LLVM_SOURCES_FILE |
||
18 | |||
21 | pmbaty | 19 | export LLVM_PROJECTS_TO_BUILD="clang;polly;lld" # semicolon-separated list of LLVM projects to build (full list: "clang;clang-tools-extra;cross-project-tests;libc;libclc;lld;lldb;openmp;polly;pstl") |
14 | pmbaty | 20 | export LLVM_RUNTIMES_TO_BUILD="compiler-rt" # semicolon-separated list of LLVM runtimes to build (full list: "compiler-rt;libc;libcxx;libcxxabi;libunwind;openmp") |
13 | pmbaty | 21 | export LLVM_BUILD_CONFIGURATION_TYPE="MinSizeRel" # build configuration, one of "Debug", "Release", "RelWithDebugInfo" or "MinSizeRel" |
22 | |||
23 | export REQUIRED_TOOLS="wget python3 cmake gcc g++ ninja ccache" # list of build tools required to build all this on the build host |
||
24 | |||
2 | pmbaty | 25 | # see where we are |
26 | export CURRENT_DIR="$(pwd)" |
||
27 | |||
13 | pmbaty | 28 | print_error_and_die() |
29 | { |
||
30 | # a handy function that does what it says. Args: error strings to print, one line per argument |
||
31 | test -z "${COLUMNS}" && COLUMNS=80 |
||
2 | pmbaty | 32 | echo "" |
13 | pmbaty | 33 | while [ -n "${1}" ]; do |
34 | echo "${1}"|fold -s -w "${COLUMNS}" |
||
35 | shift |
||
36 | done |
||
2 | pmbaty | 37 | echo "" |
38 | exit 1 |
||
13 | pmbaty | 39 | } |
40 | |||
41 | # verify we're a x86_64 Linux host |
||
42 | if [ ! "$(uname)" = "Linux" ] || [ ! "$(uname -m)" = "x86_64" ]; then |
||
43 | print_error_and_die "Error: this script requires a x86_64 Linux machine (possibly a virtual machine) as the build host." |
||
2 | pmbaty | 44 | fi |
45 | |||
46 | # verify that we have the QNX platform SDK |
||
47 | if [ ! -d "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" ] || [ ! -d "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" ]; then |
||
19 | pmbaty | 48 | 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." |
2 | pmbaty | 49 | fi |
50 | |||
13 | pmbaty | 51 | # verify that we have the required tools |
52 | for REQUIRED_TOOL in ${REQUIRED_TOOLS}; do |
||
53 | "${REQUIRED_TOOL}" --version > /dev/null 2>&1 || print_error_and_die \ |
||
54 | "Error: this script requires at the very least the following tools installed:" \ |
||
55 | " $(echo "${REQUIRED_TOOLS}"|sed 's/ /\n /g')" \ |
||
56 | "Please install them (possibly as binary packages with apt-get) and try again." \ |
||
57 | "More specifically, the following tool was not found: '${REQUIRED_TOOL}'" |
||
58 | done |
||
2 | pmbaty | 59 | |
20 | pmbaty | 60 | # 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) |
61 | if [ ! -e "${QNXSDK_PATH}/${QNXSDK_HOSTPATH}/usr/bin/gcc" ] || [ ! -e "${QNXSDK_PATH}/${QNXSDK_TARGETPATH}/usr/include/readline.h" ]; then |
||
13 | pmbaty | 62 | print_error_and_die \ |
63 | "Error: the toolchain platform-specific symbolic links have not been deployed in this QNX SDK. Please run" \ |
||
64 | "(on a POSIX machine:)" \ |
||
65 | " cd ${QNXSDK_PATH}" \ |
||
66 | " find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state" \ |
||
67 | "(else on a Windows machine:)" \ |
||
68 | " cd ${QNXSDK_PATH}" \ |
||
69 | " host\\win64\\x86_64\\usr\\bin\\busybox.exe sh -c \"find . -name symlinks.lst -exec ./symlinks.sh {} create \\; && printf 'present' > .symlinks-state\"" \ |
||
70 | "Note that this step WILL take time on a Win32 machine, but is only done once." |
||
2 | pmbaty | 71 | fi |
72 | |||
25 | pmbaty | 73 | # are we running the Windows Subsystem for Linux, instead of a real Linux ? |
74 | if [ -n "${WSL_DISTRO_NAME}" ]; then |
||
75 | # yes. In order to avoid the horrible 9p protocol for file interchange between NTFS and ext4 (a remarkably bad idea by Microsoft), |
||
76 | # have a copy of $QNX_HOST and $QNX_TARGET in the WSL2 filesystem. The speed gain is considerable. |
||
77 | if [ ! -d "/tmp/qnxsdk/${QNXSDK_HOSTPATH}" ]; then |
||
78 | mkdir -p "$(dirname "/tmp/qnxsdk/${QNXSDK_HOSTPATH}")" 2>/dev/null |
||
79 | echo "Deploying QNX ${QNXSDK_VERSION} HOST SDK to WSL2 ext4 filesystem, please wait..." |
||
80 | echo "This operation *WILL* take time, but is only done once - as long as your WSL filesystem is preserved." |
||
81 | cp -R "${CURRENT_DIR}/${QNXSDK_PATH}/${QNXSDK_HOSTPATH}" "$(dirname "/tmp/qnxsdk/${QNXSDK_HOSTPATH}")" || exit 1 |
||
82 | fi |
||
83 | if [ ! -d "/tmp/qnxsdk/${QNXSDK_TARGETPATH}" ]; then |
||
84 | mkdir -p "$(dirname "/tmp/qnxsdk/${QNXSDK_TARGETPATH}")" 2>/dev/null |
||
85 | echo "Deploying QNX ${QNXSDK_VERSION} TARGET SDK to WSL2 ext4 filesystem, please wait..." |
||
86 | echo "This operation *WILL* take time, but is only done once - as long as your WSL filesystem is preserved." |
||
87 | cp -R "${CURRENT_DIR}/${QNXSDK_PATH}/${QNXSDK_TARGETPATH}" "$(dirname "/tmp/qnxsdk/${QNXSDK_TARGETPATH}")" || exit 1 |
||
88 | fi |
||
89 | else |
||
90 | # we're not in WSL. 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 |
||
91 | # (this is totally prohibitive with the official QNX toolchain) |
||
92 | if [ ! -L /tmp/qnxsdk ] || [ ! "$(readlink /tmp/qnxsdk)" = "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" ]; then |
||
93 | echo "Creating symlink to QNX toolchain in /tmp/qnxsdk..." |
||
94 | rm -rf /tmp/qnxsdk 2>/dev/null |
||
95 | ln -fs "$(realpath "${CURRENT_DIR}/${QNXSDK_PATH}")" /tmp/qnxsdk || exit 1 |
||
96 | fi |
||
7 | pmbaty | 97 | fi |
2 | pmbaty | 98 | |
99 | # setup the environment |
||
100 | export QNX_HOST="/tmp/qnxsdk/${QNXSDK_HOSTPATH}" |
||
101 | export QNX_TARGET="/tmp/qnxsdk/${QNXSDK_TARGETPATH}" |
||
102 | export MAKEFLAGS="-I${QNX_TARGET}/usr/include" |
||
103 | export PATH="${QNX_HOST}/usr/bin:${PATH}" |
||
104 | |||
13 | pmbaty | 105 | # print the build environment |
106 | echo "QNX_HOST=${QNX_HOST}" |
||
107 | echo "QNX_TARGET=${QNX_TARGET}" |
||
108 | echo "MAKEFLAGS=${MAKEFLAGS}" |
||
109 | |||
110 | # construct the target triple (actually a quadruple) |
||
111 | export TARGET_ARCH="${BUILD_TARGET_ARCH}" |
||
112 | test "${BUILD_TARGET_ARCH}" = "x86_64" && export TARGET_VENDOR="pc" || export TARGET_VENDOR="unknown" |
||
113 | export TARGET_KERNEL="nto" |
||
114 | export TARGET_SYSTEM="qnx${QNXSDK_VERSION}" |
||
115 | export TARGET_TRIPLE="${TARGET_ARCH}-${TARGET_VENDOR}-${TARGET_KERNEL}-${TARGET_SYSTEM}" |
||
116 | echo "Will build for ${TARGET_TRIPLE}" |
||
117 | |||
24 | pmbaty | 118 | # 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) |
13 | pmbaty | 119 | STAGING_PATH="$(xdg-user-dir DESKTOP 2>/dev/null || echo "${HOME}")" |
23 | pmbaty | 120 | test -d "${STAGING_PATH}" || STAGING_PATH="${HOME}" |
13 | pmbaty | 121 | cd "${STAGING_PATH}" |
122 | |||
10 | pmbaty | 123 | # download the involved source packages and unpack them if not done yet |
124 | download_and_unpack_if_necessary() |
||
125 | { |
||
126 | # helper function that downloads a sources tarball, extracts it and patches it if necessary |
||
11 | pmbaty | 127 | # args: <sources dirname> <sources filename> <download URL> [optional patchset URL] |
128 | test -d "${1}" && return 0 # if sources directory already exists, nothing to do |
||
129 | if [ ! -s "${CURRENT_DIR}/${2}" ]; then # if sources archive isn't a nonempty file... |
||
130 | echo "Downloading ${1} sources from ${3}..." |
||
131 | if ! wget -O "${CURRENT_DIR}/${2}" "${3}"; then |
||
132 | # remove output file in case an error occurs |
||
133 | rm -f "${CURRENT_DIR}/${2}" |
||
10 | pmbaty | 134 | exit 1 |
135 | fi |
||
136 | fi |
||
11 | pmbaty | 137 | echo "Extracting ${1} sources..." |
138 | cd "$(dirname "${1}")" |
||
139 | if echo "${2}"|grep -q "\.tar\.bz2$"; then |
||
140 | # BZip2 tarball |
||
141 | tar xjf "${CURRENT_DIR}/${2}" || exit 1 |
||
142 | elif echo "${2}"|grep -q "\.tar\.xz$"; then |
||
143 | # XZ tarball |
||
144 | tar xJf "${CURRENT_DIR}/${2}" || exit 1 |
||
145 | elif echo "${2}"|grep -q "\.tar\.gz$"; then |
||
146 | # GZipped tarball |
||
147 | tar xzf "${CURRENT_DIR}/${2}" || exit 1 |
||
148 | else |
||
13 | pmbaty | 149 | print_error_and_die "Error: unsupported file extension. Please improve the download_and_unpack_if_necessary() shell function to support it." |
11 | pmbaty | 150 | fi |
151 | # make sure the expected directory is here after extraction |
||
152 | if [ ! -d "${1}" ]; then |
||
13 | pmbaty | 153 | print_error_and_die "Error: couldn't find ${1} in extracted sources." |
11 | pmbaty | 154 | fi |
155 | # do we have a patchset to apply ? |
||
156 | if [ -n "${4}" ]; then |
||
157 | echo "Downloading ${1} patchset from ${4}..." |
||
158 | wget -O "${CURRENT_DIR}/${2}.patchset" "${4}" || exit 1 |
||
159 | echo "Applying patchset..." |
||
160 | OLDDIR="$(pwd)" |
||
161 | cd "${1}" |
||
162 | patch -N -Z -p1 < "${CURRENT_DIR}/${2}.patchset" || exit 1 |
||
163 | cd "${OLDDIR}" |
||
164 | unset OLDDIR |
||
165 | fi |
||
10 | pmbaty | 166 | return 0 |
167 | } |
||
168 | download_and_unpack_if_necessary "${LLVM_SOURCES_DIR}" "${LLVM_SOURCES_FILE}" "${LLVM_SOURCES_URL}" || exit 1 |
||
169 | |||
2 | pmbaty | 170 | # create the build directory |
171 | echo "Wiping out build directory..." |
||
172 | test -e "${BUILD_DIR_NAME}" && rm -rf "${BUILD_DIR_NAME}" |
||
173 | mkdir "${BUILD_DIR_NAME}" || exit 1 |
||
174 | cd "${BUILD_DIR_NAME}" || exit 1 |
||
175 | |||
176 | # create the QNX CMake toolchain file |
||
8 | pmbaty | 177 | test -e "${TARGET_TRIPLE}.cmake" || echo '# '${TARGET_TRIPLE}' CMake toolchain file by Pierre-Marie Baty <pm@pmbaty.com> |
2 | pmbaty | 178 | |
179 | SET(CMAKE_SYSTEM_NAME "QNX") |
||
7 | pmbaty | 180 | SET(CMAKE_SYSTEM_VERSION "'${QNXSDK_VERSION}'") |
2 | pmbaty | 181 | |
182 | SET(QNX "1") |
||
183 | SET(QNXNTO "1") |
||
184 | SET(QNX_HOST "$ENV{QNX_HOST}") |
||
185 | SET(QNX_TARGET "$ENV{QNX_TARGET}") |
||
186 | SET(QNX_PROCESSOR "'${TARGET_ARCH}'") |
||
187 | |||
188 | SET(CMAKE_ASM_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc") |
||
189 | SET(CMAKE_ASM_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}") |
||
190 | |||
191 | SET(CMAKE_C_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-gcc") |
||
192 | SET(CMAKE_C_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}") |
||
193 | SET(CMAKE_C_FLAGS_DEBUG "-g") |
||
194 | SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG") |
||
195 | SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") |
||
196 | SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g") |
||
7 | pmbaty | 197 | SET(CMAKE_C_FLAGS "-D_QNX_SOURCE=1 -I${QNX_TARGET}/usr/include/devs/include_'${TARGET_ARCH}' -I${QNX_TARGET}/usr/include/devs -DDONT_DEFINE_BSD -DDONT_DEFINE___FreeBSD_kernel__ -DDONT_DEFINE_FSCALE -DDONT_DEFINE_MACHINE") |
2 | pmbaty | 198 | |
199 | SET(CMAKE_CXX_COMPILER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-g++") |
||
200 | SET(CMAKE_CXX_COMPILER_TARGET "gcc_nto${QNX_PROCESSOR}") |
||
201 | SET(CMAKE_CXX_FLAGS_DEBUG "-g") |
||
202 | SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG") |
||
203 | SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") |
||
204 | SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") |
||
7 | pmbaty | 205 | SET(CMAKE_CXX_FLAGS "-D_QNX_SOURCE=1 -I${QNX_TARGET}/usr/include/devs/include_'${TARGET_ARCH}' -I${QNX_TARGET}/usr/include/devs -DDONT_DEFINE_BSD -DDONT_DEFINE___FreeBSD_kernel__ -DDONT_DEFINE_FSCALE -DDONT_DEFINE_MACHINE") |
2 | pmbaty | 206 | |
207 | SET(CMAKE_LINKER "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ld.bfd") |
||
4 | pmbaty | 208 | SET(CMAKE_SHARED_LINKER_FLAGS "-lsocket") |
2 | pmbaty | 209 | SET(CMAKE_EXE_LINKER_FLAGS "-lsocket") |
210 | |||
211 | SET(CMAKE_RANLIB "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ranlib") |
||
212 | |||
7 | pmbaty | 213 | SET(CMAKE_NM "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-nm") |
214 | |||
2 | pmbaty | 215 | SET(CMAKE_AR "${QNX_HOST}/usr/bin/'${TARGET_TRIPLE}'-ar") |
216 | |||
217 | SET(CMAKE_FIND_ROOT_PATH "${QNX_TARGET}") |
||
218 | SET(CMAKE_FIND_ROOT_PATH_HOST_PROGRAM NEVER) |
||
219 | SET(CMAKE_FIND_ROOT_PATH_HOST_LIBRARY ONLY) |
||
220 | SET(CMAKE_FIND_ROOT_PATH_HOST_INCLUDE ONLY) |
||
221 | ' > "${TARGET_TRIPLE}.cmake" |
||
222 | |||
7 | pmbaty | 223 | backup_and_patch_if_necessary() |
224 | { |
||
225 | # handy function that patches a file in LLVM_SOURCES_DIR with a given sed replacement regex if necessary, creating backups |
||
11 | pmbaty | 226 | # args: <file pathname> <grep_string_to_test_for_presence_of_patch> <sed regex> [<second regex> [...]] |
227 | _PATCHEE_PATHNAME="${1}" |
||
228 | _PATCHED_PATTERN="${2}" |
||
7 | pmbaty | 229 | # test if already patched |
11 | pmbaty | 230 | grep -q "${_PATCHED_PATTERN}" "../${LLVM_SOURCES_DIR}/${_PATCHEE_PATHNAME}" && return |
7 | pmbaty | 231 | # tell what we're about to do |
11 | pmbaty | 232 | echo "Patching ${_PATCHEE_PATHNAME}..." |
233 | _DOTTED_NAME="$(echo "${_PATCHEE_PATHNAME}"|tr '/' '.')" |
||
7 | pmbaty | 234 | # have a backup first |
11 | pmbaty | 235 | test -f "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" || cp "../${LLVM_SOURCES_DIR}/${_PATCHEE_PATHNAME}" "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" || exit 1 |
7 | pmbaty | 236 | # perform the patch |
11 | pmbaty | 237 | cp -f "${CURRENT_DIR}/${_DOTTED_NAME} [ORIGINAL]" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]" || exit 1 |
238 | while [ -n "${3}" ]; do |
||
239 | sed -E -i "${3}" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]" |
||
240 | shift |
||
241 | done |
||
7 | pmbaty | 242 | # verify that we did it successfully |
11 | pmbaty | 243 | if ! grep -q "${_PATCHED_PATTERN}" "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]"; then |
13 | pmbaty | 244 | print_error_and_die "Error: the file ${_PATCHEE_PATHNAME} could not be patched. Please investigate and fix manually." |
7 | pmbaty | 245 | fi |
246 | # and put the patched file in place |
||
11 | pmbaty | 247 | cp -f "${CURRENT_DIR}/${_DOTTED_NAME} [PATCHED]" "../${LLVM_SOURCES_DIR}/${_PATCHEE_PATHNAME}" || exit 1 |
7 | pmbaty | 248 | } |
249 | |||
13 | pmbaty | 250 | # patch llvm/lib/Support/Unix/Path.inc if not done yet |
251 | # replace: |
||
252 | # defined(__FreeBSD_kernel__) |
||
253 | # with: |
||
254 | # (defined(__FreeBSD_kernel__) || defined(__QNXNTO__)) |
||
7 | pmbaty | 255 | # RATIONALE: the concerned parts of the QNX platform SDK (/usr/include/devs/*) are actually largely based on FreeBSD code, to conveniently use the FreeBSD network stack. |
256 | # NONETHELESS, QNX *IS NOT* FreeBSD and "__FreeBSD_kernel__" *SHOULD NOT* be defined, else userland code may falsely assume a genuine FreeBSD include tree to be available, |
||
257 | # which leads to compilation errors caused by include files assumed to be there whereas they are in fact nowhere to be found (ex: /usr/include/sys/user.h, among others). |
||
258 | # The QNX8 platform SDK has thus been patched to NOT define __FreeBSD_kernel__ when the -DDONT_DEFINE___FreeBSD_kernel__ flag is passed, and the feature tests in Path.inc |
||
259 | # that branch into FreeBSD APIs (which ones *are* exposed in the QNX libc since QNX largely shares FreeBSD code) are about to be patched right now to accept __QNXNTO__ too. |
||
260 | # Additionally, support for statfs()/fstatfs() has been restored in the QNX8 platform SDK in /usr/include/devs/sys/mount.h where it was claimed but absent from the QNX8 libc. |
||
261 | # FIXME: these hacks should be moved elsewhere not to pollute the QNX8 platform SDK -- it *SHOULD* be possible to build without devs |
||
11 | pmbaty | 262 | backup_and_patch_if_necessary "llvm/lib/Support/Unix/Path.inc" __QNXNTO__ \ |
13 | pmbaty | 263 | 's@defined\(__FreeBSD_kernel__\)@\(defined\(__FreeBSD_kernel__\) \|\| \defined\(__QNXNTO__\)\)@g' |
6 | pmbaty | 264 | |
22 | pmbaty | 265 | # -- 20240621 -- DISABLED -- the only other place (aside from llvm/lib/Support) where getMainExecutable() is defined is in ClangLinkerWrapper.cpp which is the LLD wrapper frontend. |
266 | # -- 20240621 -- DISABLED -- actually llvm/lib/Support/Unix/Path.inc defines *getMainExecutableImpl* and not getMainExecutable, despite what the function comments say. So the code is right. |
||
267 | # -- 20240621 -- DISABLED -- This block can be removed after a few successful thorough rebuilds. |
||
268 | ## LLVM 16: patch llvm/lib/Support/Path.cpp if not done yet |
||
269 | ## replace: |
||
270 | ## std::string getMainExecutable |
||
271 | ## with: |
||
272 | ## #if 0 |
||
273 | ## std::string getMainExecutable |
||
274 | ## and: |
||
275 | ## TempFile::TempFile(StringRef Name, int FD) |
||
276 | ## with: |
||
277 | ## #endif |
||
278 | ## TempFile::TempFile(StringRef Name, int FD) |
||
279 | ## RATIONALE: std::string getMainExecutable(const char *Argv0, void *MainAddr) is already defined in llvm/lib/Support/Unix/Path.inc, this looks like a LLVM bug? |
||
280 | ## Specifically read the following patch list: https://reviews.llvm.org/D109977 -- it looks like the implementation of this diff in LLVM 16.0.6 is incomplete. |
||
281 | ## This patch restores a working state (ante D109977). |
||
282 | #test "$(echo "${LLVM_VERSION}"|cut -d . -f 1)" -lt 17 && backup_and_patch_if_necessary "llvm/lib/Support/Path.cpp" "#if 0" \ |
||
283 | # 's@std::string getMainExecutable@#if 0\nstd::string getMainExecutable@' \ |
||
284 | # 's@TempFile::TempFile\(StringRef Name, int FD\)@#endif\nTempFile::TempFile\(StringRef Name, int FD\)@' |
||
6 | pmbaty | 285 | |
16 | pmbaty | 286 | # Clang front-end patches |
287 | if echo "${LLVM_PROJECTS_TO_BUILD}"|tr ';' '\n'|grep -q "clang"; then |
||
288 | |||
289 | # patch clang/lib/Frontend/InitPreprocessor.cpp |
||
290 | # replace: |
||
291 | # TI.getTargetDefines(LangOpts, Builder); |
||
292 | # with: |
||
293 | # TI.getTargetDefines(LangOpts, Builder); |
||
294 | # #ifdef __QNXNTO__ |
||
295 | # Builder.defineMacro("__QNXNTO__", "1"); |
||
296 | # Builder.defineMacro("__QNX__", "800"); // <-- set to QNX SDK version, without the intermediary dots |
||
18 | pmbaty | 297 | # Builder.defineMacro("__X86_64__", "1"); // <-- set to either __X86_64__ or __AARCH64EL__ depending on the build target |
16 | pmbaty | 298 | # Builder.defineMacro("__LITTLEENDIAN__", "1"); |
299 | # Builder.defineMacro("__ELF__", "1"); |
||
300 | # #endif |
||
301 | # RATIONALE: this adds the QNX-specific implicit preprocessor definitions. This is not the orthodox way of doing it. But we're in a hurry. |
||
18 | pmbaty | 302 | test "${BUILD_TARGET_ARCH}" = "x86_64" && PLATORM_NATIVE_ARCH_DEFINE="__X86_64__" || PLATORM_NATIVE_ARCH_DEFINE="__AARCH64EL__" |
16 | pmbaty | 303 | backup_and_patch_if_necessary "clang/lib/Frontend/InitPreprocessor.cpp" __QNXNTO__ \ |
18 | pmbaty | 304 | 's@TI\.getTargetDefines\(LangOpts, Builder\);@TI\.getTargetDefines\(LangOpts, Builder\);\n#ifdef __QNXNTO__\n Builder.defineMacro\("__QNXNTO__", "1"\);\n Builder.defineMacro("__QNX__", "'$(echo "${QNXSDK_VERSION}"|tr -d '.')'");\n Builder.defineMacro\("'${PLATORM_NATIVE_ARCH_DEFINE}'", "1"\);\n Builder.defineMacro\("__LITTLEENDIAN__", "1"\);\n Builder.defineMacro\("__ELF__", "1"\);\n#endif@' |
16 | pmbaty | 305 | fi |
306 | |||
13 | pmbaty | 307 | # LLDB patches |
308 | if echo "${LLVM_PROJECTS_TO_BUILD}"|tr ';' '\n'|grep -q "lldb"; then |
||
6 | pmbaty | 309 | |
13 | pmbaty | 310 | # patch lldb/Source/Host/common/Host.cpp if not done yet |
311 | # replace: |
||
312 | # defined(SIGINFO) |
||
313 | # with: |
||
314 | # (defined(SIGINFO) && !defined(__QNXNTO__)) |
||
315 | # RATIONALE: once upon a time (probably in the early steps of the design of their OS), the QNX people defined SIGINFO to the value of SIGUSR1 for a mysterious reason, |
||
316 | # defeating the purpose of SIGUSR1 completely, which should be a User-Definable signal as mandated by POSIX. Consequently, any userland code that enumerates POSIX signals |
||
317 | # hits the same value twice, and all is left to solve this problem is to filter either one out. Since SIGINFO, contrarily to SIGUSR1, is an optional signal, |
||
318 | # this is the value that will be left out. It is not possible to correct this in the platform SDK since all the precompiled userland programs supplied by QNX |
||
319 | # expect the value of SIGUSR1 for SIGINFO. |
||
320 | backup_and_patch_if_necessary "lldb/source/Host/common/Host.cpp" __QNXNTO__ \ |
||
321 | 's@defined\(SIGINFO\)@\(defined\(SIGINFO\) \&\& \!defined\(__QNXNTO__\)\)@g' |
||
322 | |||
323 | # FIXME: another patch is needed for LLDB. Basically, the ptrace() POSIX system call doesn't exist on QNX. Instead |
||
324 | # QNX use their own debug utility: pdebug. Note the /proc filesystem can be used to access a debuggee's virtual memory space. |
||
325 | # in lldb/source/Host/posix/ProcessLauncherPosixFork.cpp line 196: |
||
326 | # if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1) // <--- undefined: ptrace, PT_TRACE_ME |
||
327 | fi |
||
6 | pmbaty | 328 | |
13 | pmbaty | 329 | # compiler-rt patches |
330 | if echo "${LLVM_RUNTIMES_TO_BUILD}"|tr ';' '\n'|grep -q "compiler-rt"; then |
||
6 | pmbaty | 331 | |
13 | pmbaty | 332 | # patch compiler-rt/lib/builtins/enable_execute_stack.c if not done yet |
333 | # replace: |
||
334 | # #include "int_lib.h" |
||
335 | # with: |
||
336 | # #ifdef __QNXNTO__ |
||
337 | # #define _QNX_SOURCE 1 |
||
338 | # #endif |
||
339 | # #include "int_lib.h" |
||
340 | # RATIONALE: the QNX people decided to hide most of the UNIX APIs of their libc behind a mandatory _QNX_SOURCE definition to separate the Neutrino primitives from the rest maybe. |
||
341 | # This name does not "enforce" any standard at all (like _POSIX_SOURCE and similar names), it's just a convenient hack for the QNX people to hide stuff. |
||
342 | # To put things back on track and expose the POSIX interfaces of their libc to the world, _QNX_SOURCE has been added to the toolchain CMake file, but it happens that |
||
343 | # when building compiler-rt specifically, the CMAKE_C_FLAGS directive is ignored. Patching the source file seems like the best option to fix that. |
||
344 | backup_and_patch_if_necessary "compiler-rt/lib/builtins/enable_execute_stack.c" __QNXNTO__ \ |
||
345 | 's@#include "int_lib.h"@#ifdef __QNXNTO__\n#define _QNX_SOURCE 1\n#endif\n#include "int_lib.h"@' |
||
8 | pmbaty | 346 | |
13 | pmbaty | 347 | # patch compiler-rt/lib/profile/InstrProfilingUtil.c |
348 | # replace: |
||
349 | # #include <sys/mman.h> |
||
350 | # with: |
||
351 | # #ifdef __QNXNTO__ |
||
352 | # #include <devs/sys/mman.h> |
||
353 | # #endif |
||
354 | # #include <sys/mman.h> |
||
355 | # RATIONALE: QNX doesn't define MADV_DONTNEED (but does so for POSIX_MADV_DONTNEED), unfortunately compiler-rt needs the former. By luck, its value is defined in |
||
356 | # the extra platform headers they borrowed from FreeBSD, i.e. devs/sys/mman.h, and it has the same value, so include both to fix the problem. |
||
357 | backup_and_patch_if_necessary "compiler-rt/lib/profile/InstrProfilingUtil.c" __QNXNTO__ \ |
||
358 | 's@#include <sys/mman.h>@#ifdef __QNXNTO__\n#include <devs/sys/mman.h>\n#endif\n#include <sys/mman.h>@' |
||
10 | pmbaty | 359 | |
13 | pmbaty | 360 | # patch compiler-rt/lib/orc/endianness.h |
361 | # replace: |
||
362 | # #elif defined(__MVS__) |
||
363 | # with: |
||
364 | # #elif defined(__QNXNTO__) |
||
365 | # #define BYTE_ORDER __BYTE_ORDER__ |
||
366 | # #elif defined(__MVS__) |
||
367 | # RATIONALE: QNX doesn't provide a <machine/endian.h> header directly: the user must specify the machine-specific include path (e.g. -isystem ${QNX_TARGET}/usr/include/devs/include_x86_64) |
||
368 | # Instead of going through platform detection logic, we supply the only definition this file needs, which is opportunisticly the same one that GCC and Clang provide implicitly. |
||
369 | backup_and_patch_if_necessary "compiler-rt/lib/orc/endianness.h" __QNXNTO__ \ |
||
370 | 's@#elif defined\(__MVS__\)@#elif defined\(__QNXNTO__\)\n#define BYTE_ORDER __BYTE_ORDER__\n#elif defined\(__MVS__\)@' |
||
371 | fi |
||
11 | pmbaty | 372 | |
13 | pmbaty | 373 | # libunwind patches |
374 | if echo "${LLVM_RUNTIMES_TO_BUILD}"|tr ';' '\n'|grep -q "libunwind"; then |
||
11 | pmbaty | 375 | |
13 | pmbaty | 376 | # patch libunwind/src/libunwind.cpp |
377 | # replace: |
||
378 | # #include <libunwind.h>: |
||
379 | # with: |
||
380 | # #ifdef __QNXNTO__ |
||
381 | # #define _QNX_SOURCE 1 |
||
382 | # #endif |
||
383 | # #include <libunwind.h> |
||
384 | # RATIONALE: the pthreads implementation in QNX 'steals' things from the POSIX.1 standard and hides them behind the __EXT_QNX macro, |
||
385 | # which is defined when either _QNX_SOURCE or __EXT without anything else is defined. This is not correct and low-level libraries |
||
386 | # such as libunwind that do not define either of those fail to build. Also note that a FreeBSD-compatible header was added to the QNX8 |
||
387 | # platform SDK: /usr/include/link.h, which defines Elf types as Elf64/Elf32 types depending on the architecture and adds some glue. |
||
388 | backup_and_patch_if_necessary "libunwind/src/libunwind.cpp" __QNXNTO__ \ |
||
389 | 's@#include <libunwind.h>@#ifdef __QNXNTO__\n#define _QNX_SOURCE 1\n#endif\n#include <libunwind.h>@' |
||
390 | fi |
||
11 | pmbaty | 391 | |
13 | pmbaty | 392 | # libc++ patches |
393 | if echo "${LLVM_PROJECTS_TO_BUILD}"|tr ';' '\n'|grep -q "libcxx"; then |
||
12 | pmbaty | 394 | |
13 | pmbaty | 395 | # patch libcxx/include/__config |
396 | # replace: |
||
397 | # # define _LIBCPP_HAS_THREAD_API_WIN32 |
||
398 | # with: |
||
399 | # # define _LIBCPP_HAS_THREAD_API_WIN32 |
||
400 | # # elif defined(__QNXNTO__) |
||
401 | # # define _LIBCPP_HAS_THREAD_API_PTHREAD |
||
402 | # # define _LIBCPP_HAS_COND_CLOCKWAIT |
||
403 | # # define _LIBCPP_HAS_CLOCK_GETTIME |
||
404 | # RATIONALE: simply bring QNX among the list of systems known by libc++ to expose a pthreads API; while we're at it inform it that pthread_cond_clockwait() is available |
||
405 | # and that the standard POSIX clock_gettime() is available to return a realtime wall clock value in nanoseconds (this might not be the optimal way of getting it though). |
||
406 | backup_and_patch_if_necessary "libcxx/include/__config" __QNXNTO__ \ |
||
407 | 's@# define _LIBCPP_HAS_THREAD_API_WIN32@# define _LIBCPP_HAS_THREAD_API_WIN32\n# elif defined(__QNXNTO__)\n# define _LIBCPP_HAS_THREAD_API_PTHREAD\n# define _LIBCPP_HAS_COND_CLOCKWAIT\n# define _LIBCPP_HAS_CLOCK_GETTIME@' |
||
408 | |||
409 | # patch libcxx/include/__locale |
||
410 | # replace: |
||
411 | # # include <__support/openbsd/xlocale.h> |
||
412 | # with: |
||
413 | # # include <__support/openbsd/xlocale.h> |
||
414 | # #elif defined(__QNXNTO__) |
||
415 | # # include <__support/qnx/xlocale.h> |
||
416 | # and: |
||
417 | # #elif defined(__MVS__) |
||
418 | # with: |
||
419 | # #elif defined(__QNXNTO__) |
||
420 | # typedef short mask; |
||
421 | # static const mask space = (_CN|_SP|_XS); |
||
422 | # static const mask print = (_DI|_LO|_PU|_SP|_UP|_XA); |
||
423 | # static const mask cntrl = _BB; |
||
424 | # static const mask upper = _UP; |
||
425 | # static const mask lower = _LO; |
||
426 | # static const mask alpha = (_LO|_UP|_XA); |
||
427 | # static const mask digit = _DI; |
||
428 | # static const mask punct = _PU; |
||
429 | # static const mask xdigit = _XD; |
||
430 | # static const mask blank = (_SP|_XB); |
||
431 | # static const mask __regex_word = 0x1000; |
||
432 | # #elif defined(__MVS__) |
||
433 | # RATIONALE: add QNX-specific locale C++ bindings and definitions |
||
434 | backup_and_patch_if_necessary "libcxx/include/__locale" __QNXNTO__ \ |
||
435 | 's@# include <__support/openbsd/xlocale.h>@# include <__support/openbsd/xlocale.h>\n#elif defined\(__QNXNTO__\)\n# include <__support/qnx/xlocale.h>@' \ |
||
436 | 's@#elif defined\(__MVS__\)@#elif defined\(__QNXNTO__\)\n typedef short mask;\n static const mask space = \(_CN\|_SP\|_XS\);\n static const mask print = \(_DI\|_LO\|_PU\|_SP\|_UP\|_XA\);\n static const mask cntrl = _BB;\n static const mask upper = _UP;\n static const mask lower = _LO;\n static const mask alpha = \(_LO\|_UP\|_XA\);\n static const mask digit = _DI;\n static const mask punct = _PU;\n static const mask xdigit = _XD;\n static const mask blank = \(_SP\|_XB\);\n static const mask __regex_word = 0x1000;\n#elif defined\(__MVS__\)@' |
||
437 | |||
438 | # patch libcxx/include/CMakeLists.txt |
||
439 | # replace: |
||
440 | # __support/openbsd/xlocale.h |
||
441 | # with: |
||
442 | # __support/openbsd/xlocale.h |
||
443 | # __support/qnx/xlocale.h |
||
444 | # RATIONALE: add QNX-specific locale C++ bindings and definitions |
||
445 | backup_and_patch_if_necessary "libcxx/include/CMakeLists.txt" qnx/xlocale.h \ |
||
446 | 's@ __support/openbsd/xlocale.h@ __support/openbsd/xlocale.h\n __support/qnx/xlocale.h@' |
||
447 | |||
448 | # add __support/qnx/xlocale.h to the libc++ source tree |
||
449 | # RATIONALE: add QNX-specific locale C++ bindings and definitions |
||
450 | test -d "../${LLVM_SOURCES_DIR}/libcxx/include/__support/qnx" || mkdir "../${LLVM_SOURCES_DIR}/libcxx/include/__support/qnx" || exit 1 |
||
451 | test -f "../${LLVM_SOURCES_DIR}/libcxx/include/__support/qnx/xlocale.h" || cp "${QNX_TARGET}/usr/include/c++/v1/__support/qnx/xlocale.h" "../${LLVM_SOURCES_DIR}/libcxx/include/__support/qnx/xlocale.h" || exit 1 |
||
452 | |||
453 | # patch libcxx/include/locale |
||
454 | # replace: |
||
455 | # !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__) |
||
456 | # with: |
||
457 | # !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__) && !defined(__QNXNTO__) |
||
458 | # RATIONALE: QNX doesn't have catopen() to open a "message catalog" |
||
459 | backup_and_patch_if_necessary "libcxx/include/locale" __QNXNTO__ \ |
||
460 | 's@\!defined\(__BIONIC__\) \&\& \!defined\(_NEWLIB_VERSION\) \&\& \!defined\(__EMSCRIPTEN__\)@!defined\(__BIONIC__\) \&\& !defined\(_NEWLIB_VERSION\) \&\& !defined\(__EMSCRIPTEN__\) \&\& !defined\(__QNXNTO__\)@' |
||
461 | |||
462 | # patch libcxx/include/setjmp.h |
||
463 | # replace: |
||
464 | # #endif // __cplusplus |
||
465 | # with: |
||
466 | # #ifdef __QNXNTO__ |
||
467 | # #undef longjmp |
||
468 | # [[noreturn]] inline void longjmp(jmp_buf env, int val) { ::siglongjmp(env, val); } |
||
469 | # #endif // __QNXNTO__ |
||
470 | # |
||
471 | # #endif // __cplusplus |
||
472 | # RATIONALE: restore the thread signal mask when calling longjmp() by calling siglongjmp() instead (not sure why, but that's how they do it in the libc++ 16.0.6 they ship with their SDK) |
||
473 | # FIXME: is this a good idea really? It looks like by calling siglongjmp() instead of longjmp() after setjmp() the QNX people did a broken implementation !? |
||
474 | backup_and_patch_if_necessary "libcxx/include/setjmp.h" __QNXNTO__ \ |
||
475 | 's@#endif // __cplusplus@#ifdef __QNXNTO__\n#undef longjmp\n\[\[noreturn\]\] inline void longjmp\(jmp_buf env, int val\) { ::siglongjmp\(env, val\); }\n#endif // __QNXNTO_\n\n#endif // __cplusplus@' |
||
476 | fi |
||
12 | pmbaty | 477 | |
13 | pmbaty | 478 | # polly patches |
479 | if echo "${LLVM_PROJECTS_TO_BUILD}"|tr ';' '\n'|grep -q "polly"; then |
||
480 | |||
481 | # patch polly/lib/External/isl/imath/imath.c |
||
482 | # replace: |
||
483 | # /* Select min/max. */ |
||
484 | # with: |
||
485 | # #ifdef __QNXNTO__ |
||
486 | # #undef MIN |
||
487 | # #undef MAX |
||
488 | # #endif // __QNXNTO__ |
||
489 | # /* Select min/max. */ |
||
490 | # RATIONALE: QNX already defines MIN/MAX as macros, but polly intends to define them as inline functions. Perhaps this could be avoided by passing _POSIX_SOURCE instead of _QNX_SOURCE. |
||
491 | backup_and_patch_if_necessary "polly/lib/External/isl/imath/imath.c" __QNXNTO__ \ |
||
492 | 's@\/\* Select min\/max. \*\/@#ifdef __QNXNTO__\n#undef MIN\n#undef MAX\n#endif\n\/\* Select min\/max. \*\/@' |
||
493 | fi |
||
494 | |||
495 | # now configure LLVM... |
||
2 | pmbaty | 496 | echo "Configuring LLVM build..." |
9 | pmbaty | 497 | export CCACHE_DIR="$(realpath "../${LLVM_SOURCES_DIR}/.ccache")" |
2 | pmbaty | 498 | cmake \ |
499 | -D CMAKE_TOOLCHAIN_FILE="${TARGET_TRIPLE}.cmake" \ |
||
13 | pmbaty | 500 | -D CMAKE_BUILD_TYPE="${LLVM_BUILD_CONFIGURATION_TYPE}" \ |
501 | -D CMAKE_INSTALL_PREFIX="/usr/bin" \ |
||
502 | -D CMAKE_STAGING_PREFIX="${CURRENT_DIR}/llvm-build/${BUILD_TARGET_ARCH}" \ |
||
9 | pmbaty | 503 | -D CMAKE_C_COMPILER_LAUNCHER="ccache" \ |
504 | -D CMAKE_CXX_COMPILER_LAUNCHER="ccache" \ |
||
2 | pmbaty | 505 | -D LLVM_HOST_TRIPLE="${TARGET_TRIPLE}" \ |
13 | pmbaty | 506 | -D LLVM_ENABLE_PROJECTS="${LLVM_PROJECTS_TO_BUILD}" \ |
507 | -D LLVM_ENABLE_RUNTIMES="${LLVM_RUNTIMES_TO_BUILD}" \ |
||
2 | pmbaty | 508 | -D LLVM_TARGETS_TO_BUILD="AArch64;X86" \ |
16 | pmbaty | 509 | -D DEFAULT_SYSROOT="../../../../../${QNXSDK_TARGETPATH}/${BUILD_TARGET_ARCH}" \ |
510 | -D CLANG_DEFAULT_LINKER="lld" \ |
||
511 | -D CLANG_DEFAULT_CXX_STDLIB="libc++" \ |
||
8 | pmbaty | 512 | -D COMPILER_RT_BUILD_SANITIZERS="OFF" \ |
12 | pmbaty | 513 | -D COMPILER_RT_BUILD_XRAY="OFF" \ |
13 | pmbaty | 514 | -D COMPILER_RT_BUILD_MEMPROF="OFF" \ |
515 | -D COMPILER_RT_BUILD_LIBFUZZER="OFF" \ |
||
516 | -D COMPILER_RT_BUILD_PROFILE="OFF" \ |
||
2 | pmbaty | 517 | -G Ninja \ |
518 | "../${LLVM_SOURCES_DIR}/llvm" || exit 1 |
||
519 | |||
520 | # and do the Lord's work. https://youtu.be/jcyYmCnkbEE |
||
521 | echo "Building LLVM..." |
||
13 | pmbaty | 522 | cmake --build . --target install || exit 1 |
2 | pmbaty | 523 | |
13 | pmbaty | 524 | /bin/printf "\n\xF0\x9F\x8D\xBE\x20\x43\x68\x61\x6d\x70\x61\x67\x6e\x65\x2e\n" |
2 | pmbaty | 525 | exit 0 |
13 | pmbaty | 526 | |
527 | # TODO optional: port compiler_rt sanitizer bindings and build sanitizers, xray, memprof, libfuzzer and profile |
||
528 | # TODO optional: port lldb bindings and build lldb |