Skip to content

Commit b148a2a

Browse files
committed
chore: Simplify msvc build using vcpkg.
1 parent 5cac6d7 commit b148a2a

File tree

17 files changed

+214
-120
lines changed

17 files changed

+214
-120
lines changed

.github/workflows/ci.yml

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,16 @@ jobs:
4444
- name: Run cimplefmt
4545
run: other/docker/cimplefmt/run -u $(find tox* -name "*.[ch]")
4646

47-
build-windows:
48-
strategy:
49-
matrix:
50-
bits: [32, 64]
47+
build-android:
5148
runs-on: ubuntu-latest
5249
steps:
5350
- uses: actions/checkout@v4
5451
with:
5552
submodules: recursive
56-
- name: Cross compilation
57-
run: .github/scripts/cmake-win${{ matrix.bits }} script
53+
- run: .github/scripts/cmake-android armeabi-v7a
54+
- run: .github/scripts/cmake-android arm64-v8a
55+
- run: .github/scripts/cmake-android x86
56+
- run: .github/scripts/cmake-android x86_64
5857

5958
build-macos:
6059
runs-on: macos-latest
@@ -65,16 +64,44 @@ jobs:
6564
- name: Build and test
6665
run: .github/scripts/cmake-osx
6766

68-
build-android:
67+
build-msvc:
68+
strategy:
69+
matrix:
70+
version: [2019, 2022]
71+
runs-on: windows-${{ matrix.version }}
72+
env:
73+
VCPKG_ROOT: "C:/vcpkg"
74+
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
75+
steps:
76+
- uses: actions/checkout@v4
77+
with:
78+
submodules: recursive
79+
- name: Export GitHub Actions cache environment variables
80+
uses: actions/github-script@v6
81+
with:
82+
script: |
83+
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
84+
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
85+
- name: Configure CMake
86+
run: cmake --preset windows-default
87+
- name: Build
88+
run: cmake --build _build
89+
- name: Test
90+
run: |
91+
cd _build
92+
ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 --build-config Debug
93+
94+
build-windows:
95+
strategy:
96+
matrix:
97+
bits: [32, 64]
6998
runs-on: ubuntu-latest
7099
steps:
71100
- uses: actions/checkout@v4
72101
with:
73102
submodules: recursive
74-
- run: .github/scripts/cmake-android armeabi-v7a
75-
- run: .github/scripts/cmake-android arm64-v8a
76-
- run: .github/scripts/cmake-android x86
77-
- run: .github/scripts/cmake-android x86_64
103+
- name: Cross compilation
104+
run: .github/scripts/cmake-win${{ matrix.bits }} script
78105

79106
mypy:
80107
runs-on: ubuntu-latest

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Thumbs.db
1414
/_build
1515
/_install
1616
/tox-0.0.0*
17+
/.vs
18+
/CppProperties.json
1719
CMakeCache.txt
1820
CMakeFiles
1921
Makefile

CMakeLists.txt

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ find_package(GTest)
8181
set(CMAKE_MACOSX_RPATH ON)
8282

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

