Skip to content

Commit

Permalink
Generate protobuf source files in build dir. (netdata#19576)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkalintiris authored Feb 5, 2025
1 parent 9625364 commit 2d4062e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 55 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ src/collectors/ebpf.plugin/reset_netdata_trace.sh
!ebpf.plugin/
src/libnetdata/ebpf/includes/

# protoc generated files
*.pb.cc
*.pb.h

# installation artifacts
packaging/installer/.environment.sh
*.tar.*
Expand Down
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,7 @@ endif()
# proto definitions
#
netdata_protoc_generate_cpp("${CMAKE_SOURCE_DIR}/src/aclk/aclk-schemas"
"${CMAKE_SOURCE_DIR}/src/aclk/aclk-schemas"
"${CMAKE_BINARY_DIR}/src/aclk/aclk-schemas"
ACLK_PROTO_BUILT_SRCS
ACLK_PROTO_BUILT_HDRS
${ACLK_PROTO_DEFS})
Expand Down Expand Up @@ -2777,7 +2777,7 @@ if(ENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE)
endif()

netdata_protoc_generate_cpp("${CMAKE_SOURCE_DIR}/src/exporting/prometheus/remote_write"
"${CMAKE_SOURCE_DIR}/src/exporting/prometheus/remote_write"
"${CMAKE_BINARY_DIR}/src/exporting/prometheus/remote_write"
PROMETHEUS_REMOTE_WRITE_BUILT_SRCS
PROMETHEUS_REMOTE_WRITE_BUILT_HDRS
"src/exporting/prometheus/remote_write/remote_write.proto")
Expand Down Expand Up @@ -2836,7 +2836,7 @@ target_compile_options(netdata PRIVATE
)

target_include_directories(netdata PRIVATE
"${CMAKE_SOURCE_DIR}/src/aclk/aclk-schemas"
"${CMAKE_BINARY_DIR}/src/aclk/aclk-schemas"
"$<$<BOOL:${ENABLE_EXPORTER_MONGODB}>:${MONGOC_INCLUDE_DIRS}>"
"$<$<BOOL:${ENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE}>:${SNAPPY_INCLUDE_DIRS}>"
)
Expand Down
105 changes: 64 additions & 41 deletions packaging/cmake/Modules/NetdataProtobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -140,48 +140,71 @@ macro(netdata_detect_protobuf)
endif()
endmacro()

# Helper function to compile protocol definitions into C++ code.
function(netdata_protoc_generate_cpp INC_DIR OUT_DIR SRCS HDRS)
if(NOT ARGN)
message(SEND_ERROR "Error: protoc_generate_cpp() called without any proto files")
return()
endif()

set(${INC_DIR})
set(${OUT_DIR})
set(${SRCS})
set(${HDRS})

foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(DIR ${ABS_FIL} DIRECTORY)
get_filename_component(FIL_WE ${FIL} NAME_WE)

set(GENERATED_PB_CC "${DIR}/${FIL_WE}.pb.cc")
list(APPEND ${SRCS} ${GENERATED_PB_CC})

set(GENERATED_PB_H "${DIR}/${FIL_WE}.pb.h")
list(APPEND ${HDRS} ${GENERATED_PB_H})

list(APPEND _PROTOC_INCLUDE_DIRS ${INC_DIR})

if(ENABLE_BUNDLED_PROTOBUF)
list(APPEND _PROTOC_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/_deps/protobuf-src/src/)
endif()

add_custom_command(OUTPUT ${GENERATED_PB_CC} ${GENERATED_PB_H}
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS "-I$<JOIN:${_PROTOC_INCLUDE_DIRS},;-I>" --cpp_out=${OUT_DIR} ${ABS_FIL}
DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
COMMAND_EXPAND_LISTS)
endforeach()

set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES COMPILE_OPTIONS -Wno-deprecated-declarations)

set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
# Helper function to compile protocol definitions into C++ code.
function(netdata_protoc_generate_cpp PROTO_ROOT_DIR OUTPUT_ROOT_DIR GENERATED_SOURCES GENERATED_HEADERS)
if(NOT ARGN)
message(SEND_ERROR "Error: netdata_protoc_generate_cpp() called without any proto files")
return()
endif()

