Skip to content

Commit b16b604

Browse files
authored
Enable libc-wasi for windows msvc build (#3852)
The default iwasm building in Windows MSVC enables libc-uvwasi because libc-wasi isn't supported at the beginning. Since libc-wasi had been refactored and is supported in Windows msys2 building, and libc-wasi supports more functionalities(e.g. sockets) than libc-uvwasi, this PR fixes some issues to enable libc-wasi in windows MSVC buidlings.
1 parent 36d4380 commit b16b604

File tree

8 files changed

+95
-27
lines changed

8 files changed

+95
-27
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,6 +2512,15 @@ try_merge_data_and_text(const uint8 **buf, const uint8 **buf_end,
25122512
/* merge failed but may be not critical for some targets */
25132513
return false;
25142514
}
2515+
2516+
#ifdef BH_PLATFORM_WINDOWS
2517+
if (!os_mem_commit(sections, code_size,
2518+
MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC)) {
2519+
os_munmap(sections, (uint32)total_size);
2520+
return false;
2521+
}
2522+
#endif
2523+
25152524
/* change the code part to be executable */
25162525
if (os_mprotect(sections, code_size,
25172526
MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC)

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/refcount.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,35 @@ refcount_release(struct refcount *r)
132132
#error "Reference counter isn't implemented"
133133
#endif /* end of __GNUC_PREREQ (4.7) */
134134

135+
#elif defined(_MSC_VER)
136+
137+
/* Simple reference counter. */
138+
struct LOCKABLE refcount {
139+
LONG count;
140+
};
141+
142+
/* Initialize the reference counter. */
143+
static inline void
144+
refcount_init(struct refcount *r, unsigned int count)
145+
{
146+
InterlockedExchange(&r->count, (LONG)count);
147+
}
148+
149+
/* Increment the reference counter. */
150+
static inline void
151+
refcount_acquire(struct refcount *r)
152+
{
153+
InterlockedIncrement(&r->count);
154+
}
155+
156+
/* Decrement the reference counter, returning whether the reference
157+
dropped to zero. */
158+
static inline bool
159+
refcount_release(struct refcount *r)
160+
{
161+
return InterlockedDecrement(&r->count) == 0 ? true : false;
162+
}
163+
135164
#else /* else of CONFIG_HAS_STD_ATOMIC */
136165
#error "Reference counter isn't implemented"
137166
#endif /* end of CONFIG_HAS_STD_ATOMIC */

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,21 @@
7070
#endif
7171

7272
#if !defined(BH_PLATFORM_LINUX_SGX)
73+
7374
/* Clang's __GNUC_PREREQ macro has a different meaning than GCC one,
7475
so we have to handle this case specially */
7576
#if defined(__clang__)
77+
7678
/* Clang provides stdatomic.h since 3.6.0
7779
See https://releases.llvm.org/3.6.0/tools/clang/docs/ReleaseNotes.html */
7880
#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 6)
7981
#define CONFIG_HAS_STD_ATOMIC 1
8082
#else
8183
#define CONFIG_HAS_STD_ATOMIC 0
8284
#endif
85+
8386
#elif defined(__GNUC_PREREQ)
87+
8488
/* Even though older versions of GCC support C11, atomics were
8589
not implemented until 4.9. See
8690
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58016 */
@@ -89,11 +93,21 @@ not implemented until 4.9. See
8993
#else /* else of __GNUC_PREREQ(4, 9) */
9094
#define CONFIG_HAS_STD_ATOMIC 0
9195
#endif /* end of __GNUC_PREREQ(4, 9) */
92-
#else /* else of defined(__GNUC_PREREQ) */
96+
97+
#elif defined(_MSC_VER)
98+
99+
#define CONFIG_HAS_STD_ATOMIC 0
100+
101+
#else
102+
93103
#define CONFIG_HAS_STD_ATOMIC 1
94-
#endif /* end of defined(__GNUC_PREREQ) */
95-
#else /* else of !defined(BH_PLATFORM_LINUX_SGX) */
104+
105+
#endif /* end of defined(__clang__) */
106+
107+
#else /* else of !defined(BH_PLATFORM_LINUX_SGX) */
108+
96109
#define CONFIG_HAS_STD_ATOMIC 0
110+
97111
#endif /* end of !defined(BH_PLATFORM_LINUX_SGX) */
98112

99-
#endif
113+
#endif /* end of SSP_CONFIG_H */

core/shared/platform/windows/platform_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#ifndef _PLATFORM_INTERNAL_H
77
#define _PLATFORM_INTERNAL_H
88

9+
/*
10+
* Suppress the noisy warnings:
11+
* winbase.h: warning C5105: macro expansion producing 'defined' has
12+
* undefined behavior
13+
*/
14+
#pragma warning(disable : 5105)
915
#include <inttypes.h>
1016
#include <stdbool.h>
1117
#include <assert.h>

core/shared/platform/windows/win_clock.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#define NANOSECONDS_PER_SECOND 1000000000ULL
1111
#define NANOSECONDS_PER_TICK 100
1212

13+
extern NTSTATUS
14+
NtQueryTimerResolution(PULONG MinimumResolution, PULONG MaximumResolution,
15+
PULONG CurrentResolution);
16+
1317
static __wasi_errno_t
1418
calculate_monotonic_clock_frequency(uint64 *out_frequency)
1519
{
@@ -140,4 +144,4 @@ os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
140144
default:
141145
return __WASI_EINVAL;
142146
}
143-
}
147+
}

