Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ project(${PROJECT})
# python is needed for compiling proto files using nanopb
# also for generating & appending firmware signature headers
find_package( Python3 REQUIRED COMPONENTS Interpreter )
execute_process(COMMAND sh utilities/proto/generate-protob.sh WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY )

# Conditionally generate protobuf files based on the BTC_ONLY option
IF(BTC_ONLY)
message(STATUS "Generating protobufs for BTC-only build")
execute_process(COMMAND bash utilities/proto/generate-protob.sh --btc-only
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
ELSE()
message(STATUS "Generating protobufs for full build")
execute_process(COMMAND bash utilities/proto/generate-protob.sh
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND_ERROR_IS_FATAL ANY)
ENDIF()

# Populate version.c
include(utilities/cmake/version.cmake)
Expand Down
74 changes: 50 additions & 24 deletions utilities/proto/generate-protob.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,57 @@

set -e

NANOPB_GEN="$(pwd)/vendor/nanopb/generator/nanopb_generator.py"
# Default to a full build
BUILD_MODE="FULL"

# Generate protobuf sources using nanopb
PYTHON_VERSION="$(python3 --version)" || exit 1
OUTPUT_DIR="$(pwd)/generated/proto"
PROTO_SRC="$(pwd)/common/cypherock-common/proto"
OPTIONS_DIR="$(pwd)/common/proto-options/"
# Path for tron-specific device protocol structures
PROTO_TRON="$(pwd)/common/coin_support/tron_parse_txn/"
# Check for the --btc-only flag
if [[ "$1" == "--btc-only" ]]; then
BUILD_MODE="BTC_ONLY"
echo "Build mode: BTC-only"
fi

test -d "${PROTO_SRC}"
test -d "${PROTO_TRON}"
test -f "${NANOPB_GEN}"
# --- Define paths and tools relative to the project root ---
PROTO_COMMON_SRC="common/cypherock-common/proto"
PROTO_TRON="common/coin_support/tron_parse_txn"
OUTPUT_DIR="generated/proto"
OPTIONS_DIR="common/proto-options"
NANOPB_GEN="vendor/nanopb/generator/nanopb_generator.py"

echo -e "Detected ${PYTHON_VERSION} (at $(which python3))"
# --- Validate paths and create output directory ---
test -d "${PROTO_COMMON_SRC}" || { echo "Error: Directory ${PROTO_COMMON_SRC} not found."; exit 1; }
echo -e "Detected $(python3 --version) (at $(which python3))"
mkdir -p "${OUTPUT_DIR}" || exit 1
cd "${PROTO_SRC}"

# --generated-include-format specifies the include format. Two viable options
# are "#include <%s>" and "#include \"proto/%s\"".
# The default include format "#include \"%s\"" creates problems in cases of
# duplicate file names residing under different packages (eg. error.proto and
# btc.error.proto) as it would result into a collision.
# By using <> format instead of "proto/" helps because in future, relocation
# of generated files would not require updating this parameter.
python3 "${NANOPB_GEN}" -q --generated-include-format "#include <%s>" --proto-path="${PROTO_SRC}" --options-path="${OPTIONS_DIR}" $(find "${PROTO_SRC}" -name "*.proto") --output-dir="${OUTPUT_DIR}" --c-style -s anonymous_oneof:true -s long_names:false
# generate tron files
python3 "${NANOPB_GEN}" -q --generated-include-format "#include <%s>" --proto-path="${PROTO_TRON}" --options-path="${OPTIONS_DIR}" $(find "${PROTO_TRON}" -name "*.proto") --output-dir="${OUTPUT_DIR}" --c-style -s anonymous_oneof:true -s long_names:false

# --- Select and Generate Protobufs ---
PROTO_FILES_TO_GENERATE=()

if [[ "${BUILD_MODE}" == "BTC_ONLY" ]]; then
echo "Finding protobufs for BTC-only build..."
# For a BTC-only build, find all .proto files within the essential directories.
# This is robust and does not depend on specific filenames.
BTC_ONLY_DIRS=(
"${PROTO_COMMON_SRC}/manager"
"${PROTO_COMMON_SRC}/btc"
"${PROTO_COMMON_SRC}/inheritance"
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top level common proto files(like core.proto, error.proto) are missing in btc only

PROTO_FILES_TO_GENERATE=($(find "${BTC_ONLY_DIRS[@]}" -name "*.proto"))
else
echo "Finding all protobufs for full build..."
# For a full build, find all .proto files in all subdirectories
ALL_COMMON_PROTOS=($(find "${PROTO_COMMON_SRC}" -name "*.proto"))
ALL_TRON_PROTOS=($(find "${PROTO_TRON}" -name "*.proto"))
PROTO_FILES_TO_GENERATE=("${ALL_COMMON_PROTOS[@]}" "${ALL_TRON_PROTOS[@]}")
fi

echo "Generating selected protobuf files..."
python3 "${NANOPB_GEN}" \
-q \
--generated-include-format "#include <%s>" \
--proto-path="${PROTO_COMMON_SRC}" \
--proto-path="${PROTO_TRON}" \
--options-path="${OPTIONS_DIR}" \
--output-dir="${OUTPUT_DIR}" \
--c-style -s anonymous_oneof:true -s long_names:false \
"${PROTO_FILES_TO_GENERATE[@]}"

echo "Protobuf generation complete."