Skip to content

cc,wasi: ship WASI libc and autobuild it when needed #8837

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

Merged
merged 9 commits into from
May 21, 2021
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/translate_c/ast.zig"
"${CMAKE_SOURCE_DIR}/src/type.zig"
"${CMAKE_SOURCE_DIR}/src/value.zig"
"${CMAKE_SOURCE_DIR}/src/wasi_libc.zig"
"${CMAKE_SOURCE_DIR}/src/windows_sdk.zig"
"${CMAKE_SOURCE_DIR}/src/Zir.zig"
"${CMAKE_SOURCE_DIR}/src/Sema.zig"
Expand Down
2 changes: 2 additions & 0 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -10280,6 +10280,7 @@ Operating Systems:
ps4
elfiamcu
tvos
wasi
watchos
mesa3d
contiki
Expand Down Expand Up @@ -10348,6 +10349,7 @@ Available libcs:
sparc-linux-gnu
sparcv9-linux-gnu
wasm32-freestanding-musl
wasm32-wasi-musl
x86_64-linux-gnu
x86_64-linux-gnux32
x86_64-linux-musl</code></pre>
Expand Down
20 changes: 20 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __wasilibc___errno_h
#define __wasilibc___errno_h

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
extern thread_local int errno;
#else
extern _Thread_local int errno;
#endif

#define errno errno

#ifdef __cplusplus
}
#endif

#endif
86 changes: 86 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__errno_values.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef __wasilibc___errno_values_h
#define __wasilibc___errno_values_h

#include <wasi/api.h>

#define E2BIG __WASI_ERRNO_2BIG
#define EACCES __WASI_ERRNO_ACCES
#define EADDRINUSE __WASI_ERRNO_ADDRINUSE
#define EADDRNOTAVAIL __WASI_ERRNO_ADDRNOTAVAIL
#define EAFNOSUPPORT __WASI_ERRNO_AFNOSUPPORT
#define EAGAIN __WASI_ERRNO_AGAIN
#define EALREADY __WASI_ERRNO_ALREADY
#define EBADF __WASI_ERRNO_BADF
#define EBADMSG __WASI_ERRNO_BADMSG
#define EBUSY __WASI_ERRNO_BUSY
#define ECANCELED __WASI_ERRNO_CANCELED
#define ECHILD __WASI_ERRNO_CHILD
#define ECONNABORTED __WASI_ERRNO_CONNABORTED
#define ECONNREFUSED __WASI_ERRNO_CONNREFUSED
#define ECONNRESET __WASI_ERRNO_CONNRESET
#define EDEADLK __WASI_ERRNO_DEADLK
#define EDESTADDRREQ __WASI_ERRNO_DESTADDRREQ
#define EDOM __WASI_ERRNO_DOM
#define EDQUOT __WASI_ERRNO_DQUOT
#define EEXIST __WASI_ERRNO_EXIST
#define EFAULT __WASI_ERRNO_FAULT
#define EFBIG __WASI_ERRNO_FBIG
#define EHOSTUNREACH __WASI_ERRNO_HOSTUNREACH
#define EIDRM __WASI_ERRNO_IDRM
#define EILSEQ __WASI_ERRNO_ILSEQ
#define EINPROGRESS __WASI_ERRNO_INPROGRESS
#define EINTR __WASI_ERRNO_INTR
#define EINVAL __WASI_ERRNO_INVAL
#define EIO __WASI_ERRNO_IO
#define EISCONN __WASI_ERRNO_ISCONN
#define EISDIR __WASI_ERRNO_ISDIR
#define ELOOP __WASI_ERRNO_LOOP
#define EMFILE __WASI_ERRNO_MFILE
#define EMLINK __WASI_ERRNO_MLINK
#define EMSGSIZE __WASI_ERRNO_MSGSIZE
#define EMULTIHOP __WASI_ERRNO_MULTIHOP
#define ENAMETOOLONG __WASI_ERRNO_NAMETOOLONG
#define ENETDOWN __WASI_ERRNO_NETDOWN
#define ENETRESET __WASI_ERRNO_NETRESET
#define ENETUNREACH __WASI_ERRNO_NETUNREACH
#define ENFILE __WASI_ERRNO_NFILE
#define ENOBUFS __WASI_ERRNO_NOBUFS
#define ENODEV __WASI_ERRNO_NODEV
#define ENOENT __WASI_ERRNO_NOENT
#define ENOEXEC __WASI_ERRNO_NOEXEC
#define ENOLCK __WASI_ERRNO_NOLCK
#define ENOLINK __WASI_ERRNO_NOLINK
#define ENOMEM __WASI_ERRNO_NOMEM
#define ENOMSG __WASI_ERRNO_NOMSG
#define ENOPROTOOPT __WASI_ERRNO_NOPROTOOPT
#define ENOSPC __WASI_ERRNO_NOSPC
#define ENOSYS __WASI_ERRNO_NOSYS
#define ENOTCONN __WASI_ERRNO_NOTCONN
#define ENOTDIR __WASI_ERRNO_NOTDIR
#define ENOTEMPTY __WASI_ERRNO_NOTEMPTY
#define ENOTRECOVERABLE __WASI_ERRNO_NOTRECOVERABLE
#define ENOTSOCK __WASI_ERRNO_NOTSOCK
#define ENOTSUP __WASI_ERRNO_NOTSUP
#define ENOTTY __WASI_ERRNO_NOTTY
#define ENXIO __WASI_ERRNO_NXIO
#define EOVERFLOW __WASI_ERRNO_OVERFLOW
#define EOWNERDEAD __WASI_ERRNO_OWNERDEAD
#define EPERM __WASI_ERRNO_PERM
#define EPIPE __WASI_ERRNO_PIPE
#define EPROTO __WASI_ERRNO_PROTO
#define EPROTONOSUPPORT __WASI_ERRNO_PROTONOSUPPORT
#define EPROTOTYPE __WASI_ERRNO_PROTOTYPE
#define ERANGE __WASI_ERRNO_RANGE
#define EROFS __WASI_ERRNO_ROFS
#define ESPIPE __WASI_ERRNO_SPIPE
#define ESRCH __WASI_ERRNO_SRCH
#define ESTALE __WASI_ERRNO_STALE
#define ETIMEDOUT __WASI_ERRNO_TIMEDOUT
#define ETXTBSY __WASI_ERRNO_TXTBSY
#define EXDEV __WASI_ERRNO_XDEV
#define ENOTCAPABLE __WASI_ERRNO_NOTCAPABLE

