Skip to content

Commit 3be70cd

Browse files
committed
msvc build adjustment
1 parent 972d4d2 commit 3be70cd

File tree

5 files changed

+114
-91
lines changed

5 files changed

+114
-91
lines changed

CMakeLists.txt

+91-75
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ set (VERSION "${opendht_VERSION}")
2020

2121
# Options
2222
option (BUILD_SHARED_LIBS "Build shared library" ON)
23-
CMAKE_DEPENDENT_OPTION (OPENDHT_STATIC "Build static library" OFF BUILD_SHARED_LIBS ON)
2423
option (OPENDHT_PYTHON "Build Python bindings" OFF)
2524
option (OPENDHT_TOOLS "Build DHT tools" ON)
2625
option (OPENDHT_SYSTEMD "Install systemd module" OFF)
@@ -140,25 +139,70 @@ if (NOT MSVC)
140139
set(OPENDHT_PROXY_OPENSSL OFF)
141140
endif ()
142141
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)
142+
include_directories(src/compat/msvc)
143+
144+
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)
145+
146+
pkg_search_module(GnuTLS REQUIRED IMPORTED_TARGET GnuTLS)
147+
include_directories(${GnuTLS_INCLUDEDIR})
148+
link_directories(${GnuTLS_LIBDIR})
149+
150+
pkg_search_module(nettle REQUIRED IMPORTED_TARGET nettle)
151+
include_directories(${nettle_INCLUDEDIR})
152+
link_directories(${nettle_LIBDIR})
153+
154+
find_package(msgpack-cxx CONFIG REQUIRED)
155+
get_target_property(MSGPACK_INCLUDEDIR msgpack-cxx INTERFACE_INCLUDE_DIRECTORIES)
156+
include_directories(${MSGPACK_INCLUDEDIR})
157+
158+
find_package(unofficial-argon2 CONFIG REQUIRED)
159+
get_target_property(ARGON2_INCLUDEDIR unofficial::argon2::libargon2 INTERFACE_INCLUDE_DIRECTORIES)
160+
include_directories(${ARGON2_INCLUDEDIR})
161+
162+
set(argon2_lib ", libargon2")
163+
164+
find_package(jsoncpp CONFIG REQUIRED)
165+
get_target_property(JSONCPP_LIB JsonCpp::JsonCpp INTERFACE_LINK_LIBRARIES)
166+
get_target_property(JSONCPP_INCLUDEDIR ${JSONCPP_LIB} INTERFACE_INCLUDE_DIRECTORIES)
167+
include_directories(${JSONCPP_INCLUDEDIR})
168+
169+
set(jsoncpp_lib ", jsoncpp")
170+
171+
find_package(fmt CONFIG REQUIRED)
172+
get_target_property(FMT_INCLUDEDIR fmt::fmt-header-only INTERFACE_INCLUDE_DIRECTORIES)
173+
include_directories(${FMT_INCLUDEDIR})
174+
147175
list (APPEND opendht_SOURCES
148176
src/base64.h
149177
src/base64.cpp
150178
)
151179
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 ()
180+
181+
if(OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
182+
find_package(asio CONFIG REQUIRED)
183+
get_target_property(ASIO_INCLUDEDIR asio INTERFACE_INCLUDE_DIRECTORIES)
184+
include_directories(${ASIO_INCLUDEDIR})
185+
endif()
186+
187+
if (OPENDHT_HTTP)
188+
find_package(restinio CONFIG REQUIRED)
189+
get_target_property(RESTINIO_INCLUDEDIR restinio::restinio INTERFACE_INCLUDE_DIRECTORIES)
190+
include_directories(${RESTINIO_INCLUDEDIR})
191+
192+
find_package(llhttp CONFIG REQUIRED)
193+
get_target_property(LLHTTP_INCLUDEDIR llhttp::llhttp_static INTERFACE_INCLUDE_DIRECTORIES)
194+
include_directories(${LLHTTP_INCLUDEDIR})
195+
set(http_lib "-lllhttp")
196+
197+
if (OPENDHT_PROXY_OPENSSL)
198+
find_package(OpenSSL REQUIRED)
199+
get_target_property(OPENSSL_INCLUDEDIR OpenSSL::SSL INTERFACE_INCLUDE_DIRECTORIES)
200+
include_directories(${OPENSSL_INCLUDEDIR})
201+
set(openssl_lib ", openssl")
202+
endif()
203+
else()
204+
set(OPENDHT_PROXY_OPENSSL OFF)
205+
endif()
162206
endif ()
163207

164208
if (OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
@@ -182,7 +226,7 @@ else ()
182226
-DGNUTLS_INTERNAL_BUILD)
183227
set(DISABLE_MSC_WARNINGS "/wd4101 /wd4244 /wd4267 /wd4273 /wd4804 /wd4834 /wd4996")
184228
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DISABLE_MSC_WARNINGS}")
185-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
229+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /bigobj")
186230
endif ()
187231
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMSGPACK_NO_BOOST -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT")
188232

