From 68b2e1db08a8e85cac102499751e36a7a2666034 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Tue, 25 Jul 2023 15:29:39 +0200 Subject: [PATCH] CMake: Make examples build as external projects A couple of things are needed to ensure examples can be built as external projects. Change FindWrapgRPCPlugin to look for a cache var in addition to _ROOT env var, because we can't easily pass env vars to external projects. Ensure one of the non-Qt test libraries is built with at least C++17 to avoid compilation errors. Derive a path to absl_ROOT from gRPC_ROOT assuming they are in the same parent directory. Use the new QT_EXAMPLE_CMAKE_VARS_TO_PASS variable introduced in qtbase to allow passing additional cmake variables to all examples that are built as external projects. We need to use it for all 3rd party dependencies that have to be found by the examples. Make sure to look for GrpcTools package for magic8ball example, to ensure the public api is available when cross-compiling, but there is no target native grpc library available. Task-number: QTBUG-90820 Task-number: QTBUG-96232 Change-Id: I4420b7348c6a3b02b5516f34a96b19cceaccc649 Reviewed-by: Alexey Edelev --- cmake/FindWrapgRPCPlugin.cmake | 2 +- examples/CMakeLists.txt | 28 +++++++++++++++++++ examples/grpc/magic8ball/CMakeLists.txt | 1 + .../grpc_server_example/CMakeLists.txt | 4 +++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cmake/FindWrapgRPCPlugin.cmake b/cmake/FindWrapgRPCPlugin.cmake index 536972f3..280a370f 100644 --- a/cmake/FindWrapgRPCPlugin.cmake +++ b/cmake/FindWrapgRPCPlugin.cmake @@ -10,7 +10,7 @@ find_program(__WrapgRPCPlugin_plugin_imported_location NAMES grpc_cpp_plugin grpc_cpp_plugin.exe # Support for vcpkg-based directory layout PATH_SUFFIXES tools/grpc - HINTS "$ENV{gRPC_ROOT}/bin") + HINTS "$ENV{gRPC_ROOT}/bin" "${gRPC_ROOT}/bin") set(WrapgRPCPlugin_FOUND FALSE) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6f592b24..d66f9d14 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,6 +1,34 @@ # Copyright (C) 2023 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause +# Pass 3rd party dependency locations to examples as external projects. +if(NOT Protobuf_ROOT AND "$ENV{Protobuf_ROOT}") + set(Protobuf_ROOT "$ENV{Protobuf_ROOT}") +endif() + +if(NOT gRPC_ROOT AND "$ENV{gRPC_ROOT}") + set(gRPC_ROOT "$ENV{gRPC_ROOT}") +endif() + +if(NOT absl_ROOT AND "$ENV{absl_ROOT}") + set(absl_ROOT "$ENV{absl_ROOT}") +endif() + +if(NOT OPENSSL_ROOT_DIR AND "$ENV{OPENSSL_ROOT_DIR}") + set(OPENSSL_ROOT_DIR "$ENV{OPENSSL_ROOT_DIR}") +endif() + +set(QT_EXAMPLE_CMAKE_VARS_TO_PASS Protobuf_ROOT:STRING gRPC_ROOT:STRING OPENSSL_ROOT_DIR:STRING) + +# In the CI, we assume the absl dependency is next to the gRPC one. +if(gRPC_ROOT AND NOT absl_ROOT) + get_filename_component(absl_ROOT "${gRPC_ROOT}" DIRECTORY) + string(APPEND absl_ROOT "/absl") + if(EXISTS "${absl_ROOT}") + list(APPEND QT_EXAMPLE_CMAKE_VARS_TO_PASS absl_ROOT:STRING) + endif() +endif() + qt_examples_build_begin(EXTERNAL_BUILD) add_subdirectory(protobuf) diff --git a/examples/grpc/magic8ball/CMakeLists.txt b/examples/grpc/magic8ball/CMakeLists.txt index 2659e265..a7baafc8 100644 --- a/examples/grpc/magic8ball/CMakeLists.txt +++ b/examples/grpc/magic8ball/CMakeLists.txt @@ -15,6 +15,7 @@ find_package(Qt6 REQUIRED COMPONENTS Protobuf Grpc GrpcQuick + GrpcTools Quick ) diff --git a/examples/grpc/magic8ball/grpc_server_example/CMakeLists.txt b/examples/grpc/magic8ball/grpc_server_example/CMakeLists.txt index cb7401cf..ab7e8dc6 100644 --- a/examples/grpc/magic8ball/grpc_server_example/CMakeLists.txt +++ b/examples/grpc/magic8ball/grpc_server_example/CMakeLists.txt @@ -16,6 +16,10 @@ if(NOT TARGET WrapgRPC::WrapgRPCPlugin OR NOT TARGET WrapProtoc::WrapProtoc return() endif() +# Avoid "Protobuf requires at least C++11." errors +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(proto_files "${CMAKE_CURRENT_LIST_DIR}/../proto/exampleservice.proto") set(out_dir ${CMAKE_CURRENT_BINARY_DIR})