Skip to content

Commit 66b1354

Browse files
Merge pull request google#2495 from johnplatts:hwy_win32_build_fix_022525
PiperOrigin-RevId: 731194663
2 parents 60ff22e + eb4f598 commit 66b1354

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

Diff for: CMakeLists.txt

+54
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,38 @@ check_cxx_source_compiles(
136136
HWY_RISCV
137137
)
138138

139+
if (WIN32)
140+
set (ORIG_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
141+
set (CMAKE_REQUIRED_LIBRARIES synchronization)
142+
check_cxx_source_compiles(
143+
"#ifndef NOMINMAX
144+
#define NOMINMAX
145+
#endif
146+
147+
#include <windows.h>
148+
149+
int main() {
150+
unsigned val1 = 0u;
151+
unsigned val2 = 1u;
152+
WaitOnAddress(&val1, &val2, sizeof(unsigned), 1);
153+
WakeByAddressAll(&val1);
154+
WakeByAddressSingle(&val1);
155+
return 0;
156+
}"
157+
HWY_HAVE_WIN32_SYNCHRONIZATION_LIB)
158+
set (CMAKE_REQUIRED_LIBRARIES ${ORIG_CMAKE_REQUIRED_LIBRARIES})
159+
else()
160+
set (HWY_HAVE_WIN32_SYNCHRONIZATION_LIB OFF)
161+
endif ()
162+
163+
if (HWY_HAVE_WIN32_SYNCHRONIZATION_LIB OR NOT WIN32)
164+
set (HWY_DISABLE_FUTEX OFF CACHE BOOL "Disable futex for thread_pool")
165+
else()
166+
# Force HWY_DISABLE_FUTEX to ON if compiling for Win32 and
167+
# libsynchronization.a or synchronization.lib is not available
168+
set (HWY_DISABLE_FUTEX ON CACHE BOOL "Disable futex for thread_pool" FORCE)
169+
endif()
170+
139171
if (HWY_RISCV OR CMAKE_CXX_COMPILER_ARCHITECTURE_ID MATCHES "RISCV32|RISCV64|RISCV128" OR CMAKE_SYSTEM_PROCESSOR MATCHES "riscv32|riscv64|riscv128")
140172
include(CheckCSourceCompiles)
141173
check_c_source_compiles("
@@ -441,6 +473,10 @@ else()
441473

442474
endif() # !MSVC
443475

476+
if (HWY_DISABLE_FUTEX)
477+
list(APPEND HWY_FLAGS -DHWY_DISABLE_FUTEX)
478+
endif()
479+
444480
if (HWY_CMAKE_HEADER_ONLY)
445481
list(APPEND HWY_FLAGS -DHWY_HEADER_ONLY)
446482
endif()
@@ -517,6 +553,11 @@ set_target_properties(hwy_contrib PROPERTIES
517553
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/hwy/hwy.version)
518554
# For GCC __atomic_store_8, see #887
519555
target_link_libraries(hwy_contrib PRIVATE ${ATOMICS_LIBRARIES})
556+
557+
if (WIN32 AND NOT MSVC AND NOT HWY_DISABLE_FUTEX)
558+
target_link_libraries(hwy_contrib PUBLIC synchronization)
559+
endif()
560+
520561
# not supported by MSVC/Clang, safe to skip (we use DLLEXPORT annotations)
521562
if(UNIX AND NOT APPLE)
522563
set_property(TARGET hwy_contrib APPEND_STRING PROPERTY
@@ -618,6 +659,19 @@ endif() # HWY_ENABLE_TESTS
618659
# Add a pkg-config file for libhwy and the contrib/test libraries.
619660
set(HWY_LIBRARY_VERSION "${CMAKE_PROJECT_VERSION}")
620661
set(HWY_PC_FILES libhwy.pc)
662+
663+
if (HWY_DISABLE_FUTEX)
664+
set(HWY_PC_DISABLE_FUTEX_CFLAGS "-DHWY_DISABLE_FUTEX")
665+
else()
666+
set(HWY_PC_DISABLE_FUTEX_CFLAGS "")
667+
endif()
668+
669+
if (WIN32 AND NOT MSVC AND NOT HWY_DISABLE_FUTEX)
670+
set(HWY_PC_WIN32_SYNCHRONIZATION_LIBS "-lsynchronization")
671+
else()
672+
set(HWY_PC_WIN32_SYNCHRONIZATION_LIBS "")
673+
endif()
674+
621675
if (HWY_ENABLE_CONTRIB)
622676
list(APPEND HWY_PC_FILES libhwy-contrib.pc)
623677
endif() # HWY_ENABLE_CONTRIB

Diff for: hwy/contrib/thread_pool/futex.h

+2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ int __ulock_wake(uint32_t op, void* address, uint64_t zero);
7474
#define WIN32_LEAN_AND_MEAN
7575
#endif // WIN32_LEAN_AND_MEAN
7676
#include <windows.h>
77+
#if HWY_COMPILER_MSVC || HWY_COMPILER_CLANGCL
7778
#pragma comment(lib, "synchronization.lib")
79+
#endif
7880

7981
#elif HWY_CXX_LANG < 202002L // NOT C++20, which has native support
8082
#define HWY_FUTEX_SLEEP

Diff for: libhwy-contrib.pc.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
66
Name: libhwy-contrib
77
Description: Additions to Highway: dot product, image, math, sort
88
Version: @HWY_LIBRARY_VERSION@
9-
Libs: -L${libdir} -lhwy_contrib
10-
Cflags: -I${includedir}
9+
Libs: -L${libdir} -lhwy_contrib @HWY_PC_WIN32_SYNCHRONIZATION_LIBS@
10+
Cflags: -I${includedir} @HWY_PC_DISABLE_FUTEX_CFLAGS@

0 commit comments

Comments
 (0)