Skip to content

Commit 2f9170e

Browse files
committed
Fix build errors/warnings on Windows
1 parent b522592 commit 2f9170e

File tree

8 files changed

+147
-12
lines changed

8 files changed

+147
-12
lines changed

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ set(CPPKAFKA_VERSION_MAJOR 0)
66
set(CPPKAFKA_VERSION_MINOR 1)
77
set(CPPKAFKA_VERSION "${CPPKAFKA_VERSION_MAJOR}.${CPPKAFKA_VERSION_MINOR}")
88

9-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
9+
if(MSVC)
10+
# Don't always use Wall, since VC's /Wall is ridiculously verbose.
11+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
12+
13+
# Disable VC secure checks, since these are not really issues
14+
add_definitions("-D_CRT_SECURE_NO_WARNINGS=1")
15+
add_definitions("-D_SCL_SECURE_NO_WARNINGS=1")
16+
add_definitions("-DNOGDI=1")
17+
else()
18+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
19+
endif()
1020
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
1121

1222
# Set output directories

include/cppkafka/consumer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class CPPKAFKA_API Consumer : public KafkaHandleBase {
111111
*/
112112
Consumer(Configuration config);
113113
Consumer(const Consumer&) = delete;
114-
Consumer(Consumer&) = delete;
114+
Consumer(Consumer&&) = delete;
115115
Consumer& operator=(const Consumer&) = delete;
116116
Consumer& operator=(Consumer&&) = delete;
117117

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
// "License": Public Domain
3+
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
4+
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
5+
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
6+
// an example on how to get the endian conversion functions on different platforms.
7+
8+
#ifndef PORTABLE_ENDIAN_H__
9+
#define PORTABLE_ENDIAN_H__
10+
11+
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
12+
13+
# define __WINDOWS__
14+
15+
#endif
16+
17+
#if defined(__linux__) || defined(__CYGWIN__)
18+
19+
# include <endian.h>
20+
21+
#elif defined(__APPLE__)
22+
23+
# include <libkern/OSByteOrder.h>
24+
25+
# define htobe16(x) OSSwapHostToBigInt16(x)
26+
# define htole16(x) OSSwapHostToLittleInt16(x)
27+
# define be16toh(x) OSSwapBigToHostInt16(x)
28+
# define le16toh(x) OSSwapLittleToHostInt16(x)
29+
30+
# define htobe32(x) OSSwapHostToBigInt32(x)
31+
# define htole32(x) OSSwapHostToLittleInt32(x)
32+
# define be32toh(x) OSSwapBigToHostInt32(x)
33+
# define le32toh(x) OSSwapLittleToHostInt32(x)
34+
35+
# define htobe64(x) OSSwapHostToBigInt64(x)
36+
# define htole64(x) OSSwapHostToLittleInt64(x)
37+
# define be64toh(x) OSSwapBigToHostInt64(x)
38+
# define le64toh(x) OSSwapLittleToHostInt64(x)
39+
40+
# define __BYTE_ORDER BYTE_ORDER
41+
# define __BIG_ENDIAN BIG_ENDIAN
42+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
43+
# define __PDP_ENDIAN PDP_ENDIAN
44+
45+
#elif defined(__OpenBSD__)
46+
47+
# include <sys/endian.h>
48+
49+
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
50+
51+
# include <sys/endian.h>
52+
53+
# define be16toh(x) betoh16(x)
54+
# define le16toh(x) letoh16(x)
55+
56+
# define be32toh(x) betoh32(x)
57+
# define le32toh(x) letoh32(x)
58+
59+
# define be64toh(x) betoh64(x)
60+
# define le64toh(x) letoh64(x)
61+
62+
#elif defined(__WINDOWS__)
63+
64+
# include <winsock2.h>
65+
# include <sys/param.h>
66+
67+
# if BYTE_ORDER == LITTLE_ENDIAN
68+
69+
# define htobe16(x) htons(x)
70+
# define htole16(x) (x)
71+
# define be16toh(x) ntohs(x)
72+
# define le16toh(x) (x)
73+
74+
# define htobe32(x) htonl(x)
75+
# define htole32(x) (x)
76+
# define be32toh(x) ntohl(x)
77+
# define le32toh(x) (x)
78+
79+
# define htobe64(x) htonll(x)
80+
# define htole64(x) (x)
81+
# define be64toh(x) ntohll(x)
82+
# define le64toh(x) (x)
83+
84+
# elif BYTE_ORDER == BIG_ENDIAN
85+
86+
/* that would be xbox 360 */
87+
# define htobe16(x) (x)
88+
# define htole16(x) __builtin_bswap16(x)
89+
# define be16toh(x) (x)
90+
# define le16toh(x) __builtin_bswap16(x)
91+
92+
# define htobe32(x) (x)
93+
# define htole32(x) __builtin_bswap32(x)
94+
# define be32toh(x) (x)
95+
# define le32toh(x) __builtin_bswap32(x)
96+
97+
# define htobe64(x) (x)
98+
# define htole64(x) __builtin_bswap64(x)
99+
# define be64toh(x) (x)
100+
# define le64toh(x) __builtin_bswap64(x)
101+
102+
# else
103+
104+
# error byte order not supported
105+
106+
# endif
107+
108+
# define __BYTE_ORDER BYTE_ORDER
109+
# define __BIG_ENDIAN BIG_ENDIAN
110+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
111+
# define __PDP_ENDIAN PDP_ENDIAN
112+
113+
#else
114+
115+
# error platform not supported
116+
117+
#endif
118+
119+
#endif

src/consumer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ TopicPartitionList
136136
Consumer::get_offsets_committed(const TopicPartitionList& topic_partitions) const {
137137
TopicPartitionsListPtr topic_list_handle = convert(topic_partitions);
138138
rd_kafka_resp_err_t error = rd_kafka_committed(get_handle(), topic_list_handle.get(),
139-
get_timeout().count());
139+
static_cast<int>(get_timeout().count()));
140140
check_error(error);
141141
return convert(topic_list_handle);
142142
}
@@ -192,7 +192,8 @@ Message Consumer::poll() {
192192
}
193193

