Skip to content

Instantly share code, notes, and snippets.

@StefanHamminga
Created November 21, 2017 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanHamminga/eef90fd72f9fa0e7a560524f378c973f to your computer and use it in GitHub Desktop.
Save StefanHamminga/eef90fd72f9fa0e7a560524f378c973f to your computer and use it in GitHub Desktop.
Bash function to build, sanitise, debug and PGO profile a single source C++ file such as a (header-only) library test file
function compile++ {
local outfn="${1%.*}"
ionice -c 3 \
nice -n 19 \
g++ \
-std=gnu++17 \
-pipe \
-march=haswell \
-mtune=skylake \
-O3 \
-fconstexpr-loop-limit=2147483647 \
-fstack-protector-strong \
-flto \
-fno-fat-lto-objects \
-fuse-linker-plugin \
-fdiagnostics-color=always \
-fbounds-check \
-fstack-check \
-fsanitize=undefined,address \
-ggdb \
-Wl\,--unresolved-symbols=report-all \
-Wl\,--print-memory-usage \
-Wno-packed-bitfield-compat \
-Wall \
-Wextra \
-Wuninitialized \
-Wnull-dereference \
-Wdouble-promotion \
-Wchkp \
-Wmisleading-indentation \
-Wduplicated-cond \
-Wduplicated-branches \
-Wlogical-op \
-Wrestrict \
-Wshadow \
-Wformat=2 \
-Wno-pmf-conversions \
-Wuseless-cast \
-o "${outfn}" "$1" || return $?
# Valgrind does not combine with the run-time sanitizer:
# valgrind --track-origins=yes --leak-check=full ./"${outfn}"
echo "*** Running with full instrumentation (run-time sanitizer) and debugging:" && sleep 30s && \
./"${outfn}"
if [ $? -eq 0 ]; then
ionice -c 3 \
nice -n 19 \
g++ \
-std=gnu++17 \
-pipe \
-march=haswell \
-mtune=skylake \
-O3 \
-fconstexpr-loop-limit=2147483647 \
-flto \
-fno-fat-lto-objects \
-fuse-linker-plugin \
-fdiagnostics-color=always \
-fprofile-generate \
--coverage \
-o "${outfn}" "$1" && \
echo "*** Running optimized and PGO instrumented:" && sleep 30s &&\
./"${outfn}" && \
ionice -c 3 \
nice -n 19 \
g++ \
-std=gnu++17 \
-pipe \
-march=haswell \
-mtune=skylake \
-O3 \
-fconstexpr-loop-limit=2147483647 \
-flto \
-fno-fat-lto-objects \
-fuse-linker-plugin \
-fdiagnostics-color=always \
-fprofile-use \
-o "${outfn}.pgo" "$1" && \
echo "*** Running optimized and PGO profiled:" && sleep 30s && \
./"${outfn}.pgo"
gcov "$1" -mrblf > /dev/null
else
gdb -ex "set logging file /tmp/gdb-$1-`date +%Y-%m-%d_%H:%M:%S`.txt" -ex "set logging on" -ex run -ex bt -ex quit "${outfn}"
fi
}
complete -f -X '!*.@(cpp|cxx|c++|CPP|CXX|C++)' compile++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment