Skip to content

Commit

Permalink
pw_rpc: Build proto path arg list for Soong
Browse files Browse the repository at this point in the history
For every .proto file in the Soong genrule to build a nanopb proto, add
its parent's parent's directory as a --proto-path argument to the
protobuf compiler. This allows genrules to have protos from different
sources. The requirement is that all sources to the same genrule are
prefixed once.

Test: Compiled prefixed proto with prefixed dependencies. Passes
  presubmits in Android main.
Change-Id: Iaaac640c336340d9f698827f6f26085978ebc59c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/225031
Commit-Queue: Carlos Chinchilla <[email protected]>
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Matt Stokes <[email protected]>
  • Loading branch information
ChinchillaWithGoggles authored and CQ Bot Account committed Jul 22, 2024
1 parent dd2720f commit 74bc3ca
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions pw_rpc/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,23 @@ genrule {
],
}

// Generate cc and header nanopb files.
// The output file names are based on the srcs file name with a .pb.c / .pb.h
// extension. Only .proto files are passed to the compile script.
// Generates .pb.c and .pb.h nanopb files.
// The output file name is based on the srcs file name with an added extension.
// All dependencies, including .options files, must be listed in srcs and cannot
// be prefixed. Use the _with_prefix rules if prefixed. Only .proto files are
// passed to the compile script.
//
// Args:
// srcs: The list of source files without a prefix.
// out: A one-element list with the output file without a prefix.
genrule_defaults {
name: "pw_rpc_generate_nanopb_proto",
cmd: "in_files=($(in)); compile_dir=$$(dirname $${in_files[0]}); " +
"proto_files=(); " +
"for f in \"$${in_files[@]}\"; do " +
"if [[ \"$${f##*.}\" == \"proto\" ]]; then " +
"proto_files+=(\"$${f}\"); fi; done; " +
"proto_files+=(\"$${f}\"); " +
"fi; done; " +
"python3 $(location pw_protobuf_compiler_py) " +
"--plugin-path=$(location protoc-gen-nanopb) " +
"--out-dir=$(genDir) " +
Expand All @@ -242,19 +249,30 @@ genrule_defaults {
// single prefix, which can be added with pw_rpc_add_prefix_to_proto.
// Since pw_rpc_add_prefix_to_proto may include .option files as an input, only
// .proto files are passed to the compile script. Make sure .option files are
// prefixed in the same rule as their .proto files.
// prefixed in the same rule as their .proto files. All dependencies must also
// be listed in srcs and have a single prefix.
//
// See the pw_rpc_echo_service_nanopb target for an example. The echo.proto file
// is compiled with "pw_rpc" as the prefix.
//
// Args:
// srcs: The list of source files all with one prefix folder.
// out: A one-element list with the output file with a prefix folder. The
// prefix is based on the parent name of the first item in srcs.
genrule_defaults {
name: "pw_rpc_generate_nanopb_proto_with_prefix",
cmd: "in_files=($(in)); prefix_dir=$$(dirname $${in_files[0]}); " +
"compile_dir=$$(dirname $${prefix_dir}); proto_files=(); " +
"compile_dir=$$(dirname $${prefix_dir}); " +
"proto_files=(); " +
"proto_paths_args=\"\"; " +
"for f in \"$${in_files[@]}\"; do " +
"if [[ \"$${f##*.}\" == \"proto\" ]]; then " +
"proto_files+=(\"$${f}\"); fi; done; " +
"proto_files+=(\"$${f}\"); " +
"proto_path_args+=\"--proto-path=$$(dirname $$(dirname $${f})) \"; " +
"fi; done; " +
"python3 $(location pw_protobuf_compiler_py) " +
"--plugin-path=$(location protoc-gen-nanopb) " +
"$${proto_path_args} " +
"--out-dir=$(genDir) " +
"--compile-dir=$${compile_dir} " +
"--language nanopb " +
Expand All @@ -268,8 +286,12 @@ genrule_defaults {
],
}

// Generate the header nanopb RPC file.
// The output file name is based on the srcs file name with a .rpc.pb.h extension.
// Generate the header nanopb RPC file (.rpc.pb.h).
//
// Args:
// srcs: The list of source files without a prefix.
// out: A one-element list with the output file without a prefix. The name is
// based on the srcs file name with a .rpc.pb.h extension.
genrule_defaults {
name: "pw_rpc_generate_nanopb_rpc_header",
cmd: "in_files=($(in)); compile_dir=$$(dirname $${in_files[0]}); " +
Expand All @@ -292,19 +314,30 @@ genrule_defaults {
// with a single prefix, which can be added with pw_rpc_add_prefix_to_proto.
// Since pw_rpc_add_prefix_to_proto may include .option files as an input, only
// .proto files are passed to the compile script. Make sure .option files are
// prefixed in the same rule as their .proto files.
// prefixed in the same rule as their .proto files. All dependencies must also
// be listed in srcs and have a single prefix.
//
// See the pw_rpc_echo_service_nanopb target for an example. The echo.proto file
// is compiled with "pw_rpc" as the prefix.
//
// Args:
// srcs: The list of source files all with one prefix folder.
// out: A one-element list with the output file with a prefix folder. The
// prefix is based on the parent name of the first item in srcs.
genrule_defaults {
name: "pw_rpc_generate_nanopb_rpc_header_with_prefix",
cmd: "in_files=($(in)); prefix_dir=$$(dirname $${in_files[0]}); " +
"compile_dir=$$(dirname $${prefix_dir}); proto_files=(); " +
"compile_dir=$$(dirname $${prefix_dir}); " +
"proto_files=(); " +
"proto_paths_args=\"\"; " +
"for f in \"$${in_files[@]}\"; do " +
"if [[ \"$${f##*.}\" == \"proto\" ]]; then " +
"proto_files+=(\"$${f}\"); fi; done; " +
"proto_files+=(\"$${f}\"); " +
"proto_path_args+=\"--proto-path=$$(dirname $$(dirname $${f})) \"; " +
"fi; done; " +
"python3 $(location pw_protobuf_compiler_py) " +
"--plugin-path=$(location pw_rpc_plugin_nanopb_py) " +
"$${proto_path_args} " +
"--out-dir=$(genDir) " +
"--compile-dir=$${compile_dir} " +
"--language nanopb_rpc " +
Expand Down

0 comments on commit 74bc3ca

Please sign in to comment.