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