Files
vectorscan/cmake/build_wrapper.sh
tnias 90c0ba195b Improve the (cross-)build experience especially with LLVM (#353)
* Suppress clang warnings caused by boost headers

* Make binutils selectable via environment variables

Some build environments specify what tools to use via environment variables.

e.g. NM could be 'x86_64-unknown-linux-gnu-nm' when cross-compiling

* Use llvm-nm friendly non-short format value

LLVM's nm implementaion (llvm-nm) wants the entire string to match.
It doesn't allow just 'p' it wants 'posix'.

- https://github.com/llvm/llvm-project/blob/llvmorg-21.1.3/llvm/tools/llvm-nm/llvm-nm.cpp#L2435-L2448

The nm implementations from GNU binutils and FreeBSD parse the format
argument value only by looking at the first character.
They are accept both 'p' and 'posix'.

- https://github.com/bminor/binutils-gdb/blob/binutils-2_45/binutils/nm.c#L410-L438
- https://github.com/freebsd/freebsd-src/blob/release/14.3.0/contrib/elftoolchain/nm/nm.c#L506-L529
2025-10-12 18:37:47 +03:00

40 lines
1.2 KiB
Bash
Executable File

#!/bin/sh -e
# This is used for renaming symbols for the fat runtime, don't call directly
# TODO: make this a lot less fragile!
cleanup () {
rm -f ${SYMSFILE} ${KEEPSYMS}
}
NM="${NM:-nm}"
OBJCOPY="${OBJCOPY:-objcopy}"
PREFIX=$1
KEEPSYMS_IN=$2
shift 2
# $@ contains the actual build command
OUT=$(echo "$@" | rev | cut -d ' ' -f 2- | rev | sed 's/.* -o \(.*\.o\).*/\1/')
trap cleanup INT QUIT EXIT
SYMSFILE=$(mktemp -p /tmp ${PREFIX}_rename.syms.XXXXX)
KEEPSYMS=$(mktemp -p /tmp keep.syms.XXXXX)
# find the libc used by gcc
LIBC_SO=$("$@" --print-file-name=libc.so.6)
NM_FLAG="-f"
if [ `uname` = "FreeBSD" ]; then
# for freebsd, we will specify the name,
# we will leave it work as is in linux
LIBC_SO=/lib/libc.so.7
# also, in BSD, the nm flag -F corresponds to the -f flag in linux.
NM_FLAG="-F"
fi
cp ${KEEPSYMS_IN} ${KEEPSYMS}
# get all symbols from libc and turn them into patterns
${NM} ${NM_FLAG} posix -g -D ${LIBC_SO} | sed 's/\([^ @]*\).*/^\1$/' >> ${KEEPSYMS}
# build the object
"$@"
# rename the symbols in the object
${NM} ${NM_FLAG} posix -g ${OUT} | cut -f1 -d' ' | grep -v -f ${KEEPSYMS} | sed -e "s/\(.*\)/\1\ ${PREFIX}_\1/" >> ${SYMSFILE}
if test -s ${SYMSFILE}
then
${OBJCOPY} --redefine-syms=${SYMSFILE} ${OUT}
fi