summaryrefslogtreecommitdiffstats
path: root/Modules/FindProtobuf.cmake
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2018-10-31 14:10:40 (GMT)
committerBrad King <brad.king@kitware.com>2018-10-31 14:24:01 (GMT)
commit03454b0d0d5083530f87e3b2f4ed4fe93b182112 (patch)
tree99b7096dee3447d8f35a7f21a0d65b5d73fb2aeb /Modules/FindProtobuf.cmake
parent44cc305ac12d58fe476f815b89f42288455e44a9 (diff)
downloadCMake-03454b0d0d5083530f87e3b2f4ed4fe93b182112.zip
CMake-03454b0d0d5083530f87e3b2f4ed4fe93b182112.tar.gz
CMake-03454b0d0d5083530f87e3b2f4ed4fe93b182112.tar.bz2
FindProtobuf: Add missing link dependencies on threads
Protobuf headers have dependencies on threads. On UNIX platforms this requires linking to a threads library. We've long done this in the `Protobuf_LIBRARIES` result variable. However, the imported targets added by commit v3.9.0-rc1~81^2~2 (FindProtobuf: add targets, 2017-05-17) and commit v3.9.0-rc1~68^2 (FindProtobuf: Rename imported targets to match upstream names, 2017-05-22) were missing a dependency on threads. Add the dependency to the imported targets, and to the variables `Protobuf_LITE_LIBRARIES` and `Protobuf_PROTOC_LIBRARIES`. While this did not seem to matter in practice for a long time, protobuf 3.6 throws exceptions in some cases when threads are missing. Fixes: #18533
Diffstat (limited to 'Modules/FindProtobuf.cmake')
-rw-r--r--Modules/FindProtobuf.cmake36
1 files changed, 22 insertions, 14 deletions
diff --git a/Modules/FindProtobuf.cmake b/Modules/FindProtobuf.cmake
index d6d1ec6..eda0361 100644
--- a/Modules/FindProtobuf.cmake
+++ b/Modules/FindProtobuf.cmake
@@ -381,21 +381,16 @@ function(_protobuf_find_libraries name filename)
mark_as_advanced(${name}_LIBRARY_DEBUG)
select_library_configurations(${name})
+
+ if(UNIX AND Threads_FOUND)
+ list(APPEND ${name}_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
+ endif()
+
set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
set(${name}_LIBRARIES "${${name}_LIBRARIES}" PARENT_SCOPE)
endif()
endfunction()
-# Internal function: find threads library
-function(_protobuf_find_threads)
- set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
- find_package(Threads)
- if(Threads_FOUND)
- list(APPEND Protobuf_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
- set(Protobuf_LIBRARIES "${Protobuf_LIBRARIES}" PARENT_SCOPE)
- endif()
-endfunction()
-
#
# Main.
#
@@ -416,6 +411,11 @@ if(MSVC)
find_path(Protobuf_SRC_ROOT_FOLDER protobuf.pc.in)
endif()
+if(UNIX)
+ # Protobuf headers may depend on threading.
+ find_package(Threads QUIET)
+endif()
+
# The Protobuf library
_protobuf_find_libraries(Protobuf protobuf)
#DOC "The Google Protocol Buffers RELEASE Library"
@@ -430,10 +430,6 @@ if(MSVC)
set(CMAKE_FIND_LIBRARY_PREFIXES "${Protobuf_ORIG_FIND_LIBRARY_PREFIXES}")
endif()
-if(UNIX)
- _protobuf_find_threads()
-endif()
-
# Find the include directory
find_path(Protobuf_INCLUDE_DIR
google/protobuf/service.h
@@ -521,6 +517,10 @@ if(Protobuf_INCLUDE_DIR)
set_target_properties(protobuf::libprotobuf PROPERTIES
IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}")
endif()
+ if(UNIX AND TARGET Threads::Threads)
+ set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
+ INTERFACE_LINK_LIBRARIES Threads::Threads)
+ endif()
endif()
endif()
@@ -545,6 +545,10 @@ if(Protobuf_INCLUDE_DIR)
set_target_properties(protobuf::libprotobuf-lite PROPERTIES
IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}")
endif()
+ if(UNIX AND TARGET Threads::Threads)
+ set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
+ INTERFACE_LINK_LIBRARIES Threads::Threads)
+ endif()
endif()
endif()
@@ -569,6 +573,10 @@ if(Protobuf_INCLUDE_DIR)
set_target_properties(protobuf::libprotoc PROPERTIES
IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}")
endif()
+ if(UNIX AND TARGET Threads::Threads)
+ set_property(TARGET protobuf::libprotoc APPEND PROPERTY
+ INTERFACE_LINK_LIBRARIES Threads::Threads)
+ endif()
endif()
endif()