Skip to content

Commit f48518b

Browse files
committed
Fix mingw-w64 building
1 parent 3f44c80 commit f48518b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ if(CMAKE_SIZEOF_VOID_P LESS 8)
124124
message(WARNING "Pointer size ${CMAKE_SIZEOF_VOID_P} is not supported. Please use a 64-bit compiler.")
125125
endif()
126126

127+
# check CreateFile2 for mingw
128+
INCLUDE(CheckCXXSourceCompiles)
129+
if(MINGW)
130+
check_cxx_source_compiles("#include <windows.h>
131+
#include <fileapi.h>
132+
int main() {
133+
HANDLE h = CreateFile2(L\"nul\", GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL);
134+
if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
135+
return 0;
136+
}" HAVE_CREATEFILE2)
137+
138+
if(HAVE_CREATEFILE2)
139+
add_compile_definitions(HAVE_CREATEFILE2)
140+
endif()
141+
endif()
142+
127143
# Set some variables that are used in-tree and while building based on our options
128144
set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE})
129145
set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN})

httplib.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,8 +3078,13 @@ inline bool mmap::open(const char *path) {
30783078
auto wpath = u8string_to_wstring(path);
30793079
if (wpath.empty()) { return false; }
30803080

3081+
#if defined(HAVE_CREATEFILE2)
30813082
hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
30823083
OPEN_EXISTING, NULL);
3084+
#else
3085+
hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
3086+
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3087+
#endif
30833088

30843089
if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
30853090

0 commit comments

Comments
 (0)