1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-21 05:48:23 +00:00
trezor-firmware/tools/build_protobuf

71 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
set -e
2019-05-16 11:28:20 +00:00
cd $(dirname $0)/..
PROTOB=common/protob
# Bootloader messages cannot end up in core
CORE_PROTOBUF_SOURCES=$(ls $PROTOB/*.proto | grep -v "bootloader")
2019-05-16 11:28:20 +00:00
# Taking all files for python
2019-05-16 11:28:20 +00:00
PYTHON_PROTOBUF_SOURCES=$PROTOB/*.proto
RETURN=0
do_rebuild() {
local FILE_OR_DIR="$1"
2019-05-16 11:28:20 +00:00
shift
local OUTPUT="$1"
shift
local SOURCES="$1"
shift
2019-05-16 11:28:20 +00:00
if [ "$FILE_OR_DIR" == file ]; then
local param="--outfile"
else
local param="--python-outdir"
fi
# note $SOURCES is unquoted - we want wildcard expansion and multiple args
$PROTOB/pb2py "$@" $param="$OUTPUT" $SOURCES
2019-05-16 11:28:20 +00:00
}
do_check() {
# rebuild protobuf in tmpdir and check result against specified directory
local TMPDIR=$(mktemp -d proto-check.XXXXXX)
trap "rm -r $TMPDIR" RETURN
local FILE_OR_DIR="$1"
shift
local OUTPUT="$1"
2019-05-16 11:28:20 +00:00
shift
if [ "$FILE_OR_DIR" == file ]; then
local TMPDEST="$TMPDIR/testfile"
else
cp -rT "$OUTPUT" "$TMPDIR"
local TMPDEST="$TMPDIR"
fi
do_rebuild "$FILE_OR_DIR" "$TMPDEST" "$@"
if ! diff -ur --exclude __pycache__ "$OUTPUT" "$TMPDEST"; then
2019-05-16 11:28:20 +00:00
RETURN=1
fi
}
if [ "$1" == "--check" ]; then
func=do_check
else
func=do_rebuild
fi
$func dir core/src/trezor/enums "$CORE_PROTOBUF_SOURCES"
$func file core/src/trezor/enums/__init__.py "$CORE_PROTOBUF_SOURCES" --template=core/src/trezor/enums/_proto_init.mako
$func file core/src/trezor/messages.py "$CORE_PROTOBUF_SOURCES" --template=core/src/trezor/_proto_messages.mako
$func file python/src/trezorlib/messages.py "$PYTHON_PROTOBUF_SOURCES" \
--template=python/src/trezorlib/_proto_messages.mako \
--include-deprecated
2019-05-16 11:28:20 +00:00
exit $RETURN