# Initialize output variables
set(output_sources)
set(output_headers)

# Setup include paths for protoc
set(protoc_include_paths ${PROTO_ROOT_DIR})
if(ENABLE_BUNDLED_PROTOBUF)
list(APPEND protoc_include_paths ${CMAKE_BINARY_DIR}/_deps/protobuf-src/src/)
endif()

# Process each proto file
foreach(proto_file ${ARGN})
# Get absolute paths and component parts
get_filename_component(proto_file_abs_path ${proto_file} ABSOLUTE)
get_filename_component(proto_file_name_no_ext ${proto_file} NAME_WE)
get_filename_component(proto_file_dir ${proto_file} DIRECTORY)

# Calculate relative output path to maintain directory structure
get_filename_component(proto_root_abs_path ${PROTO_ROOT_DIR} ABSOLUTE)
get_filename_component(proto_dir_abs_path ${proto_file_dir} ABSOLUTE)
file(RELATIVE_PATH proto_relative_path ${proto_root_abs_path} ${proto_dir_abs_path})

# Construct output file paths
set(output_dir "${OUTPUT_ROOT_DIR}/${proto_relative_path}")
set(generated_source "${output_dir}/${proto_file_name_no_ext}.pb.cc")
set(generated_header "${output_dir}/${proto_file_name_no_ext}.pb.h")

# Add to output lists
list(APPEND output_sources "${generated_source}")
list(APPEND output_headers "${generated_header}")

# Create custom command to generate the protobuf files
add_custom_command(
OUTPUT "${generated_source}" "${generated_header}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${output_dir}"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS "-I$<JOIN:${protoc_include_paths},;-I>"
--cpp_out=${OUTPUT_ROOT_DIR}
${proto_file_abs_path}
DEPENDS ${proto_file_abs_path} ${PROTOBUF_PROTOC_EXECUTABLE}
COMMENT "Generating C++ protocol buffer files from ${proto_file}"
COMMAND_EXPAND_LISTS
VERBATIM
)
endforeach()

# Mark generated files with proper properties
set_source_files_properties(
${output_sources} ${output_headers}
PROPERTIES
GENERATED TRUE
COMPILE_OPTIONS -Wno-deprecated-declarations
)

# Set output variables in parent scope
set(${GENERATED_SOURCES} ${output_sources} PARENT_SCOPE)
set(${GENERATED_HEADERS} ${output_headers} PARENT_SCOPE)
endfunction()

# Add protobuf to a specified target.
Expand Down
2 changes: 1 addition & 1 deletion src/aclk/aclk_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "aclk_query.h"
#include "aclk_tx_msgs.h"
#include "../../web/server/web_client_cache.h"
#include "web/server/web_client_cache.h"

static HTTP_ACL default_aclk_http_acl = HTTP_ACL_ALL_FEATURES;

Expand Down
4 changes: 2 additions & 2 deletions src/aclk/schema-wrappers/connection.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "proto/agent/v1/connection.pb.h"
#include "proto/agent/v1/disconnect.pb.h"
#include "src/aclk/aclk-schemas/proto/agent/v1/connection.pb.h"
#include "src/aclk/aclk-schemas/proto/agent/v1/disconnect.pb.h"
#include "connection.h"

#include "schema_wrapper_utils.h"
Expand Down
6 changes: 3 additions & 3 deletions src/database/sqlite/sqlite_aclk.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ void sanity_check(void) {
}

#include "sqlite_aclk_node.h"
#include "../aclk_query_queue.h"
#include "../aclk_query.h"
#include "../aclk_capas.h"
#include "aclk/aclk_query_queue.h"
#include "aclk/aclk_query.h"
#include "aclk/aclk_capas.h"

static void create_node_instance_result_job(const char *machine_guid, const char *node_id)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include <snappy.h>
#include "remote_write.pb.h"
#include "src/exporting/prometheus/remote_write/remote_write.pb.h"
#include "remote_write_request.h"

using namespace prometheus;
Expand Down

0 comments on commit 2d4062e

Please sign in to comment.