Skip to content

Commit 26ea7b2

Browse files
committed
msvc build adjustment
1 parent 972d4d2 commit 26ea7b2

File tree

12 files changed

+193
-127
lines changed

12 files changed

+193
-127
lines changed

CMakeLists.txt

+88-72
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,70 @@ if (NOT MSVC)
140140
set(OPENDHT_PROXY_OPENSSL OFF)
141141
endif ()
142142
else ()
143-
set (WIN32_DEP_DIR ${PROJECT_SOURCE_DIR}/../)
144-
include_directories(${WIN32_DEP_DIR}/../msvc/include) # SMP gnutls
145-
include_directories(${WIN32_DEP_DIR}/argon2/include)
146-
include_directories(${WIN32_DEP_DIR}/jsoncpp/include)
143+
include_directories(src/compat/msvc)
144+
145+
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)
146+
147+
pkg_search_module(GnuTLS REQUIRED IMPORTED_TARGET GnuTLS)
148+
include_directories(${GnuTLS_INCLUDEDIR})
149+
link_directories(${GnuTLS_LIBDIR})
150+
151+
pkg_search_module(nettle REQUIRED IMPORTED_TARGET nettle)
152+
include_directories(${nettle_INCLUDEDIR})
153+
link_directories(${nettle_LIBDIR})
154+
155+
find_package(msgpack-cxx CONFIG REQUIRED)
156+
get_target_property(MSGPACK_INCLUDEDIR msgpack-cxx INTERFACE_INCLUDE_DIRECTORIES)
157+
include_directories(${MSGPACK_INCLUDEDIR})
158+
159+
find_package(unofficial-argon2 CONFIG REQUIRED)
160+
get_target_property(ARGON2_INCLUDEDIR unofficial::argon2::libargon2 INTERFACE_INCLUDE_DIRECTORIES)
161+
include_directories(${ARGON2_INCLUDEDIR})
162+
163+
set(argon2_lib ", libargon2")
164+
165+
find_package(jsoncpp CONFIG REQUIRED)
166+
get_target_property(JSONCPP_LIB JsonCpp::JsonCpp INTERFACE_LINK_LIBRARIES)
167+
get_target_property(JSONCPP_INCLUDEDIR ${JSONCPP_LIB} INTERFACE_INCLUDE_DIRECTORIES)
168+
include_directories(${JSONCPP_INCLUDEDIR})
169+
170+
set(jsoncpp_lib ", jsoncpp")
171+
172+
find_package(fmt CONFIG REQUIRED)
173+
get_target_property(FMT_INCLUDEDIR fmt::fmt-header-only INTERFACE_INCLUDE_DIRECTORIES)
174+
include_directories(${FMT_INCLUDEDIR})
175+
147176
list (APPEND opendht_SOURCES
148177
src/base64.h
149178
src/base64.cpp
150179
)
151180
add_definitions(-DOPENDHT_JSONCPP)
152-
include_directories(${WIN32_DEP_DIR}/msgpack-c/include)
153-
if (OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
154-
include_directories(
155-
${WIN32_DEP_DIR}/asio/asio/include
156-
${WIN32_DEP_DIR}/openssl/include
157-
${WIN32_DEP_DIR}/restinio/dev
158-
${WIN32_DEP_DIR}/fmt/include
159-
${WIN32_DEP_DIR}/http_parser
160-
)
161-
endif ()
181+
182+
if(OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
183+
find_package(asio CONFIG REQUIRED)
184+
get_target_property(ASIO_INCLUDEDIR asio INTERFACE_INCLUDE_DIRECTORIES)
185+
include_directories(${ASIO_INCLUDEDIR})
186+
endif()
187+
188+
if (OPENDHT_HTTP)
189+
find_package(restinio CONFIG REQUIRED)
190+
get_target_property(RESTINIO_INCLUDEDIR restinio::restinio INTERFACE_INCLUDE_DIRECTORIES)
191+
include_directories(${RESTINIO_INCLUDEDIR})
192+
193+
find_package(llhttp CONFIG REQUIRED)
194+
get_target_property(LLHTTP_INCLUDEDIR llhttp::llhttp_static INTERFACE_INCLUDE_DIRECTORIES)
195+
include_directories(${LLHTTP_INCLUDEDIR})
196+
set(http_lib "-lllhttp")
197+
198+
if (OPENDHT_PROXY_OPENSSL)
199+
find_package(OpenSSL REQUIRED)
200+
get_target_property(OPENSSL_INCLUDEDIR OpenSSL::SSL INTERFACE_INCLUDE_DIRECTORIES)
201+
include_directories(${OPENSSL_INCLUDEDIR})
202+
set(openssl_lib ", openssl")
203+
endif()
204+
else()
205+
set(OPENDHT_PROXY_OPENSSL OFF)
206+
endif()
162207
endif ()
163208

164209
if (OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
@@ -182,7 +227,7 @@ else ()
182227
-DGNUTLS_INTERNAL_BUILD)
183228
set(DISABLE_MSC_WARNINGS "/wd4101 /wd4244 /wd4267 /wd4273 /wd4804 /wd4834 /wd4996")
184229
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DISABLE_MSC_WARNINGS}")
185-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
230+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /bigobj")
186231
endif ()
187232
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMSGPACK_NO_BOOST -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT")
188233

@@ -204,7 +249,7 @@ include_directories (
204249
# Install dirs
205250
include (GNUInstallDirs)
206251
set (prefix ${CMAKE_INSTALL_PREFIX})
207-
set (exec_prefix "\${prefix}")
252+
set (exec_prefix "${prefix}")
208253
set (libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
209254
set (includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
210255
set (bindir "${CMAKE_INSTALL_FULL_BINDIR}")
@@ -326,69 +371,36 @@ if (MSVC)
326371
endif ()
327372

328373
# Targets
329-
if (MSVC)
330-
if (OPENDHT_STATIC)
331-
if (OPENDHT_TOOLS)
332-
function (add_obj_lib name libfile)
333-
add_library(${name} OBJECT IMPORTED)
334-
set_property(TARGET ${name} PROPERTY IMPORTED_OBJECTS ${libfile})
335-
endfunction ()
336-
add_obj_lib (win32_json ${WIN32_DEP_DIR}/../msvc/lib/x64/lib_json.lib)
337-
add_obj_lib (win32_gnutls ${WIN32_DEP_DIR}/../msvc/lib/x64/libgnutls.lib)
338-
add_obj_lib (win32_argon2 ${WIN32_DEP_DIR}/argon2/vs2015/Argon2Ref/vs2015/build/Argon2Ref.lib)
339-
list (APPEND obj_libs
340-
$<TARGET_OBJECTS:win32_json>
341-
$<TARGET_OBJECTS:win32_gnutls>
342-
$<TARGET_OBJECTS:win32_argon2>
343-
)
344-
if (OPENDHT_HTTP)
345-
add_obj_lib (win32_fmt ${WIN32_DEP_DIR}/fmt/msvc/Release/fmt.lib)
346-
add_obj_lib (win32_http_parser ${WIN32_DEP_DIR}/http_parser/x64/Release/http-parser.lib)
347-
add_obj_lib (win32_ssl ${WIN32_DEP_DIR}/openssl/libssl_static.lib)
348-
add_obj_lib (win32_crypto ${WIN32_DEP_DIR}/openssl/libcrypto_static.lib)
349-
list (APPEND obj_libs
350-
$<TARGET_OBJECTS:win32_fmt>
351-
$<TARGET_OBJECTS:win32_http_parser>
352-
$<TARGET_OBJECTS:win32_ssl>
353-
$<TARGET_OBJECTS:win32_crypto>
354-
)
355-
endif ()
356-
else ()
357-
list (APPEND win32_Libs
358-
${PROJECT_SOURCE_DIR}/../../msvc/lib/x64/libgnutls.lib
359-
${PROJECT_SOURCE_DIR}/../../msvc/lib/x64/lib_json.lib
360-
${PROJECT_SOURCE_DIR}/../argon2/vs2015/Argon2Ref/vs2015/build/Argon2Ref.lib
361-
)
362-
list (APPEND win32_Libs
363-
${PROJECT_SOURCE_DIR}/../fmt/msvc/Release/fmt.lib
364-
${PROJECT_SOURCE_DIR}/../http_parser/x64/Release/http-parser.lib
365-
${PROJECT_SOURCE_DIR}/../openssl/libssl.lib
366-
${PROJECT_SOURCE_DIR}/../openssl/libcrypto.lib
367-
)
368-
endif ()
369-
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4006")
370-
endif()
371-
endif ()
372374

373-
add_library (opendht
374-
${opendht_SOURCES}
375-
${opendht_HEADERS}
376-
${obj_libs}
375+
add_library(opendht ${opendht_SOURCES} ${opendht_HEADERS})
376+
377+
target_include_directories(opendht
378+
PUBLIC
379+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
380+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
377381
)
378-
set_target_properties (opendht PROPERTIES OUTPUT_NAME "opendht")
379382

380-
if (NOT HAVE_MSGPACKCXX)
381-
target_link_libraries(opendht PUBLIC ${MSGPACK_TARGET})
382-
endif()
383-
if (APPLE)
384-
target_link_libraries(opendht PRIVATE "-framework CoreFoundation" "-framework Security")
385-
endif()
383+
set_target_properties(opendht PROPERTIES OUTPUT_NAME "opendht")
384+
386385
if (MSVC)
386+
target_link_libraries(opendht PUBLIC PkgConfig::GnuTLS JsonCpp::JsonCpp fmt::fmt-header-only PRIVATE PkgConfig::nettle unofficial::argon2::libargon2)
387+
if (OPENDHT_HTTP)
388+
target_link_libraries(opendht PUBLIC llhttp::llhttp_static)
389+
if (OPENDHT_PROXY_OPENSSL)
390+
target_link_libraries(opendht PUBLIC OpenSSL::SSL OpenSSL::Crypto)
391+
endif()
392+
endif()
387393
if (OPENDHT_STATIC)
388394
target_link_libraries(opendht PUBLIC ${Win32_STATIC_LIBRARIES} ${Win32_IMPORT_LIBRARIES})
389-
set_target_properties (opendht PROPERTIES OUTPUT_NAME "libopendht")
395+
set_target_properties(opendht PROPERTIES OUTPUT_NAME "libopendht")
390396
endif()
391397
else()
398+
if (APPLE)
399+
target_link_libraries(opendht PRIVATE "-framework CoreFoundation" "-framework Security")
400+
endif ()
401+
if (NOT HAVE_MSGPACKCXX)
402+
target_link_libraries(opendht PUBLIC ${MSGPACK_TARGET})
403+
endif()
392404
target_link_libraries(opendht
393405
PRIVATE
394406
PkgConfig::argon2
@@ -415,9 +427,9 @@ else()
415427
endif()
416428

417429
if (BUILD_SHARED_LIBS)
418-
set_target_properties (opendht PROPERTIES IMPORT_SUFFIX "_import.lib")
419430
set_target_properties (opendht PROPERTIES SOVERSION ${opendht_VERSION_MAJOR} VERSION ${opendht_VERSION})
420431
target_compile_definitions(opendht PRIVATE OPENDHT_BUILD)
432+
target_compile_definitions(opendht PRIVATE opendht_EXPORTS)
421433
endif ()
422434
install (TARGETS opendht DESTINATION ${CMAKE_INSTALL_LIBDIR} EXPORT opendht)
423435

@@ -455,6 +467,7 @@ write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/opendhtConfigVersi
455467
VERSION ${opendht_VERSION}
456468
COMPATIBILITY AnyNewerVersion
457469
)
470+
458471
# PkgConfig module
459472
configure_file (
460473
opendht.pc.in
@@ -467,6 +480,9 @@ install (DIRECTORY include DESTINATION ${CMAKE_INSTALL_PREFIX})
467480
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/opendht.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
468481
install (EXPORT opendht DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opendht FILE opendhtConfig.cmake)
469482
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/opendhtConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opendht)
483+
if(MSVC AND BUILD_SHARED_LIBS)
484+
install(FILES $<TARGET_PDB_FILE:opendht> CONFIGURATIONS "RelWithDebInfo" "Debug" DESTINATION ${CMAKE_INSTALL_LIBDIR})
485+
endif()
470486

471487
# Unit tests
472488
if (BUILD_TESTING AND NOT MSVC)

include/opendht/default_types.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace dht {
3636
class OPENDHT_PUBLIC DhtMessage : public Value::Serializable<DhtMessage>
3737
{
3838
public:
39-
static const ValueType TYPE;
39+
static const ValueType& Type();
4040

4141
DhtMessage(const std::string& s = {}, const Blob& msg = {}) : service(s), data(msg) {}
4242

@@ -112,7 +112,7 @@ class OPENDHT_PUBLIC ImMessage : public SignedValue<ImMessage>
112112
using BaseClass = SignedValue<ImMessage>;
113113

114114
public:
115-
static const ValueType TYPE;
115+
static const ValueType& Type();
116116

117117
ImMessage() {}
118118
ImMessage(dht::Value::Id id, std::string&& m, long d = 0)
@@ -144,7 +144,7 @@ class OPENDHT_PUBLIC TrustRequest : public EncryptedValue<TrustRequest>
144144
using BaseClass = EncryptedValue<TrustRequest>;
145145

146146
public:
147-
static const ValueType TYPE;
147+
static const ValueType& Type();
148148

149149
TrustRequest() {}
150150
TrustRequest(std::string s, std::string ci = {}) : service(s), conversationId(ci) {}
@@ -167,7 +167,7 @@ class OPENDHT_PUBLIC IceCandidates : public EncryptedValue<IceCandidates>
167167
using BaseClass = EncryptedValue<IceCandidates>;
168168

169169
public:
170-
static const ValueType TYPE;
170+
static const ValueType& Type();
171171

172172
IceCandidates() {}
173173
IceCandidates(Value::Id msg_id, Blob ice) : id(msg_id), ice_data(ice) {}
@@ -213,7 +213,7 @@ class OPENDHT_PUBLIC IpServiceAnnouncement : public Value::Serializable<IpServic
213213
using BaseClass = Value::Serializable<IpServiceAnnouncement>;
214214

215215
public:
216-
static const ValueType TYPE;
216+
static const ValueType& Type();
217217

218218
IpServiceAnnouncement(sa_family_t family = AF_UNSPEC, in_port_t p = 0) {
219219
addr.setFamily(family);
@@ -253,7 +253,7 @@ class OPENDHT_PUBLIC IpServiceAnnouncement : public Value::Serializable<IpServic
253253
}
254254

255255
virtual const ValueType& getType() const {
256-
return TYPE;
256+
return Type();
257257
}
258258

259259
static bool storePolicy(InfoHash, std::shared_ptr<Value>&, const InfoHash&, const SockAddr&);

include/opendht/indexation/pht.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ ee *
266266

267267
using Value = std::pair<InfoHash, dht::Value::Id>;
268268
struct OPENDHT_PUBLIC IndexEntry : public dht::Value::Serializable<IndexEntry> {
269-
static const ValueType TYPE;
269+
static const ValueType& Type();
270270

271271
virtual void unpackValue(const dht::Value& v) {
272272
Serializable<IndexEntry>::unpackValue(v);

include/opendht/infohash.h

+10-13
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,14 @@ using h256 = Hash<32>;
285285
using PkId = h256;
286286

287287
template <size_t N>
288-
std::ostream& operator<< (std::ostream& s, const Hash<N>& h)
288+
OPENDHT_PUBLIC std::ostream& operator<< (std::ostream& s, const Hash<N>& h)
289289
{
290290
s.write(h.to_c_str(), N*2);
291291
return s;
292292
}
293293

294294
template <size_t N>
295-
std::istream& operator>> (std::istream& s, Hash<N>& h)
295+
OPENDHT_PUBLIC std::istream& operator>> (std::istream& s, Hash<N>& h)
296296
{
297297
std::array<char, h.size()*2> dat;
298298
s.exceptions(std::istream::eofbit | std::istream::failbit);
@@ -344,22 +344,18 @@ Hash<N>::getRandom(Rd& rdev)
344344
return h;
345345
}
346346

347-
struct alignas(std::max_align_t) HexMap : public std::array<std::array<char, 2>, 256> {
348-
HexMap() {
349-
for (size_t i=0; i<size(); i++) {
350-
auto& e = (*this)[i];
351-
e[0] = hex_digits[(i >> 4) & 0x0F];
352-
e[1] = hex_digits[i & 0x0F];
353-
}
354-
}
347+
struct alignas(std::max_align_t) HexMap : public std::array<std::array<char, 2>, 256>
348+
{
349+
static OPENDHT_PUBLIC const HexMap& instance();
350+
355351
private:
356-
static constexpr const char* hex_digits = "0123456789abcdef";
357-
};
358352

359-
OPENDHT_PUBLIC extern const HexMap hex_map;
353+
HexMap();
354+
};
360355

361356
inline std::string
362357
toHex(const uint8_t* data, size_t size) {
358+
const HexMap& hex_map = HexMap::instance();
363359
std::string ret(size * 2, '\0');
364360
for (size_t i=0; i<size; i++) {
365361
auto b = ret.data()+i*2;
@@ -378,6 +374,7 @@ template <size_t N>
378374
const char*
379375
Hash<N>::to_c_str() const
380376
{
377+
const HexMap& hex_map = HexMap::instance();
381378
alignas(std::max_align_t) thread_local std::array<char, N*2+1> buf;
382379
for (size_t i=0; i<N; i++) {
383380
auto b = buf.data()+i*2;

0 commit comments

Comments
 (0)