@@ -204,7 +248,7 @@ include_directories (
204248
# Install dirs
205249
include (GNUInstallDirs)
206250
set (prefix ${CMAKE_INSTALL_PREFIX})
207-
set (exec_prefix "\${prefix}")
251+
set (exec_prefix "${prefix}")
208252
set (libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
209253
set (includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
210254
set (bindir "${CMAKE_INSTALL_FULL_BINDIR}")
@@ -326,69 +370,36 @@ if (MSVC)
326370
endif ()
327371

328372
# 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 ()
372373

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

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()
382+
set_target_properties(opendht PROPERTIES OUTPUT_NAME "opendht")
383+
386384
if (MSVC)
387-
if (OPENDHT_STATIC)
385+
target_link_libraries(opendht PUBLIC PkgConfig::GnuTLS JsonCpp::JsonCpp fmt::fmt-header-only PRIVATE PkgConfig::nettle unofficial::argon2::libargon2)
386+
if (OPENDHT_HTTP)
387+
target_link_libraries(opendht PUBLIC llhttp::llhttp_static)
388+
if (OPENDHT_PROXY_OPENSSL)
389+
target_link_libraries(opendht PUBLIC OpenSSL::SSL OpenSSL::Crypto)
390+
endif()
391+
endif()
392+
if (NOT BUILD_SHARED_LIBS)
388393
target_link_libraries(opendht PUBLIC ${Win32_STATIC_LIBRARIES} ${Win32_IMPORT_LIBRARIES})
389-
set_target_properties (opendht PROPERTIES OUTPUT_NAME "libopendht")
394+
set_target_properties(opendht PROPERTIES OUTPUT_NAME "libopendht")
390395
endif()
391396
else()
397+
if (APPLE)
398+
target_link_libraries(opendht PRIVATE "-framework CoreFoundation" "-framework Security")
399+
endif ()
400+
if (NOT HAVE_MSGPACKCXX)
401+
target_link_libraries(opendht PUBLIC ${MSGPACK_TARGET})
402+
endif()
392403
target_link_libraries(opendht
393404
PRIVATE
394405
PkgConfig::argon2
@@ -415,9 +426,10 @@ else()
415426
endif()
416427

417428
if (BUILD_SHARED_LIBS)
418-
set_target_properties (opendht PROPERTIES IMPORT_SUFFIX "_import.lib")
419429
set_target_properties (opendht PROPERTIES SOVERSION ${opendht_VERSION_MAJOR} VERSION ${opendht_VERSION})
420-
target_compile_definitions(opendht PRIVATE OPENDHT_BUILD)
430+
target_compile_definitions(opendht PRIVATE opendht_EXPORTS)
431+
else ()
432+
target_compile_definitions(opendht PRIVATE OPENDHT_STATIC) # to build library with default class attributes
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/def.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
// OPENDHT_LOCAL is used for non-api symbols.
1717

1818
#ifdef opendht_EXPORTS // defined if OpenDHT is compiled as a shared library
19-
#ifdef OPENDHT_BUILD // defined if we are building the OpenDHT shared library (instead of using it)
20-
#define OPENDHT_PUBLIC OPENDHT_EXPORT
19+
#define OPENDHT_PUBLIC OPENDHT_EXPORT
20+
#define OPENDHT_LOCAL OPENDHT_HIDDEN
21+
#else // opendht_EXPORTS is not defined: this means OpenDHT is imported or static.
22+
#ifdef OPENDHT_STATIC
23+
#define OPENDHT_PUBLIC
2124
#else
2225
#define OPENDHT_PUBLIC OPENDHT_IMPORT
23-
#endif // OPENDHT_BUILD
24-
#define OPENDHT_LOCAL OPENDHT_HIDDEN
25-
#else // opendht_EXPORTS is not defined: this means OpenDHT is a static lib.
26-
#define OPENDHT_PUBLIC
26+
#endif
2727
#define OPENDHT_LOCAL
2828
#endif // opendht_EXPORTS
2929

include/opendht/default_types.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class OPENDHT_PUBLIC DhtMessage : public Value::Serializable<DhtMessage>
5959
};
6060

6161
template <typename T>
62-
class OPENDHT_PUBLIC SignedValue : public Value::Serializable<T>
62+
class SignedValue : public Value::Serializable<T>
6363
{
6464
private:
6565
using BaseClass = Value::Serializable<T>;
@@ -82,7 +82,7 @@ class OPENDHT_PUBLIC SignedValue : public Value::Serializable<T>
8282
};
8383

8484
template <typename T>
85-
class OPENDHT_PUBLIC EncryptedValue : public SignedValue<T>
85+
class EncryptedValue : public SignedValue<T>
8686
{
8787
public:
8888
using BaseClass = SignedValue<T>;

include/opendht/infohash.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace crypto {
6363
* Hashes identify nodes and values in the Dht.
6464
*/
6565
template <size_t N>
66-
class OPENDHT_PUBLIC Hash {
66+
class Hash {
6767
public:
6868
using T = std::array<uint8_t, N>;
6969
typedef typename T::iterator iterator;
@@ -251,10 +251,10 @@ class OPENDHT_PUBLIC Hash {
251251
static Hash getRandom(Rd&);
252252

253253
template <size_t M>
254-
OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const Hash<M>& h);
254+
friend std::ostream& operator<< (std::ostream& s, const Hash<M>& h);
255255

256256
template <size_t M>
257-
OPENDHT_PUBLIC friend std::istream& operator>> (std::istream& s, Hash<M>& h);
257+
friend std::istream& operator>> (std::istream& s, Hash<M>& h);
258258

259259
/** Returns view to thread-allocated memory, only valid until the next call to this function. */
260260
std::string_view to_view() const { return std::string_view(to_c_str(), N*2); }

tools/CMakeLists.txt

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
if (OPENDHT_STATIC)
2-
if (MSVC)
3-
set (MSC_COMPAT_SOURCES ${MSC_COMPAT_DIR}/wingetopt.c)
4-
endif ()
5-
endif ()
1+
if (MSVC)
2+
set (MSC_COMPAT_DIR ../src/compat/msvc)
3+
set (MSC_COMPAT_SOURCES ${MSC_COMPAT_DIR}/wingetopt.c)
4+
find_library(ws2_32_LIB ws2_32)
5+
endif()
66

77
function (configure_tool name extra_files)
88
add_executable (${name} ${name}.cpp ${extra_files})
99
add_dependencies(${name} opendht)
1010
target_link_libraries (${name} LINK_PUBLIC opendht ${READLINE_LIBRARIES})
11+
if (NOT BUILD_SHARED_LIBS)
12+
target_compile_definitions(${name} PRIVATE OPENDHT_STATIC) # to define proper attributes for the opendht classes
13+
endif()
1114
if (MSVC)
1215
target_sources(${name} PRIVATE ${MSC_COMPAT_SOURCES})
16+
target_link_libraries (${name} LINK_PUBLIC ${ws2_32_LIB})
1317
target_include_directories (${name} PRIVATE ${MSC_COMPAT_DIR})
1418
endif ()
1519
endfunction ()
@@ -36,6 +40,9 @@ if (NOT DEFINED CMAKE_INSTALL_BINDIR)
3640
endif ()
3741

3842
install (TARGETS dhtnode dhtscanner dhtchat RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
43+
if(MSVC)
44+
install(FILES $<TARGET_PDB_FILE:dhtnode> $<TARGET_PDB_FILE:dhtscanner> $<TARGET_PDB_FILE:dhtchat> CONFIGURATIONS "Debug" "RelWithDebInfo" DESTINATION ${CMAKE_INSTALL_BINDIR})
45+
endif()
3946

4047
if (OPENDHT_SYSTEMD)
4148
if (NOT DEFINED OPENDHT_SYSTEMD_UNIT_FILE_LOCATION OR NOT OPENDHT_SYSTEMD_UNIT_FILE_LOCATION)

0 commit comments

Comments
 (0)