#define EOPNOTSUPP ENOTSUP
#define EWOULDBLOCK EAGAIN

#endif
76 changes: 76 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__fd_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef __wasilibc___fd_set_h
#define __wasilibc___fd_set_h

#include <__typedef_fd_set.h>

#ifdef __cplusplus
extern "C" {
#endif

static __inline void FD_CLR(int __fd, fd_set *__set) {
size_t __n = __set->__nfds;
for (int *__p = __set->__fds, *__e = __p + __n;
__p < __e; ++__p)
{
if (*__p == __fd) {
*__p = __e[-1];
__set->__nfds = __n - 1;
return;
}
}
}

static __inline
#ifdef __cplusplus
bool
#else
_Bool
#endif
FD_ISSET(int __fd, const fd_set *__set)
{
size_t __n = __set->__nfds;
for (const int *__p = __set->__fds, *__e = __p + __n;
__p < __e; ++__p)
{
if (*__p == __fd) {
return 1;
}
}
return 0;
}

static __inline void FD_SET(int __fd, fd_set *__set) {
size_t __n = __set->__nfds;
for (const int *__p = __set->__fds, *__e = __p + __n;
__p < __e; ++__p)
{
if (*__p == __fd) {
return;
}
}
__set->__nfds = __n + 1;
__set->__fds[__n] = __fd;
}

static __inline void FD_ZERO(fd_set *__set) {
__set->__nfds = 0;
}

static __inline void FD_COPY(const fd_set *__restrict __from,
fd_set *__restrict __to) {
size_t __n = __from->__nfds;
__to->__nfds = __n;
__builtin_memcpy(__to->__fds, __from->__fds, __n * sizeof(int));
}

#define FD_CLR(fd, set) (FD_CLR((fd), (set)))
#define FD_ISSET(fd, set) (FD_ISSET((fd), (set)))
#define FD_SET(fd, set) (FD_SET((fd), (set)))
#define FD_ZERO(set) (FD_ZERO((set)))
#define FD_COPY(from, to) (FD_COPY((from), (to)))

#ifdef __cplusplus
}
#endif

#endif
14 changes: 14 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__function___isatty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __wasilibc___function___isatty_h
#define __wasilibc___function___isatty_h

#ifdef __cplusplus
extern "C" {
#endif

int __isatty(int fd);

#ifdef __cplusplus
}
#endif

#endif
26 changes: 26 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__functions_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef __wasilibc___functions_malloc_h
#define __wasilibc___functions_malloc_h

#define __need_size_t
#define __need_wchar_t
#define __need_NULL
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

void *malloc(size_t __size) __attribute__((__malloc__, __warn_unused_result__));
void free(void *__ptr);
void *calloc(size_t __nmemb, size_t __size) __attribute__((__malloc__, __warn_unused_result__));
void *realloc(void *__ptr, size_t __size) __attribute__((__warn_unused_result__));

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
void *reallocarray(void *__ptr, size_t __nmemb, size_t __size) __attribute__((__warn_unused_result__));
#endif

