Skip to content

Commit

Permalink
chore: Simplify msvc build using vcpkg.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Dec 27, 2023
1 parent 6a90ddf commit f1366de
Show file tree
Hide file tree
Showing 20 changed files with 215 additions and 181 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,27 @@ jobs:
- run: .github/scripts/cmake-android arm64-v8a
- run: .github/scripts/cmake-android x86
- run: .github/scripts/cmake-android x86_64

build-msvc:
runs-on: windows-2022
env:
VCPKG_ROOT: "C:/vcpkg"
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Export GitHub Actions cache environment variables
uses: actions/github-script@v6
with:
script: |
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
- name: Configure CMake
run: cmake --preset windows-default
- name: Build
run: cmake --build _build
- name: Test
run: |
cd _build
ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 --build-config Debug
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Thumbs.db
/_build
/_install
/tox-0.0.0*
/.vs
/CppProperties.json
CMakeCache.txt
CMakeFiles
Makefile
Expand Down
66 changes: 54 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ find_package(GTest)
set(CMAKE_MACOSX_RPATH ON)

# Set standard version for compiler.
if(MSVC)
if(MSVC AND MSVC_TOOLSET_VERSION LESS 143)
# https://developercommunity.visualstudio.com/t/older-winsdk-headers-are-incompatible-with-zcprepr/1593479
set(CMAKE_C_STANDARD 99)
else()
Expand All @@ -94,6 +94,36 @@ set(CMAKE_CXX_EXTENSIONS OFF)
message(STATUS "Supported C compiler features = ${CMAKE_C_COMPILE_FEATURES}")
message(STATUS "Supported C++ compiler features = ${CMAKE_CXX_COMPILE_FEATURES}")

# Enable some warnings if we know the compiler.
if(MSVC)
add_compile_options(/W4 /analyze)
add_compile_options(/wd4100) # unreferenced formal parameter
add_compile_options(/wd4267) # narrowing conversion
add_compile_options(/wd4244) # narrowing conversion
add_compile_options(/wd4127) # conditional expression is constant
add_compile_options(/wd4995) # #pragma deprecated
add_compile_options(/wd4018) # signed/unsigned compare
add_compile_options(/wd4310) # cast truncates constant value
add_compile_options(/wd4389) # signed/unsigned compare
add_compile_options(/wd4245) # signed/unsigned assign/return/function call
add_compile_options(/wd4200) # nonstandard extension used: zero-sized array in struct/union
add_compile_options(/wd4702) # unreachable code
add_compile_options(/wd6340) # unsigned int passed to signed parameter
add_compile_options(/wd6326) # potential comparison of a constant with another constant

# TODO(iphydf): Look into these
add_compile_options(/wd4996) # use WSAAddressToStringW() instead of WSAAddressToStringA()
add_compile_options(/wd6255) # don't use alloca
add_compile_options(/wd6385) # reading invalid data
add_compile_options(/wd6001) # using uninitialized memory
add_compile_options(/wd6101) # returning uninitialized memory
add_compile_options(/wd6386) # buffer overrun
add_compile_options(/wd6011) # NULL dereference
add_compile_options(/wd6031) # sscanf return value ignored
add_compile_options(/wd6387) # passing NULL to fwrite
add_compile_options(/wd28159) # use GetTickCount64 instead of GetTickCount
endif()

set(MIN_LOGGER_LEVEL "" CACHE STRING "Logging level to use (TRACE, DEBUG, INFO, WARNING, ERROR)")
if(MIN_LOGGER_LEVEL)
if(("${MIN_LOGGER_LEVEL}" STREQUAL "TRACE") OR
Expand Down Expand Up @@ -307,10 +337,14 @@ set(toxcore_SOURCES
toxcore/tox_unpack.h
toxcore/util.c
toxcore/util.h)
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${LIBSODIUM_LIBRARIES})
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${LIBSODIUM_LIBRARY_DIRS})
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${LIBSODIUM_INCLUDE_DIRS})
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${LIBSODIUM_CFLAGS_OTHER})
if(MSVC)
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} unofficial-sodium::sodium)
else()
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${LIBSODIUM_LIBRARIES})
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${LIBSODIUM_LIBRARY_DIRS})
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${LIBSODIUM_INCLUDE_DIRS})
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${LIBSODIUM_CFLAGS_OTHER})
endif()
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium)
set(toxcore_API_HEADERS
${toxcore_SOURCE_DIR}/toxcore/tox.h^tox
Expand Down Expand Up @@ -346,10 +380,14 @@ if(BUILD_TOXAV)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav)

