Add support for wasm32-freestanding

This commit is contained in:
Frank Denis
2026-08-26 23:54:24 +02:00
parent 00a57ecc0a
commit bb1770a08f
21 changed files with 239 additions and 10 deletions
+1
View File
@@ -69,6 +69,7 @@ jobs:
zig build -Dtarget=x86_64-freebsd
zig build -Dtarget=aarch64-freebsd
zig build -Dtarget=wasm32-wasi
zig build -Dtarget=wasm32-freestanding
zig build -Doptimize=ReleaseFast
rm -fr zig-cache zig-out
+23 -1
View File
@@ -10,6 +10,7 @@ const Dir = if (pre_zig17) std.Io.Dir else std.fs.Dir;
const Io = if (pre_zig17) std.Io else void;
const has_build_root_handle = @hasField(std.Build, "build_root");
const wasm_libc_path = "builds/wasm32-freestanding";
fn initLibConfig(b: *std.Build, target: std.Build.ResolvedTarget, lib: *Compile) void {
lib.root_module.link_libc = true;
@@ -103,6 +104,12 @@ fn initLibConfig(b: *std.Build, target: std.Build.ResolvedTarget, lib: *Compile)
lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1");
lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1");
},
.freestanding => {
if (target.result.cpu.arch.isWasm()) {
lib.root_module.link_libc = false;
lib.root_module.addSystemIncludePath(b.path(wasm_libc_path ++ "/include"));
}
},
.freebsd => {
lib.root_module.addCMacro("ASM_HIDE_SYMBOL", ".hidden");
lib.root_module.addCMacro("TLS", "_Thread_local");
@@ -197,11 +204,19 @@ pub fn build(b: *std.Build) !void {
var build_static = b.option(bool, "static", "Build libsodium as a static library.") orelse true;
var build_shared = b.option(bool, "shared", "Build libsodium as a shared library.") orelse true;
const build_tests = b.option(bool, "test", "Build the tests (implies -Dstatic=true)") orelse true;
var build_tests = b.option(bool, "test", "Build the tests (implies -Dstatic=true)") orelse true;
const wasm_freestanding =
target.result.cpu.arch.isWasm() and target.result.os.tag == .freestanding;
if (target.result.cpu.arch.isWasm()) {
build_shared = false;
}
// The test programs need a hosted environment: standard input and output,
// and files to read the expected results from.
if (wasm_freestanding) {
build_tests = false;
}
if (build_tests) {
build_static = true;
}
@@ -268,6 +283,13 @@ pub fn build(b: *std.Build) !void {
"-Werror=vla",
};
if (wasm_freestanding) {
lib.root_module.addCSourceFiles(.{
.files = &.{wasm_libc_path ++ "/libc.c"},
.flags = flags,
});
}
const allocator = heap.page_allocator;
var walker = try src_dir.walk(allocator);
+12 -1
View File
@@ -84,4 +84,15 @@ EXTRA_DIST = \
msvc/vs2026/libsodium/libsodium.xml \
msvc/vs2026/libsodium.import.props \
msvc/vs2026/libsodium.import.xml \
msvc/vs2026/libsodium.sln
msvc/vs2026/libsodium.sln \
wasm32-freestanding/libc.c \
wasm32-freestanding/include/assert.h \
wasm32-freestanding/include/errno.h \
wasm32-freestanding/include/fcntl.h \
wasm32-freestanding/include/stdlib.h \
wasm32-freestanding/include/string.h \
wasm32-freestanding/include/time.h \
wasm32-freestanding/include/unistd.h \
wasm32-freestanding/include/sys/stat.h \
wasm32-freestanding/include/sys/time.h \
wasm32-freestanding/include/sys/types.h
+12 -1
View File
@@ -389,7 +389,18 @@ EXTRA_DIST = \
msvc/vs2026/libsodium/libsodium.xml \
msvc/vs2026/libsodium.import.props \
msvc/vs2026/libsodium.import.xml \
msvc/vs2026/libsodium.sln
msvc/vs2026/libsodium.sln \
wasm32-freestanding/libc.c \
wasm32-freestanding/include/assert.h \
wasm32-freestanding/include/errno.h \
wasm32-freestanding/include/fcntl.h \
wasm32-freestanding/include/stdlib.h \
wasm32-freestanding/include/string.h \
wasm32-freestanding/include/time.h \
wasm32-freestanding/include/unistd.h \
wasm32-freestanding/include/sys/stat.h \
wasm32-freestanding/include/sys/time.h \
wasm32-freestanding/include/sys/types.h
all: all-am
@@ -0,0 +1,7 @@
#undef assert
#ifdef NDEBUG
#define assert(cond) ((void)0)
#else
#define assert(cond) ((cond) ? (void)0 : __builtin_trap())
#endif
@@ -0,0 +1,17 @@
#ifndef _ERRNO_H
#define _ERRNO_H
extern int errno;
#define EPERM 1
#define EINTR 4
#define EIO 5
#define ENXIO 6
#define EAGAIN 11
#define ENOMEM 12
#define EINVAL 22
#define EFBIG 27
#define ERANGE 34
#define ENOSYS 38
#endif
@@ -0,0 +1,3 @@
#ifndef _FCNTL_H
#define _FCNTL_H
#endif
@@ -0,0 +1,17 @@
#ifndef _STDLIB_H
#define _STDLIB_H
#include <stddef.h>
#include <stdint.h>
/* arc4random_buf, malloc and free come from the embedder. */
_Noreturn void abort(void);
uint32_t arc4random(void);
void arc4random_buf(void *buf, size_t size);
void *calloc(size_t nmemb, size_t size);
void free(void *ptr);
void *malloc(size_t size);
#endif
@@ -0,0 +1,16 @@
#ifndef _STRING_H
#define _STRING_H
#include <stddef.h>
void *memchr(const void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
char *strchr(const char *s, int c);
size_t strlen(const char *s);
int strncmp(const char *s1, const char *s2, size_t n);
char *strrchr(const char *s, int c);
#endif
@@ -0,0 +1,3 @@
#ifndef _SYS_STAT_H
#define _SYS_STAT_H
#endif
@@ -0,0 +1,11 @@
#ifndef _SYS_TIME_H
#define _SYS_TIME_H
struct timeval {
long long tv_sec;
long tv_usec;
};
int gettimeofday(struct timeval *tv, void *tz);
#endif
@@ -0,0 +1,8 @@
#ifndef _SYS_TYPES_H
#define _SYS_TYPES_H
#include <stddef.h>
typedef ptrdiff_t ssize_t;
#endif
@@ -0,0 +1,3 @@
#ifndef _TIME_H
#define _TIME_H
#endif
@@ -0,0 +1,3 @@
#ifndef _UNISTD_H
#define _UNISTD_H
#endif
+94
View File
@@ -0,0 +1,94 @@
#include <stddef.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
int errno;
void *calloc(size_t nmemb, size_t size) {
void *ptr;
if (nmemb > (size_t)0U && size > SIZE_MAX / nmemb) {
return NULL;
}
if ((ptr = malloc(nmemb * size)) != NULL) {
memset(ptr, 0, nmemb * size);
}
return ptr;
}
_Noreturn void abort(void) {
__builtin_trap();
}
uint32_t arc4random(void) {
uint32_t r;
arc4random_buf(&r, sizeof r);
return r;
}
void *memchr(const void *s, int c, size_t n) {
const unsigned char *p = (const unsigned char *)s;
while (n-- > (size_t)0U) {
if (*p == (unsigned char) c) {
return (void *) (uintptr_t) p;
}
p++;
}
return NULL;
}
size_t strlen(const char *s) {
const char *p = s;
while (*p != 0) {
p++;
}
return (size_t)(p - s);
}
int strncmp(const char *s1, const char *s2, size_t n) {
while (n-- > (size_t) 0U) {
if (*s1 != *s2) {
return (int) (unsigned char) *s1 - (int) (unsigned char) *s2;
}
if (*s1 == 0) {
break;
}
s1++;
s2++;
}
return 0;
}
char *strchr(const char *s, int c) {
for (;;) {
if (*s == (char) c) {
return (char *) (uintptr_t) s;
}
if (*s == 0) {
return NULL;
}
s++;
}
}
char *strrchr(const char *s, int c) {
const char *found = NULL;
for (;;) {
if (*s == (char) c) {
found = s;
}
if (*s == 0) {
break;
}
s++;
}
return (char *) (uintptr_t) found;
}
@@ -12,8 +12,8 @@
*/
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -2,7 +2,7 @@
#include "argon2-core.h"
#include "utils.h"
#include <limits.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
+1 -1
View File
@@ -12,8 +12,8 @@
*/
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -1,8 +1,8 @@
#ifndef crypto_onetimeauth_poly1305_H
#define crypto_onetimeauth_poly1305_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -73,7 +73,8 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
#define INTERNAL_RANDOM_BLOCK_SIZE crypto_core_hchacha20_OUTPUTBYTES
#if defined(__OpenBSD__) || defined(__CloudABI__) || defined(__wasi__)
#if defined(__OpenBSD__) || defined(__CloudABI__) || \
(defined(__wasm__) && !defined(__EMSCRIPTEN__))
# define HAVE_SAFE_ARC4RANDOM 1
#endif
#if defined(__CloudABI__) || defined(__wasm__)
@@ -503,7 +504,7 @@ randombytes_internal_random_close(void)
if (global.getrandom_available != 0) {
ret = 0;
}
# elif !defined(NONEXISTENT_DEV_RANDOM) && defined(HAVE_SAFE_ARC4RANDOM)
# elif defined(HAVE_SAFE_ARC4RANDOM)
ret = 0;
# else
if (global.random_data_source_fd != -1 &&
@@ -75,7 +75,8 @@ BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
# pragma comment(lib, "advapi32.lib")
#endif
#if defined(__OpenBSD__) || defined(__CloudABI__) || defined(__wasi__)
#if defined(__OpenBSD__) || defined(__CloudABI__) || \
(defined(__wasm__) && !defined(__EMSCRIPTEN__))
# define HAVE_SAFE_ARC4RANDOM 1
#endif