Skip to content

Commit f92ad9d

Browse files
[Windows] [file_data_loader.cpp] Add compat_unistd.h (#8913)
* fix windows build issue * revert xnnpack cmakelist and fix file_data_loader * fix lint warning * undo non-relevant changes * linter error - extra newline --------- Co-authored-by: Chao Zhang <[email protected]>
1 parent 9f5ad86 commit f92ad9d

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

extension/data_loader/file_data_loader.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#include <cstring>
1515
#include <limits>
1616

17+
#include <executorch/runtime/platform/compat_unistd.h>
1718
#include <fcntl.h>
1819
#include <sys/stat.h>
1920
#include <sys/types.h>
20-
#include <unistd.h>
2121

2222
#include <executorch/runtime/core/error.h>
2323
#include <executorch/runtime/core/result.h>
@@ -71,6 +71,9 @@ FileDataLoader::~FileDataLoader() {
7171
std::free(const_cast<char*>(file_name_));
7272
// fd_ can be -1 if this instance was moved from, but closing a negative fd is
7373
// safe (though it will return an error).
74+
if (fd_ == -1) {
75+
return;
76+
}
7477
::close(fd_);
7578
}
7679

runtime/platform/compat_unistd.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* @file
11+
* unistd.h related macros for POSIX/Windows compatibility.
12+
*/
13+
#pragma once
14+
15+
#if defined(_WIN32) && !defined(_WIN64)
16+
#error \
17+
"You're trying to build ExecuTorch with a too old version of Windows. We need Windows 64-bit."
18+
#endif
19+
20+
#if !defined(_WIN64)
21+
#include <unistd.h>
22+
#else
23+
#include <io.h>
24+
#define O_RDONLY _O_RDONLY
25+
#define open _open
26+
#define close _close
27+
#define read _read
28+
#define write _write
29+
#define stat _stat64
30+
#define fstat _fstat64
31+
#define off_t _off_t
32+
#define lseek _lseeki64
33+
34+
#include <executorch/runtime/platform/compiler.h> // For ssize_t.
35+
#include <windows.h>
36+
// To avoid conflicts with std::numeric_limits<int32_t>::max() in
37+
// file_data_loader.cpp.
38+
#undef max
39+
40+
inline ssize_t pread(int fd, void* buf, size_t nbytes, size_t offset) {
41+
OVERLAPPED overlapped; /* The offset for ReadFile. */
42+
memset(&overlapped, 0, sizeof(overlapped));
43+
overlapped.Offset = offset;
44+
overlapped.OffsetHigh = offset >> 32;
45+
46+
BOOL result; /* The result of ReadFile. */
47+
DWORD bytes_read; /* The number of bytes read. */
48+
HANDLE file = (HANDLE)_get_osfhandle(fd);
49+
50+
result = ReadFile(file, buf, nbytes, &bytes_read, &overlapped);
51+
DWORD error = GetLastError();
52+
if (!result) {
53+
if (error == ERROR_IO_PENDING) {
54+
result = GetOverlappedResult(file, &overlapped, &bytes_read, TRUE);
55+
if (!result) {
56+
error = GetLastError();
57+
}
58+
}
59+
}
60+
if (!result) {
61+
// Translate error into errno.
62+
switch (error) {
63+
case ERROR_HANDLE_EOF:
64+
errno = 0;
65+
break;
66+
default:
67+
errno = EIO;
68+
break;
69+
}
70+
return -1;
71+
}
72+
return bytes_read;
73+
}
74+
75+
#endif // !defined(_WIN64)

runtime/platform/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def define_common_targets():
6868
"log.h",
6969
"profiler.h",
7070
"runtime.h",
71+
"compat_unistd.h",
7172
],
7273
srcs = [
7374
"abort.cpp",

0 commit comments

Comments
 (0)