core/shared/platform/windows/win_util.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
#define _WIN_UTIL_H
88

99
#include "platform_wasi_types.h"
10-
#include "windows.h"
10+
/*
11+
* Suppress the noisy warnings:
12+
* winbase.h: warning C5105: macro expansion producing 'defined' has
13+
* undefined behavior
14+
*/
15+
#pragma warning(disable : 5105)
16+
#include <windows.h>
1117

1218
__wasi_timestamp_t
1319
convert_filetime_to_wasi_timestamp(LPFILETIME filetime);
@@ -23,4 +29,4 @@ convert_windows_error_code(DWORD windows_error_code);
2329
__wasi_errno_t
2430
convert_winsock_error_code(int error_code);
2531

26-
#endif /* end of _WIN_UTIL_H */
32+
#endif /* end of _WIN_UTIL_H */

product-mini/platforms/windows/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
5252
set (WAMR_BUILD_LIBC_BUILTIN 1)
5353
endif ()
5454

55-
if (NOT DEFINED WAMR_BUILD_LIBC_UVWASI)
56-
# Enable libc uvwasi support by default
57-
set (WAMR_BUILD_LIBC_UVWASI 1)
55+
if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
56+
# Enable libc wasi support by default
57+
set (WAMR_BUILD_LIBC_WASI 1)
5858
endif ()
5959

6060
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)

wamr-compiler/CMakeLists.txt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,24 +223,14 @@ include_directories (${SHARED_DIR}/include
223223

224224
enable_language (ASM)
225225

226-
if (NOT MINGW AND NOT MSVC)
227-
if ((NOT DEFINED WAMR_BUILD_LIBC_WASI) AND (NOT DEFINED WAMR_BUILD_LIBC_UVWASI))
228-
set (WAMR_BUILD_LIBC_WASI 1)
229-
endif ()
230-
231-
if ((WAMR_BUILD_LIBC_WASI EQUAL 1) AND (WAMR_BUILD_LIBC_UVWASI EQUAL 1))
232-
message (WARNING "-- pick WAMR_BULID_LIBC_UVWASI when both are enabled")
233-
set (WAMR_BUILD_LIBC_WASI 0)
234-
endif ()
235-
else ()
236-
if (NOT DEFINED WAMR_BUILD_LIBC_UVWASI)
237-
set (WAMR_BUILD_LIBC_UVWASI 1)
238-
endif ()
226+
if ((NOT DEFINED WAMR_BUILD_LIBC_WASI) AND (NOT DEFINED WAMR_BUILD_LIBC_UVWASI))
227+
# Enable WAMR_BUILD_LIBC_WASI if both are not set
228+
set (WAMR_BUILD_LIBC_WASI 1)
229+
endif ()
239230

240-
if (WAMR_BUILD_LIBC_WASI EQUAL 1)
241-
message (WARNING "-- don't accept WAMR_BUILD_LIBC_WASI=1 on MINGW or MSVC")
242-
set (WAMR_BUILD_LIBC_WASI 0)
243-
endif ()
231+
if ((WAMR_BUILD_LIBC_WASI EQUAL 1) AND (WAMR_BUILD_LIBC_UVWASI EQUAL 1))
232+
message (WARNING "-- pick WAMR_BULID_LIBC_UVWASI when both are enabled")
233+
set (WAMR_BUILD_LIBC_WASI 0)
244234
endif ()
245235

246236
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
@@ -308,6 +298,16 @@ if (WAMR_BUILD_LIBC_WASI EQUAL 1)
308298
include (${IWASM_DIR}/libraries/libc-wasi/libc_wasi.cmake)
309299
endif ()
310300

301+
if (WAMR_BUILD_LIBC_WASI EQUAL 1)
302+
# Enable _Static_assert
303+
set (CMAKE_C_STANDARD 11)
304+
if (MSVC)
305+
add_compile_options(/experimental:c11atomics)
306+
endif()
307+
else()
308+
set (CMAKE_C_STANDARD 99)
309+
endif()
310+
311311
if (WAMR_BUILD_LIB_PTHREAD EQUAL 1)
312312
include (${IWASM_DIR}/libraries/lib-pthread/lib_pthread.cmake)
313313
endif ()

0 commit comments

Comments
 (0)