Skip to content

Commit

Permalink
Merge pull request #276 from alltilla/grpc-factor-out-common-classes
Browse files Browse the repository at this point in the history
grpc: factor out base classes for gRPC based drivers
  • Loading branch information
MrAnno authored Oct 7, 2024
2 parents 8d8eab0 + 9413815 commit e241431
Show file tree
Hide file tree
Showing 69 changed files with 1,871 additions and 1,903 deletions.
79 changes: 55 additions & 24 deletions lib/merge-grammar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3
#############################################################################
# Copyright (c) 2024 Axoflow
# Copyright (c) 2024 Attila Szakacs <[email protected]>
# Copyright (c) 2010-2017 Balabit
#
# This library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -33,36 +35,65 @@
import sys
import codecs

grammar_file = os.path.join(os.environ.get('top_srcdir', ''), 'lib/cfg-grammar.y')
if not os.path.isfile(grammar_file):
grammar_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'cfg-grammar.y')
if not os.path.isfile(grammar_file):
sys.exit('Error opening cfg-grammar.y')
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
COMMON_GRAMMAR_FILE = 'lib/cfg-grammar.y'
BLOCKS = {}


def locate_file(file_name):
full_path = os.path.join(os.environ.get('top_srcdir', ''), file_name)
if not os.path.isfile(full_path):
full_path = os.path.join(ROOT_DIR, file_name)
if not os.path.isfile(full_path):
sys.exit('Error opening ' + file_name)
return full_path


def print_to_stdout(line):
if sys.hexversion >= 0x3000000:
sys.stdout.buffer.write(line.encode("utf-8"))
else:
print(line.encode("utf-8"), end='')

def include_block(block_type):
start_marker = 'START_' + block_type
end_marker = 'END_' + block_type

with codecs.open(grammar_file, encoding="utf-8") as f:
in_block = False
def collect_block_definitions(file_name):
with codecs.open(locate_file(file_name), encoding="utf-8") as f:
block_name = None
block = []
for line in f:
if start_marker in line:
in_block = True
elif end_marker in line:
in_block = False
elif in_block:
print_to_stdout(line)

for line in fileinput.input(openhook=fileinput.hook_encoded("utf-8")):
if 'INCLUDE_DECLS' in line:
include_block('DECLS')
elif 'INCLUDE_RULES' in line:
include_block('RULES')
else:
print_to_stdout(line)
if line.startswith('/* START_') and line.endswith(' */\n'):
block_name = line[len('/* START_'):-len(' */\n')]
elif block_name and line == '/* END_' + block_name + ' */\n':
BLOCKS[block_name] = BLOCKS.get(block_name, '') + ''.join(block)
block_name = None
block = []
elif block_name:
block.append(line)


def include_block(block_type):
try:
print_to_stdout(BLOCKS[block_type])
except KeyError:
sys.exit('Block not found: ' + block_type)


def print_preprocessed_grammar():
for line in fileinput.input(openhook=fileinput.hook_encoded("utf-8")):
if line.startswith('/* INCLUDE_') and line.endswith(' */\n'):
block_name = line[len('/* INCLUDE_'):-len(' */\n')]
include_block(block_name)
elif line.startswith('/* REQUIRE ') and line.endswith(' */\n'):
file_name = line[len('/* REQUIRE '):-len(' */\n')]
collect_block_definitions(file_name)
else:
print_to_stdout(line)


def main():
collect_block_definitions(COMMON_GRAMMAR_FILE)
print_preprocessed_grammar()


if __name__ == '__main__':
main()
3 changes: 1 addition & 2 deletions modules/grpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ if (ENABLE_GRPC)
gRPC::grpc++
protobuf::libprotobuf)

add_subdirectory(credentials)
add_subdirectory(metrics)
add_subdirectory(common)
add_subdirectory(protos)
endif()

Expand Down
3 changes: 1 addition & 2 deletions modules/grpc/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include modules/grpc/protos/Makefile.am

