Skip to content

Commit 2bef5f2

Browse files
authored
ci: use bzlmod module for curl (#14467)
1 parent c1a5982 commit 2bef5f2

File tree

11 files changed

+62
-40
lines changed

11 files changed

+62
-40
lines changed

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ build:tsan --action_env=TSAN_OPTIONS=halt_on_error=1:second_deadlock_stack=1:rep
102102
build:ubsan --config=sanitizer
103103
build:ubsan --config=libcxx
104104
build:ubsan --copt=-fsanitize=undefined
105+
# By default libcurl calls functions through pointers not matching the original
106+
# type.
107+
build:ubsan --copt=-DCURL_STRICTER
105108
build:ubsan --linkopt=-fsanitize=undefined
106109
build:ubsan --linkopt=-fsanitize-link-c++-runtime
107110
build:ubsan --action_env=UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ bazel_dep(name = "protobuf", version = "27.2", repo_name = "com_google_protobuf"
2828
bazel_dep(name = "boringssl", version = "0.0.0-20230215-5c22014")
2929
bazel_dep(name = "grpc", version = "1.63.1.bcr.1", repo_name = "com_github_grpc_grpc")
3030
bazel_dep(name = "nlohmann_json", version = "3.11.3", repo_name = "com_github_nlohmann_json")
31+
bazel_dep(name = "curl", version = "8.8.0.bcr.1", repo_name = "com_github_curl_curl")
3132
bazel_dep(name = "crc32c", version = "1.1.0", repo_name = "com_github_google_crc32c")
3233
bazel_dep(name = "opentelemetry-cpp", version = "1.16.0", repo_name = "io_opentelemetry_cpp")
3334

bazel/bzlmod0.bzl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
"""Load dependencies needed to use the google-cloud-cpp libraries."""
1616

17-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
18-
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
19-
2017
def gl_cpp_bzlmod0(name = None):
2118
"""Loads dependencies need to compile the google-cloud-cpp libraries.
2219
@@ -27,16 +24,3 @@ def gl_cpp_bzlmod0(name = None):
2724
name: Unused. It is conventional to provide a `name` argument to all
2825
workspace functions.
2926
"""
30-
31-
# TODO(#11485) - use some bazel_dep() from BCR.
32-
# We need libcurl for the Google Cloud Storage client.
33-
maybe(
34-
http_archive,
35-
name = "com_github_curl_curl",
36-
urls = [
37-
"https://curl.haxx.se/download/curl-7.69.1.tar.gz",
38-
],
39-
sha256 = "01ae0c123dee45b01bbaef94c0bc00ed2aec89cb2ee0fd598e0d302a6b5e0a98",
40-
strip_prefix = "curl-7.69.1",
41-
build_file = Label("//bazel:curl.BUILD"),
42-
)

ci/cloudbuild/notifiers/alerts/function/function.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ nlohmann::json MakeChatPayload(BuildStatus const& bs) {
6060
return nlohmann::json{{"text", std::move(text)}};
6161
}
6262

63+
struct CurlPtrCleanup {
64+
void operator()(CURL* arg) const { return curl_easy_cleanup(arg); }
65+
};
66+
67+
struct CurlSListFreeAll {
68+
void operator()(curl_slist* arg) const { return curl_slist_free_all(arg); }
69+
};
70+
6371
void HttpPost(std::string const& url, std::string const& data) {
6472
static constexpr auto kContentType = "Content-Type: application/json";
65-
using Headers = std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)>;
66-
auto const headers =
67-
Headers{curl_slist_append(nullptr, kContentType), curl_slist_free_all};
68-
using CurlHandle = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>;
69-
auto curl = CurlHandle(curl_easy_init(), curl_easy_cleanup);
73+
using Headers = std::unique_ptr<curl_slist, CurlSListFreeAll>;
74+
auto const headers = Headers{curl_slist_append(nullptr, kContentType)};
75+
using CurlHandle = std::unique_ptr<CURL, CurlPtrCleanup>;
76+
auto curl = CurlHandle(curl_easy_init());
7077
if (!curl) throw std::runtime_error("Failed to create CurlHandle");
7178
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
7279
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers.get());

examples/grpc_credential_types.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,26 @@ extern "C" size_t CurlOnWriteData(char* ptr, size_t size, size_t nmemb,
4141
return size * nmemb;
4242
}
4343

44+
struct CurlPtrCleanup {
45+
void operator()(CURL* arg) const { return curl_easy_cleanup(arg); }
46+
};
47+
48+
struct CurlSListFreeAll {
49+
void operator()(curl_slist* arg) const { return curl_slist_free_all(arg); }
50+
};
51+
4452
google::cloud::StatusOr<std::string> HttpGet(std::string const& url,
4553
std::string const& token) {
4654
static auto const kCurlInit = [] {
4755
return curl_global_init(CURL_GLOBAL_ALL);
4856
}();
4957
(void)kCurlInit;
5058
auto const authorization = "Authorization: Bearer " + token;
51-
using Headers = std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)>;
52-
auto const headers = Headers{
53-
curl_slist_append(nullptr, authorization.c_str()), curl_slist_free_all};
54-
using CurlHandle = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>;
55-
auto curl = CurlHandle(curl_easy_init(), curl_easy_cleanup);
59+
using Headers = std::unique_ptr<curl_slist, CurlSListFreeAll>;
60+
auto const headers =
61+
Headers{curl_slist_append(nullptr, authorization.c_str())};
62+
using CurlHandle = std::unique_ptr<CURL, CurlPtrCleanup>;
63+
auto curl = CurlHandle(curl_easy_init());
5664
if (!curl) throw std::runtime_error("Failed to create CurlHandle");
5765
std::string buffer;
5866
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());

