Skip to content

Commit

Permalink
pw_presubmit: Check that files appear in bp files
Browse files Browse the repository at this point in the history
Add a check that files appear in Android.bp files, with some built-in
exceptions:

* Only check files that have an adjacent Android.bp file
* Ignore files with "test" or "mock" in their names
* Ignore header files

Not running this check as part of 'pw presubmit' because there are 60
files it finds that "should" be referenced from an Android.bp file and
are not.

Change-Id: Icb97e889da3e6815772cbdc01ea07895c65163de
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/250432
Reviewed-by: Wyatt Hepler <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Commit-Queue: Rob Mohr <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
mohrr authored and CQ Bot Account committed Nov 21, 2024
1 parent f776679 commit acd4c15
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pw_presubmit/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ included by adding ``json_check.presubmit_check()`` to a presubmit program.
Source in Build
^^^^^^^^^^^^^^^
Pigweed provides checks that source files are configured as part of the build
for GN, Bazel, and CMake. These can be included by adding
for GN, Bazel, CMake, and Soong. These can be included by adding
``source_in_build.gn(filter)`` and similar functions to a presubmit check. The
CMake check additionally requires a callable that invokes CMake with appropriate
options.
Expand Down
36 changes: 36 additions & 0 deletions pw_presubmit/py/pw_presubmit/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,42 @@ def check_gn_build_for_files(
return missing


def check_soong_build_for_files(
soong_extensions_to_check: Container[str],
files: Iterable[Path],
soong_build_files: Iterable[Path] = (),
) -> list[Path]:
"""Checks that source files are in the Soong build.
Args:
bp_extensions_to_check: which file suffixes to look for in Soong files
files: the files that should be checked
bp_build_files: paths to Android.bp files to directly search for paths
Returns:
a list of missing files; will be empty if there were no missing files
"""

# Collect all paths in Soong builds.
soong_builds = set(_search_files_for_paths(soong_build_files))

missing: list[Path] = []

if soong_build_files:
for path in (p for p in files if p.suffix in soong_extensions_to_check):
if path not in soong_builds:
missing.append(path)

if missing:
_LOG.warning(
'%s missing from the Soong build:\n%s',
plural(missing, 'file', are=True),
'\n'.join(str(x) for x in missing),
)

return missing


def check_builds_for_files(
bazel_extensions_to_check: Container[str],
gn_extensions_to_check: Container[str],
Expand Down
1 change: 1 addition & 0 deletions pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@ def owners_lint_checks(ctx: PresubmitContext):
# TODO(hepler): Many files are missing from the CMake build. Add this check
# to lintformat when the missing files are fixed.
source_in_build.cmake(SOURCE_FILES_FILTER, _run_cmake),
source_in_build.soong(SOURCE_FILES_FILTER),
static_analysis,
stm32f429i,
todo_check.create(todo_check.BUGS_OR_USERNAMES),
Expand Down
55 changes: 55 additions & 0 deletions pw_presubmit/py/pw_presubmit/source_in_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""Checks that source files are listed in build files, such as BUILD.bazel."""

import logging
from pathlib import Path
from typing import Callable, Sequence

from pw_cli.file_filter import FileFilter
Expand Down Expand Up @@ -169,3 +170,57 @@ def source_is_in_cmake_build(ctx: PresubmitContext):
raise PresubmitFailure

return source_is_in_cmake_build


_DEFAULT_SOONG_EXTENSIONS = (*format_code.CPP_SOURCE_EXTS, '.proto')


def soong( # pylint: disable=invalid-name
source_filter: FileFilter,
files_and_extensions_to_check: Sequence[str] = _DEFAULT_SOONG_EXTENSIONS,
) -> Check:
"""Create a presubmit check that ensures sources are in Android.bp files.
Args:
source_filter: filter that selects files that must be in the Soong files
files_and_extensions_to_check: files and extensions to look for (the
source_filter might match build files that won't be in the build but
this should only match source files)
"""

@filter_paths(file_filter=source_filter)
def source_is_in_soong_build(ctx: PresubmitContext):
"""Checks that source files are in the Soong build."""

paths = source_filter.filter(ctx.all_paths)
paths = presubmit_context.apply_exclusions(ctx, paths)

# For now, only check modules where there is an Android.bp file.
relevant_paths: list[Path] = []
for path in paths:
# For now, don't require tests be included in Android.bp files.
split = path.stem.split('_')
if 'test' in split or 'mock' in split:
continue

if (path.parent / 'Android.bp').is_file():
relevant_paths.append(path)

missing = build.check_soong_build_for_files(
files_and_extensions_to_check,
relevant_paths,
soong_build_files=git_repo.list_files(
pathspecs=['Android.bp', '*Android.bp'], repo_path=ctx.root
),
)

if missing:
with ctx.failure_summary_log.open('w') as outs:
print('Missing files:', file=outs)
for miss in missing:
print(miss, file=outs)

_LOG.warning('All source files must appear in Android.bp files')
raise PresubmitFailure

return source_is_in_soong_build

0 comments on commit acd4c15

Please sign in to comment.