Skip to content

Commit dc24154

Browse files
committed
win7 compat
1 parent 0bcc861 commit dc24154

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

conan_patches/onnxruntime/conandata.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ patches:
77
- patch_file: "patches/msvc-x86.diff"
88
patch_description: |
99
Fixes detection of x86 windows targets during cross-compilation.
10+
- patch_file: "patches/win7.diff"
11+
patch_description: |
12+
Fixes compile errors on for windows 7 targets:
13+
error C3861: 'CreateFile2': identifier not found
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--- a/onnxruntime/test/shared_lib/test_model_loading.cc
2+
+++ b/onnxruntime/test/shared_lib/test_model_loading.cc
3+
@@ -227,7 +227,7 @@ using ScopedFileDescriptor = ScopedResource<FileDescriptorTraits>;
4+
5+
void FileMmap(const ORTCHAR_T* file_path, void*& mapped_base) {
6+
#ifdef _WIN32
7+
- wil::unique_hfile file_handle{CreateFile2(file_path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL)};
8+
+ wil::unique_hfile file_handle{CreateFileW(file_path.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)};
9+
ASSERT_TRUE(file_handle.get() != INVALID_HANDLE_VALUE);
10+
11+
wil::unique_hfile file_mapping_handle{
12+
--- a/onnxruntime/core/platform/windows/env.cc
13+
+++ b/onnxruntime/core/platform/windows/env.cc
14+
@@ -314,7 +314,7 @@ PIDType WindowsEnv::GetSelfPid() const {
15+
16+
Status WindowsEnv::GetFileLength(_In_z_ const ORTCHAR_T* file_path, size_t& length) const {
17+
wil::unique_hfile file_handle{
18+
- CreateFile2(file_path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, OPEN_EXISTING, NULL)};
19+
+ CreateFileW(file_path.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)};
20+
if (file_handle.get() == INVALID_HANDLE_VALUE) {
21+
const auto error_code = GetLastError();
22+
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "open file ", ToUTF8String(Basename(file_path)), " fail, errcode = ", error_code, " - ", std::system_category().message(error_code));
23+
@@ -361,7 +361,7 @@ Status WindowsEnv::ReadFileIntoBuffer(_In_z_ const ORTCHAR_T* const file_path, c
24+
ORT_RETURN_IF_NOT(offset >= 0, "offset < 0");
25+
ORT_RETURN_IF_NOT(length <= buffer.size(), "length > buffer.size()");
26+
wil::unique_hfile file_handle{
27+
- CreateFile2(file_path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL)};
28+
+ CreateFileW(file_path.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)};
29+
if (file_handle.get() == INVALID_HANDLE_VALUE) {
30+
const auto error_code = GetLastError();
31+
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "open file ", ToUTF8String(Basename(file_path)), " fail, errcode = ", error_code, " - ", std::system_category().message(error_code));
32+
@@ -414,7 +414,7 @@ Status WindowsEnv::MapFileIntoMemory(_In_z_ const ORTCHAR_T* file_path,
33+
}
34+
35+
wil::unique_hfile file_handle{
36+
- CreateFile2(file_path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL)};
37+
+ CreateFileW(file_path.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)};
38+
if (file_handle.get() == INVALID_HANDLE_VALUE) {
39+
const auto error_code = GetLastError();
40+
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL,
41+
@@ -600,16 +600,14 @@ common::Status WindowsEnv::GetCanonicalPath(
42+
PathString& canonical_path) const {
43+
// adapted from MSVC STL std::filesystem::canonical() implementation
44+
// https://github.com/microsoft/STL/blob/ed3cbf36416a385828e7a5987ca52cb42882d84b/stl/inc/filesystem#L2986
45+
- CREATEFILE2_EXTENDED_PARAMETERS param;
46+
- memset(&param, 0, sizeof(param));
47+
- param.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
48+
- param.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
49+
- wil::unique_hfile file_handle{CreateFile2(
50+
- path.c_str(),
51+
- FILE_READ_ATTRIBUTES,
52+
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
53+
- OPEN_EXISTING,
54+
- &param)};
55+
+ wil::unique_hfile file_handle{CreateFileW(
56+
+ path.c_str(), // LPCWSTR lpFileName
57+
+ FILE_READ_ATTRIBUTES, // DWORD dwDesiredAccess
58+
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // DWORD dwShareMode
59+
+ nullptr, // LPSECURITY_ATTRIBUTES lpSecurityAttributes
60+
+ OPEN_EXISTING, // DWORD dwCreationDisposition
61+
+ FILE_FLAG_BACKUP_SEMANTICS, // DWORD dwFlagsAndAttributes
62+
+ nullptr)}; // HANDLE hTemplateFile
63+
64+
if (file_handle.get() == INVALID_HANDLE_VALUE) {
65+
const auto error_code = GetLastError();

0 commit comments

Comments
 (0)