feat: add partial support for WASI

This change allows building the `cmark-gfm` target for both `wasm32-unknown-wasi` and `wasm32-unknown-wasip1-threads`. The other targets are not tested.

The differences between two target triples:

- `wasm32-unknown-wasi`
  - C
    - can import `unistd.h`
    - has `__wasi__`
    - does not have `__unix__`
    - **does not have `_REENTRANT`**
    - **does not have `_POSIX_THREADS`**
    - **does not support pthreads API at all**
  - Swift
    - can use `#if os(WASI)`
  - SwiftPM
    - `BuildSettingsCondition`'s `Platform` is `.wasi`
- `wasm32-unknown-wasip1-threads`
  - C
    - can import `unistd.h`
    - has `__wasi__`
    - does not have `__unix__`
    - **has `_REENTRANT` (defined in wasi-libc)**
    - **has `_POSIX_THREADS` (defined in wasi-libc)**
    - **supports a subset of pthreads API**
  - Swift
    - can use `#if os(WASI)`
  - SwiftPM
    - `BuildSettingsCondition`'s `Platform` is `.wasi`
This commit is contained in:
Kenta Kubo
2024-10-12 15:06:08 +09:00
parent b97d09472e
commit ebf5f2b4bd
3 changed files with 10 additions and 7 deletions

View File

@@ -11,12 +11,9 @@ import PackageDescription
// link time.
let cSettings: [CSetting] = [
.define("CMARK_GFM_STATIC_DEFINE", .when(platforms: [.windows])),
.define("CMARK_THREADING"),
]
#else
let cSettings: [CSetting] = [
.define("CMARK_THREADING"),
]
let cSettings: [CSetting] = []
#endif
let package = Package(

View File

@@ -54,7 +54,7 @@ void INT_EQ(test_batch_runner *runner, int got, int expected, const char *msg,
}
}
#ifndef _WIN32
#if !defined(_WIN32) && !defined(__wasi__)
#include <unistd.h>
static char *write_tmp(char const *header, char const *data) {
@@ -79,7 +79,7 @@ void STR_EQ(test_batch_runner *runner, const char *got, const char *expected,
va_end(ap);
if (!cond) {
#ifndef _WIN32
#if !defined(_WIN32) && !defined(__wasi__)
char *got_fn = write_tmp("actual\n", got);
char *expected_fn = write_tmp("expected\n", expected);
char buf[1024];

View File

@@ -3,9 +3,15 @@
#include <stdbool.h>
#ifndef CMARK_THREADING
#if !defined(__wasi__) || defined(_REENTRANT)
#define CMARK_THREADING
#endif
#endif
#ifdef CMARK_THREADING
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__wasi__)
#include <unistd.h>
#endif