set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES})
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${OPUS_LIBRARY_DIRS} ${VPX_LIBRARY_DIRS})
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${OPUS_INCLUDE_DIRS} ${VPX_INCLUDE_DIRS})
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${OPUS_CFLAGS_OTHER} ${VPX_CFLAGS_OTHER})
if(MSVC)
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} PkgConfig::OPUS PkgConfig::VPX)
else()
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES})
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${OPUS_LIBRARY_DIRS} ${VPX_LIBRARY_DIRS})
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${OPUS_INCLUDE_DIRS} ${VPX_INCLUDE_DIRS})
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${OPUS_CFLAGS_OTHER} ${VPX_CFLAGS_OTHER})
endif()
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} opus vpx)
endif()

Expand Down Expand Up @@ -380,7 +418,6 @@ if(CMAKE_THREAD_LIBS_INIT)
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} ${CMAKE_THREAD_LIBS_INIT})
endif()


if(NSL_LIBRARIES)
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${NSL_LIBRARIES})
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lnsl)
Expand All @@ -396,9 +433,11 @@ if(SOCKET_LIBRARIES)
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lsocket)
endif()

if(MSVC)
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} PThreads4W::PThreads4W)
endif()
if(WIN32)
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ws2_32 iphlpapi)
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lws2_32 -liphlpapi)
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} iphlpapi wsock32 ws2_32)
endif()

################################################################################
Expand Down Expand Up @@ -496,6 +535,9 @@ if(DHT_BOOTSTRAP)
target_link_libraries(DHT_bootstrap PRIVATE toxcore_shared)
endif()
target_link_libraries(DHT_bootstrap PRIVATE misc_tools)
if(MSVC)
target_link_libraries(DHT_bootstrap PRIVATE unofficial-sodium::sodium PThreads4W::PThreads4W)
endif()
install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin)
endif()

Expand Down
21 changes: 21 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-default",
"binaryDir": "${sourceDir}/_build",
"cacheVariables": {
"ENABLE_SHARED": true,
"ENABLE_STATIC": true,
"AUTOTEST": true,
"BUILD_MISC_TESTS": true,
"BOOTSTRAP_DAEMON": false,
"MUST_BUILD_TOXAV": true,
"TEST_TIMEOUT_SECONDS": "60",
"CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS": true,
"CMAKE_COMPILE_WARNING_AS_ERROR": true,
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
}
]
}
35 changes: 22 additions & 13 deletions auto_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ if(TARGET toxcore_static)
else()
target_link_libraries(auto_test_support PRIVATE toxcore_shared)
endif()
if(MSVC)
target_link_libraries(auto_test_support PRIVATE PThreads4W::PThreads4W)
endif()

function(auto_test target)
add_executable(auto_${target}_test ${target}_test.c)
Expand All @@ -18,12 +21,13 @@ function(auto_test target)
else()
target_link_libraries(auto_${target}_test PRIVATE toxcore_shared)
endif()
if(NOT ARGV1 STREQUAL "DONT_RUN")
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")
# add the source dir as environment variable, so the testdata can be found
set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
if(MSVC)
target_link_libraries(auto_${target}_test PRIVATE PThreads4W::PThreads4W)
endif()
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")
# add the source dir as environment variable, so the testdata can be found
set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
endfunction()

auto_test(TCP)
Expand Down Expand Up @@ -89,15 +93,20 @@ if(BUILD_TOXAV)
auto_test(toxav_basic)
auto_test(toxav_many)

