Skip to content

Commit

Permalink
nanopb creation scripts added (#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky authored Oct 11, 2024
1 parent cd7f327 commit 51ed6f0
Show file tree
Hide file tree
Showing 33 changed files with 273 additions and 126 deletions.
1 change: 1 addition & 0 deletions build_win/nanopb/compile-nanopb.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python compile-nanopb.py --nano_pb_path c:\nanopb\nanopb-0.4.9 --ecal_repository "..\.."
112 changes: 112 additions & 0 deletions build_win/nanopb/compile-nanopb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import sys
import re
import subprocess
import logging
from pathlib import Path
import shutil
import argparse

def setup_logging():
"""Sets up the logging configuration."""
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


def list_files(directory: Path, extension: str) -> list[Path]:
"""Returns a list of files with specific extension in the given directory."""
if not directory.exists():
logging.error(f"Directory {directory} does not exist.")
return []
return list(directory.glob(f"*{extension}"))


def copy_files(source_dir: Path, target_dir: Path, file_types: list[str]):
"""Copies files of specific types from source to target directory."""
target_dir.mkdir(parents=True, exist_ok=True)
for file_type in file_types:
for file in source_dir.glob(file_type):
try:
shutil.copy2(file, target_dir)
logging.info(f"Copied {file.name} to {target_dir}")
except Exception as e:
logging.error(f"Error copying {file.name} to {target_dir}: {e}")


def check_nanopb_compiler_exists(compiler_path: Path):
"""Checks if the nanopb_generator exists."""
if not compiler_path.exists():
logging.error(f"nanopb generator not found at {compiler_path}")
sys.exit(1)


def run_nanopb_generator(proto_files: list[Path], compiler: Path, ecal_pb_base_path: Path, ecal_pb_sub_path: Path, target_dir: Path):
"""Runs the nanopb generator for each proto file."""
for proto_file_path in proto_files:
proto_file_name = proto_file_path.name

command = [
str(compiler),
"-I" + str(ecal_pb_base_path),
"-I" + str(ecal_pb_base_path / ecal_pb_sub_path),
"-D" + str(target_dir / ecal_pb_sub_path),
"-e" + ".npb",
proto_file_name
]

logging.info(f"Running: {' '.join(command)}")

try:
result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, cwd=proto_file_path.parent)
logging.info(f"Success: {proto_file_name} processed.")
logging.debug(result.stdout)
except subprocess.CalledProcessError as e:
logging.error(f"Error running nanopb_generator for {proto_file_name}: {e.stderr}")


def main(nano_pb_path: Path, ecal_repository: Path):
setup_logging()

# Define paths based on the provided arguments
ecal_pb_base_path = Path(ecal_repository / "ecal/core_pb/src")
ecal_pb_sub_path = Path("ecal/core/pb")
ecal_target_path = Path("../../core/src/serialization/nanopb")

# Combine paths and list .proto files
proto_files_dir = ecal_pb_base_path / ecal_pb_sub_path
ecal_pb_files = list_files(proto_files_dir, ".proto")

# Check if nanopb generator exists
nano_pb_compiler = nano_pb_path / "generator-bin/nanopb_generator.exe"
check_nanopb_compiler_exists(nano_pb_compiler)

# Check if any .proto files are found
if not ecal_pb_files:
logging.error(f"No .proto files found in {proto_files_dir}")
sys.exit(1)

# Prepare target directory
absolute_target_path = (ecal_pb_base_path / ecal_target_path).resolve()
absolute_target_path.mkdir(parents=True, exist_ok=True)

# Copy nanopb common decoder and encoder to target nanopb directory
target_nanopb_dir = absolute_target_path
copy_files(nano_pb_path, target_nanopb_dir, ["*.c", "*.h"])

# Run nanopb_generator for each .proto file
run_nanopb_generator(ecal_pb_files, nano_pb_compiler, ecal_pb_base_path, ecal_pb_sub_path, absolute_target_path)


if __name__ == "__main__":
# Set up argument parsing
parser = argparse.ArgumentParser(description="Process nanopb and eCAL protobuf files.")
parser.add_argument("--nano_pb_path", type=Path, required=True, help="Path to the nanopb directory")
parser.add_argument("--ecal_repository", type=Path, required=True, help="Path to the eCAL repository")

# Parse the arguments
args = parser.parse_args()

# Resolve paths to absolute paths to support relative paths
nano_pb_path = Path(args.nano_pb_path).resolve()
ecal_repository = Path(args.ecal_repository).resolve()

