Debian Codegraph @

mkosi/mkosi.coverage/mkosi.postinst

raw
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -e
4
5 (
6 shopt -s nullglob
7 rm -f "$BUILDROOT"/coverage/*.gcda
8 )
9
10 mkdir -p "$BUILDROOT/coverage"
11
12 # When using -fprofile-dir=, GCC creates all gcda files under the given directory at the same location as the
13 # gcno file in the build directory, but with each '/' replaced with '#'. LLVM creates each gcda file under
14 # the given directory without replacing each '/' with '#'. Because we want all processes to be able to write
15 # gcda files under /coverage regardless of which user they are running as, we pre-create all files under
16 # /coverage and make them world readable and writable so that we don't have to mess with umasks for each
17 # process that writes to /coverage.
18 if ((LLVM)); then
19 rsync --recursive --include='*/' --exclude='*' --relative "$BUILDDIR" "$BUILDROOT/coverage"
20 find "$BUILDDIR" -name '*.gcno' | sed 's/gcno/gcda/' | xargs -I '{}' touch "$BUILDROOT/coverage/{}"
21 else
22 find "$BUILDDIR" -name '*.gcno' | sed 's/gcno/gcda/' | sed 's/\//#/g' | xargs -I '{}' touch "$BUILDROOT/coverage/{}"
23 fi
24
25 chmod --recursive 777 "$BUILDROOT/coverage"
26
27 # When built with gcov, disable ProtectSystem= and ProtectHome= in the test images, since it prevents gcov to
28 # write the coverage reports (*.gcda files).
29 mkdir -p "$BUILDROOT/usr/lib/systemd/system/service.d/"
30 cat >"$BUILDROOT/usr/lib/systemd/system/service.d/99-gcov-override.conf" <<EOF
31 [Service]
32 ProtectSystem=no
33 ProtectHome=no
34 EOF
35
36 # Similarly, set ReadWritePaths= to the coverage directory in the test image to make the coverage work with
37 # units using DynamicUser=yes. Do this only for services with test- prefix and a couple of known-to-use
38 # DynamicUser=yes services, as setting this system-wide has many undesirable side-effects, as it creates its
39 # own namespace.
40 for service in capsule@ test- systemd-journal-{gatewayd,upload}; do
41 mkdir -p "$BUILDROOT/usr/lib/systemd/system/$service.service.d/"
42 cat >"$BUILDROOT/usr/lib/systemd/system/$service.service.d/99-gcov-rwpaths-override.conf" <<EOF
43 [Service]
44 ReadWritePaths=/coverage
45 EOF
46 done
47
48 # Ditto, but for the user daemon.
49 mkdir -p "$BUILDROOT/usr/lib/systemd/user/test-.service.d/"
50 cat >"$BUILDROOT/usr/lib/systemd/user/test-.service.d/99-gcov-rwpaths-override.conf" <<EOF
51 [Service]
52 ReadWritePaths=/coverage
53 EOF
54
55 # Bind the coverage directory into nspawn containers that are executed using machinectl. Unfortunately, the
56 # .nspawn files don't support drop-ins so we have to inject the bind mount directly into the
57 # [email protected] unit.
58 sed -ri "s/^ExecStart=.+$/& --bind=\/coverage/" "$BUILDROOT/usr/lib/systemd/system/[email protected]"