scons: fix protobuf action for imports

The current protoc_action generates declarations that impose the requirement
for .proto files to import from the BUILDDIR. This prevents .proto files to
import themselves relative to their own directory.

For example, if there are two files build/pkg/a.proto and build/pkg/b.proto,
and b.proto imports a.proto, it must do so as "import pkg/a.proto", as opposed
to "import a.proto"; otherwise, the generated declaration will give rise to a
compilation error.

This is a problem for EXTRAS where .proto files are gem5-independent, so
they cannot import from gem5's build directory. An example is
https://github.com/ARM-software/ATP-Engine

This commit changes the protoc_action so that declarations are generated
relative to the SOURCE directory. This enables .proto files to import
other .proto files within the same directory.

Note it is not possible to import other .proto files in different
directories, but support for it is not currently necessary.

More details on the proto path:
https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#invocation

Related:
https://gem5-review.googlesource.com/c/public/gem5/+/55903

Change-Id: Ib3e67ae817f8ad0b6803c90d23469267eff16178
Signed-off-by: Adrián Herrera Arcila <adrian.herrera@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/64491
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
This commit is contained in:
Adrián Herrera Arcila
2022-10-06 16:02:17 +00:00
committed by Adrian Herrera
parent 039e9438c1
commit 9e76e7a321

View File

@@ -250,9 +250,10 @@ def protoc_emitter(target, source, env):
root, ext = os.path.splitext(source[0].get_abspath())
return [root + '.pb.cc', root + '.pb.h'], source
protoc_action = MakeAction('${PROTOC} --cpp_out ${BUILDDIR} '
'--proto_path ${BUILDDIR} --proto_path ${SOURCE.dir} '
'${SOURCE.get_abspath()}',
# To understand these variables and attributes, see:
# https://scons.org/doc/production/HTML/scons-man.html#variable_substitution
protoc_action = MakeAction('${PROTOC} --cpp_out ${TARGET.dir.abspath} '
'--proto_path ${SOURCE.dir.abspath} ${SOURCE.abspath}',
Transform("PROTOC"))
protobuf_builder = Builder(action=protoc_action, emitter=protoc_emitter,
src_suffix='.proto')