Skip to content

Commit 8a86b22

Browse files
committed
build: fix nix coverage test
1 parent 43a9e6c commit 8a86b22

1 file changed

Lines changed: 88 additions & 94 deletions

File tree

flake.nix

Lines changed: 88 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
outputs =
55
{ self, nixpkgs }:
66
let
7-
systems =
7+
localSystems = [
8+
"aarch64-linux"
9+
"x86_64-linux"
10+
"aarch64-darwin"
11+
];
12+
forAllSystems =
13+
f: nixpkgs.lib.genAttrs localSystems (system: f (import nixpkgs { inherit system; }));
14+
forAllSystemsWithCross =
815
f:
916
let
10-
localSystems = [
11-
"aarch64-linux"
12-
"x86_64-linux"
13-
"aarch64-darwin"
14-
];
1517
crossSystem = "aarch64-linux";
1618
in
1719
nixpkgs.lib.genAttrs localSystems (
@@ -25,106 +27,98 @@
2527
);
2628
in
2729
{
28-
packages = systems (
29-
pkgs: crossPkgs: rec {
30+
packages = forAllSystems (
31+
pkgs:
32+
let
3033
smart-piano = pkgs.callPackage ./engine.nix {
3134
inherit self pkgs;
3235
stdenv = pkgs.clangStdenv;
3336
};
37+
in
38+
{
39+
inherit smart-piano;
40+
default = smart-piano;
41+
}
42+
);
43+
crossPackages = forAllSystemsWithCross (
44+
pkgs: crossPkgs:
45+
let
3446
cross-smart-piano = crossPkgs.callPackage ./engine.nix {
3547
inherit self;
3648
stdenv = crossPkgs.clangStdenv;
3749
pkgs = crossPkgs;
3850
};
39-
default = smart-piano;
40-
cross = cross-smart-piano;
41-
}
42-
);
43-
checks = systems (
44-
pkgs: _:
45-
let
46-
smart-piano = self.packages.${pkgs.stdenv.hostPlatform.system}.smart-piano;
4751
in
4852
{
49-
coverage = pkgs.clangStdenv.mkDerivation rec {
50-
pname = "coverage-check";
51-
src = self;
52-
nativeBuildInputs =
53-
(smart-piano.nativeBuildInputs or [ ])
54-
++ (with pkgs; [
55-
lcov
56-
cmake
57-
]);
58-
buildInputs = smart-piano.buildInputs or [ ];
59-
cmakeFlags = [
60-
"-DCMAKE_BUILD_TYPE=Debug"
61-
"-DCOVERAGE=ON"
62-
];
63-
configurePhase = ''
64-
runHook preConfigure
65-
cmake -S . -B build ${toString cmakeFlags}
66-
runHook postConfigure
67-
'';
68-
buildPhase = ''
69-
runHook preBuild
70-
cmake --build build -j''${NIX_BUILD_CORES:-1}
71-
runHook postBuild
72-
'';
73-
checkPhase = ''
74-
runHook preCheck
75-
cd build
76-
ctest --output-on-failure
77-
runHook postCheck
78-
'';
79-
installPhase = ''
80-
runHook preInstall
81-
mkdir -p $out/
82-
cd build
83-
lcov --capture --directory . --output-file coverage.info
84-
lcov --remove coverage.info '/usr/*' --output-file coverage.info.filtered
85-
lcov --remove coverage.info.filtered '*/test/*' --output-file coverage.info.filtered
86-
lcov --remove coverage.info.filtered '*/build/_deps/*' --output-file coverage.info.filtered
87-
genhtml coverage.info.filtered --output-directory $out
88-
echo "Coverage report generated in $out"
89-
lcov --summary coverage.info.filtered
90-
runHook postInstall
91-
'';
92-
meta.description = "Code coverage report for Smart Piano Engine";
93-
};
94-
}
95-
);
96-
devShells = systems (
97-
pkgs: crossPkgs: {
98-
default =
99-
pkgs.mkShell.override
100-
{
101-
stdenv = pkgs.clangStdenv; # Clang instead of GCC
102-
}
103-
{
104-
packages = with pkgs; [
105-
bashInteractive
106-
clang-tools # Clang CLIs, including LSP
107-
clang-uml # UML diagram generator
108-
cmake-format # CMake formatter
109-
cmake-language-server # Cmake LSP
110-
# cppcheck # C++ Static analysis
111-
doxygen # Documentation generator
112-
# fluidsynth # JACK Synthesizer
113-
lldb # Clang debug adapter
114-
# neocmakelsp # CMake LSP
115-
# qsynth # FluidSynth GUI
116-
socat # Serial terminal for manual testing
117-
# valgrind # Debugging and profiling
118-
];
119-
nativeBuildInputs = self.packages.${pkgs.stdenv.hostPlatform.system}.smart-piano.nativeBuildInputs;
120-
buildInputs = self.packages.${pkgs.stdenv.hostPlatform.system}.smart-piano.buildInputs;
121-
# Export compile commands JSON for LSP and other tools
122-
shellHook = ''
123-
mkdir --verbose build
124-
cmake -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S . -B build
125-
'';
126-
};
53+
inherit cross-smart-piano;
54+
default = cross-smart-piano;
12755
}
12856
);
57+
checks = forAllSystems (pkgs: {
58+
coverage = pkgs.clangStdenv.mkDerivation {
59+
name = "coverage-check";
60+
src = self;
61+
nativeBuildInputs =
62+
(self.packages.${pkgs.stdenv.hostPlatform.system}.smart-piano.nativeBuildInputs or [ ])
63+
++ [ pkgs.lcov ];
64+
buildInputs = (self.packages.${pkgs.stdenv.hostPlatform.system}.smart-piano.buildInputs or [ ]);
65+
configurePhase = ''
66+
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=ON
67+
'';
68+
buildPhase = ''
69+
cmake --build build
70+
'';
71+
checkPhase = ''
72+
cmake --build build --target tests
73+
cmake --build build --target coverage
74+
llvm-cov report build/src/main -instr-profile=build/coverage.profdata -ignore-filename-regex='test/.*' > build/coverage.txt
75+
cat build/coverage.txt
76+
echo "Verifying functions coverage is > 90%"
77+
grep TOTAL build/coverage.txt | awk '{ if ($7 + 0 > 90) exit 0; else { print "Function coverage is " $7 "% (< 90%)"; exit 1 } }'
78+
echo "Verifying line coverage is > 90%"
79+
grep TOTAL build/coverage.txt | awk '{ if ($10 + 0 > 90) exit 0; else { print "Line coverage is " $10 "% (< 90%)"; exit 1 } }'
80+
'';
81+
installPhase = ''
82+
mkdir -p $out
83+
if [ -f "build/coverage.txt" ]; then
84+
cp build/coverage.txt $out/
85+
fi
86+
if [ -d "build/coverage" ]; then
87+
cp -R build/coverage $out/html
88+
fi
89+
'';
90+
};
91+
});
92+
devShells = forAllSystems (pkgs: {
93+
default =
94+
pkgs.mkShell.override
95+
{
96+
stdenv = pkgs.clangStdenv; # Clang instead of GCC
97+
}
98+
{
99+
packages = with pkgs; [
100+
bashInteractive
101+
clang-tools # Clang CLIs, including LSP
102+
clang-uml # UML diagram generator
103+
cmake-format # CMake formatter
104+
cmake-language-server # Cmake LSP
105+
# cppcheck # C++ Static analysis
106+
doxygen # Documentation generator
107+
# fluidsynth # JACK Synthesizer
108+
lldb # Clang debug adapter
109+
# neocmakelsp # CMake LSP
110+
# qsynth # FluidSynth GUI
111+
socat # Serial terminal for manual testing
112+
# valgrind # Debugging and profiling
113+
];
114+
nativeBuildInputs = self.packages.${pkgs.stdenv.hostPlatform.system}.smart-piano.nativeBuildInputs;
115+
buildInputs = self.packages.${pkgs.stdenv.hostPlatform.system}.smart-piano.buildInputs;
116+
# Export compile commands JSON for LSP and other tools
117+
shellHook = ''
118+
mkdir --verbose build
119+
cmake -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S . -B build
120+
'';
121+
};
122+
});
129123
};
130124
}

0 commit comments

Comments
 (0)