2022-02-24 11:52:05 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2022-06-08 10:18:46 +00:00
|
|
|
set -e
|
|
|
|
#set -x
|
2022-02-24 11:52:05 +00:00
|
|
|
|
|
|
|
prog_NAME=$(basename "${0}")
|
|
|
|
|
|
|
|
warn()
|
|
|
|
{
|
|
|
|
echo "${prog_NAME}: WARN: $*"
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:18:46 +00:00
|
|
|
info()
|
|
|
|
{
|
|
|
|
echo "${prog_NAME}: INFO: $*"
|
|
|
|
}
|
|
|
|
|
2022-02-24 11:52:05 +00:00
|
|
|
err()
|
|
|
|
{
|
|
|
|
echo "${prog_NAME}: ERROR: $*" 1>&2
|
|
|
|
}
|
|
|
|
|
|
|
|
die()
|
|
|
|
{
|
|
|
|
echo "${prog_NAME}: ERROR: $*" 1>&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
cat <<EOM 1>&2
|
2022-06-08 10:18:46 +00:00
|
|
|
usage: ${prog_NAME} [ OPTIONS ]
|
2022-02-24 11:52:05 +00:00
|
|
|
Generates visualizations
|
|
|
|
Options:
|
|
|
|
--uuid=STRING
|
|
|
|
UUID of build.
|
2022-06-08 10:18:46 +00:00
|
|
|
--data-dir=STRING
|
|
|
|
Path to the data directory.
|
2022-02-24 11:52:05 +00:00
|
|
|
--cache-dir=STRING
|
|
|
|
Path to the cache directory.
|
|
|
|
EOM
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
UUID=
|
2022-06-08 10:18:46 +00:00
|
|
|
CACHE_DIR=
|
|
|
|
DATA_DIR=
|
2022-02-24 11:52:05 +00:00
|
|
|
|
2022-06-08 10:18:46 +00:00
|
|
|
while [ $# -gt 0 ]; do
|
2022-02-24 11:52:05 +00:00
|
|
|
OPT="$1"
|
|
|
|
|
|
|
|
case "${OPT}" in
|
|
|
|
--uuid=*)
|
|
|
|
UUID="${OPT##*=}"
|
|
|
|
;;
|
|
|
|
--cache-dir=*)
|
2022-06-08 10:18:46 +00:00
|
|
|
CACHE_DIR="${OPT##*=}"
|
2022-02-24 11:52:05 +00:00
|
|
|
;;
|
2022-06-08 10:18:46 +00:00
|
|
|
--data-dir=*)
|
|
|
|
DATA_DIR="${OPT##*=}"
|
2022-02-24 11:52:05 +00:00
|
|
|
;;
|
|
|
|
*)
|
2022-06-08 10:18:46 +00:00
|
|
|
warn "Ignoring unknown option: '${OPT}' (Note that this script reads DB)"
|
2022-02-24 11:52:05 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
[ -z "${UUID}" ] && die "The --uuid option must be specified"
|
2022-06-08 10:18:46 +00:00
|
|
|
[ -z "${CACHE_DIR}" ] && die "The --cache-dir option must be specified"
|
|
|
|
[ -z "${DATA_DIR}" ] && die "The --data-dir option must be specified"
|
|
|
|
|
2023-03-13 14:43:54 +00:00
|
|
|
info "processing UUID '${UUID}'"
|
2022-06-08 10:18:46 +00:00
|
|
|
|
|
|
|
DB="${DATA_DIR}/builder.sqlite3"
|
|
|
|
|
|
|
|
get_main_binary () {
|
2024-01-09 14:27:52 +00:00
|
|
|
sqlite3 "${DB}" "SELECT '_artifacts/' || substr(lower(hex(ba.sha256)), 1, 2) || '/' || lower(hex(ba.sha256))
|
|
|
|
FROM build AS b
|
2022-06-08 10:14:08 +00:00
|
|
|
JOIN build_artifact AS ba ON ba.build = b.id AND b.main_binary = ba.id
|
2023-03-13 14:43:54 +00:00
|
|
|
WHERE uuid = '${UUID}';"
|
2022-06-08 10:18:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:14:08 +00:00
|
|
|
BIN="${DATA_DIR}/$(get_main_binary)" || die "Failed to get main binary from database"
|
2023-03-13 14:43:54 +00:00
|
|
|
[ -z "${BIN}" ] && die "No main-binary found in db '${DB}' for build '${UUID}'"
|
2022-06-08 10:18:46 +00:00
|
|
|
|
|
|
|
get_debug_binary () {
|
2024-01-09 14:27:52 +00:00
|
|
|
sqlite3 "${DB}" "SELECT '_artifacts/' || substr(lower(hex(ba.sha256)), 1, 2) || '/' || lower(hex(ba.sha256))
|
|
|
|
FROM build AS b
|
2022-06-08 10:14:08 +00:00
|
|
|
JOIN build_artifact AS ba ON ba.build = b.id
|
|
|
|
WHERE
|
2023-03-13 14:43:54 +00:00
|
|
|
uuid = '${UUID}'
|
2024-01-09 14:27:52 +00:00
|
|
|
AND ba.filepath LIKE '%.debug';"
|
2022-06-08 10:18:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:14:08 +00:00
|
|
|
DEBUG_BIN_RELATIVE="$(get_debug_binary)" || die "Failed to get debug binary from database"
|
2022-06-08 10:18:46 +00:00
|
|
|
|
|
|
|
get_opam_switch () {
|
2024-01-09 14:27:52 +00:00
|
|
|
sqlite3 "${DB}" "SELECT '_artifacts/' || substr(lower(hex(ba.sha256)), 1, 2) || '/' || lower(hex(ba.sha256))
|
|
|
|
FROM build AS b
|
2022-06-08 10:14:08 +00:00
|
|
|
JOIN build_artifact AS ba ON ba.build = b.id
|
|
|
|
WHERE
|
2023-03-13 14:43:54 +00:00
|
|
|
uuid = '${UUID}'
|
2022-06-08 10:14:08 +00:00
|
|
|
AND ba.filepath = 'opam-switch';"
|
2022-06-08 10:18:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:14:08 +00:00
|
|
|
OPAM_SWITCH="$(get_opam_switch)" || die "Failed to get opam switch from database"
|
2023-03-13 14:43:54 +00:00
|
|
|
[ -z "${OPAM_SWITCH}" ] && die "No 'opam-switch' found in db '${DB}' for build '${UUID}'"
|
2022-06-08 10:18:46 +00:00
|
|
|
OPAM_SWITCH="${DATA_DIR}/${OPAM_SWITCH}"
|
|
|
|
|
|
|
|
OPAM_GRAPH="opam-graph"
|
|
|
|
MODULECTOMY="modulectomy"
|
2022-02-24 11:52:05 +00:00
|
|
|
|
2023-03-13 14:43:54 +00:00
|
|
|
LATEST_TREEMAPVIZ_VERSION="$(${MODULECTOMY} --version)" || die "Failed to get modulectomy version"
|
|
|
|
LATEST_DEPENDENCIESVIZ_VERSION="$(${OPAM_GRAPH} --version)" || die "Failed to get opam-graph version"
|
2022-06-08 10:18:46 +00:00
|
|
|
|
|
|
|
TREEMAP_CACHE_DIR="${CACHE_DIR}/treemap_${LATEST_TREEMAPVIZ_VERSION}"
|
|
|
|
DEPENDENCIES_CACHE_DIR="${CACHE_DIR}/dependencies_${LATEST_DEPENDENCIESVIZ_VERSION}"
|
2022-02-24 11:52:05 +00:00
|
|
|
|
2022-03-21 12:51:23 +00:00
|
|
|
mktemp_aux () {
|
|
|
|
if [ "$(uname)" = "Linux" ]; then
|
|
|
|
mktemp -t "$1.XXX"
|
|
|
|
elif [ "$(uname)" = "FreeBSD" ]; then
|
|
|
|
mktemp -t "$1"
|
|
|
|
else
|
2022-06-08 10:18:46 +00:00
|
|
|
die 'Unsupported platform'
|
2022-03-21 12:51:23 +00:00
|
|
|
fi
|
|
|
|
}
|
2022-06-08 10:18:46 +00:00
|
|
|
|
|
|
|
TMPTREE=$(mktemp_aux viz_treemap)
|
|
|
|
TMPDEPENDENCIES=$(mktemp_aux viz_dependencies)
|
|
|
|
|
2022-02-24 11:52:05 +00:00
|
|
|
cleanup () {
|
2022-06-08 10:18:46 +00:00
|
|
|
rm -rf "${TMPTREE}" "${TMPDEPENDENCIES}"
|
2022-02-24 11:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
2022-06-08 10:18:46 +00:00
|
|
|
# /// Dependencies viz
|
|
|
|
|
|
|
|
if [ ! -d "${DEPENDENCIES_CACHE_DIR}" ]; then
|
2023-03-13 14:43:54 +00:00
|
|
|
mkdir "${DEPENDENCIES_CACHE_DIR}" || die "Failed to create directory '${DEPENDENCIES_CACHE_DIR}'"
|
2022-06-08 10:18:46 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
OPAM_SWITCH_FILEPATH='opam-switch'
|
|
|
|
|
|
|
|
get_opam_switch_hash () {
|
2022-06-08 10:14:08 +00:00
|
|
|
sqlite3 "${DB}" "SELECT lower(hex(ba.sha256)) FROM build AS b
|
|
|
|
JOIN build_artifact AS ba ON ba.build = b.id
|
2023-03-13 14:43:54 +00:00
|
|
|
WHERE uuid = '${UUID}'
|
|
|
|
AND ba.filepath = '${OPAM_SWITCH_FILEPATH}';"
|
2022-06-08 10:18:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:14:08 +00:00
|
|
|
DEPENDENCIES_INPUT_HASH="$(get_opam_switch_hash)" || die "Failed to get opam-switch hash from database"
|
2022-06-08 10:18:46 +00:00
|
|
|
DEPENDENCIES_VIZ_FILENAME="${DEPENDENCIES_CACHE_DIR}/${DEPENDENCIES_INPUT_HASH}.html"
|
|
|
|
|
|
|
|
if [ -e "${DEPENDENCIES_VIZ_FILENAME}" ]; then
|
|
|
|
info "Dependency visualization already exists: '${DEPENDENCIES_VIZ_FILENAME}'"
|
2022-02-24 11:52:05 +00:00
|
|
|
else
|
2022-06-08 10:18:46 +00:00
|
|
|
if ${OPAM_GRAPH} --output-format=html "${OPAM_SWITCH}" > "${TMPDEPENDENCIES}"; then
|
2022-08-25 13:48:21 +00:00
|
|
|
cp "${TMPDEPENDENCIES}" "${DEPENDENCIES_VIZ_FILENAME}"
|
2023-03-13 14:43:54 +00:00
|
|
|
rm "${TMPDEPENDENCIES}"
|
2022-06-08 10:18:46 +00:00
|
|
|
else
|
|
|
|
die "opam-graph failed to generate visualization"
|
2022-02-24 11:52:05 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-06-08 10:18:46 +00:00
|
|
|
# /// Treemap viz
|
|
|
|
|
2022-03-21 12:51:23 +00:00
|
|
|
stat_aux () {
|
|
|
|
if [ "$(uname)" = "Linux" ]; then
|
|
|
|
stat -c "%s" "$1"
|
|
|
|
elif [ "$(uname)" = "FreeBSD" ]; then
|
|
|
|
stat -f "%z" "$1"
|
|
|
|
else
|
2022-06-08 10:18:46 +00:00
|
|
|
die 'Unsupported platform'
|
2022-03-21 12:51:23 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-13 14:43:54 +00:00
|
|
|
SIZE="$(stat_aux "${BIN}")"
|
2022-06-08 10:18:46 +00:00
|
|
|
|
|
|
|
if [ ! -d "${TREEMAP_CACHE_DIR}" ]; then
|
2023-03-13 14:43:54 +00:00
|
|
|
mkdir "${TREEMAP_CACHE_DIR}" || die "Failed to create directory '${TREEMAP_CACHE_DIR}'"
|
2022-06-08 10:18:46 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
get_debug_bin_hash () {
|
2022-06-08 10:14:08 +00:00
|
|
|
sqlite3 "${DB}" "SELECT lower(hex(ba.sha256)) FROM build AS b
|
|
|
|
JOIN build_artifact AS ba ON ba.build = b.id
|
2023-03-13 14:43:54 +00:00
|
|
|
WHERE uuid = '${UUID}'
|
2022-06-08 10:14:08 +00:00
|
|
|
AND ba.filepath LIKE '%.debug';"
|
2022-06-08 10:18:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:14:08 +00:00
|
|
|
TREEMAP_INPUT_HASH="$(get_debug_bin_hash)" || die "Failed to get treemap input hash from database"
|
2022-06-08 10:18:46 +00:00
|
|
|
TREEMAP_VIZ_FILENAME="${TREEMAP_CACHE_DIR}/${TREEMAP_INPUT_HASH}.html"
|
2022-02-24 11:52:05 +00:00
|
|
|
|
2022-06-08 10:18:46 +00:00
|
|
|
if [ -n "${DEBUG_BIN_RELATIVE}" ]; then
|
|
|
|
DEBUG_BIN="${DATA_DIR}/$(get_debug_binary)"
|
|
|
|
if [ -e "${TREEMAP_VIZ_FILENAME}" ]; then
|
|
|
|
info "Treemap visualization already exists: '${TREEMAP_VIZ_FILENAME}'"
|
2022-02-24 11:52:05 +00:00
|
|
|
else
|
2022-06-08 10:18:46 +00:00
|
|
|
if
|
|
|
|
${MODULECTOMY} \
|
|
|
|
--robur-defaults \
|
|
|
|
--with-scale="${SIZE}" \
|
|
|
|
"${DEBUG_BIN}" \
|
|
|
|
> "${TMPTREE}"
|
|
|
|
then
|
2022-08-25 13:48:21 +00:00
|
|
|
cp "${TMPTREE}" "${TREEMAP_VIZ_FILENAME}"
|
2023-03-13 14:43:54 +00:00
|
|
|
rm "${TMPTREE}"
|
2022-06-08 10:18:46 +00:00
|
|
|
else
|
|
|
|
die "modulectomy failed to generate visualization"
|
2022-02-24 11:52:05 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
2022-06-08 10:18:46 +00:00
|
|
|
info "No --debug-binary provided, not producing any treemap"
|
2022-02-24 11:52:05 +00:00
|
|
|
fi
|