import oe.spdx_common
import oe.sdk
import os
+import re
from contextlib import contextmanager
from datetime import datetime, timezone
file_counter = 1
if not os.path.exists(topdir):
bb.note(f"Skip {topdir}")
- return spdx_files
+ return spdx_files, set()
check_compiled_sources = d.getVar("SPDX_INCLUDE_COMPILED_SOURCES") == "1"
if check_compiled_sources:
compiled_sources, types = oe.spdx_common.get_compiled_sources(d)
bb.debug(1, f"Total compiled files: {len(compiled_sources)}")
+ exclude_patterns = [
+ re.compile(pattern)
+ for pattern in (d.getVar("SPDX_FILE_EXCLUDE_PATTERNS") or "").split()
+ ]
+ excluded_files = set()
+
for subdir, dirs, files in os.walk(topdir, onerror=walk_error):
- dirs[:] = [d for d in dirs if d not in ignore_dirs]
+ dirs[:] = [directory for directory in dirs if directory not in ignore_dirs]
if subdir == str(topdir):
- dirs[:] = [d for d in dirs if d not in ignore_top_level_dirs]
+ dirs[:] = [
+ directory
+ for directory in dirs
+ if directory not in ignore_top_level_dirs
+ ]
dirs.sort()
files.sort()
continue
filename = str(filepath.relative_to(topdir))
+
+ if exclude_patterns and any(
+ pattern.search(filename) for pattern in exclude_patterns
+ ):
+ excluded_files.add(filename)
+ continue
+
file_purposes = get_purposes(filepath)
- # Check if file is compiled
- if check_compiled_sources:
- if not oe.spdx_common.is_compiled_source(
- filename, compiled_sources, types
- ):
- continue
+ if check_compiled_sources and not oe.spdx_common.is_compiled_source(
+ filename, compiled_sources, types
+ ):
+ continue
spdx_file = objset.new_file(
get_spdxid(file_counter),
bb.debug(1, "Added %d files to %s" % (len(spdx_files), objset.doc._id))
- return spdx_files
+ return spdx_files, excluded_files
def get_package_sources_from_debug(
- d, package, package_files, sources, source_hash_cache
+ d, package, package_files, sources, source_hash_cache, excluded_files=None
):
+ if excluded_files is None:
+ excluded_files = set()
+
def file_path_match(file_path, pkg_file):
if file_path.lstrip("/") == pkg_file.name.lstrip("/"):
return True
continue
if not any(file_path_match(file_path, pkg_file) for pkg_file in package_files):
+ if file_path.lstrip("/") in excluded_files:
+ bb.debug(
+ 1,
+ f"Skipping debug source lookup for excluded file {file_path} in {package}",
+ )
+ continue
bb.fatal(
"No package file found for %s in %s; SPDX found: %s"
% (str(file_path), package, " ".join(p.name for p in package_files))
bb.debug(1, "Adding source files to SPDX")
oe.spdx_common.get_patched_src(d)
- files = add_package_files(
+ files, _ = add_package_files(
d,
build_objset,
spdx_workdir,
)
bb.debug(1, "Adding package files to SPDX for package %s" % pkg_name)
- package_files = add_package_files(
+ package_files, excluded_files = add_package_files(
d,
pkg_objset,
pkgdest / package,
if include_sources:
debug_sources = get_package_sources_from_debug(
- d, package, package_files, dep_sources, source_hash_cache
+ d, package, package_files, dep_sources, source_hash_cache,
+ excluded_files=excluded_files,
)
debug_source_ids |= set(
oe.sbom30.get_element_link_id(d) for d in debug_sources
if include_sources:
bb.debug(1, "Adding sysroot files to SPDX")
- sysroot_files = add_package_files(
+ sysroot_files, _ = add_package_files(
d,
build_objset,
d.expand("${COMPONENTS_DIR}/${PACKAGE_ARCH}/${PN}"),
image_filename = image["filename"]
image_path = image_deploy_dir / image_filename
if os.path.isdir(image_path):
- a = add_package_files(
- d,
- objset,
- image_path,
- lambda file_counter: objset.new_spdxid(
- "imagefile", str(file_counter)
- ),
- lambda filepath: [],
- license_data=None,
- ignore_dirs=[],
- ignore_top_level_dirs=[],
- archive=None,
+ a, _ = add_package_files(
+ d,
+ objset,
+ image_path,
+ lambda file_counter: objset.new_spdxid(
+ "imagefile", str(file_counter)
+ ),
+ lambda filepath: [],
+ license_data=None,
+ ignore_dirs=[],
+ ignore_top_level_dirs=[],
+ archive=None,
)
artifacts.extend(a)
else: