Skip to content

Commit 01837f5

Browse files
authored
Fix protobuf compilation warning in gRPC example (#830)
1 parent f65b07a commit 01837f5

File tree

3 files changed

+87
-84
lines changed

3 files changed

+87
-84
lines changed

cmake/opentelemetry-proto.cmake

+1-82
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,4 @@
1-
macro(check_append_cxx_compiler_flag OUTPUT_VAR)
2-
foreach(CHECK_FLAG ${ARGN})
3-
check_cxx_compiler_flag(${CHECK_FLAG}
4-
"check_cxx_compiler_flag_${CHECK_FLAG}")
5-
if(check_cxx_compiler_flag_${CHECK_FLAG})
6-
list(APPEND ${OUTPUT_VAR} ${CHECK_FLAG})
7-
endif()
8-
endforeach()
9-
endmacro()
10-
11-
if(NOT PATCH_PROTOBUF_SOURCES_OPTIONS_SET)
12-
if(MSVC)
13-
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
14-
set(PATCH_PROTOBUF_SOURCES_OPTIONS /wd4244 /wd4251 /wd4267 /wd4309)
15-
16-
if(MSVC_VERSION GREATER_EQUAL 1922)
17-
# see
18-
# https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=vs-2019#improvements_162
19-
# for detail
20-
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd5054)
21-
endif()
22-
23-
if(MSVC_VERSION GREATER_EQUAL 1925)
24-
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4996)
25-
endif()
26-
27-
if(MSVC_VERSION LESS 1910)
28-
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4800)
29-
endif()
30-
else()
31-
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
32-
include(CheckCXXCompilerFlag)
33-
check_append_cxx_compiler_flag(
34-
PATCH_PROTOBUF_SOURCES_OPTIONS -Wno-type-limits
35-
-Wno-deprecated-declarations -Wno-unused-parameter)
36-
endif()
37-
set(PATCH_PROTOBUF_SOURCES_OPTIONS_SET TRUE)
38-
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
39-
set(PATCH_PROTOBUF_SOURCES_OPTIONS
40-
${PATCH_PROTOBUF_SOURCES_OPTIONS}
41-
CACHE INTERNAL
42-
"Options to disable warning of generated protobuf sources" FORCE)
43-
endif()
44-
endif()
45-
46-
function(patch_protobuf_sources)
47-
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
48-
foreach(PROTO_SRC ${ARGN})
49-
unset(PROTO_SRC_OPTIONS)
50-
get_source_file_property(PROTO_SRC_OPTIONS ${PROTO_SRC} COMPILE_OPTIONS)
51-
if(PROTO_SRC_OPTIONS)
52-
list(APPEND PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
53-
else()
54-
set(PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
55-
endif()
56-
57-
set_source_files_properties(
58-
${PROTO_SRC} PROPERTIES COMPILE_OPTIONS "${PROTO_SRC_OPTIONS}")
59-
endforeach()
60-
unset(PROTO_SRC)
61-
unset(PROTO_SRC_OPTIONS)
62-
endif()
63-
endfunction()
64-
65-
function(patch_protobuf_targets)
66-
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
67-
foreach(PROTO_TARGET ${ARGN})
68-
unset(PROTO_TARGET_OPTIONS)
69-
get_target_property(PROTO_TARGET_OPTIONS ${PROTO_TARGET} COMPILE_OPTIONS)
70-
if(PROTO_TARGET_OPTIONS)
71-
list(APPEND PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
72-
else()
73-
set(PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
74-
endif()
75-
76-
set_target_properties(
77-
${PROTO_TARGET} PROPERTIES COMPILE_OPTIONS "${PROTO_TARGET_OPTIONS}")
78-
endforeach()
79-
unset(PROTO_TARGET)
80-
unset(PROTO_TARGET_OPTIONS)
81-
endif()
82-
endfunction()
1+
include(${PROJECT_SOURCE_DIR}/cmake/proto-options-patch.cmake)
832

843
set(PROTO_PATH "${CMAKE_CURRENT_SOURCE_DIR}/third_party/opentelemetry-proto")
854

cmake/proto-options-patch.cmake

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
macro(check_append_cxx_compiler_flag OUTPUT_VAR)
2+
foreach(CHECK_FLAG ${ARGN})
3+
check_cxx_compiler_flag(${CHECK_FLAG}
4+
"check_cxx_compiler_flag_${CHECK_FLAG}")
5+
if(check_cxx_compiler_flag_${CHECK_FLAG})
6+
list(APPEND ${OUTPUT_VAR} ${CHECK_FLAG})
7+
endif()
8+
endforeach()
9+
endmacro()
10+
11+
if(NOT PATCH_PROTOBUF_SOURCES_OPTIONS_SET)
12+
if(MSVC)
13+
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
14+
set(PATCH_PROTOBUF_SOURCES_OPTIONS /wd4244 /wd4251 /wd4267 /wd4309)
15+
16+
if(MSVC_VERSION GREATER_EQUAL 1922)
17+
# see
18+
# https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=vs-2019#improvements_162
19+
# for detail
20+
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd5054)
21+
endif()
22+
23+
if(MSVC_VERSION GREATER_EQUAL 1925)
24+
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4996)
25+
endif()
26+
27+
if(MSVC_VERSION LESS 1910)
28+
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4800)
29+
endif()
30+
else()
31+
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
32+
include(CheckCXXCompilerFlag)
33+
check_append_cxx_compiler_flag(
34+
PATCH_PROTOBUF_SOURCES_OPTIONS -Wno-type-limits
35+
-Wno-deprecated-declarations -Wno-unused-parameter)
36+
endif()
37+
set(PATCH_PROTOBUF_SOURCES_OPTIONS_SET TRUE)
38+
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
39+
set(PATCH_PROTOBUF_SOURCES_OPTIONS
40+
${PATCH_PROTOBUF_SOURCES_OPTIONS}
41+
CACHE INTERNAL
42+
"Options to disable warning of generated protobuf sources" FORCE)
43+
endif()
44+
endif()
45+
46+
function(patch_protobuf_sources)
47+
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
48+
foreach(PROTO_SRC ${ARGN})
49+
unset(PROTO_SRC_OPTIONS)
50+
get_source_file_property(PROTO_SRC_OPTIONS ${PROTO_SRC} COMPILE_OPTIONS)
51+
if(PROTO_SRC_OPTIONS)
52+
list(APPEND PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
53+
else()
54+
set(PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
55+
endif()
56+
57+
set_source_files_properties(
58+
${PROTO_SRC} PROPERTIES COMPILE_OPTIONS "${PROTO_SRC_OPTIONS}")
59+
endforeach()
60+
unset(PROTO_SRC)
61+
unset(PROTO_SRC_OPTIONS)
62+
endif()
63+
endfunction()
64+
65+
function(patch_protobuf_targets)
66+
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
67+
foreach(PROTO_TARGET ${ARGN})
68+
unset(PROTO_TARGET_OPTIONS)
69+
get_target_property(PROTO_TARGET_OPTIONS ${PROTO_TARGET} COMPILE_OPTIONS)
70+
if(PROTO_TARGET_OPTIONS)
71+
list(APPEND PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
72+
else()
73+
set(PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
74+
endif()
75+
76+
set_target_properties(
77+
${PROTO_TARGET} PROPERTIES COMPILE_OPTIONS "${PROTO_TARGET_OPTIONS}")
78+
endforeach()
79+
unset(PROTO_TARGET)
80+
unset(PROTO_TARGET_OPTIONS)
81+
endif()
82+
endfunction()

examples/grpc/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ add_custom_command(
1616
${PROTOBUF_PROTOC_EXECUTABLE} ARGS "--grpc_out=${CMAKE_CURRENT_BINARY_DIR}"
1717
"--cpp_out=${CMAKE_CURRENT_BINARY_DIR}" "--proto_path=${proto_file_path}"
1818
--plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}" "${proto_file}")
19-
# DEPENDS "${proto_file}")
2019

21-
# hw_grpc_proto
2220
add_library(example_grpc_proto ${example_grpc_srcs} ${example_grpc_hdrs}
2321
${example_proto_srcs} ${example_proto_hdrs})
2422

23+
include(${PROJECT_SOURCE_DIR}/cmake/proto-options-patch.cmake)
24+
patch_protobuf_targets(example_grpc_proto)
25+
2526
include_directories(
2627
${CMAKE_SOURCE_DIR}/exporters/ostream/include ${CMAKE_SOURCE_DIR}/ext/include
2728
${CMAKE_SOURCE_DIR}/api/include/ ${CMAKE_SOURCE_DIR/})
@@ -45,4 +46,5 @@ foreach(_target client server)
4546
gRPC::grpc++_reflection
4647
opentelemetry_trace
4748
opentelemetry_exporter_ostream_span)
49+
patch_protobuf_targets(${_target})
4850
endforeach()

0 commit comments

Comments
 (0)