core-lightning/tools/update-mocks.sh
Vasil Dimov b2c4d5e952 build: fix developer mode compilation on FreeBSD
Recent FreeBSD versions use LLVM's lld as a linker, not the GNU ld.
Their behavior slightly differs, so adapt the build system to handle
either one.

* The LLVM's linker prints "undefined symbol:" instead of
  "undefined reference to". Tweak tools/mockup.sh to also look for that
  message.

* The LLVM's linker may only print the first dozen errors (omitting
  the rest to avoid flooding the screen). tools/update-mocks.sh relies
  on getting all errors as it extracts the missing symbols' names from
  the error output and creates mocks for them. Detect if errors were
  omitted and re-run, telling the linker to not omit any. The GNU linker
  does not support -error-limit=0, so unfortunately we can't just run
  with that option unconditionally from the first attempt.

* Nit: FreeBSD's sed(1) prints "t" for "\t" instead of a horizontal tab.
  Use a verbatim tab in the command, instead of "\t" which works on
  both.

Changelog-Fixed: Developer mode compilation on FreeBSD.
2020-01-02 16:56:20 +01:00

51 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Script to rewrite the autogenerated mocks in a unit test between
# /* AUTOGENERATED MOCKS START */ and /* AUTOGENERATED MOCKS END */
# based on link failures.
set -e
FILE="$1"
BASE=/tmp/mocktmp.$$.$(echo "$@" | tr / _)
trap 'mv $BASE.old $FILE; rm -f $BASE.*' EXIT
START=$(grep -F -n '/* AUTOGENERATED MOCKS START */' "$FILE" | cut -d: -f1)
END=$(grep -F -n '/* AUTOGENERATED MOCKS END */' "$FILE" | cut -d: -f1)
function make_binary() {
$MAKE "${FILE/%.c/}" 2> "${BASE}.err" >/dev/null
}
if [ -n "$START" ]; then
mv "$FILE" "${BASE}.old"
echo "${FILE}:"
head -n "$START" "${BASE}.old" > "$FILE"
tail -n +"$END" "${BASE}.old" >> "$FILE"
# Try to make binary.
if ! make_binary; then
# Some linkers (e.g. LLVM's one) don't print all errors. If this is the
# case, then re-run, asking them to do so. Search for something like
# this in the output:
# ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
if grep -q 'too many errors emitted.*-error-limit=0' "${BASE}.err"; then
LDFLAGS=-Wl,-error-limit=0 make_binary || :
fi
tools/mockup.sh < "${BASE}.err" >> "${BASE}.stubs"
# If there are no link errors, maybe compile fail for other reason?
if ! grep -F -q 'Generated stub for' "${BASE}.stubs"; then
cat "${BASE}.err"
exit 1
fi
sed -n 's,.*Generated stub for \(.*\) .*, \1,p' < "${BASE}.stubs"
head -n "$START" "${BASE}.old" > "$FILE"
cat "${BASE}.stubs" >> "$FILE"
tail -n +"$END" "${BASE}.old" >> "$FILE"
else
echo "...build succeeded without stubs"
fi
fi
# All good.
rm -f "$BASE".*
trap "" EXIT