-
Notifications
You must be signed in to change notification settings - Fork 171
[rule-based toolchains] Add cc_coverage_config rule
#352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Yannic
wants to merge
8
commits into
bazelbuild:main
Choose a base branch
from
EngFlow:yannic-coverage-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2293e09
[rule-based toolchains] Add `cc_coverage_config` rule
Yannic cbdb13a
library_search_directories
Yannic 9ad7a31
more
Yannic 995f6cb
Merge remote-tracking branch 'upstream/main' into yannic-coverage-config
Yannic 709025e
Merge remote-tracking branch 'upstream/main' into yannic-coverage-config
Yannic 30ba173
fix
Yannic 0e3e152
CR
Yannic 3169b19
more
Yannic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Copyright 2025 The Bazel Authors. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Rules to configure cc coverage collection.""" | ||
|
|
||
| load("//cc/toolchains/impl:collect.bzl", "collect_data") | ||
| load(":cc_toolchain_info.bzl", "CoverageConfigInfo") | ||
|
|
||
| visibility("public") | ||
|
|
||
| def _cc_coverage_config_impl(ctx): | ||
| exe_info = ctx.attr.src[DefaultInfo] | ||
| if exe_info.files_to_run != None and exe_info.files_to_run.executable != None: | ||
| exe = exe_info.files_to_run.executable | ||
| elif len(exe_info.files.to_list()) == 1: | ||
| exe = exe_info.files.to_list()[0] | ||
| else: | ||
| fail("Expected cc_coverage_config's src attribute to be either an executable or a single file") | ||
|
|
||
| runfiles = collect_data(ctx, ctx.attr.data + [ctx.attr.src]) | ||
| config = CoverageConfigInfo( | ||
| label = ctx.label, | ||
| type = ctx.attr.type, | ||
| exe = exe, | ||
| runfiles = runfiles, | ||
| ) | ||
|
|
||
| link = ctx.actions.declare_file(ctx.label.name) | ||
| ctx.actions.symlink( | ||
| output = link, | ||
| target_file = exe, | ||
| is_executable = True, | ||
| ) | ||
| return [ | ||
| config, | ||
| # This isn't required, but now we can do "bazel run <config>", which can | ||
| # be very helpful when debugging toolchains. | ||
| DefaultInfo( | ||
| files = depset([link]), | ||
| runfiles = runfiles, | ||
| executable = link, | ||
| ), | ||
| ] | ||
|
|
||
| cc_coverage_config = rule( | ||
| implementation = _cc_coverage_config_impl, | ||
| attrs = { | ||
| "type": attr.string( | ||
| mandatory = True, | ||
| values = [ | ||
| "gcov", | ||
| "llvm-cov", | ||
| ], | ||
| doc = """ | ||
| The type of coverage this config is for (e.g., gcov). | ||
| """ | ||
| ), | ||
| "src": attr.label( | ||
| mandatory = True, | ||
| allow_files = True, | ||
| cfg = "exec", | ||
| doc = """ | ||
| The tool to collect coverage with. | ||
| """ | ||
| ), | ||
| "data": attr.label_list( | ||
| mandatory = False, | ||
| allow_files = True, | ||
| doc = """ | ||
| Additional files that are required for this coverage config to run. | ||
| """, | ||
| ), | ||
| }, | ||
| doc = """ | ||
| Defines the configuration to collect CC coverage. | ||
|
|
||
| Example: | ||
| ``` | ||
| load("//cc/toolchains:cc_coverage_config.bzl", "cc_coverage_config") | ||
|
|
||
| cc_coverage_config( | ||
| name = "gcov", | ||
| type = "gcov", | ||
| src = "bin/gcov", | ||
| ) | ||
| ``` | ||
| """, | ||
| provides = [ | ||
| CoverageConfigInfo, | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's tool path only, would I be correct in assuming that runfiles aren't accessible, and args don't do anything?
You might find it significantly simplifies things to just make the API as simple as:
Defining coverage types probably doesn't do too much for you - we defined action types because they were a useful link between args and tools, but since you don't have args, it doesn't seem too useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, just double checking that coverage tools do indeed run on the target platform, not the exec platform? Fairly certain that should be the case, but just making sure because my above proposal won't work well if they run on the exec platform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The platform on which tests run on is unfortunately a bit tricky. One can make a case for the target platform (e.g., using x86_64 machine to build an arm64 binary, and then the the test only runs on the target platform). One can also make a case for the exec platform (e.g., I build an Android unit test, which has target Android, but the test runs in a simulator so the "test platform" is the exec platform).
FWIW,
_collect_cc_coverageuses the exec platform [1]. I think for consistency, we should stick with that.[1] https://cs.opensource.google/bazel/bazel/+/master:src/main/starlark/builtins_bzl/common/cc/semantics.bzl;l=79;drc=ea29e8177757286733d32b91304ab9f53974a138
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, if that's the case, then we should do the following instead:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not aware of a way to pass args to
gcovvia the toolchain today. However, runfiles are accessible (in our private repo, we callrunfiles.filesand pass that tocc_toolchain.all_files. I prototyped with this PR internally, and the files fromcc_coverage_configare available).bazel coverage --test_env=VERBOSE_COVERAGE=1 //my/cc/targethas the following in itstest.log:So theoretically, this could work. However, I can see it becoming useful in the future (and hopefully providing a path to clean up the madness of magic coverage collector shell scripts there is).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it makes sense to be able to specify multiple coverage collectors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be fine change
cc_coverage_config.typetoattr.string(values = ["gcov", "llvm-cov", "..."]), although I think it might become useful in the future to have a target for that (e.g., to pass_collect_cc_coverage, which generally depends only on the type of the tool, not the exact tool).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think attr.string is fine. If a tool wants to use the coverage type later, they can also use an attr.string, and they can get the current tool's type from the cc_toolchain_config rule directly.