97+
# Enable some warnings if we know the compiler.
98+
if(MSVC)
99+
add_compile_options(/W4 /analyze)
100+
add_compile_options(/wd4100) # unreferenced formal parameter
101+
add_compile_options(/wd4267) # narrowing conversion
102+
add_compile_options(/wd4244) # narrowing conversion
103+
add_compile_options(/wd4127) # conditional expression is constant
104+
add_compile_options(/wd4995) # #pragma deprecated
105+
add_compile_options(/wd4018) # signed/unsigned compare
106+
add_compile_options(/wd4310) # cast truncates constant value
107+
add_compile_options(/wd4389) # signed/unsigned compare
108+
add_compile_options(/wd4245) # signed/unsigned assign/return/function call
109+
add_compile_options(/wd4200) # nonstandard extension used: zero-sized array in struct/union
110+
add_compile_options(/wd4702) # unreachable code
111+
add_compile_options(/wd6340) # unsigned int passed to signed parameter
112+
add_compile_options(/wd6326) # potential comparison of a constant with another constant
113+
114+
# TODO(iphydf): Look into these
115+
add_compile_options(/wd4996) # use WSAAddressToStringW() instead of WSAAddressToStringA()
116+
add_compile_options(/wd6255) # don't use alloca
117+
add_compile_options(/wd6385) # reading invalid data
118+
add_compile_options(/wd6001) # using uninitialized memory
119+
add_compile_options(/wd6101) # returning uninitialized memory
120+
add_compile_options(/wd6386) # buffer overrun
121+
add_compile_options(/wd6011) # NULL dereference
122+
add_compile_options(/wd6031) # sscanf return value ignored
123+
add_compile_options(/wd6387) # passing NULL to fwrite
124+
endif()
125+
97126
set(MIN_LOGGER_LEVEL "" CACHE STRING "Logging level to use (TRACE, DEBUG, INFO, WARNING, ERROR)")
98127
if(MIN_LOGGER_LEVEL)
99128
if(("${MIN_LOGGER_LEVEL}" STREQUAL "TRACE") OR
@@ -307,10 +336,14 @@ set(toxcore_SOURCES
307336
toxcore/tox_unpack.h
308337
toxcore/util.c
309338
toxcore/util.h)
310-
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${LIBSODIUM_LIBRARIES})
311-
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${LIBSODIUM_LIBRARY_DIRS})
312-
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${LIBSODIUM_INCLUDE_DIRS})
313-
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${LIBSODIUM_CFLAGS_OTHER})
339+
if(TARGET unofficial-sodium::sodium)
340+
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} unofficial-sodium::sodium)
341+
else()
342+
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${LIBSODIUM_LIBRARIES})
343+
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${LIBSODIUM_LIBRARY_DIRS})
344+
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${LIBSODIUM_INCLUDE_DIRS})
345+
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${LIBSODIUM_CFLAGS_OTHER})
346+
endif()
314347
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium)
315348
set(toxcore_API_HEADERS
316349
${toxcore_SOURCE_DIR}/toxcore/tox.h^tox
@@ -346,10 +379,14 @@ if(BUILD_TOXAV)
346379
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
347380
${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav)
348381

349-
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES})
350-
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${OPUS_LIBRARY_DIRS} ${VPX_LIBRARY_DIRS})
351-
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${OPUS_INCLUDE_DIRS} ${VPX_INCLUDE_DIRS})
352-
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${OPUS_CFLAGS_OTHER} ${VPX_CFLAGS_OTHER})
382+
if(MSVC)
383+
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} PkgConfig::OPUS PkgConfig::VPX)
384+
else()
385+
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES})
386+
set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${OPUS_LIBRARY_DIRS} ${VPX_LIBRARY_DIRS})
387+
set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${OPUS_INCLUDE_DIRS} ${VPX_INCLUDE_DIRS})
388+
set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${OPUS_CFLAGS_OTHER} ${VPX_CFLAGS_OTHER})
389+
endif()
353390
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} opus vpx)
354391
endif()
355392

@@ -380,7 +417,6 @@ if(CMAKE_THREAD_LIBS_INIT)
380417
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} ${CMAKE_THREAD_LIBS_INIT})
381418
endif()
382419

383-
384420
if(NSL_LIBRARIES)
385421
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${NSL_LIBRARIES})
386422
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lnsl)
@@ -396,9 +432,13 @@ if(SOCKET_LIBRARIES)
396432
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lsocket)
397433
endif()
398434

435+
if(TARGET PThreads4W::PThreads4W)
436+
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} PThreads4W::PThreads4W)
437+
elseif(TARGET Threads::Threads)
438+
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} Threads::Threads)
439+
endif()
399440
if(WIN32)
400-
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ws2_32 iphlpapi)
401-
set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lws2_32 -liphlpapi)
441+
set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} iphlpapi wsock32 ws2_32)
402442
endif()
403443

404444
################################################################################
@@ -496,6 +536,14 @@ if(DHT_BOOTSTRAP)
496536
target_link_libraries(DHT_bootstrap PRIVATE toxcore_shared)
497537
endif()
498538
target_link_libraries(DHT_bootstrap PRIVATE misc_tools)
539+
if(TARGET unofficial-sodium::sodium)
540+
target_link_libraries(DHT_bootstrap PRIVATE unofficial-sodium::sodium)
541+
endif()
542+
if(TARGET PThreads4W::PThreads4W)
543+
target_link_libraries(DHT_bootstrap PRIVATE PThreads4W::PThreads4W)
544+
elseif(TARGET Threads::Threads)
545+
target_link_libraries(DHT_bootstrap PRIVATE Threads::Threads)
546+
endif()
499547
install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin)
500548
endif()
501549

