Skip to content

Commit c32217e

Browse files
ganesh-k13stefanv
andauthored
Add gcov flags for build (#146)
* ENH: Added gcov flags for build * MAINT: Changed build flag name to `gcov` * TST: Added tests for `gcov` flag * ENH: Added `perror` and few visual changes * DOC: Added details on generating coverage reports * Expand gcov documentation * Minor tidy-up of meson_args list conversion --------- Co-authored-by: Stefan van der Walt <[email protected]>
1 parent e73d7d7 commit c32217e

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

.github/workflows/test.sh

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ set -e
33
PLATFORM=$(python -c 'import sys; print(sys.platform)')
44

55
RED="\033[31;1m"
6+
BLUE="\033[34m"
67
MAGENTA="\033[35m"
78
NORMAL="\033[0m"
89

910
ptest() { echo -e "\n${MAGENTA}[TEST] $@${NORMAL}\n" ; }
10-
prun() { echo -e "$RED\$ $@ $NORMAL" ; "$@" ; }
11+
perror() { echo -e "\n${RED}[ERROR] $@${NORMAL}\n" ; }
12+
prun() { echo -e "${BLUE}\$ $@ ${NORMAL}" ; "$@" ; }
1113

1214
prun cd example_pkg
1315

@@ -16,13 +18,32 @@ prun spin --version
1618

1719
ptest build command runs
1820
pip install meson-python ninja
19-
prun spin build
21+
22+
23+
# Test spin build + debug builds
24+
echo "Creating debug builds"
25+
prun spin build --gcov
26+
ptest Did the build folder get generated?
27+
if [ ! -d "build" ] || [ ! -d "build-install" ]; then
28+
perror build and/or build-install folders did not get generated
29+
exit 1
30+
else
31+
echo "Yes"
32+
fi
33+
ptest Does the debug build contain gcov files?
34+
matching_files=$(find . -type f -name "*.gc*")
35+
if [ -z "$matching_files" ]; then
36+
perror Debug files did not get generated
37+
exit 1
38+
else
39+
echo "Yes"
40+
fi
2041

2142
ptest Does spin expand \$PYTHONPATH?
2243
SPIN_PYTHONPATH=$(spin run 'echo $PYTHONPATH')
2344
echo spin sees PYTHONPATH=\"${SPIN_PYTHONPATH}\"
2445
if [[ ${SPIN_PYTHONPATH} == "\$PYTHONPATH" ]]; then
25-
echo "Expected Python path, but got $SPIN_PYTHONPATH instead"
46+
perror Expected Python path, but got $SPIN_PYTHONPATH instead
2647
exit 1
2748
fi
2849

@@ -39,7 +60,7 @@ VERSION=$(spin run python -c 'import sys; del sys.path[0]; import example_pkg; p
3960
if [[ $VERSION == "0.0.0dev0" ]]; then
4061
echo "Yes"
4162
else
42-
echo "No, output is $VERSION"
63+
perror No, output is $VERSION
4364
exit 1
4465
fi
4566

@@ -49,7 +70,7 @@ OUT=$(spin run ls)
4970
if [[ $OUT == *"Warning! An editable installation"* ]]; then
5071
echo "Yes"
5172
else
52-
echo "No"
73+
perror No
5374
exit 1
5475
fi
5576
prun pip uninstall --quiet -y example_pkg
@@ -63,7 +84,7 @@ if [[ $PLATFORM == linux || $PLATFORM == darwin ]]; then
6384
if [[ $OUT == *"Did you mean to call"* ]]; then
6485
echo "Yes"
6586
else
66-
echo "No, output is: $OUT"
87+
perror No, output is: $OUT
6788
exit 1
6889
fi
6990
fi

spin/cmds/meson.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,24 @@ def _meson_version_configured():
130130
@click.option(
131131
"-v", "--verbose", is_flag=True, help="Print detailed build and installation output"
132132
)
133+
@click.option(
134+
"--gcov",
135+
is_flag=True,
136+
help="""Enable C code coverage via `gcov`.
137+
138+
The meson-generated `build/build.ninja` has targets for compiling
139+
coverage reports.
140+
141+
E.g., to build an HTML report, in the `build` directory run
142+
`ninja coverage-html`.
143+
144+
To see a list all supported formats, run
145+
`ninja -t targets | grep coverage-`.
146+
147+
Also see https://mesonbuild.com/howtox.html#producing-a-coverage-report.""",
148+
)
133149
@click.argument("meson_args", nargs=-1)
134-
def build(meson_args, jobs=None, clean=False, verbose=False, quiet=False):
150+
def build(meson_args, jobs=None, clean=False, verbose=False, gcov=False, quiet=False):
135151
"""🔧 Build package with Meson/ninja and install
136152
137153
MESON_ARGS are passed through e.g.:
@@ -150,7 +166,12 @@ def build(meson_args, jobs=None, clean=False, verbose=False, quiet=False):
150166
CFLAGS="-O0 -g" spin build
151167
"""
152168
build_dir = "build"
153-
setup_cmd = _meson_cli() + ["setup", build_dir, "--prefix=/usr"] + list(meson_args)
169+
meson_args = list(meson_args)
170+
171+
if gcov:
172+
meson_args = meson_args + ["-Db_coverage=true"]
173+
174+
setup_cmd = _meson_cli() + ["setup", build_dir, "--prefix=/usr"] + meson_args
154175

155176
if clean:
156177
print(f"Removing `{build_dir}`")
@@ -236,8 +257,15 @@ def _get_configured_command(command_name):
236257
is_flag=True,
237258
help="Generate a coverage report of executed tests. An HTML copy of the report is written to `build/coverage`.",
238259
)
260+
@click.option(
261+
"--gcov",
262+
is_flag=True,
263+
help="Enable C code coverage via `gcov`. `gcov` output goes to `build/**/*.gc*`. "
264+
"Reports can be generated using `ninja coverage*` commands. "
265+
"See https://mesonbuild.com/howtox.html#producing-a-coverage-report",
266+
)
239267
@click.pass_context
240-
def test(ctx, pytest_args, n_jobs, tests, verbose, coverage=False):
268+
def test(ctx, pytest_args, n_jobs, tests, verbose, coverage=False, gcov=False):
241269
"""🔧 Run tests
242270
243271
PYTEST_ARGS are passed through directly to pytest, e.g.:
@@ -283,7 +311,7 @@ def test(ctx, pytest_args, n_jobs, tests, verbose, coverage=False):
283311
click.secho(
284312
"Invoking `build` prior to running tests:", bold=True, fg="bright_green"
285313
)
286-
ctx.invoke(build_cmd)
314+
ctx.invoke(build_cmd, gcov=gcov)
287315

288316
package = cfg.get("tool.spin.package", None)
289317
if (not pytest_args) and (not tests):

0 commit comments

Comments
 (0)