target_link_libraries(auto_toxav_basic_test PRIVATE ${VPX_LIBRARIES})
target_link_directories(auto_toxav_basic_test PRIVATE ${VPX_LIBRARY_DIRS})
target_include_directories(auto_toxav_basic_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
target_compile_options(auto_toxav_basic_test PRIVATE ${VPX_CFLAGS_OTHER})
if(MSVC)
target_link_libraries(auto_toxav_basic_test PRIVATE PkgConfig::VPX)
target_link_libraries(auto_toxav_many_test PRIVATE PkgConfig::VPX)
else()
target_link_libraries(auto_toxav_basic_test PRIVATE ${VPX_LIBRARIES})
target_link_directories(auto_toxav_basic_test PRIVATE ${VPX_LIBRARY_DIRS})
target_include_directories(auto_toxav_basic_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
target_compile_options(auto_toxav_basic_test PRIVATE ${VPX_CFLAGS_OTHER})

target_link_libraries(auto_toxav_many_test PRIVATE ${VPX_LIBRARIES})
target_link_directories(auto_toxav_many_test PRIVATE ${VPX_LIBRARY_DIRS})
target_include_directories(auto_toxav_many_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
target_compile_options(auto_toxav_many_test PRIVATE ${VPX_CFLAGS_OTHER})
target_link_libraries(auto_toxav_many_test PRIVATE ${VPX_LIBRARIES})
target_link_directories(auto_toxav_many_test PRIVATE ${VPX_LIBRARY_DIRS})
target_include_directories(auto_toxav_many_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
target_compile_options(auto_toxav_many_test PRIVATE ${VPX_CFLAGS_OTHER})
endif()
endif()

if(PROXY_TEST)
Expand Down
2 changes: 2 additions & 0 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ void set_mono_time_callback(AutoTox *autotox)
Mono_Time *mono_time = autotox->tox->mono_time;

autotox->clock = current_time_monotonic(mono_time);
ck_assert_msg(autotox->clock >= 1000,
"clock is too low (not initialised?): %lu", (unsigned long)autotox->clock);
mono_time_set_current_time_callback(mono_time, nullptr, nullptr); // set to default first
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &autotox->clock);
}
Expand Down
59 changes: 6 additions & 53 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
###############################################################################
#
# :: For UNIX-like systems that have pkg-config.
# :: For systems that have pkg-config.
#
###############################################################################

include(ModulePackage)
find_package(PkgConfig REQUIRED)

find_package(Threads REQUIRED)

Expand All @@ -13,7 +13,7 @@ find_library(RT_LIBRARIES rt )
find_library(SOCKET_LIBRARIES socket)

# For toxcore.
pkg_search_module(LIBSODIUM libsodium IMPORTED_TARGET)
pkg_search_module(LIBSODIUM libsodium IMPORTED_TARGET REQUIRED)

# For toxav.
pkg_search_module(OPUS opus IMPORTED_TARGET)
Expand All @@ -28,55 +28,8 @@ endif()
# For tox-bootstrapd.
pkg_search_module(LIBCONFIG libconfig IMPORTED_TARGET)

###############################################################################
#
# :: For MSVC Windows builds.
#
# These require specific installation paths of dependencies:
# - libsodium in third-party/libsodium/Win32/Release/v140/dynamic
# - pthreads in third-party/pthreads-win32/Pre-built.2
#
###############################################################################

if(MSVC)
# libsodium
# ---------
if(NOT LIBSODIUM_FOUND)
find_library(LIBSODIUM_LIBRARIES
NAMES sodium libsodium
PATHS
"third_party/libsodium/Win32/Release/v140/dynamic"
"third_party/libsodium/x64/Release/v140/dynamic"
)
if(LIBSODIUM_LIBRARIES)
include_directories("third_party/libsodium/include")
set(LIBSODIUM_FOUND TRUE)
message("libsodium: ${LIBSODIUM_LIBRARIES}")
else()
message(FATAL_ERROR "libsodium libraries not found")
endif()
endif()

# pthreads
# --------
if(CMAKE_USE_WIN32_THREADS_INIT)
find_library(CMAKE_THREAD_LIBS_INIT
NAMES pthreadVC2
PATHS
"third_party/pthreads-win32/Pre-built.2/lib/x86"
"third_party/pthreads-win32/Pre-built.2/lib/x64"
)
if(CMAKE_THREAD_LIBS_INIT)
include_directories("third_party/pthreads-win32/Pre-built.2/include")
add_definitions(-DHAVE_STRUCT_TIMESPEC)
message("libpthreads: ${CMAKE_THREAD_LIBS_INIT}")
else()
find_package(pthreads4w)
if(NOT pthreads4w_FOUND)
message(FATAL_ERROR "libpthreads libraries not found")
endif()
include_directories(${pthreads4w_INCLUDE_DIR})
link_libraries(${pthreads4w_LIBRARIES})
endif()
endif()
# For toxcore.
find_package(PThreads4W REQUIRED)
find_package(unofficial-sodium REQUIRED)
endif()
8 changes: 3 additions & 5 deletions cmake/ModulePackage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ if(FULLY_STATIC)
set(ENABLE_STATIC ON)
endif()

find_package(PkgConfig)

function(add_module lib)
set(${lib}_SOURCES ${ARGN} PARENT_SCOPE)

if(ENABLE_SHARED)
add_library(${lib}_shared SHARED ${ARGN})
set_target_properties(${lib}_shared PROPERTIES OUTPUT_NAME ${lib})
endif()
if(ENABLE_STATIC)
add_library(${lib}_static STATIC ${ARGN})
set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib})
if(NOT MSVC)
set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib})
endif()
endif()
endfunction()