CMakePresets.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": 3,
3+
"configurePresets": [
4+
{
5+
"name": "windows-default",
6+
"binaryDir": "${sourceDir}/_build",
7+
"cacheVariables": {
8+
"ENABLE_SHARED": true,
9+
"ENABLE_STATIC": true,
10+
"AUTOTEST": true,
11+
"BUILD_MISC_TESTS": true,
12+
"BOOTSTRAP_DAEMON": false,
13+
"MUST_BUILD_TOXAV": true,
14+
"TEST_TIMEOUT_SECONDS": "60",
15+
"CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS": true,
16+
"CMAKE_COMPILE_WARNING_AS_ERROR": true,
17+
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
18+
}
19+
}
20+
]
21+
}

auto_tests/CMakeLists.txt

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ if(TARGET toxcore_static)
99
else()
1010
target_link_libraries(auto_test_support PRIVATE toxcore_shared)
1111
endif()
12+
if(TARGET PThreads4W::PThreads4W)
13+
target_link_libraries(auto_test_support PRIVATE PThreads4W::PThreads4W)
14+
elseif(TARGET Threads::Threads)
15+
target_link_libraries(auto_test_support PRIVATE Threads::Threads)
16+
endif()
1217

1318
function(auto_test target)
1419
add_executable(auto_${target}_test ${target}_test.c)
@@ -18,12 +23,15 @@ function(auto_test target)
1823
else()
1924
target_link_libraries(auto_${target}_test PRIVATE toxcore_shared)
2025
endif()
21-
if(NOT ARGV1 STREQUAL "DONT_RUN")
22-
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
23-
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")
24-
# add the source dir as environment variable, so the testdata can be found
25-
set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
26+
if(TARGET PThreads4W::PThreads4W)
27+
target_link_libraries(auto_${target}_test PRIVATE PThreads4W::PThreads4W)
28+
elseif(TARGET Threads::Threads)
29+
target_link_libraries(auto_${target}_test PRIVATE Threads::Threads)
2630
endif()
31+
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test)
32+
set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}")
33+
# add the source dir as environment variable, so the testdata can be found
34+
set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
2735
endfunction()
2836

2937
auto_test(TCP)
@@ -89,15 +97,20 @@ if(BUILD_TOXAV)
8997
auto_test(toxav_basic)
9098
auto_test(toxav_many)
9199

