Skip to content

Commit

Permalink
Fix protobuf compilation warning in gRPC example (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Jun 8, 2021
1 parent f65b07a commit 01837f5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 84 deletions.
83 changes: 1 addition & 82 deletions cmake/opentelemetry-proto.cmake
Original file line number Diff line number Diff line change
@@ -1,85 +1,4 @@
macro(check_append_cxx_compiler_flag OUTPUT_VAR)
foreach(CHECK_FLAG ${ARGN})
check_cxx_compiler_flag(${CHECK_FLAG}
"check_cxx_compiler_flag_${CHECK_FLAG}")
if(check_cxx_compiler_flag_${CHECK_FLAG})
list(APPEND ${OUTPUT_VAR} ${CHECK_FLAG})
endif()
endforeach()
endmacro()

if(NOT PATCH_PROTOBUF_SOURCES_OPTIONS_SET)
if(MSVC)
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
set(PATCH_PROTOBUF_SOURCES_OPTIONS /wd4244 /wd4251 /wd4267 /wd4309)

if(MSVC_VERSION GREATER_EQUAL 1922)
# see
# https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=vs-2019#improvements_162
# for detail
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd5054)
endif()

if(MSVC_VERSION GREATER_EQUAL 1925)
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4996)
endif()

if(MSVC_VERSION LESS 1910)
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4800)
endif()
else()
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
include(CheckCXXCompilerFlag)
check_append_cxx_compiler_flag(
PATCH_PROTOBUF_SOURCES_OPTIONS -Wno-type-limits
-Wno-deprecated-declarations -Wno-unused-parameter)
endif()
set(PATCH_PROTOBUF_SOURCES_OPTIONS_SET TRUE)
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
set(PATCH_PROTOBUF_SOURCES_OPTIONS
${PATCH_PROTOBUF_SOURCES_OPTIONS}
CACHE INTERNAL
"Options to disable warning of generated protobuf sources" FORCE)
endif()
endif()

