]> git.lizzy.rs Git - rust.git/blob - src/test/run-make-fulldeps/coverage-reports/Makefile
rustdoc-json: Make the `fns/generics.rs` test much more robust
[rust.git] / src / test / run-make-fulldeps / coverage-reports / Makefile
1 # needs-profiler-support
2 # ignore-windows-gnu
3
4 # Rust coverage maps support LLVM Coverage Mapping Format versions 5 and 6,
5 # corresponding with LLVM versions 12 and 13, respectively.
6 # When upgrading LLVM versions, consider whether to enforce a minimum LLVM
7 # version during testing, with an additional directive at the top of this file
8 # that sets, for example: `min-llvm-version: 12.0`
9
10 # FIXME(mati865): MinGW GCC miscompiles compiler-rt profiling library but with Clang it works
11 # properly. Since we only have GCC on the CI ignore the test for now.
12
13 -include ../coverage/coverage_tools.mk
14
15 BASEDIR=../coverage-reports
16 SOURCEDIR=../coverage
17
18 # The `llvm-cov show` flag `--debug`, used to generate the `counters` output files, is only
19 # enabled if LLVM assertions are enabled. This requires Rust config `llvm/optimize` and not
20 # `llvm/release_debuginfo`. Note that some CI builds disable debug assertions (by setting
21 # `NO_LLVM_ASSERTIONS=1`), so the tests must still pass even if the `--debug` flag is
22 # not supported. (Note that `counters` files are only produced in the `$(TMPDIR)`
23 # directory, for inspection and debugging support. They are *not* copied to `expected_*`
24 # files when `--bless`ed.)
25 LLVM_COV_DEBUG := $(shell \
26                 "$(LLVM_BIN_DIR)"/llvm-cov show --debug 2>&1 | \
27                 grep -q "Unknown command line argument '--debug'"; \
28                 echo $$?)
29 ifeq ($(LLVM_COV_DEBUG), 1)
30 DEBUG_FLAG=--debug
31 endif
32
33 # FIXME(richkadel): I'm adding `--ignore-filename-regex=` line(s) for specific test(s) that produce
34 # `llvm-cov` results for multiple files (for example `uses_crate.rs` and `used_crate/mod.rs`) as a
35 # workaround for two problems causing tests to fail on Windows:
36 #
37 # 1. When multiple files appear in the `llvm-cov show` results, each file's coverage results can
38 #    appear in different a different order. Whether this is random or, somehow, platform-specific,
39 #    the Windows output flips the order of the files, compared to Linux. In the `uses_crate.rs`
40 #    test, the only test-unique (interesting) results we care about are the results for only one
41 #    of the two files, `mod/uses_crate.rs`, so the workaround is to ignore all but this one file.
42 #    In the future, we may want a more sophisticated solution that splits apart `llvm-cov show`
43 #    results into separate results files for each result (taking care not to create new file
44 #    paths that might be too long for Windows MAX_PATH limits when creating these new sub-results,
45 #    as well).
46 # 2. When multiple files appear in the `llvm-cov show` results, the results for each file are
47 #    prefixed with their filename, including platform-specific path separators (`\` for Windows,
48 #    and `/` everywhere else). This could be filtered or normalized of course, but by ignoring
49 #    coverage results for all but one of the file, the filenames are no longer included anyway.
50 #    If this changes (if/when we decide to support `llvm-cov show` results for multiple files),
51 #    the file path separator differences may need to be addressed.
52 #
53 # Since this is only a workaround, I decided to implement the override by adding an option for
54 # each file to be ignored, using a `--ignore-filename-regex=` entry for each one, rather than
55 # implement some more sophisticated solution with a new custom test directive in the test file
56 # itself (similar to `expect-exit-status`) because that would add a lot of complexity and still
57 # be a workaround, with the same result, with no benefit.
58 #
59 # Yes these `--ignore-filename-regex=` options are included in all invocations of `llvm-cov show`
60 # for now, but it is effectively ignored for all tests that don't include this file anyway.
61 #
62 # (Note that it's also possible the `_counters.<test>.txt` and `<test>.json` files (if generated)
63 # may order results from multiple files inconsistently, which might also have to be accomodated
64 # if and when we allow `llvm-cov` to produce results for multiple files. Note, the path separators
65 # appear to be normalized to `/` in those files, thankfully.)
66 LLVM_COV_IGNORE_FILES=\
67         --ignore-filename-regex='(uses_crate.rs|uses_inline_crate.rs|unused_mod.rs)'
68
69 all: $(patsubst $(SOURCEDIR)/lib/%.rs,%,$(wildcard $(SOURCEDIR)/lib/*.rs)) $(patsubst $(SOURCEDIR)/%.rs,%,$(wildcard $(SOURCEDIR)/*.rs))
70
71 # Ensure there are no `expected` results for tests that may have been removed or renamed
72 .PHONY: clear_expected_if_blessed
73 clear_expected_if_blessed:
74 ifdef RUSTC_BLESS_TEST
75         rm -f expected_*
76 endif
77
78 -include clear_expected_if_blessed
79
80 %: $(SOURCEDIR)/lib/%.rs
81         # Compile the test library with coverage instrumentation
82         $(RUSTC) $(SOURCEDIR)/lib/$@.rs \
83                         $$( sed -n 's/^\/\/ compile-flags: \([^#]*\).*/\1/p' $(SOURCEDIR)/lib/$@.rs ) \
84                         --crate-type rlib -Cinstrument-coverage
85
86 %: $(SOURCEDIR)/%.rs
87         # Compile the test program with coverage instrumentation
88         $(RUSTC) $(SOURCEDIR)/$@.rs \
89                         $$( sed -n 's/^\/\/ compile-flags: \([^#]*\).*/\1/p' $(SOURCEDIR)/$@.rs ) \
90                         -L "$(TMPDIR)" -Cinstrument-coverage
91
92         # Run it in order to generate some profiling data,
93         # with `LLVM_PROFILE_FILE=<profdata_file>` environment variable set to
94         # output the coverage stats for this run.
95         LLVM_PROFILE_FILE="$(TMPDIR)"/$@.profraw \
96                         $(call RUN,$@) || \
97                         ( \
98                                 status=$$?; \
99                                 grep -q "^\/\/ expect-exit-status-$$status" $(SOURCEDIR)/$@.rs || \
100                                 ( >&2 echo "program exited with an unexpected exit status: $$status"; \
101                                         false \
102                                 ) \
103                         )
104
105         # Run it through rustdoc as well to cover doctests.
106         # `%p` is the pid, and `%m` the binary signature. We suspect that the pid alone
107         # might result in overwritten files and failed tests, as rustdoc spawns each
108         # doctest as its own process, so make sure the filename is as unique as possible.
109         LLVM_PROFILE_FILE="$(TMPDIR)"/$@-%p-%m.profraw \
110                         $(RUSTDOC) --crate-name workaround_for_79771 --test $(SOURCEDIR)/$@.rs \
111                         $$( sed -n 's/^\/\/ compile-flags: \([^#]*\).*/\1/p' $(SOURCEDIR)/$@.rs ) \
112                         -L "$(TMPDIR)" -Cinstrument-coverage \
113                         -Z unstable-options --persist-doctests=$(TMPDIR)/rustdoc-$@
114
115         # Postprocess the profiling data so it can be used by the llvm-cov tool
116         "$(LLVM_BIN_DIR)"/llvm-profdata merge --sparse \
117                         "$(TMPDIR)"/$@*.profraw \
118                         -o "$(TMPDIR)"/$@.profdata
119
120         # Generate a coverage report using `llvm-cov show`.
121         "$(LLVM_BIN_DIR)"/llvm-cov show \
122                         $(DEBUG_FLAG) \
123                         $(LLVM_COV_IGNORE_FILES) \
124                         --compilation-dir=. \
125                         --Xdemangler="$(RUST_DEMANGLER)" \
126                         --show-line-counts-or-regions \
127                         --instr-profile="$(TMPDIR)"/$@.profdata \
128                         $(call BIN,"$(TMPDIR)"/$@) \
129                         $$( \
130                                 for file in $(TMPDIR)/rustdoc-$@/*/rust_out; do \
131                                 [ -x "$$file" ] && printf "%s %s " -object $$file; \
132                                 done \
133                         ) \
134                 2> "$(TMPDIR)"/show_coverage_stderr.$@.txt \
135                 | "$(PYTHON)" $(BASEDIR)/normalize_paths.py \
136                 > "$(TMPDIR)"/actual_show_coverage.$@.txt || \
137         ( status=$$? ; \
138                 >&2 cat "$(TMPDIR)"/show_coverage_stderr.$@.txt ; \
139                 exit $$status \
140         )
141
142 ifdef DEBUG_FLAG
143         # The first line (beginning with "Args:" contains hard-coded, build-specific
144         # file paths. Strip that line and keep the remaining lines with counter debug
145         # data.
146         tail -n +2 "$(TMPDIR)"/show_coverage_stderr.$@.txt \
147                 > "$(TMPDIR)"/actual_show_coverage_counters.$@.txt
148 endif
149
150 ifdef RUSTC_BLESS_TEST
151         cp "$(TMPDIR)"/actual_show_coverage.$@.txt \
152                         expected_show_coverage.$@.txt
153 else
154         # Compare the show coverage output (`--bless` refreshes `typical` files).
155         #
156         # FIXME(richkadel): None of the Rust test source samples have the
157         # `// ignore-llvm-cov-show-diffs` anymore. This directive exists to work around a limitation
158         # with `llvm-cov show`. When reporting coverage for multiple instantiations of a generic function,
159         # with different type substitutions, `llvm-cov show` prints these in a non-deterministic order,
160         # breaking the `diff` comparision.
161         #
162         # A partial workaround is implemented below, with `diff --ignore-matching-lines=RE`
163         # to ignore each line prefixing each generic instantiation coverage code region.
164         #
165         # This workaround only works if the coverage counts are identical across all reported
166         # instantiations. If there is no way to ensure this, you may need to apply the
167         # `// ignore-llvm-cov-show-diffs` directive, and check for differences using the
168         # `.json` files to validate that results have not changed. (Until then, the JSON
169         # files are redundant, so there is no need to generate `expected_*.json` files or
170         # compare actual JSON results.)
171
172         $(DIFF) --ignore-matching-lines='^  | .*::<.*>.*:$$' --ignore-matching-lines='^  | <.*>::.*:$$' \
173                 expected_show_coverage.$@.txt "$(TMPDIR)"/actual_show_coverage.$@.txt || \
174                 ( grep -q '^\/\/ ignore-llvm-cov-show-diffs' $(SOURCEDIR)/$@.rs && \
175                         >&2 echo 'diff failed, but suppressed with `// ignore-llvm-cov-show-diffs` in $(SOURCEDIR)/$@.rs' \
176                 ) || \
177                 ( >&2 echo 'diff failed, and not suppressed without `// ignore-llvm-cov-show-diffs` in $(SOURCEDIR)/$@.rs'; \
178                         false \
179                 )
180 endif