2022-02-19 23:09:40 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2022-12-05 00:31:27 +00:00
|
|
|
SCRIPT_PATH="$(dirname "${0}")"
|
|
|
|
cd "${SCRIPT_PATH}"
|
|
|
|
|
2022-02-19 23:09:40 +00:00
|
|
|
BEST_CLANG_CANDIDATE=""
|
|
|
|
|
|
|
|
die() {
|
|
|
|
>&2 echo "die: $*"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pick_clang() {
|
|
|
|
local BEST_VERSION=0
|
2023-05-21 09:04:47 +00:00
|
|
|
for CLANG_CANDIDATE in clang clang-14 clang-15 /opt/homebrew/opt/llvm/bin/clang ; do
|
2022-02-19 23:09:40 +00:00
|
|
|
if ! command -v $CLANG_CANDIDATE >/dev/null 2>&1; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if $CLANG_CANDIDATE --version 2>&1 | grep "Apple clang" >/dev/null; then
|
2023-02-26 03:28:58 +00:00
|
|
|
echo "Skipping Apple clang, as Apple does not ship libfuzzer with Xcode..."
|
2022-02-19 23:09:40 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if ! $CLANG_CANDIDATE -dumpversion >/dev/null 2>&1; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
local VERSION=""
|
|
|
|
VERSION="$($CLANG_CANDIDATE -dumpversion)"
|
|
|
|
local MAJOR_VERSION="${VERSION%%.*}"
|
|
|
|
if [ "$MAJOR_VERSION" -gt "$BEST_VERSION" ]; then
|
|
|
|
BEST_VERSION=$MAJOR_VERSION
|
|
|
|
BEST_CLANG_CANDIDATE="$CLANG_CANDIDATE"
|
|
|
|
fi
|
|
|
|
done
|
2023-05-21 09:04:47 +00:00
|
|
|
if [ "$BEST_VERSION" -lt 14 ]; then
|
|
|
|
die "Please make sure that Clang version 14 or higher is installed."
|
2022-02-19 23:09:40 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Save flags for oss-fuzz to avoid fuzzing Tools/
|
|
|
|
# https://google.github.io/oss-fuzz/getting-started/new-project-guide/#temporarily-disabling-code-instrumentation-during-builds
|
|
|
|
CFLAGS_SAVE="$CFLAGS"
|
|
|
|
CXXFLAGS_SAVE="$CXXFLAGS"
|
|
|
|
unset CFLAGS
|
|
|
|
unset CXXFLAGS
|
|
|
|
export AFL_NOOPT=1
|
|
|
|
|
|
|
|
# FIXME: Replace these CMake invocations with a CMake superbuild?
|
|
|
|
echo "Building Lagom Tools..."
|
|
|
|
cmake -GNinja -B Build/tools \
|
|
|
|
-DBUILD_LAGOM=OFF \
|
2022-07-11 07:07:47 +00:00
|
|
|
-DCMAKE_INSTALL_PREFIX=Build/tool-install \
|
|
|
|
-Dpackage=LagomTools
|
2022-02-19 23:09:40 +00:00
|
|
|
ninja -C Build/tools install
|
|
|
|
|
|
|
|
# Restore flags for oss-fuzz
|
|
|
|
export CFLAGS="${CFLAGS_SAVE}"
|
|
|
|
export CXXFLAGS="${CXXFLAGS_SAVE}"
|
|
|
|
unset AFL_NOOPT
|
|
|
|
|
|
|
|
echo "Building Lagom Fuzzers..."
|
|
|
|
|
|
|
|
if [ "$#" -gt "0" ] && [ "--oss-fuzz" = "$1" ] ; then
|
|
|
|
echo "Building for oss-fuzz configuration..."
|
|
|
|
cmake -GNinja -B Build/fuzzers \
|
|
|
|
-DBUILD_LAGOM=ON \
|
|
|
|
-DBUILD_SHARED_LIBS=OFF \
|
2022-03-29 14:31:51 +00:00
|
|
|
-DENABLE_FUZZERS_OSSFUZZ=ON \
|
2022-02-19 23:09:40 +00:00
|
|
|
-DCMAKE_C_COMPILER="$CC" \
|
|
|
|
-DCMAKE_CXX_COMPILER="$CXX" \
|
|
|
|
-DCMAKE_CXX_FLAGS="$CXXFLAGS -DOSS_FUZZ=ON" \
|
|
|
|
-DLINKER_FLAGS="$LIB_FUZZING_ENGINE" \
|
|
|
|
-DCMAKE_PREFIX_PATH=Build/tool-install
|
|
|
|
ninja -C Build/fuzzers
|
|
|
|
cp Build/fuzzers/Fuzzers/Fuzz* "$OUT"/
|
2023-04-13 14:22:03 +00:00
|
|
|
elif [ "$#" -gt "0" ] && [ "--standalone" = "$1" ] ; then
|
|
|
|
echo "Building for standalone fuzz configuration..."
|
|
|
|
cmake -GNinja -B Build/lagom-fuzzers-standalone \
|
|
|
|
-DBUILD_LAGOM=ON \
|
|
|
|
-DENABLE_FUZZERS=ON \
|
|
|
|
-DCMAKE_PREFIX_PATH=Build/tool-install
|
|
|
|
ninja -C Build/lagom-fuzzers-standalone
|
2022-02-19 23:09:40 +00:00
|
|
|
else
|
|
|
|
echo "Building for local fuzz configuration..."
|
|
|
|
pick_clang
|
|
|
|
cmake -GNinja -B Build/lagom-fuzzers \
|
|
|
|
-DBUILD_LAGOM=ON \
|
2022-03-29 14:31:51 +00:00
|
|
|
-DENABLE_FUZZERS_LIBFUZZER=ON \
|
2022-02-19 23:09:40 +00:00
|
|
|
-DENABLE_ADDRESS_SANITIZER=ON \
|
|
|
|
-DENABLE_UNDEFINED_SANITIZER=ON \
|
|
|
|
-DCMAKE_PREFIX_PATH=Build/tool-install \
|
|
|
|
-DCMAKE_C_COMPILER=$BEST_CLANG_CANDIDATE \
|
|
|
|
-DCMAKE_CXX_COMPILER="${BEST_CLANG_CANDIDATE/clang/clang++}"
|
|
|
|
ninja -C Build/lagom-fuzzers
|
|
|
|
fi
|