Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows build adjustment #716

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 91 additions & 75 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ set (VERSION "${opendht_VERSION}")

# Options
option (BUILD_SHARED_LIBS "Build shared library" ON)
CMAKE_DEPENDENT_OPTION (OPENDHT_STATIC "Build static library" OFF BUILD_SHARED_LIBS ON)
option (OPENDHT_PYTHON "Build Python bindings" OFF)
option (OPENDHT_TOOLS "Build DHT tools" ON)
option (OPENDHT_SYSTEMD "Install systemd module" OFF)
Expand Down Expand Up @@ -140,25 +139,70 @@ if (NOT MSVC)
set(OPENDHT_PROXY_OPENSSL OFF)
endif ()
else ()
set (WIN32_DEP_DIR ${PROJECT_SOURCE_DIR}/../)
include_directories(${WIN32_DEP_DIR}/../msvc/include) # SMP gnutls
include_directories(${WIN32_DEP_DIR}/argon2/include)
include_directories(${WIN32_DEP_DIR}/jsoncpp/include)
include_directories(src/compat/msvc)

set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)

pkg_search_module(GnuTLS REQUIRED IMPORTED_TARGET GnuTLS)
include_directories(${GnuTLS_INCLUDEDIR})
link_directories(${GnuTLS_LIBDIR})

pkg_search_module(nettle REQUIRED IMPORTED_TARGET nettle)
include_directories(${nettle_INCLUDEDIR})
link_directories(${nettle_LIBDIR})

find_package(msgpack-cxx CONFIG REQUIRED)
get_target_property(MSGPACK_INCLUDEDIR msgpack-cxx INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${MSGPACK_INCLUDEDIR})

find_package(unofficial-argon2 CONFIG REQUIRED)
get_target_property(ARGON2_INCLUDEDIR unofficial::argon2::libargon2 INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${ARGON2_INCLUDEDIR})

set(argon2_lib ", libargon2")

find_package(jsoncpp CONFIG REQUIRED)
get_target_property(JSONCPP_LIB JsonCpp::JsonCpp INTERFACE_LINK_LIBRARIES)
get_target_property(JSONCPP_INCLUDEDIR ${JSONCPP_LIB} INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSONCPP_INCLUDEDIR})

set(jsoncpp_lib ", jsoncpp")

find_package(fmt CONFIG REQUIRED)
get_target_property(FMT_INCLUDEDIR fmt::fmt-header-only INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${FMT_INCLUDEDIR})