Expand Down
1 change: 0 additions & 1 deletion other/astyle/astylerc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
--align-reference=name

# Formatting Options
--add-brackets
--convert-tabs
--max-code-length=120

Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7e935410e534383805cf9579f0f1855a31a034a9d04d498524ef5eb4bbabd496 /usr/local/bin/tox-bootstrapd
bedc7177abb3416d5d4ec45c439fdfe1d346abf896c1f71d3f2363250bc7688e /usr/local/bin/tox-bootstrapd
15 changes: 11 additions & 4 deletions testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ if(TARGET toxcore_static)
else()
target_link_libraries(misc_tools PRIVATE toxcore_shared)
endif()
target_link_libraries(misc_tools PRIVATE ${LIBSODIUM_LIBRARIES})
target_link_directories(misc_tools PUBLIC ${LIBSODIUM_LIBRARY_DIRS})
target_include_directories(misc_tools SYSTEM PRIVATE ${LIBSODIUM_INCLUDE_DIRS})
target_compile_options(misc_tools PRIVATE ${LIBSODIUM_CFLAGS_OTHER})
if(MSVC)
target_link_libraries(misc_tools PRIVATE unofficial-sodium::sodium PThreads4W::PThreads4W)
else()
target_link_libraries(misc_tools PRIVATE ${LIBSODIUM_LIBRARIES})
target_link_directories(misc_tools PUBLIC ${LIBSODIUM_LIBRARY_DIRS})
target_include_directories(misc_tools SYSTEM PRIVATE ${LIBSODIUM_INCLUDE_DIRS})
target_compile_options(misc_tools PRIVATE ${LIBSODIUM_CFLAGS_OTHER})
endif()

################################################################################
#
Expand All @@ -27,4 +31,7 @@ if(BUILD_MISC_TESTS)
else()
target_link_libraries(Messenger_test PRIVATE toxcore_shared)
endif()
if(MSVC)
target_link_libraries(Messenger_test PRIVATE PThreads4W::PThreads4W)
endif()
endif()
2 changes: 1 addition & 1 deletion toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool
return -1;
}

*ip_port = empty_ip_port;
ipport_reset(ip_port);

if (is_ipv4) {
const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t);
Expand Down
Loading

0 comments on commit f1366de

Please sign in to comment.