function(patch_protobuf_sources)
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
foreach(PROTO_SRC ${ARGN})
unset(PROTO_SRC_OPTIONS)
get_source_file_property(PROTO_SRC_OPTIONS ${PROTO_SRC} COMPILE_OPTIONS)
if(PROTO_SRC_OPTIONS)
list(APPEND PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
else()
set(PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
endif()

set_source_files_properties(
${PROTO_SRC} PROPERTIES COMPILE_OPTIONS "${PROTO_SRC_OPTIONS}")
endforeach()
unset(PROTO_SRC)
unset(PROTO_SRC_OPTIONS)
endif()
endfunction()

function(patch_protobuf_targets)
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
foreach(PROTO_TARGET ${ARGN})
unset(PROTO_TARGET_OPTIONS)
get_target_property(PROTO_TARGET_OPTIONS ${PROTO_TARGET} COMPILE_OPTIONS)
if(PROTO_TARGET_OPTIONS)
list(APPEND PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
else()
set(PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
endif()

set_target_properties(
${PROTO_TARGET} PROPERTIES COMPILE_OPTIONS "${PROTO_TARGET_OPTIONS}")
endforeach()
unset(PROTO_TARGET)
unset(PROTO_TARGET_OPTIONS)
endif()
endfunction()
include(${PROJECT_SOURCE_DIR}/cmake/proto-options-patch.cmake)

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

Expand Down
82 changes: 82 additions & 0 deletions cmake/proto-options-patch.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
macro(check_append_cxx_compiler_flag OUTPUT_VAR)
foreach(CHECK_FLAG ${ARGN})
check_cxx_compiler_flag(${CHECK_FLAG}
"check_cxx_compiler_flag_${CHECK_FLAG}")
if(check_cxx_compiler_flag_${CHECK_FLAG})
list(APPEND ${OUTPUT_VAR} ${CHECK_FLAG})
endif()
endforeach()
endmacro()

if(NOT PATCH_PROTOBUF_SOURCES_OPTIONS_SET)
if(MSVC)
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
set(PATCH_PROTOBUF_SOURCES_OPTIONS /wd4244 /wd4251 /wd4267 /wd4309)

if(MSVC_VERSION GREATER_EQUAL 1922)
# see
# https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=vs-2019#improvements_162
# for detail
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd5054)
endif()

if(MSVC_VERSION GREATER_EQUAL 1925)
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4996)
endif()

if(MSVC_VERSION LESS 1910)
list(APPEND PATCH_PROTOBUF_SOURCES_OPTIONS /wd4800)
endif()
else()
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
include(CheckCXXCompilerFlag)
check_append_cxx_compiler_flag(
PATCH_PROTOBUF_SOURCES_OPTIONS -Wno-type-limits
-Wno-deprecated-declarations -Wno-unused-parameter)
endif()
set(PATCH_PROTOBUF_SOURCES_OPTIONS_SET TRUE)
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
set(PATCH_PROTOBUF_SOURCES_OPTIONS
${PATCH_PROTOBUF_SOURCES_OPTIONS}
CACHE INTERNAL
"Options to disable warning of generated protobuf sources" FORCE)
endif()
endif()

function(patch_protobuf_sources)
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
foreach(PROTO_SRC ${ARGN})
unset(PROTO_SRC_OPTIONS)
get_source_file_property(PROTO_SRC_OPTIONS ${PROTO_SRC} COMPILE_OPTIONS)
if(PROTO_SRC_OPTIONS)
list(APPEND PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
else()
set(PROTO_SRC_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
endif()

set_source_files_properties(
${PROTO_SRC} PROPERTIES COMPILE_OPTIONS "${PROTO_SRC_OPTIONS}")
endforeach()
unset(PROTO_SRC)
unset(PROTO_SRC_OPTIONS)
endif()
endfunction()

function(patch_protobuf_targets)
if(PATCH_PROTOBUF_SOURCES_OPTIONS)
foreach(PROTO_TARGET ${ARGN})
unset(PROTO_TARGET_OPTIONS)
get_target_property(PROTO_TARGET_OPTIONS ${PROTO_TARGET} COMPILE_OPTIONS)
if(PROTO_TARGET_OPTIONS)
list(APPEND PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
else()
set(PROTO_TARGET_OPTIONS ${PATCH_PROTOBUF_SOURCES_OPTIONS})
endif()

set_target_properties(
${PROTO_TARGET} PROPERTIES COMPILE_OPTIONS "${PROTO_TARGET_OPTIONS}")
endforeach()
unset(PROTO_TARGET)
unset(PROTO_TARGET_OPTIONS)
endif()
endfunction()
6 changes: 4 additions & 2 deletions examples/grpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ add_custom_command(
${PROTOBUF_PROTOC_EXECUTABLE} ARGS "--grpc_out=${CMAKE_CURRENT_BINARY_DIR}"
"--cpp_out=${CMAKE_CURRENT_BINARY_DIR}" "--proto_path=${proto_file_path}"
--plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}" "${proto_file}")
# DEPENDS "${proto_file}")

# hw_grpc_proto
add_library(example_grpc_proto ${example_grpc_srcs} ${example_grpc_hdrs}
${example_proto_srcs} ${example_proto_hdrs})

include(${PROJECT_SOURCE_DIR}/cmake/proto-options-patch.cmake)
patch_protobuf_targets(example_grpc_proto)

include_directories(
${CMAKE_SOURCE_DIR}/exporters/ostream/include ${CMAKE_SOURCE_DIR}/ext/include
${CMAKE_SOURCE_DIR}/api/include/ ${CMAKE_SOURCE_DIR/})
Expand All @@ -45,4 +46,5 @@ foreach(_target client server)
gRPC::grpc++_reflection
opentelemetry_trace
opentelemetry_exporter_ostream_span)
patch_protobuf_targets(${_target})
endforeach()

0 comments on commit 01837f5

Please sign in to comment.