SPDX-License-Identifier: BSD-3-Clause

Prefer Protobuf's CMake config target so its Abseil and utf8_range link
dependencies propagate to the compatibility probe and Trezor library.
Compile the probe with the C++17 standard required by packaged Protobuf.

Index: cmake/CheckTrezor.cmake
--- cmake/CheckTrezor.cmake.orig
+++ cmake/CheckTrezor.cmake
@@ -34,7 +34,15 @@
 if (USE_DEVICE_TREZOR)
-    # Protobuf is required to build protobuf messages for Trezor
-    include(FindProtobuf OPTIONAL)
-    find_package(Protobuf)
-    _trezor_protobuf_fix_vars()
-
+    # Prefer protobuf-config.cmake so transitive Abseil dependencies are linked.
+    find_package(Protobuf CONFIG)
+
+    if (Protobuf_FOUND)
+        find_program(Protobuf_PROTOC_EXECUTABLE protoc REQUIRED)
+        set(Protobuf_LIBRARY protobuf::libprotobuf)
+        get_target_property(Protobuf_INCLUDE_DIR protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES)
+    else()
+        include(FindProtobuf OPTIONAL)
+        find_package(Protobuf)
+        _trezor_protobuf_fix_vars()
+    endif()
+
     # Protobuf handling the cache variables set in docker.
@@ -41,12 +49,12 @@
     if(NOT Protobuf_FOUND AND NOT Protobuf_LIBRARY AND NOT Protobuf_PROTOC_EXECUTABLE AND NOT Protobuf_INCLUDE_DIR)
         message(STATUS "Could not find Protobuf")
-    elseif(NOT Protobuf_LIBRARY OR NOT EXISTS "${Protobuf_LIBRARY}")
+    elseif(NOT Protobuf_LIBRARY OR (NOT TARGET protobuf::libprotobuf AND NOT EXISTS "${Protobuf_LIBRARY}"))
         message(STATUS "Protobuf library not found: ${Protobuf_LIBRARY}")
         unset(Protobuf_FOUND)
     elseif(NOT Protobuf_PROTOC_EXECUTABLE OR NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
         message(STATUS "Protobuf executable not found: ${Protobuf_PROTOC_EXECUTABLE}")
         unset(Protobuf_FOUND)
-    elseif(NOT Protobuf_INCLUDE_DIR OR NOT EXISTS "${Protobuf_INCLUDE_DIR}")
+    elseif(NOT TARGET protobuf::libprotobuf AND (NOT Protobuf_INCLUDE_DIR OR NOT EXISTS "${Protobuf_INCLUDE_DIR}"))
         message(STATUS "Protobuf include dir not found: ${Protobuf_INCLUDE_DIR}")
         unset(Protobuf_FOUND)
     else()
@@ -107,7 +115,7 @@
         "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.cpp"
         CMAKE_FLAGS
         "-DINCLUDE_DIRECTORIES=${Protobuf_INCLUDE_DIR};${CMAKE_BINARY_DIR}"
-        "-DCMAKE_CXX_STANDARD=11"
+        "-DCMAKE_CXX_STANDARD=17"
         LINK_LIBRARIES ${Protobuf_LIBRARY}
         OUTPUT_VARIABLE OUTPUT
     )