92-
target_link_libraries(auto_toxav_basic_test PRIVATE ${VPX_LIBRARIES})
93-
target_link_directories(auto_toxav_basic_test PRIVATE ${VPX_LIBRARY_DIRS})
94-
target_include_directories(auto_toxav_basic_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
95-
target_compile_options(auto_toxav_basic_test PRIVATE ${VPX_CFLAGS_OTHER})
100+
if(MSVC)
101+
target_link_libraries(auto_toxav_basic_test PRIVATE PkgConfig::VPX)
102+
target_link_libraries(auto_toxav_many_test PRIVATE PkgConfig::VPX)
103+
else()
104+
target_link_libraries(auto_toxav_basic_test PRIVATE ${VPX_LIBRARIES})
105+
target_link_directories(auto_toxav_basic_test PRIVATE ${VPX_LIBRARY_DIRS})
106+
target_include_directories(auto_toxav_basic_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
107+
target_compile_options(auto_toxav_basic_test PRIVATE ${VPX_CFLAGS_OTHER})
96108

97-
target_link_libraries(auto_toxav_many_test PRIVATE ${VPX_LIBRARIES})
98-
target_link_directories(auto_toxav_many_test PRIVATE ${VPX_LIBRARY_DIRS})
99-
target_include_directories(auto_toxav_many_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
100-
target_compile_options(auto_toxav_many_test PRIVATE ${VPX_CFLAGS_OTHER})
109+
target_link_libraries(auto_toxav_many_test PRIVATE ${VPX_LIBRARIES})
110+
target_link_directories(auto_toxav_many_test PRIVATE ${VPX_LIBRARY_DIRS})
111+
target_include_directories(auto_toxav_many_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
112+
target_compile_options(auto_toxav_many_test PRIVATE ${VPX_CFLAGS_OTHER})
113+
endif()
101114
endif()
102115

103116
if(PROXY_TEST)

cmake/Dependencies.cmake

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
###############################################################################
22
#
3-
# :: For UNIX-like systems that have pkg-config.
3+
# :: For systems that have pkg-config.
44
#
55
###############################################################################
66

7-
include(ModulePackage)
8-
9-
find_package(Threads REQUIRED)
7+
find_package(PkgConfig REQUIRED)
108

119
find_library(NSL_LIBRARIES nsl )
1210
find_library(RT_LIBRARIES rt )
1311
find_library(SOCKET_LIBRARIES socket)
1412

13+
find_package(pthreads QUIET)
14+
if(NOT TARGET PThreads4W::PThreads4W)
15+
set(THREADS_PREFER_PTHREAD_FLAG ON)
16+
find_package(Threads REQUIRED)
17+
endif()
18+
1519
# For toxcore.
16-
pkg_search_module(LIBSODIUM libsodium IMPORTED_TARGET)
20+
pkg_search_module(LIBSODIUM libsodium IMPORTED_TARGET REQUIRED)
21+
if(MSVC)
22+
find_package(unofficial-sodium REQUIRED)
23+
endif()
1724

1825
# For toxav.
1926
pkg_search_module(OPUS opus IMPORTED_TARGET)
@@ -27,56 +34,3 @@ endif()
2734

2835
# For tox-bootstrapd.
2936
pkg_search_module(LIBCONFIG libconfig IMPORTED_TARGET)
30-
31-
###############################################################################
32-
#
33-
# :: For MSVC Windows builds.
34-
#
35-
# These require specific installation paths of dependencies:
36-
# - libsodium in third-party/libsodium/Win32/Release/v140/dynamic
37-
# - pthreads in third-party/pthreads-win32/Pre-built.2
38-
#
39-
###############################################################################
40-
41-
if(MSVC)
42-
# libsodium
43-
# ---------
44-
if(NOT LIBSODIUM_FOUND)
45-
find_library(LIBSODIUM_LIBRARIES
46-
NAMES sodium libsodium
47-
PATHS
48-
"third_party/libsodium/Win32/Release/v140/dynamic"
49-
"third_party/libsodium/x64/Release/v140/dynamic"
50-
)
51-
if(LIBSODIUM_LIBRARIES)
52-
include_directories("third_party/libsodium/include")
53-
set(LIBSODIUM_FOUND TRUE)
54-
message("libsodium: ${LIBSODIUM_LIBRARIES}")
55-
else()
56-
message(FATAL_ERROR "libsodium libraries not found")
57-
endif()
58-
endif()
59-
60-
# pthreads
61-
# --------
62-
if(CMAKE_USE_WIN32_THREADS_INIT)
63-
find_library(CMAKE_THREAD_LIBS_INIT
64-
NAMES pthreadVC2
65-
PATHS
66-
"third_party/pthreads-win32/Pre-built.2/lib/x86"
67-
"third_party/pthreads-win32/Pre-built.2/lib/x64"
68-
)
69-
if(CMAKE_THREAD_LIBS_INIT)
70-
include_directories("third_party/pthreads-win32/Pre-built.2/include")
71-
add_definitions(-DHAVE_STRUCT_TIMESPEC)
72-
message("libpthreads: ${CMAKE_THREAD_LIBS_INIT}")
73-
else()
74-
find_package(pthreads4w)
75-
if(NOT pthreads4w_FOUND)
76-
message(FATAL_ERROR "libpthreads libraries not found")
77-
endif()
78-
include_directories(${pthreads4w_INCLUDE_DIR})
79-
link_libraries(${pthreads4w_LIBRARIES})
80-
endif()
81-
endif()
82-
endif()

cmake/ModulePackage.cmake

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@ if(FULLY_STATIC)
1919
set(ENABLE_STATIC ON)
2020
endif()
2121

22-
find_package(PkgConfig)
23-
2422
function(add_module lib)
25-
set(${lib}_SOURCES ${ARGN} PARENT_SCOPE)
26-
2723
if(ENABLE_SHARED)
2824
add_library(${lib}_shared SHARED ${ARGN})
2925
set_target_properties(${lib}_shared PROPERTIES OUTPUT_NAME ${lib})
3026
endif()
3127
if(ENABLE_STATIC)
3228
add_library(${lib}_static STATIC ${ARGN})
33-
set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib})
29+
if(NOT MSVC)
30+
set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib})
31+
endif()
3432
endif()
3533
endfunction()
3634

0 commit comments

Comments
 (0)