aboutsummaryrefslogtreecommitdiff
path: root/ports/google-cloud-cpp/cmake-libcurl-find-config.patch
blob: 0c1d6dd6fcfa810a7224e80e449c0848a296f69e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
diff --git a/cmake/IncludeCurl.cmake b/cmake/IncludeCurl.cmake
index 6ea7ca3e6..3c2db6b28 100644
--- a/cmake/IncludeCurl.cmake
+++ b/cmake/IncludeCurl.cmake
@@ -34,49 +34,57 @@ set_property(CACHE GOOGLE_CLOUD_CPP_CURL_PROVIDER
 if ("${GOOGLE_CLOUD_CPP_CURL_PROVIDER}" STREQUAL "external")
     include(external/curl)
 elseif("${GOOGLE_CLOUD_CPP_CURL_PROVIDER}" STREQUAL "package")
-    # Search for libcurl, in CMake 3.5 this does not define a target, but it
-    # will in 3.12 (see https://cmake.org/cmake/help/git-
-    # stage/module/FindCURL.html for details).  Until then, define the target
-    # ourselves if it is missing.
-    find_package(CURL REQUIRED)
-    if (NOT TARGET CURL::libcurl)
-        add_library(CURL::libcurl UNKNOWN IMPORTED)
-        set_property(TARGET CURL::libcurl
-                     APPEND
-                     PROPERTY INTERFACE_INCLUDE_DIRECTORIES
-                              "${CURL_INCLUDE_DIR}")
-        set_property(TARGET CURL::libcurl
-                     APPEND
-                     PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
-    endif ()
-    # If the library is static, we need to explicitly link its dependencies.
-    # However, we should not do so for shared libraries, because the version of
-    # OpenSSL (for example) found by find_package() may be newer than the
-    # version linked against libcurl.
-    if ("${CURL_LIBRARY}" MATCHES "${CMAKE_STATIC_LIBRARY_SUFFIX}$")
-        find_package(OpenSSL REQUIRED)
-        find_package(ZLIB REQUIRED)
-        set_property(TARGET CURL::libcurl
-                     APPEND
-                     PROPERTY INTERFACE_LINK_LIBRARIES
-                              OpenSSL::SSL
-                              OpenSSL::Crypto
-                              ZLIB::ZLIB)
-        message(STATUS "CURL linkage will be static")
-        if (WIN32)
+    # Search for libcurl, first using CONFIG mode, and retrying
+    # using MODULE mode if that fails
+    find_package(CURL CONFIG QUIET) # Deliberately quiet, so we can handle the result
+    if(CURL_FOUND)
+        message(STATUS "CURL library found via CONFIG mode")
+    else()
+        # CONFIG mode failed - fallback to MODULE mode
+        # In CMake 3.5 this does not define a target, but it
+        # will in 3.12 (see https://cmake.org/cmake/help/git-
+        # stage/module/FindCURL.html for details).  Until then, define the target
+        # ourselves if it is missing.
+        find_package(CURL MODULE REQUIRED) # Use REQUIRED the second time to fail out
+        if (NOT TARGET CURL::libcurl)
+            add_library(CURL::libcurl UNKNOWN IMPORTED)
             set_property(TARGET CURL::libcurl
                          APPEND
-                         PROPERTY INTERFACE_LINK_LIBRARIES
-                                  crypt32
-                                  wsock32
-                                  ws2_32)
+                         PROPERTY INTERFACE_INCLUDE_DIRECTORIES
+                                  "${CURL_INCLUDE_DIR}")
+            set_property(TARGET CURL::libcurl
+                         APPEND
+                         PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
         endif ()
-        if (APPLE)
+        # If the library is static, we need to explicitly link its dependencies.
+        # However, we should not do so for shared libraries, because the version of
+        # OpenSSL (for example) found by find_package() may be newer than the
+        # version linked against libcurl.
+        if ("${CURL_LIBRARY}" MATCHES "${CMAKE_STATIC_LIBRARY_SUFFIX}$")
+            find_package(OpenSSL REQUIRED)
+            find_package(ZLIB REQUIRED)
             set_property(TARGET CURL::libcurl
                          APPEND
-                         PROPERTY INTERFACE_LINK_LIBRARIES ldap)
+                         PROPERTY INTERFACE_LINK_LIBRARIES
+                                  OpenSSL::SSL
+                                  OpenSSL::Crypto
+                                  ZLIB::ZLIB)
+            message(STATUS "CURL linkage will be static")
+            if (WIN32)
+                set_property(TARGET CURL::libcurl
+                             APPEND
+                             PROPERTY INTERFACE_LINK_LIBRARIES
+                                      crypt32
+                                      wsock32
+                                      ws2_32)
+            endif ()
+            if (APPLE)
+                set_property(TARGET CURL::libcurl
+                             APPEND
+                             PROPERTY INTERFACE_LINK_LIBRARIES ldap)
+            endif ()
+        else()
+            message(STATUS "CURL linkage will be non-static")
         endif ()
-    else()
-        message(STATUS "CURL linkage will be non-static")
     endif ()
 endif ()
diff --git a/google/cloud/storage/config.cmake.in b/google/cloud/storage/config.cmake.in
index a4d261815..640089e09 100644
--- a/google/cloud/storage/config.cmake.in
+++ b/google/cloud/storage/config.cmake.in
@@ -13,21 +13,25 @@
 # limitations under the License.
 
 include(CMakeFindDependencyMacro)
-find_dependency(CURL)
+# Search for libcurl, first using CONFIG mode, and retrying
+# using MODULE mode if that fails
+find_package(CURL CONFIG QUIET) # find_package so we can explicitly specify QUIET
+if(NOT CURL_FOUND)
+    find_dependency(CURL MODULE)
+    # Some versions of FindCURL do not define CURL::libcurl, so we define it ourselves.
+    if (NOT TARGET CURL::libcurl)
+        add_library(CURL::libcurl UNKNOWN IMPORTED)
+        set_property(TARGET CURL::libcurl
+                     APPEND
+                     PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIR}")
+        set_property(TARGET CURL::libcurl
+                     APPEND
+                     PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
+    endif ()
+endif()
 find_dependency(Crc32c)
 find_dependency(google_cloud_cpp_common)
 find_dependency(OpenSSL)
 find_dependency(ZLIB)
 
-# Some versions of FindCURL do not define CURL::libcurl, so we define it ourselves.
-if (NOT TARGET CURL::libcurl)
-    add_library(CURL::libcurl UNKNOWN IMPORTED)
-    set_property(TARGET CURL::libcurl
-                 APPEND
-                 PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIR}")
-    set_property(TARGET CURL::libcurl
-                 APPEND
-                 PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
-endif ()
-
 include("${CMAKE_CURRENT_LIST_DIR}/storage-targets.cmake")