diff --git a/pw_protobuf/py/pw_protobuf/options.py b/pw_protobuf/py/pw_protobuf/options.py index 7168b11c6..3e020a8b8 100644 --- a/pw_protobuf/py/pw_protobuf/options.py +++ b/pw_protobuf/py/pw_protobuf/options.py @@ -14,7 +14,6 @@ """Options file parsing for proto generation.""" from fnmatch import fnmatchcase -import itertools from pathlib import Path import re @@ -54,40 +53,46 @@ def load_options_from(options: ParsedOptions, options_file_name: Path): continue -_PWPB_OPTIONS_EXTENSION = '.pwpb_options' - - -def load_options( +def _load_options_with_suffix( include_paths: list[Path], proto_file_name: Path, options_files: list[Path], - allow_generic_options_extension: bool = False, + suffix: str, ) -> ParsedOptions: """Loads the options for the given .proto file.""" options: ParsedOptions = [] - suffixes = [_PWPB_OPTIONS_EXTENSION] - if allow_generic_options_extension: - suffixes.append('.options') - # First load from any files that were directly specified. - for options_file, suffix in itertools.product(options_files, suffixes): + for options_file in options_files: if ( options_file.name == proto_file_name.with_suffix(suffix).name and options_file.exists() ): load_options_from(options, options_file) - break # .pwpb_options is always first; stop if it is found. # Then search specified search paths for options files. for include_path in include_paths: - for suffix in suffixes: - options_file_name = include_path / proto_file_name.with_suffix( - suffix - ) - if options_file_name.exists(): - load_options_from(options, options_file_name) - break # .pwpb_options is always first; stop if it is found. + options_file_name = include_path / proto_file_name.with_suffix(suffix) + if options_file_name.exists(): + load_options_from(options, options_file_name) + + return options + + +def load_options( + include_paths: list[Path], + proto_file_name: Path, + options_files: list[Path], + allow_generic_options_extension: bool = True, +) -> ParsedOptions: + # Try to load pwpb_options first. If they exist, ignore regular options. + options = _load_options_with_suffix( + include_paths, proto_file_name, options_files, ".pwpb_options" + ) + if allow_generic_options_extension and not options: + options = _load_options_with_suffix( + include_paths, proto_file_name, options_files, ".options" + ) return options diff --git a/pw_protobuf_compiler/pw_proto_library.bzl b/pw_protobuf_compiler/pw_proto_library.bzl index 471bd1808..10d4c5f46 100644 --- a/pw_protobuf_compiler/pw_proto_library.bzl +++ b/pw_protobuf_compiler/pw_proto_library.bzl @@ -667,18 +667,15 @@ _pw_nanopb_rpc_proto_library = rule( def _pw_proto_filegroup_impl(ctx): source_files = list() - pwpb_options_files = list() - legacy_options_files = list() + options_files = list() for src in ctx.attr.srcs: source_files += src.files.to_list() for options_src in ctx.attr.options_files: for file in options_src.files.to_list(): - if file.extension == "pwpb_options": - pwpb_options_files.append(file) - elif file.extension == "options": - legacy_options_files.append(file) + if file.extension == "options" or file.extension == "pwpb_options": + options_files.append(file) else: fail(( "Files provided as `options_files` to a " + @@ -686,10 +683,9 @@ def _pw_proto_filegroup_impl(ctx): "`.pwpb_options` extensions; the file `{}` was provided." ).format(file.basename)) - # Ensure that .pwpb_options appear before regular .options. return [ DefaultInfo(files = depset(source_files)), - PwProtoOptionsInfo(options_files = depset(pwpb_options_files + legacy_options_files)), + PwProtoOptionsInfo(options_files = depset(options_files)), ] pw_proto_filegroup = rule(