google/cloud/internal/curl_handle.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ CurlHandle CurlHandle::MakeFromPool(CurlHandleFactory& factory) {
123123
}
124124

125125
void CurlHandle::ReturnToPool(CurlHandleFactory& factory, CurlHandle h) {
126-
CurlPtr tmp(nullptr, curl_easy_cleanup);
126+
CurlPtr tmp;
127127
h.handle_.swap(tmp);
128128
factory.CleanupHandle(std::move(tmp), HandleDisposition::kKeep);
129129
}
130130

131131
void CurlHandle::DiscardFromPool(CurlHandleFactory& factory, CurlHandle h) {
132-
CurlPtr tmp(nullptr, curl_easy_cleanup);
132+
CurlPtr tmp;
133133
h.handle_.swap(tmp);
134134
factory.CleanupHandle(std::move(tmp), HandleDisposition::kDiscard);
135135
}

google/cloud/internal/curl_handle.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ class CurlHandle {
7272

7373
/// URL-escapes a string.
7474
CurlString MakeEscapedString(std::string const& s) {
75-
return CurlString(
76-
curl_easy_escape(handle_.get(), s.data(), static_cast<int>(s.length())),
77-
&curl_free);
75+
return CurlString(curl_easy_escape(handle_.get(), s.data(),
76+
static_cast<int>(s.length())));
7877
}
7978

8079
template <typename T>

google/cloud/internal/curl_handle_factory.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void DefaultCurlHandleFactory::CleanupHandle(CurlPtr h, HandleDisposition) {
6565
}
6666

6767
CurlMulti DefaultCurlHandleFactory::CreateMultiHandle() {
68-
return CurlMulti(curl_multi_init(), &curl_multi_cleanup);
68+
return CurlMulti(curl_multi_init());
6969
}
7070

7171
void DefaultCurlHandleFactory::CleanupMultiHandle(CurlMulti m,
@@ -161,7 +161,7 @@ CurlMulti PooledCurlHandleFactory::CreateMultiHandle() {
161161
}
162162
++active_multi_handles_;
163163
lk.unlock();
164-
return CurlMulti(curl_multi_init(), &curl_multi_cleanup);
164+
return CurlMulti(curl_multi_init());
165165
}
166166

167167
void PooledCurlHandleFactory::CleanupMultiHandle(CurlMulti m,

google/cloud/internal/curl_impl.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ CurlImpl::CurlImpl(CurlHandle handle,
169169
std::shared_ptr<CurlHandleFactory> factory,
170170
Options const& options)
171171
: factory_(std::move(factory)),
172-
request_headers_(nullptr, &curl_slist_free_all),
173172
handle_(std::move(handle)),
174173
multi_(factory_->CreateMultiHandle()) {
175174
CurlInitializeOnce(options);

google/cloud/internal/curl_wrappers.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ bool SslLockingCallbacksInstalled() {
231231
}
232232

233233
CurlPtr MakeCurlPtr() {
234-
auto handle = CurlPtr(curl_easy_init(), &curl_easy_cleanup);
234+
auto handle = CurlPtr(curl_easy_init());
235235
// We get better performance using a slightly larger buffer (128KiB) than the
236236
// default buffer size set by libcurl (16KiB). We ignore errors because
237237
// failing to set this parameter just affects performance by a small amount.

0 commit comments

Comments
 (0)