1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-27 00:28:10 +00:00
trezor-firmware/tools/build_protobuf

53 lines
1.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
set -e
if [ -n "$1" ]; then
OUTDIR=`readlink -f "$1"`
fi
cd "$(dirname "$0")"
# set up paths
INDEX="__init__.py"
GENPATH="${OUTDIR:-../trezorlib/messages}"
PROTO_PATH="../vendor/trezor-common/protob"
PROTO_FILES="types messages"
# set up temporary directory & cleanup
TMPDIR=$(mktemp -d)
function cleanup {
rm -r $TMPDIR
}
trap cleanup EXIT
# set up pb2 outdir
PB2_OUT="$TMPDIR/pb2"
mkdir -p "$PB2_OUT"
# compile .proto files to python2 modules using google protobuf library
for file in $PROTO_FILES; do
2018-03-15 18:55:06 +00:00
protoc --python_out="$PB2_OUT" -I/usr/include -I"$PROTO_PATH" "$PROTO_PATH/$file.proto"
done
# create index (__init__.py)
cat > "$TMPDIR/$INDEX" << EOF
# Automatically generated by pb2py
EOF
# convert google protobuf library to trezor's internal format
for file in $PROTO_FILES; do
./pb2py -P "trezorlib.protobuf" -p "$PB2_OUT" -l "$TMPDIR/$INDEX" "$file" "$TMPDIR"
done
# ensure $GENPATH exists and is empty of messages
mkdir -p "$GENPATH"
# only remove messages - there could possibly be other files not starting with capital letter
rm -f "$GENPATH"/[A-Z]*.py
# move generated files to the destination
# (this assumes $INDEX is *.py, otherwise we'd have to add $INDEX separately)
mv "$TMPDIR"/*.py "$GENPATH"
# the exit trap handles removing the tmp directory