#ifdef __cplusplus
}
#endif

#endif
20 changes: 20 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__functions_memcpy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __wasilibc___functions_memcpy_h
#define __wasilibc___functions_memcpy_h

#define __need_size_t
#define __need_NULL
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

void *memcpy(void *__restrict__ __dst, const void *__restrict__ __src, size_t __n) __attribute__((__nothrow__, __leaf__, __nonnull__(1, 2)));
void *memmove(void *__dst, const void *__src, size_t __n) __attribute__((__nothrow__, __leaf__, __nonnull__(1, 2)));
void *memset(void *__dst, int __c, size_t __n) __attribute__((__nothrow__, __leaf__, __nonnull__(1)));

#ifdef __cplusplus
}
#endif

#endif
39 changes: 39 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__header_dirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef __wasilibc___header_dirent_h
#define __wasilibc___header_dirent_h

#include <wasi/api.h>

#define DT_BLK __WASI_FILETYPE_BLOCK_DEVICE
#define DT_CHR __WASI_FILETYPE_CHARACTER_DEVICE
#define DT_DIR __WASI_FILETYPE_DIRECTORY
#define DT_FIFO __WASI_FILETYPE_SOCKET_STREAM
#define DT_LNK __WASI_FILETYPE_SYMBOLIC_LINK
#define DT_REG __WASI_FILETYPE_REGULAR_FILE
#define DT_UNKNOWN __WASI_FILETYPE_UNKNOWN

#include <__struct_dirent.h>
#include <__typedef_DIR.h>

#ifdef __cplusplus
extern "C" {
#endif

int closedir(DIR *);
DIR *opendir(const char *);
DIR *fdopendir(int);
int fdclosedir(DIR *);
struct dirent *readdir(DIR *);
void rewinddir(DIR *);
void seekdir(DIR *, long);
long telldir(DIR *);
DIR *opendirat(int, const char *);
void rewinddir(DIR *);
int scandirat(int, const char *, struct dirent ***,
int (*)(const struct dirent *),
int (*)(const struct dirent **, const struct dirent **));

#ifdef __cplusplus
}
#endif

#endif
61 changes: 61 additions & 0 deletions lib/libc/include/wasm-wasi-musl/__header_fcntl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef __wasilibc___header_fcntl_h
#define __wasilibc___header_fcntl_h

#include <wasi/api.h>
#include <__seek.h>
#include <__mode_t.h>

#define O_APPEND __WASI_FDFLAGS_APPEND
#define O_DSYNC __WASI_FDFLAGS_DSYNC
#define O_NONBLOCK __WASI_FDFLAGS_NONBLOCK
#define O_RSYNC __WASI_FDFLAGS_RSYNC
#define O_SYNC __WASI_FDFLAGS_SYNC
#define O_CREAT (__WASI_OFLAGS_CREAT << 12)
#define O_DIRECTORY (__WASI_OFLAGS_DIRECTORY << 12)
#define O_EXCL (__WASI_OFLAGS_EXCL << 12)
#define O_TRUNC (__WASI_OFLAGS_TRUNC << 12)

#define O_NOFOLLOW (0x01000000)
#define O_EXEC (0x02000000)
#define O_RDONLY (0x04000000)
#define O_SEARCH (0x08000000)
#define O_WRONLY (0x10000000)

/*
* O_CLOEXEC is defined to be zero, as WASI has no exec-style functions.
*/
#define O_CLOEXEC (0)

/*
* O_TTY_INIT is defined to be zero, meaning that WASI implementations are
* expected to always initialize a terminal the first time it's opened.
*/
#define O_TTY_INIT (0)

#define O_NOCTTY (0)

#define O_RDWR (O_RDONLY | O_WRONLY)
#define O_ACCMODE (O_EXEC | O_RDWR | O_SEARCH)

#define POSIX_FADV_DONTNEED __WASI_ADVICE_DONTNEED
#define POSIX_FADV_NOREUSE __WASI_ADVICE_NOREUSE
#define POSIX_FADV_NORMAL __WASI_ADVICE_NORMAL
#define POSIX_FADV_RANDOM __WASI_ADVICE_RANDOM
#define POSIX_FADV_SEQUENTIAL __WASI_ADVICE_SEQUENTIAL
#define POSIX_FADV_WILLNEED __WASI_ADVICE_WILLNEED

#define F_GETFD (1)
#define F_SETFD (2)
#define F_GETFL (3)
#define F_SETFL (4)

#define FD_CLOEXEC (1)

#define AT_EACCESS (0x0)
#define AT_SYMLINK_NOFOLLOW (0x1)
#define AT_SYMLINK_FOLLOW (0x2)
#define AT_REMOVEDIR (0x4)

#define AT_FDCWD (-2)

#endif
Loading