From 06251f8cd6a36f8406bf91874af2211de7e81441 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 9 Nov 2017 11:09:29 +0100 Subject: [PATCH] make helloworld CMakeLists compile on windows --- examples/cpp/helloworld/CMakeLists.txt | 23 +++++++++++++++++---- test/distrib/cpp/run_distrib_test_cmake.bat | 10 +++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/examples/cpp/helloworld/CMakeLists.txt b/examples/cpp/helloworld/CMakeLists.txt index 71a8db4f24f..a1869d21bbb 100644 --- a/examples/cpp/helloworld/CMakeLists.txt +++ b/examples/cpp/helloworld/CMakeLists.txt @@ -6,13 +6,28 @@ project(HelloWorld C CXX) if(NOT MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +else() + add_definitions(-D_WIN32_WINNT=0x600) endif() # Protobuf -set(protobuf_MODULE_COMPATIBLE TRUE) -find_package(protobuf CONFIG REQUIRED) +# NOTE: we cannot use "CONFIG" mode here because protobuf-config.cmake +# is broken when used with CMAKE_INSTALL_PREFIX +find_package(protobuf REQUIRED) message(STATUS "Using protobuf ${protobuf_VERSION}") +if(Protobuf_FOUND) + # Protobuf_FOUND is set for package type "CONFIG" + set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf) + set(_PROTOBUF_PROTOC protobuf::protoc) +endif() +if(PROTOBUF_FOUND) + # PROTOBUF_FOUND is set for package type "MODULE" + set(_PROTOBUF_LIBPROTOBUF ${PROTOBUF_LIBRARIES}) + set(_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE}) + include_directories(${PROTOBUF_INCLUDE_DIRS}) +endif() + # gRPC find_package(gRPC CONFIG REQUIRED) message(STATUS "Using gRPC ${gRPC_VERSION}") @@ -31,7 +46,7 @@ set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc") set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h") add_custom_command( OUTPUT "${hw_grpc_srcs}" "${hw_grpc_hdrs}" - COMMAND protobuf::protoc + COMMAND ${_PROTOBUF_PROTOC} ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" -I "${hw_proto_path}" --plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}" "${hw_proto}" @@ -48,6 +63,6 @@ foreach(_target ${hw_proto_srcs} ${hw_grpc_srcs}) target_link_libraries(${_target} - protobuf::libprotobuf + ${_PROTOBUF_LIBPROTOBUF} gRPC::grpc++_unsecure) endforeach() diff --git a/test/distrib/cpp/run_distrib_test_cmake.bat b/test/distrib/cpp/run_distrib_test_cmake.bat index 58d4a2a4c95..ac4f6b77c00 100644 --- a/test/distrib/cpp/run_distrib_test_cmake.bat +++ b/test/distrib/cpp/run_distrib_test_cmake.bat @@ -57,6 +57,16 @@ cmake -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DOPENSSL_ROOT_DIR=C:/OpenSSL-Win32 - cmake --build . --config Release --target install || goto :error cd ../.. +# Build helloworld example using cmake +cd examples/cpp/helloworld +mkdir cmake +cd cmake +mkdir build +cd build +cmake -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% ../.. || goto :error +cmake --build . --config Release || goto :error +cd ../../../../.. + goto :EOF :error