move vendor:libc to vendor:libc-shim

This commit is contained in:
Laytan Laats
2026-01-22 21:34:46 +01:00
parent ce71227b6b
commit 5f974059f4
29 changed files with 45 additions and 42 deletions

View File

@@ -1,4 +1,4 @@
#+build wasm32, wasm64p32
package vendor_box2d
@(require) import _ "vendor:libc"
@(require) import _ "vendor:libc-shim"

View File

@@ -11,7 +11,7 @@ VERSION = 3.1.1
SRCS = $(wildcard box2d-$(VERSION)/src/*.c)
OBJS_SIMD = $(SRCS:.c=_simd.o)
OBJS = $(SRCS:.c=.o)
SYSROOT = $(shell odin root)/vendor/libc
SYSROOT = $(shell odin root)/vendor/libc-shim
CFLAGS = -Ibox2d-$(VERSION)/include --target=wasm32 -D__EMSCRIPTEN__ -DNDEBUG -O3 --sysroot=$(SYSROOT)
all: lib/box2d_wasm.o lib/box2d_wasm_simd.o

View File

@@ -1,4 +1,4 @@
#+build wasm32, wasm64p32
package cgltf
@(require) import _ "vendor:libc"
@(require) import _ "vendor:libc-shim"

View File

@@ -8,7 +8,7 @@ endif
wasm:
mkdir -p ../lib
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc cgltf.c -o ../lib/cgltf_wasm.o
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc-shim cgltf.c -o ../lib/cgltf_wasm.o
unix:
mkdir -p ../lib

View File

@@ -1,11 +1,11 @@
# vendor:libc
# vendor:libc-shim
A (very small) subset of a libc implementation over Odin libraries.
This is mainly intended for use in Odin WASM builds to allow using libraries like box2d, cgltf etc. without emscripten hacks.
You can use this with clang by doing `clang -c --target=wasm32 --sysroot=$(odin root)/vendor/libc` (+ all other flags and inputs).
You can use this with clang by doing `clang -c --target=wasm32 --sysroot=$(odin root)/vendor/libc-shim` (+ all other flags and inputs).
This will (if all the libc usage of the library is implemented) spit out a `.o` file you can use with the foreign import system.
If you then also make sure this package is included in the Odin side of the project (`@(require) import "vendor:libc"`) you will be able
If you then also make sure this package is included in the Odin side of the project (`@(require) import "vendor:libc-shim"`) you will be able
compile to WASM like Odin expects.
This is currently used by `vendor:box2d`, `vendor:stb/image`, `vendor:stb/truetype`, `vendor:stb/rect_pack`, and `vendor:cgltf`.

26
vendor/libc-shim/libc.odin vendored Normal file
View File

@@ -0,0 +1,26 @@
package odin_libc
import "base:runtime"
import "core:mem"
@(private)
g_ctx: runtime.Context
@(private)
g_allocator: mem.Compat_Allocator
@(init)
init_context :: proc "contextless" () {
g_ctx = runtime.default_context()
context = g_ctx
// Wrapping the allocator with the mem.Compat_Allocator so we can
// mimic the realloc semantics.
mem.compat_allocator_init(&g_allocator, g_ctx.allocator)
g_ctx.allocator = mem.compat_allocator(&g_allocator)
}
// NOTE: the allocator must respect an `old_size` of `-1` on resizes!
set_context :: proc(ctx := context) {
g_ctx = ctx
}

View File

@@ -17,7 +17,7 @@ EOF :: -1
@(require, linkage="strong", link_name="fopen")
fopen :: proc "c" (path: cstring, mode: cstring) -> FILE {
context = g_ctx
unimplemented("vendor/libc: fopen")
unimplemented("vendor/libc-shim: fopen")
}
@(require, linkage="strong", link_name="fseek")
@@ -366,7 +366,7 @@ _sscanf :: proc "c" (str, fmt: [^]byte, orig_ptrs: [^]rawptr) -> i32 {
i = 0
k = t == 'c' ? width + 1 : 31
if size == .l {
unimplemented("vendor/libc: sscanf wide character support")
unimplemented("vendor/libc-shim: sscanf wide character support")
} else if alloc {
s = make([^]byte, k)
if s == nil {

25
vendor/libc/libc.odin vendored
View File

@@ -1,26 +1,3 @@
package odin_libc
import "base:runtime"
import "core:mem"
@(private)
g_ctx: runtime.Context
@(private)
g_allocator: mem.Compat_Allocator
@(init)
init_context :: proc "contextless" () {
g_ctx = runtime.default_context()
context = g_ctx
// Wrapping the allocator with the mem.Compat_Allocator so we can
// mimic the realloc semantics.
mem.compat_allocator_init(&g_allocator, g_ctx.allocator)
g_ctx.allocator = mem.compat_allocator(&g_allocator)
}
// NOTE: the allocator must respect an `old_size` of `-1` on resizes!
set_context :: proc(ctx := context) {
g_ctx = ctx
}
#panic("`vendor:libc` has been moved, use `vendor:libc-shim` instead.")

View File

@@ -1,4 +1,4 @@
#+build wasm32, wasm64p32
package stb_image
@(require) import _ "vendor:libc"
@(require) import _ "vendor:libc-shim"

View File

@@ -1,4 +1,4 @@
#+build wasm32, wasm64p32
package stb_rect_pack
@(require) import _ "vendor:libc"
@(require) import _ "vendor:libc-shim"

View File

@@ -8,12 +8,12 @@ endif
wasm:
mkdir -p ../lib
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc stb_image.c -o ../lib/stb_image_wasm.o -DSTBI_NO_STDIO
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc stb_image_write.c -o ../lib/stb_image_write_wasm.o -DSTBI_WRITE_NO_STDIO
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc stb_image_resize.c -o ../lib/stb_image_resize_wasm.o
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc stb_truetype.c -o ../lib/stb_truetype_wasm.o
# $(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc stb_vorbis.c -o ../lib/stb_vorbis_wasm.o -DSTB_VORBIS_NO_STDIO
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc stb_rect_pack.c -o ../lib/stb_rect_pack_wasm.o
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc-shim stb_image.c -o ../lib/stb_image_wasm.o -DSTBI_NO_STDIO
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc-shim stb_image_write.c -o ../lib/stb_image_write_wasm.o -DSTBI_WRITE_NO_STDIO
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc-shim stb_image_resize.c -o ../lib/stb_image_resize_wasm.o
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc-shim stb_truetype.c -o ../lib/stb_truetype_wasm.o
# $(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc-shim stb_vorbis.c -o ../lib/stb_vorbis_wasm.o -DSTB_VORBIS_NO_STDIO
$(CC) -c -Os --target=wasm32 --sysroot=$(shell odin root)/vendor/libc-shim stb_rect_pack.c -o ../lib/stb_rect_pack_wasm.o
$(CC) -c -Os --target=wasm32 stb_sprintf.c -o ../lib/stb_sprintf_wasm.o
unix:

View File

@@ -1,4 +1,4 @@
#+build wasm32, wasm64p32
package stb_truetype
@(require) import _ "vendor:libc"
@(require) import _ "vendor:libc-shim"