Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable libc-wasi for windows msvc build #3852

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2512,6 +2512,15 @@ try_merge_data_and_text(const uint8 **buf, const uint8 **buf_end,
/* merge failed but may be not critical for some targets */
return false;
}

#ifdef BH_PLATFORM_WINDOWS
if (!os_mem_commit(sections, code_size,
MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC)) {
os_munmap(sections, (uint32)total_size);
return false;
}
#endif

/* change the code part to be executable */
if (os_mprotect(sections, code_size,
MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,35 @@ refcount_release(struct refcount *r)
#error "Reference counter isn't implemented"
#endif /* end of __GNUC_PREREQ (4.7) */

#elif defined(_MSC_VER)

/* Simple reference counter. */
struct LOCKABLE refcount {
LONG count;
};

/* Initialize the reference counter. */
static inline void
refcount_init(struct refcount *r, unsigned int count)
{
InterlockedExchange(&r->count, (LONG)count);
}

/* Increment the reference counter. */
static inline void
refcount_acquire(struct refcount *r)
{
InterlockedIncrement(&r->count);
}

/* Decrement the reference counter, returning whether the reference
dropped to zero. */
static inline bool
refcount_release(struct refcount *r)
{
return InterlockedDecrement(&r->count) == 0 ? true : false;
}

#else /* else of CONFIG_HAS_STD_ATOMIC */
#error "Reference counter isn't implemented"
#endif /* end of CONFIG_HAS_STD_ATOMIC */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@
#endif

#if !defined(BH_PLATFORM_LINUX_SGX)

/* Clang's __GNUC_PREREQ macro has a different meaning than GCC one,
so we have to handle this case specially */
#if defined(__clang__)

/* Clang provides stdatomic.h since 3.6.0
See https://releases.llvm.org/3.6.0/tools/clang/docs/ReleaseNotes.html */
#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 6)
#define CONFIG_HAS_STD_ATOMIC 1
#else
#define CONFIG_HAS_STD_ATOMIC 0
#endif

#elif defined(__GNUC_PREREQ)

/* Even though older versions of GCC support C11, atomics were
not implemented until 4.9. See
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58016 */
Expand All @@ -89,11 +93,21 @@ not implemented until 4.9. See
#else /* else of __GNUC_PREREQ(4, 9) */
#define CONFIG_HAS_STD_ATOMIC 0
#endif /* end of __GNUC_PREREQ(4, 9) */
#else /* else of defined(__GNUC_PREREQ) */

#elif defined(_MSC_VER)

#define CONFIG_HAS_STD_ATOMIC 0

#else

#define CONFIG_HAS_STD_ATOMIC 1
#endif /* end of defined(__GNUC_PREREQ) */
#else /* else of !defined(BH_PLATFORM_LINUX_SGX) */

#endif /* end of defined(__clang__) */

#else /* else of !defined(BH_PLATFORM_LINUX_SGX) */

#define CONFIG_HAS_STD_ATOMIC 0

#endif /* end of !defined(BH_PLATFORM_LINUX_SGX) */

#endif
#endif /* end of SSP_CONFIG_H */
6 changes: 6 additions & 0 deletions core/shared/platform/windows/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#ifndef _PLATFORM_INTERNAL_H
#define _PLATFORM_INTERNAL_H

/*
* Suppress the noisy warnings:
* winbase.h: warning C5105: macro expansion producing 'defined' has
* undefined behavior
*/
#pragma warning(disable : 5105)
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
Expand Down
6 changes: 5 additions & 1 deletion core/shared/platform/windows/win_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#define NANOSECONDS_PER_SECOND 1000000000ULL
#define NANOSECONDS_PER_TICK 100

extern NTSTATUS
NtQueryTimerResolution(PULONG MinimumResolution, PULONG MaximumResolution,
PULONG CurrentResolution);

static __wasi_errno_t
calculate_monotonic_clock_frequency(uint64 *out_frequency)
{
Expand Down Expand Up @@ -140,4 +144,4 @@ os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
default:
return __WASI_EINVAL;
}
}
}
10 changes: 8 additions & 2 deletions core/shared/platform/windows/win_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
#define _WIN_UTIL_H

#include "platform_wasi_types.h"
#include "windows.h"
/*
* Suppress the noisy warnings:
* winbase.h: warning C5105: macro expansion producing 'defined' has
* undefined behavior
*/
#pragma warning(disable : 5105)
#include <windows.h>

__wasi_timestamp_t
convert_filetime_to_wasi_timestamp(LPFILETIME filetime);
Expand All @@ -23,4 +29,4 @@ convert_windows_error_code(DWORD windows_error_code);
__wasi_errno_t
convert_winsock_error_code(int error_code);

#endif /* end of _WIN_UTIL_H */
#endif /* end of _WIN_UTIL_H */
6 changes: 3 additions & 3 deletions product-mini/platforms/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
set (WAMR_BUILD_LIBC_BUILTIN 1)
endif ()

if (NOT DEFINED WAMR_BUILD_LIBC_UVWASI)
# Enable libc uvwasi support by default
set (WAMR_BUILD_LIBC_UVWASI 1)
if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
# Enable libc wasi support by default
set (WAMR_BUILD_LIBC_WASI 1)
endif ()

if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
Expand Down
34 changes: 17 additions & 17 deletions wamr-compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,14 @@ include_directories (${SHARED_DIR}/include

enable_language (ASM)

if (NOT MINGW AND NOT MSVC)
if ((NOT DEFINED WAMR_BUILD_LIBC_WASI) AND (NOT DEFINED WAMR_BUILD_LIBC_UVWASI))
set (WAMR_BUILD_LIBC_WASI 1)
endif ()

if ((WAMR_BUILD_LIBC_WASI EQUAL 1) AND (WAMR_BUILD_LIBC_UVWASI EQUAL 1))
message (WARNING "-- pick WAMR_BULID_LIBC_UVWASI when both are enabled")
set (WAMR_BUILD_LIBC_WASI 0)
endif ()
else ()
if (NOT DEFINED WAMR_BUILD_LIBC_UVWASI)
set (WAMR_BUILD_LIBC_UVWASI 1)
endif ()
if ((NOT DEFINED WAMR_BUILD_LIBC_WASI) AND (NOT DEFINED WAMR_BUILD_LIBC_UVWASI))
# Enable WAMR_BUILD_LIBC_WASI if both are not set
set (WAMR_BUILD_LIBC_WASI 1)
endif ()

if (WAMR_BUILD_LIBC_WASI EQUAL 1)
message (WARNING "-- don't accept WAMR_BUILD_LIBC_WASI=1 on MINGW or MSVC")
set (WAMR_BUILD_LIBC_WASI 0)
endif ()
if ((WAMR_BUILD_LIBC_WASI EQUAL 1) AND (WAMR_BUILD_LIBC_UVWASI EQUAL 1))
message (WARNING "-- pick WAMR_BULID_LIBC_UVWASI when both are enabled")
set (WAMR_BUILD_LIBC_WASI 0)
endif ()

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

if (WAMR_BUILD_LIBC_WASI EQUAL 1)
# Enable _Static_assert
set (CMAKE_C_STANDARD 11)
if (MSVC)
add_compile_options(/experimental:c11atomics)
endif()
else()
set (CMAKE_C_STANDARD 99)
endif()

if (WAMR_BUILD_LIB_PTHREAD EQUAL 1)
include (${IWASM_DIR}/libraries/lib-pthread/lib_pthread.cmake)
endif ()
Expand Down