include modules/grpc/credentials/Makefile.am
include modules/grpc/metrics/Makefile.am
include modules/grpc/common/Makefile.am

include modules/grpc/otel/Makefile.am
include modules/grpc/loki/Makefile.am
Expand Down
5 changes: 2 additions & 3 deletions modules/grpc/bigquery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ set(BIGQUERY_CPP_SOURCES
bigquery-dest.h
bigquery-worker.hpp
bigquery-worker.cpp
bigquery-worker.h
)

set(BIGQUERY_SOURCES
Expand All @@ -21,15 +20,15 @@ set(BIGQUERY_SOURCES
add_module(
TARGET bigquery-cpp
SOURCES ${BIGQUERY_CPP_SOURCES}
DEPENDS ${MODULE_GRPC_LIBS} grpc-protos
DEPENDS ${MODULE_GRPC_LIBS} grpc-protos grpc-common-cpp
INCLUDES ${BIGQUERY_PROTO_BUILDDIR} ${PROJECT_SOURCE_DIR}/modules/grpc
LIBRARY_TYPE STATIC
)

add_module(
TARGET bigquery
GRAMMAR bigquery-grammar
DEPENDS bigquery-cpp
DEPENDS bigquery-cpp grpc-common-cpp
INCLUDES ${PROJECT_SOURCE_DIR}/modules/grpc
SOURCES ${BIGQUERY_SOURCES}
)
Expand Down
6 changes: 4 additions & 2 deletions modules/grpc/bigquery/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ if ENABLE_GRPC
noinst_LTLIBRARIES += modules/grpc/bigquery/libbigquery_cpp.la

modules_grpc_bigquery_libbigquery_cpp_la_SOURCES = \
$(grpc_metrics_sources) \
modules/grpc/bigquery/bigquery-dest.h \
modules/grpc/bigquery/bigquery-dest.hpp \
modules/grpc/bigquery/bigquery-dest.cpp \
modules/grpc/bigquery/bigquery-worker.h \
modules/grpc/bigquery/bigquery-worker.hpp \
modules/grpc/bigquery/bigquery-worker.cpp

modules_grpc_bigquery_libbigquery_cpp_la_CXXFLAGS = \
$(AM_CXXFLAGS) \
$(PROTOBUF_CFLAGS) \
$(GRPCPP_CFLAGS) \
$(GRPC_COMMON_CFLAGS) \
-I$(GOOGLEAPIS_PROTO_BUILDDIR) \
-I$(top_srcdir)/modules/grpc \
-I$(top_srcdir)/modules/grpc/bigquery \
Expand All @@ -34,12 +33,14 @@ modules_grpc_bigquery_libbigquery_la_SOURCES = \

modules_grpc_bigquery_libbigquery_la_CPPFLAGS = \
$(AM_CPPFLAGS) \
$(GRPC_COMMON_CFLAGS) \
-I$(top_srcdir)/modules/grpc/bigquery \
-I$(top_builddir)/modules/grpc/bigquery \
-I$(top_srcdir)/modules/grpc

modules_grpc_bigquery_libbigquery_la_LIBADD = \
$(MODULE_DEPS_LIBS) \
$(GRPC_COMMON_LIBS) \
$(top_builddir)/modules/grpc/protos/libgrpc-protos.la \
$(top_builddir)/modules/grpc/bigquery/libbigquery_cpp.la

Expand All @@ -48,6 +49,7 @@ nodist_EXTRA_modules_grpc_bigquery_libbigquery_la_SOURCES = force-cpp-linker-wit
modules_grpc_bigquery_libbigquery_la_LDFLAGS = $(MODULE_LDFLAGS)
EXTRA_modules_grpc_bigquery_libbigquery_la_DEPENDENCIES = \
$(MODULE_DEPS_LIBS) \
$(GRPC_COMMON_LIBS) \
$(top_builddir)/modules/grpc/protos/libgrpc-protos.la \
$(top_builddir)/modules/grpc/bigquery/libbigquery_cpp.la

Expand Down
Loading

0 comments on commit e241431

Please sign in to comment.