From 966508da2186a2d0c5f8e4e150c896c4fbe38ca4 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Mon, 7 Jun 2021 10:23:04 -0700 Subject: [PATCH] Fix protobuf compilation warning in gRPC example --- cmake/opentelemetry-proto.cmake | 83 +-------------------------------- cmake/proto-options-patch.cmake | 82 ++++++++++++++++++++++++++++++++ examples/grpc/CMakeLists.txt | 6 ++- 3 files changed, 87 insertions(+), 84 deletions(-) create mode 100644 cmake/proto-options-patch.cmake diff --git a/cmake/opentelemetry-proto.cmake b/cmake/opentelemetry-proto.cmake index 859f13b177..36f389301b 100644 --- a/cmake/opentelemetry-proto.cmake +++ b/cmake/opentelemetry-proto.cmake @@ -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") diff --git a/cmake/proto-options-patch.cmake b/cmake/proto-options-patch.cmake new file mode 100644 index 0000000000..7ed9e5ad57 --- /dev/null +++ b/cmake/proto-options-patch.cmake @@ -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() diff --git a/examples/grpc/CMakeLists.txt b/examples/grpc/CMakeLists.txt index 2b43cf18f6..e5df847807 100644 --- a/examples/grpc/CMakeLists.txt +++ b/examples/grpc/CMakeLists.txt @@ -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/}) @@ -45,4 +46,5 @@ foreach(_target client server) gRPC::grpc++_reflection opentelemetry_trace opentelemetry_exporter_ostream_span) + patch_protobuf_targets(${_target}) endforeach()