# Call the main function with resolved absolute paths
main(nano_pb_path, ecal_repository)
48 changes: 24 additions & 24 deletions ecal/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -329,29 +329,29 @@ endif()
# serialization
######################################
set(ecal_serialization_src
src/serialization/nanopb/nanopb/pb.h
src/serialization/nanopb/nanopb/pb_common.c
src/serialization/nanopb/nanopb/pb_common.h
src/serialization/nanopb/nanopb/pb_decode.c
src/serialization/nanopb/nanopb/pb_decode.h
src/serialization/nanopb/nanopb/pb_encode.c
src/serialization/nanopb/nanopb/pb_encode.h
src/serialization/nanopb/ecal.pb.c
src/serialization/nanopb/ecal.pb.h
src/serialization/nanopb/host.pb.c
src/serialization/nanopb/host.pb.h
src/serialization/nanopb/layer.pb.c
src/serialization/nanopb/layer.pb.h
src/serialization/nanopb/logging.pb.c
src/serialization/nanopb/logging.pb.h
src/serialization/nanopb/monitoring.pb.c
src/serialization/nanopb/monitoring.pb.h
src/serialization/nanopb/process.pb.c
src/serialization/nanopb/process.pb.h
src/serialization/nanopb/service.pb.c
src/serialization/nanopb/service.pb.h
src/serialization/nanopb/topic.pb.c
src/serialization/nanopb/topic.pb.h
src/serialization/nanopb/pb.h
src/serialization/nanopb/pb_common.c
src/serialization/nanopb/pb_common.h
src/serialization/nanopb/pb_decode.c
src/serialization/nanopb/pb_decode.h
src/serialization/nanopb/pb_encode.c
src/serialization/nanopb/pb_encode.h
src/serialization/nanopb/ecal/core/pb/ecal.npb.c
src/serialization/nanopb/ecal/core/pb/ecal.npb.h
src/serialization/nanopb/ecal/core/pb/host.npb.c
src/serialization/nanopb/ecal/core/pb/host.npb.h
src/serialization/nanopb/ecal/core/pb/layer.npb.c
src/serialization/nanopb/ecal/core/pb/layer.npb.h
src/serialization/nanopb/ecal/core/pb/logging.npb.c
src/serialization/nanopb/ecal/core/pb/logging.npb.h
src/serialization/nanopb/ecal/core/pb/monitoring.npb.c
src/serialization/nanopb/ecal/core/pb/monitoring.npb.h
src/serialization/nanopb/ecal/core/pb/process.npb.c
src/serialization/nanopb/ecal/core/pb/process.npb.h
src/serialization/nanopb/ecal/core/pb/service.npb.c
src/serialization/nanopb/ecal/core/pb/service.npb.h
src/serialization/nanopb/ecal/core/pb/topic.npb.c
src/serialization/nanopb/ecal/core/pb/topic.npb.h
src/serialization/ecal_serialize_common.cpp
src/serialization/ecal_serialize_common.h
src/serialization/ecal_serialize_logging.cpp
Expand Down Expand Up @@ -700,8 +700,8 @@ target_link_libraries(${PROJECT_NAME}
target_include_directories(${PROJECT_NAME}
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/serialization/>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/serialization/nanopb>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/serialization/nanopb/nanopb>
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/>
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/serialization/ecal_serialize_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @brief eCAL common (de)serialization
**/

#include "nanopb/ecal.pb.h"
#include "nanopb/ecal/core/pb/ecal.npb.h"
#include "nanopb/pb_decode.h"
#include "nanopb/pb_encode.h"
#include <cstddef>
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/serialization/ecal_serialize_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "nanopb/pb_encode.h"
#include "nanopb/pb_decode.h"
#include "nanopb/logging.pb.h"
#include "nanopb/ecal/core/pb/logging.npb.h"

#include "ecal_serialize_common.h"
#include "ecal_serialize_logging.h"
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/serialization/ecal_serialize_monitoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "nanopb/pb_encode.h"
#include "nanopb/pb_decode.h"
#include "nanopb/monitoring.pb.h"
#include "nanopb/ecal/core/pb/monitoring.npb.h"

#include "ecal_serialize_common.h"
#include "ecal_serialize_monitoring.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "nanopb/pb_encode.h"
#include "nanopb/pb_decode.h"
#include "nanopb/ecal.pb.h"
#include "nanopb/ecal/core/pb/ecal.npb.h"

#include "ecal_serialize_common.h"
#include "ecal_struct_sample_payload.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "nanopb/pb_encode.h"
#include "nanopb/pb_decode.h"
#include "nanopb/ecal.pb.h"
#include "nanopb/ecal/core/pb/ecal.npb.h"

#include "ecal_serialize_common.h"
#include "ecal_serialize_sample_registration.h"
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/serialization/ecal_serialize_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "nanopb/pb_encode.h"
#include "nanopb/pb_decode.h"
#include "nanopb/ecal.pb.h"
#include "nanopb/ecal/core/pb/ecal.npb.h"

#include "ecal_serialize_common.h"
#include "ecal_serialize_service.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#include "ecal.pb.h"
#include "ecal.npb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
Expand All @@ -17,3 +17,4 @@ PB_BIND(eCAL_pb_SampleList, eCAL_pb_SampleList, AUTO)




Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#ifndef PB_ECAL_PB_ECAL_PB_H_INCLUDED
#define PB_ECAL_PB_ECAL_PB_H_INCLUDED
#ifndef PB_ECAL_PB_ECAL_NPB_H_INCLUDED
#define PB_ECAL_PB_ECAL_NPB_H_INCLUDED
#include <pb.h>
#include "host.pb.h"
#include "process.pb.h"
#include "service.pb.h"
#include "topic.pb.h"
#include "ecal/core/pb/host.npb.h"
#include "ecal/core/pb/process.npb.h"
#include "ecal/core/pb/service.npb.h"
#include "ecal/core/pb/topic.npb.h"

#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#include "host.pb.h"
#include "host.npb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#ifndef PB_ECAL_PB_HOST_PB_H_INCLUDED
#define PB_ECAL_PB_HOST_PB_H_INCLUDED
#ifndef PB_ECAL_PB_HOST_NPB_H_INCLUDED
#define PB_ECAL_PB_HOST_NPB_H_INCLUDED
#include <pb.h>

#if PB_PROTO_HEADER_VERSION != 40
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#include "layer.pb.h"
#include "layer.npb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
Expand All @@ -23,3 +23,4 @@ PB_BIND(eCAL_pb_TLayer, eCAL_pb_TLayer, AUTO)




Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#ifndef PB_ECAL_PB_LAYER_PB_H_INCLUDED
#define PB_ECAL_PB_LAYER_PB_H_INCLUDED
#ifndef PB_ECAL_PB_LAYER_NPB_H_INCLUDED
#define PB_ECAL_PB_LAYER_NPB_H_INCLUDED
#include <pb.h>

#if PB_PROTO_HEADER_VERSION != 40
Expand Down Expand Up @@ -150,7 +150,7 @@ extern const pb_msgdesc_t eCAL_pb_TLayer_msg;
/* eCAL_pb_LayerParShm_size depends on runtime parameters */
/* eCAL_pb_ConnnectionPar_size depends on runtime parameters */
/* eCAL_pb_TLayer_size depends on runtime parameters */
#define ECAL_PB_LAYER_PB_H_MAX_SIZE eCAL_pb_LayerParTcp_size
#define ECAL_PB_LAYER_NPB_H_MAX_SIZE eCAL_pb_LayerParTcp_size
#define eCAL_pb_LayerParTcp_size 11
#define eCAL_pb_LayerParUdpMC_size 0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#include "logging.pb.h"
#include "logging.npb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#ifndef PB_ECAL_PB_LOGGING_PB_H_INCLUDED
#define PB_ECAL_PB_LOGGING_PB_H_INCLUDED
#ifndef PB_ECAL_PB_LOGGING_NPB_H_INCLUDED
#define PB_ECAL_PB_LOGGING_NPB_H_INCLUDED
#include <pb.h>

#if PB_PROTO_HEADER_VERSION != 40
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#include "monitoring.pb.h"
#include "monitoring.npb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#ifndef PB_ECAL_PB_MONITORING_PB_H_INCLUDED
#define PB_ECAL_PB_MONITORING_PB_H_INCLUDED
#ifndef PB_ECAL_PB_MONITORING_NPB_H_INCLUDED
#define PB_ECAL_PB_MONITORING_NPB_H_INCLUDED
#include <pb.h>
#include "host.pb.h"
#include "process.pb.h"
#include "service.pb.h"
#include "topic.pb.h"
#include "ecal/core/pb/host.npb.h"
#include "ecal/core/pb/process.npb.h"
#include "ecal/core/pb/service.npb.h"
#include "ecal/core/pb/topic.npb.h"

#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.4.8 */
/* Generated by nanopb-0.4.9 */

#include "process.pb.h"
#include "process.npb.h"
#if PB_PROTO_HEADER_VERSION != 40
#error Regenerate this file with the current version of nanopb generator.
#endif
Expand All @@ -16,3 +16,6 @@ PB_BIND(eCAL_pb_Process, eCAL_pb_Process, AUTO)






Loading

0 comments on commit 51ed6f0

Please sign in to comment.