list (APPEND opendht_SOURCES
src/base64.h
src/base64.cpp
)
add_definitions(-DOPENDHT_JSONCPP)
include_directories(${WIN32_DEP_DIR}/msgpack-c/include)
if (OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
include_directories(
${WIN32_DEP_DIR}/asio/asio/include
${WIN32_DEP_DIR}/openssl/include
${WIN32_DEP_DIR}/restinio/dev
${WIN32_DEP_DIR}/fmt/include
${WIN32_DEP_DIR}/http_parser
)
endif ()

if(OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
find_package(asio CONFIG REQUIRED)
get_target_property(ASIO_INCLUDEDIR asio INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${ASIO_INCLUDEDIR})
endif()

if (OPENDHT_HTTP)
find_package(restinio CONFIG REQUIRED)
get_target_property(RESTINIO_INCLUDEDIR restinio::restinio INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${RESTINIO_INCLUDEDIR})

find_package(llhttp CONFIG REQUIRED)
get_target_property(LLHTTP_INCLUDEDIR llhttp::llhttp_static INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${LLHTTP_INCLUDEDIR})
set(http_lib "-lllhttp")

if (OPENDHT_PROXY_OPENSSL)
find_package(OpenSSL REQUIRED)
get_target_property(OPENSSL_INCLUDEDIR OpenSSL::SSL INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${OPENSSL_INCLUDEDIR})
set(openssl_lib ", openssl")
endif()
else()
set(OPENDHT_PROXY_OPENSSL OFF)
endif()
endif ()

if (OPENDHT_HTTP OR OPENDHT_PEER_DISCOVERY)
Expand All @@ -182,7 +226,7 @@ else ()
-DGNUTLS_INTERNAL_BUILD)
set(DISABLE_MSC_WARNINGS "/wd4101 /wd4244 /wd4267 /wd4273 /wd4804 /wd4834 /wd4996")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DISABLE_MSC_WARNINGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /bigobj")
endif ()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMSGPACK_NO_BOOST -DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT")

Expand All @@ -204,7 +248,7 @@ include_directories (
# Install dirs
include (GNUInstallDirs)
set (prefix ${CMAKE_INSTALL_PREFIX})
set (exec_prefix "\${prefix}")
set (exec_prefix "${prefix}")
set (libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
set (includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
set (bindir "${CMAKE_INSTALL_FULL_BINDIR}")
Expand Down Expand Up @@ -326,69 +370,36 @@ if (MSVC)
endif ()

# Targets
if (MSVC)
if (OPENDHT_STATIC)
if (OPENDHT_TOOLS)
function (add_obj_lib name libfile)
add_library(${name} OBJECT IMPORTED)
set_property(TARGET ${name} PROPERTY IMPORTED_OBJECTS ${libfile})
endfunction ()
add_obj_lib (win32_json ${WIN32_DEP_DIR}/../msvc/lib/x64/lib_json.lib)
add_obj_lib (win32_gnutls ${WIN32_DEP_DIR}/../msvc/lib/x64/libgnutls.lib)
add_obj_lib (win32_argon2 ${WIN32_DEP_DIR}/argon2/vs2015/Argon2Ref/vs2015/build/Argon2Ref.lib)
list (APPEND obj_libs
$<TARGET_OBJECTS:win32_json>
$<TARGET_OBJECTS:win32_gnutls>
$<TARGET_OBJECTS:win32_argon2>
)
if (OPENDHT_HTTP)
add_obj_lib (win32_fmt ${WIN32_DEP_DIR}/fmt/msvc/Release/fmt.lib)
add_obj_lib (win32_http_parser ${WIN32_DEP_DIR}/http_parser/x64/Release/http-parser.lib)
add_obj_lib (win32_ssl ${WIN32_DEP_DIR}/openssl/libssl_static.lib)
add_obj_lib (win32_crypto ${WIN32_DEP_DIR}/openssl/libcrypto_static.lib)
list (APPEND obj_libs
$<TARGET_OBJECTS:win32_fmt>
$<TARGET_OBJECTS:win32_http_parser>
$<TARGET_OBJECTS:win32_ssl>
$<TARGET_OBJECTS:win32_crypto>
)
endif ()
else ()
list (APPEND win32_Libs
${PROJECT_SOURCE_DIR}/../../msvc/lib/x64/libgnutls.lib
${PROJECT_SOURCE_DIR}/../../msvc/lib/x64/lib_json.lib
${PROJECT_SOURCE_DIR}/../argon2/vs2015/Argon2Ref/vs2015/build/Argon2Ref.lib
)
list (APPEND win32_Libs
${PROJECT_SOURCE_DIR}/../fmt/msvc/Release/fmt.lib
${PROJECT_SOURCE_DIR}/../http_parser/x64/Release/http-parser.lib
${PROJECT_SOURCE_DIR}/../openssl/libssl.lib
${PROJECT_SOURCE_DIR}/../openssl/libcrypto.lib
)
endif ()
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4006")
endif()
endif ()

add_library (opendht
${opendht_SOURCES}
${opendht_HEADERS}
${obj_libs}
add_library(opendht ${opendht_SOURCES} ${opendht_HEADERS})

target_include_directories(opendht
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
set_target_properties (opendht PROPERTIES OUTPUT_NAME "opendht")

if (NOT HAVE_MSGPACKCXX)
target_link_libraries(opendht PUBLIC ${MSGPACK_TARGET})
endif()
if (APPLE)
target_link_libraries(opendht PRIVATE "-framework CoreFoundation" "-framework Security")
endif()
set_target_properties(opendht PROPERTIES OUTPUT_NAME "opendht")

if (MSVC)
if (OPENDHT_STATIC)
target_link_libraries(opendht PUBLIC PkgConfig::GnuTLS JsonCpp::JsonCpp fmt::fmt-header-only PRIVATE PkgConfig::nettle unofficial::argon2::libargon2)
if (OPENDHT_HTTP)
target_link_libraries(opendht PUBLIC llhttp::llhttp_static)
if (OPENDHT_PROXY_OPENSSL)
target_link_libraries(opendht PUBLIC OpenSSL::SSL OpenSSL::Crypto)
endif()
endif()
if (NOT BUILD_SHARED_LIBS)
target_link_libraries(opendht PUBLIC ${Win32_STATIC_LIBRARIES} ${Win32_IMPORT_LIBRARIES})
set_target_properties (opendht PROPERTIES OUTPUT_NAME "libopendht")
set_target_properties(opendht PROPERTIES OUTPUT_NAME "libopendht")
endif()
else()
if (APPLE)
target_link_libraries(opendht PRIVATE "-framework CoreFoundation" "-framework Security")
endif ()
if (NOT HAVE_MSGPACKCXX)
target_link_libraries(opendht PUBLIC ${MSGPACK_TARGET})
endif()
target_link_libraries(opendht
PRIVATE
PkgConfig::argon2
Expand All @@ -415,9 +426,10 @@ else()
endif()

if (BUILD_SHARED_LIBS)
set_target_properties (opendht PROPERTIES IMPORT_SUFFIX "_import.lib")
set_target_properties (opendht PROPERTIES SOVERSION ${opendht_VERSION_MAJOR} VERSION ${opendht_VERSION})
target_compile_definitions(opendht PRIVATE OPENDHT_BUILD)
target_compile_definitions(opendht PRIVATE opendht_EXPORTS)
else ()
target_compile_definitions(opendht PRIVATE OPENDHT_STATIC) # to build library with default class attributes
endif ()
install (TARGETS opendht DESTINATION ${CMAKE_INSTALL_LIBDIR} EXPORT opendht)

Expand Down Expand Up @@ -455,6 +467,7 @@ write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/opendhtConfigVersi
VERSION ${opendht_VERSION}
COMPATIBILITY AnyNewerVersion
)

# PkgConfig module
configure_file (
opendht.pc.in
Expand All @@ -467,6 +480,9 @@ install (DIRECTORY include DESTINATION ${CMAKE_INSTALL_PREFIX})
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/opendht.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
install (EXPORT opendht DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opendht FILE opendhtConfig.cmake)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/opendhtConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opendht)
if(MSVC AND BUILD_SHARED_LIBS)
install(FILES $<TARGET_PDB_FILE:opendht> CONFIGURATIONS "RelWithDebInfo" "Debug" DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

# Unit tests
if (BUILD_TESTING AND NOT MSVC)
Expand Down
12 changes: 6 additions & 6 deletions include/opendht/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
// OPENDHT_LOCAL is used for non-api symbols.

#ifdef opendht_EXPORTS // defined if OpenDHT is compiled as a shared library
#ifdef OPENDHT_BUILD // defined if we are building the OpenDHT shared library (instead of using it)
#define OPENDHT_PUBLIC OPENDHT_EXPORT
#define OPENDHT_PUBLIC OPENDHT_EXPORT
#define OPENDHT_LOCAL OPENDHT_HIDDEN
#else // opendht_EXPORTS is not defined: this means OpenDHT is imported or static.
#ifdef OPENDHT_STATIC
#define OPENDHT_PUBLIC
#else
#define OPENDHT_PUBLIC OPENDHT_IMPORT
#endif // OPENDHT_BUILD
#define OPENDHT_LOCAL OPENDHT_HIDDEN
#else // opendht_EXPORTS is not defined: this means OpenDHT is a static lib.
#define OPENDHT_PUBLIC
#endif
#define OPENDHT_LOCAL
#endif // opendht_EXPORTS

Expand Down
4 changes: 2 additions & 2 deletions include/opendht/default_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class OPENDHT_PUBLIC DhtMessage : public Value::Serializable<DhtMessage>
};

template <typename T>
class OPENDHT_PUBLIC SignedValue : public Value::Serializable<T>
class SignedValue : public Value::Serializable<T>
{
private:
using BaseClass = Value::Serializable<T>;
Expand All @@ -82,7 +82,7 @@ class OPENDHT_PUBLIC SignedValue : public Value::Serializable<T>
};

template <typename T>
class OPENDHT_PUBLIC EncryptedValue : public SignedValue<T>
class EncryptedValue : public SignedValue<T>
{
public:
using BaseClass = SignedValue<T>;
Expand Down
6 changes: 3 additions & 3 deletions include/opendht/infohash.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace crypto {
* Hashes identify nodes and values in the Dht.
*/
template <size_t N>
class OPENDHT_PUBLIC Hash {
class Hash {
public:
using T = std::array<uint8_t, N>;
typedef typename T::iterator iterator;
Expand Down Expand Up @@ -251,10 +251,10 @@ class OPENDHT_PUBLIC Hash {
static Hash getRandom(Rd&);

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

template <size_t M>
OPENDHT_PUBLIC friend std::istream& operator>> (std::istream& s, Hash<M>& h);
friend std::istream& operator>> (std::istream& s, Hash<M>& h);

/** Returns view to thread-allocated memory, only valid until the next call to this function. */
std::string_view to_view() const { return std::string_view(to_c_str(), N*2); }
Expand Down
17 changes: 12 additions & 5 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
if (OPENDHT_STATIC)
if (MSVC)
set (MSC_COMPAT_SOURCES ${MSC_COMPAT_DIR}/wingetopt.c)
endif ()
endif ()
if (MSVC)
set (MSC_COMPAT_DIR ../src/compat/msvc)
set (MSC_COMPAT_SOURCES ${MSC_COMPAT_DIR}/wingetopt.c)
find_library(ws2_32_LIB ws2_32)
endif()

function (configure_tool name extra_files)
add_executable (${name} ${name}.cpp ${extra_files})
add_dependencies(${name} opendht)
target_link_libraries (${name} LINK_PUBLIC opendht ${READLINE_LIBRARIES})
if (NOT BUILD_SHARED_LIBS)
target_compile_definitions(${name} PRIVATE OPENDHT_STATIC) # to define proper attributes for the opendht classes
endif()
if (MSVC)
target_sources(${name} PRIVATE ${MSC_COMPAT_SOURCES})
target_link_libraries (${name} LINK_PUBLIC ${ws2_32_LIB})
target_include_directories (${name} PRIVATE ${MSC_COMPAT_DIR})
endif ()
endfunction ()
Expand All @@ -36,6 +40,9 @@ if (NOT DEFINED CMAKE_INSTALL_BINDIR)
endif ()

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

if (OPENDHT_SYSTEMD)
if (NOT DEFINED OPENDHT_SYSTEMD_UNIT_FILE_LOCATION OR NOT OPENDHT_SYSTEMD_UNIT_FILE_LOCATION)
Expand Down