194194
Message Consumer::poll(milliseconds timeout) {
195-
rd_kafka_message_t* message = rd_kafka_consumer_poll(get_handle(), timeout.count());
195+
rd_kafka_message_t* message = rd_kafka_consumer_poll(get_handle(),
196+
static_cast<int>(timeout.count()));
196197
return message ? Message(message) : Message();
197198
}
198199

src/group_information.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <algorithm>
44
#include "topic_partition.h"
55
#include "exceptions.h"
6+
#include "utils/portable_endian.h"
67

78
using std::string;
89
using std::vector;

src/kafka_handle_base.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ KafkaHandleBase::query_offsets(const TopicPartition& topic_partition) const {
100100
int64_t high;
101101
const string& topic = topic_partition.get_topic();
102102
const int partition = topic_partition.get_partition();
103+
const int timeout = static_cast<int>(timeout_ms_.count());
103104
rd_kafka_resp_err_t result = rd_kafka_query_watermark_offsets(handle_.get(), topic.data(),
104105
partition, &low, &high,
105-
timeout_ms_.count());
106+
timeout);
106107
check_error(result);
107108
return make_tuple(low, high);
108109
}
@@ -141,8 +142,9 @@ KafkaHandleBase::get_offsets_for_times(const TopicPartitionsTimestampsMap& queri
141142
query.second.count());
142143
}
143144
TopicPartitionsListPtr topic_list_handle = convert(topic_partitions);
145+
const int timeout = static_cast<int>(timeout_ms_.count());
144146
rd_kafka_resp_err_t result = rd_kafka_offsets_for_times(handle_.get(), topic_list_handle.get(),
145-
timeout_ms_.count());
147+
timeout);
146148
check_error(result);
147149
return convert(topic_list_handle);
148150
}
@@ -177,15 +179,17 @@ Topic KafkaHandleBase::get_topic(const string& name, rd_kafka_topic_conf_t* conf
177179

178180
Metadata KafkaHandleBase::get_metadata(bool all_topics, rd_kafka_topic_t* topic_ptr) const {
179181
const rd_kafka_metadata_t* metadata;
180-
rd_kafka_resp_err_t error = rd_kafka_metadata(get_handle(), !!all_topics,
181-
topic_ptr, &metadata, timeout_ms_.count());
182+
const int timeout = static_cast<int>(timeout_ms_.count());
183+
rd_kafka_resp_err_t error = rd_kafka_metadata(get_handle(), !!all_topics,
184+
topic_ptr, &metadata, timeout);
182185
check_error(error);
183186
return Metadata(metadata);
184187
}
185188

186189
vector<GroupInformation> KafkaHandleBase::fetch_consumer_groups(const char* name) {
187190
const rd_kafka_group_list* list = nullptr;
188-
auto result = rd_kafka_list_groups(get_handle(), name, &list, timeout_ms_.count());
191+
const int timeout = static_cast<int>(timeout_ms_.count());
192+
auto result = rd_kafka_list_groups(get_handle(), name, &list, timeout);
189193
check_error(result);
190194

191195
// Wrap this in a unique_ptr so it gets auto deleted

src/producer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ int Producer::poll() {
8282
}
8383

8484
int Producer::poll(milliseconds timeout) {
85-
return rd_kafka_poll(get_handle(), timeout.count());
85+
return rd_kafka_poll(get_handle(), static_cast<int>(timeout.count()));
8686
}
8787

8888
void Producer::flush() {
8989
flush(get_timeout());
9090
}
9191

9292
void Producer::flush(milliseconds timeout) {
93-
auto result = rd_kafka_flush(get_handle(), timeout.count());
93+
auto result = rd_kafka_flush(get_handle(), static_cast<int>(timeout.count()));
9494
check_error(result);
9595
}
9696

src/topic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ string Topic::get_name() const {
6262
}
6363

6464
bool Topic::is_partition_available(int partition) const {
65-
return rd_kafka_topic_partition_available(handle_.get(), partition);
65+
return rd_kafka_topic_partition_available(handle_.get(), partition) == 1;
6666
}
6767

6868
rd_kafka_topic_t* Topic::get_handle() const {

0 commit comments

Comments
 (0)